So, I have the code below which is a pretty basic battleship program but there a
ID: 644800 • Letter: S
Question
So, I have the code below which is a pretty basic battleship program but there are 3 bugs that I haven't been able to fix... Please, help?
- Can't place large ships on row 10
- Can't place large ships along column 9 and 10
- Can place large ships horizontally starting at column 9
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
typedef char* CharPtr;
CharPtr* initialBoard();
void displayBoard(CharPtr* b);
//place ships functions
//aircraft carrier (5 long)
void placeBoat(string name, int length, CharPtr* &b);
//patrol boat (2 long)
//M = miss
//H = hit
//* = part of a ship
//- = open water
bool isFree(CharPtr* &b, int o, int l, int x, int y);
bool isFinished(CharPtr* &b);
const int BOARD_SIZE = 10;
int main() {
const int PATROL_BOAT_LENGTH = 2;
const string PATROL_BOAT_NAME = "Patrol boat";
const int DESTROYER_LENGTH = 3;
const string DESTROYER_NAME = "Destroyer";
const int SUB_LENGTH = 3;
const string SUB_NAME = "Submarine";
const int BATTLESHIP_LENGTH = 4;
const string BATTLESHIP_NAME = "Battleship";
const int CARRIER_LENGTH = 5;
const string CARRIER_NAME = "Aircraft Carrier";
//Initialize battleship board
CharPtr* board = initialBoard();
//Place the boats on the board
placeBoat(PATROL_BOAT_NAME, PATROL_BOAT_LENGTH,
board);
placeBoat(DESTROYER_NAME, DESTROYER_LENGTH,
board);
placeBoat(SUB_NAME, SUB_LENGTH,
board);
placeBoat(BATTLESHIP_NAME, BATTLESHIP_LENGTH,
board);
placeBoat(CARRIER_NAME, CARRIER_LENGTH,
board);
int x_coord = -1;
int y_coord = -1;
while (!isFinished(board)) {
displayBoard(board);
cout << "Enter (X,Y) coordinate for missle: " << endl;
cout << "(X and Y can be 1.." << (BOARD_SIZE) << ") " << endl;
cin >> x_coord;
cin >> y_coord;
x_coord -= 1;
y_coord -= 1;
if ((x_coord >= 0 && x_coord < BOARD_SIZE)
&& (y_coord >= 0 && y_coord < BOARD_SIZE)) {
switch (board[y_coord][x_coord]) {
case '-':
board[y_coord][x_coord] = 'M';
cout << "MISS!" << endl;
break;
case '*':
board[y_coord][x_coord] = 'H';
cout << "HIT!" << endl;
break;
default:
cout << "You are already shot here! Try again." << endl;
}
} else {
cout << "Invalid coordinates!" << endl;
}
}
}
CharPtr* initialBoard() {
CharPtr* p1 = new CharPtr[BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
p1[i] = new char[BOARD_SIZE];
for (int j=0; j < BOARD_SIZE; j++) {
p1[i][j] = '-';
}
}
return p1;
}
void displayBoard(CharPtr* b) {
cout << " ";
for (int i = 0; i < BOARD_SIZE; i++) {
cout << i+1 << ' ';
}
cout << endl;
for (int i = 0; i < BOARD_SIZE; i++) {
cout << (i+1) << ' ';
for (int j=0; j < BOARD_SIZE; j++) {
cout << b[i][j] << ' ';
}
cout << endl;
}
}
void placeBoat(string name, int length, CharPtr* &b) {
cout << "Place " << name << " on board!" << endl;
//get orientation
int orientation = -1;
while (orientation == -1) {
displayBoard(b);
cout << "Enter orientation of boat:" << endl;
cout << " 1. Horizontal" << endl;
cout << " 2. Vertical" << endl;
cout << "> ";
cin >> orientation;
if (orientation == 1) {
cout << "Horizontal orientation confirmed." << endl;
} else if (orientation == 2) {
cout << "Veritical orientation confirmed" << endl;
} else {
cout << "Invalid orientation!" << endl;
orientation = -1;
}
}
//get top/left most coordinate
int x_coord = -1;
int y_coord = -1;
while (x_coord == -1 || y_coord == -1) {
cout << "Enter top/left (X,Y) coordinate of boat: " << endl;
cout << "(X and Y can be 1.." << (BOARD_SIZE) << ") " << endl;
cin >> x_coord;
cin >> y_coord;
x_coord -= 1;
y_coord -= 1;
if ((x_coord >= 0 && x_coord < BOARD_SIZE)
&& (y_coord >= 0 && y_coord < BOARD_SIZE)) {
if (!isFree(b, orientation, length, x_coord, y_coord)) {
x_coord = -1;
y_coord = -1;
}
} else {
cout << "Invalid coordinates!" << endl;
x_coord = -1;
y_coord = -1;
}
}
for (int i=0; i < length; i++) {
if (orientation == 1) {
//horizontal placement;
b[y_coord][x_coord+i] = '*';
} else if (orientation == 2) {
//vertical placement
b[y_coord+i][x_coord] = '*';
}
}
}
bool isFree(CharPtr* &b, int o, int l, int x, int y) {
if ((o == 1) && (y+l-1 < BOARD_SIZE)) {
//horizontal placement;
for (int i=0; i < l; i++) {
//horizontal placement;
if (b[y][x+i] == '*') {
cout << "You can't place one boat on top of another!" << endl;
return false;
}
}
cout << "Coordinates confirmed." << endl;
return true;
} else if ((o == 2) && (x+l-1 < BOARD_SIZE)){
//vertical placement
for (int i=0; i < l; i++) {
//horizontal placement;
if (b[y+i][x] == '*') {
cout << "You can't place one boat on top of another!" << endl;
return false;
}
}
cout << "Coordinates confirmed." << endl;
return true;
} else {
cout << "Invalid coordinates!" << endl;
return false;
}
}
bool isFinished(CharPtr* &b) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j=0; j < BOARD_SIZE; j++) {
if (b[i][j] == '*')
return false;
}
}
return true;
}
Explanation / Answer
Program code:
// BattelShip.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
typedef char* CharPtr;
CharPtr* initialBoard();
void displayBoard(CharPtr* b);
//place ships functions
//aircraft carrier (5 long)
void placeBoat(string name, int length, CharPtr* &b);
//patrol boat (2 long)
//M = miss
//H = hit
//* = part of a ship
//- = open water
bool isFree(CharPtr* &b, int o, int l, int x, int y);
bool isFinished(CharPtr* &b);
const int BOARD_SIZE = 10;
int main()
{
const int PATROL_BOAT_LENGTH = 2;
const string PATROL_BOAT_NAME = "Patrol boat";
const int DESTROYER_LENGTH = 3;
const string DESTROYER_NAME = "Destroyer";
const int SUB_LENGTH = 3;
const string SUB_NAME = "Submarine";
const int BATTLESHIP_LENGTH = 4;
const string BATTLESHIP_NAME = "Battleship";
const int CARRIER_LENGTH = 5;
const string CARRIER_NAME = "Aircraft Carrier";
//Initialize battleship board
CharPtr* board = initialBoard();
//Place the boats on the board
placeBoat(PATROL_BOAT_NAME, PATROL_BOAT_LENGTH,
board);
placeBoat(DESTROYER_NAME, DESTROYER_LENGTH,
board);
placeBoat(SUB_NAME, SUB_LENGTH,
board);
placeBoat(BATTLESHIP_NAME, BATTLESHIP_LENGTH,
board);
placeBoat(CARRIER_NAME, CARRIER_LENGTH,
board);
int x_coord = -2;
int y_coord = -2;
while (!isFinished(board))
{
displayBoard(board);
cout << "Enter (X,Y) coordinate for missle: " << endl;
cout << "(X and Y can be 1.." << (BOARD_SIZE) << ") " << endl;
cin >> x_coord;
cin >> y_coord;
x_coord -= 1;
y_coord -= 1;
if ((x_coord >= 0 && x_coord <= BOARD_SIZE)
&& (y_coord >= 0 && y_coord <= BOARD_SIZE))
{
cout<<x_coord<<" "<<y_coord<<endl;
switch (board[y_coord][x_coord])
{
case '-':
board[y_coord][x_coord] = 'M';
cout << "MISS!" << endl;
break;
case '*':
board[y_coord][x_coord] = 'H';
cout << "HIT!" << endl;
break;
default:
cout << "You are already shot here! Try again." << endl;
}
}
else
{
cout << "Invalid coordinates!" << endl;
}
}
}
CharPtr* initialBoard()
{
CharPtr* p1 = new CharPtr[BOARD_SIZE+1];
for (int i = 0; i <= BOARD_SIZE; i++)
{
p1[i] = new char[BOARD_SIZE+1];
for (int j=0; j <= BOARD_SIZE; j++)
{
p1[i][j] = '-';
}
}
return p1;
}
void displayBoard(CharPtr* b)
{
cout << " ";
for (int i = 0; i <= BOARD_SIZE; i++)
{
cout << i+1 << ' ';
}
cout << endl;
for (int i = 0; i <= BOARD_SIZE; i++)
{
cout << (i+1) << ' ';
for (int j=0; j <= BOARD_SIZE; j++)
{
cout << b[i][j] << ' ';
}
cout << endl;
}
}
void placeBoat(string name, int length, CharPtr* &b)
{
cout << "Place " << name << " on board!" << endl;
//get orientation
int orientation = -1;
while (orientation == -1)
{
displayBoard(b);
cout << "Enter orientation of boat:" << endl;
cout << " 1. Horizontal" << endl;
cout << " 2. Vertical" << endl;
cout << "> ";
cin >> orientation;
if (orientation == 1)
{
cout << "Horizontal orientation confirmed." << endl;
}
else if (orientation == 2)
{
cout << "Veritical orientation confirmed" << endl;
}
else
{
cout << "Invalid orientation!" << endl;
orientation = -1;
}
}
//get top/left most coordinate
int x_coord = -1;
int y_coord = -1;
while (x_coord == -1 || y_coord == -1)
{
cout << "Enter top/left (X,Y) coordinate of boat: " << endl;
cout << "(X and Y can be 1.." << (BOARD_SIZE) << ") " << endl;
cin >> x_coord;
cin >> y_coord;
x_coord -= 1;
y_coord -= 1;
if ((x_coord >= 0 && x_coord <= BOARD_SIZE)
&& (y_coord >= 0 && y_coord <= BOARD_SIZE))
{
if (!isFree(b, orientation, length, x_coord, y_coord))
{
x_coord = -1;
y_coord = -1;
}
}
else
{
cout << "Invalid coordinates!" << endl;
x_coord = -1;
y_coord = -1;
}
}
for (int i=0; i < length; i++)
{
if (orientation == 1)
{
//horizontal placement;
b[y_coord][x_coord+i] = '*';
}
else if (orientation == 2)
{
//vertical placement
b[y_coord+i][x_coord] = '*';
}
}
}
bool isFree(CharPtr* &b, int o, int l, int x, int y)
{
if ((o == 1) && (y+l-1 <= BOARD_SIZE))
{
//horizontal placement;
for (int i=0; i < l; i++)
{
//horizontal placement;
if (b[y][x+i] == '*')
{
cout << "You can't place one boat on top of another!" << endl;
return false;
}
}
cout << "Coordinates confirmed." << endl;
return true;
}
else if ((o == 2) && (x+l-1 <=BOARD_SIZE))
{
//vertical placement
for (int i=0; i < l; i++)
{
//horizontal placement;
if (b[y+i][x] == '*')
{
cout << "You can't place one boat on top of another!" << endl;
return false;
}
}
cout << "Coordinates confirmed." << endl;
return true;
}
else
{
cout << "Invalid coordinates!" << endl;
return false;
}
}
bool isFinished(CharPtr* &b)
{
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int j=0; j < BOARD_SIZE; j++)
{
if (b[i][j] == '*')
return false;
}
}
return true;
}
Sample Output:
Place Patrol boat on board!
1 2 3 4 5 6 7 8 9 10 11
1 - - - - - - - - - - -
2 - - - - - - - - - - -
3 - - - - - - - - - - -
4 - - - - - - - - - - -
5 - - - - - - - - - - -
6 - - - - - - - - - - -
7 - - - - - - - - - - -
8 - - - - - - - - - - -
9 - - - - - - - - - - -
10 - - - - - - - - - - -
11 - - - - - - - - - - -
Enter orientation of boat:
1. Horizontal
2. Vertical
> 1
Horizontal orientation confirmed.
Enter top/left (X,Y) coordinate of boat:
(X and Y can be 1..10)
9
10
Coordinates confirmed.
Place Destroyer on board!
1 2 3 4 5 6 7 8 9 10 11
1 - - - - - - - - - - -
2 - - - - - - - - - - -
3 - - - - - - - - - - -
4 - - - - - - - - - - -
5 - - - - - - - - - - -
6 - - - - - - - - - - -
7 - - - - - - - - - - -
8 - - - - - - - - - - -
9 - - - - - - - - - - -
10 - - - - - - - - * * -
11 - - - - - - - - - - -
Enter orientation of boat:
1. Horizontal
2. Vertical
> 2
Veritical orientation confirmed
Enter top/left (X,Y) coordinate of boat:
(X and Y can be 1..10)
7
8
Coordinates confirmed.
Place Submarine on board!
1 2 3 4 5 6 7 8 9 10 11
1 - - - - - - - - - - -
2 - - - - - - - - - - -
3 - - - - - - - - - - -
4 - - - - - - - - - - -
5 - - - - - - - - - - -
6 - - - - - - - - - - -
7 - - - - - - - - - - -
8 - - - - - - * - - - -
9 - - - - - - * - - - -
10 - - - - - - * - * * -
11 - - - - - - - - - - -
Enter orientation of boat:
1. Horizontal
2. Vertical
> 2
Veritical orientation confirmed
Enter top/left (X,Y) coordinate of boat:
(X and Y can be 1..10)
9
4
Coordinates confirmed.
Place Battleship on board!
1 2 3 4 5 6 7 8 9 10 11
1 - - - - - - - - - - -
2 - - - - - - - - - - -
3 - - - - - - - - - - -
4 - - - - - - - - * - -
5 - - - - - - - - * - -
6 - - - - - - - - * - -
7 - - - - - - - - - - -
8 - - - - - - * - - - -
9 - - - - - - * - - - -
10 - - - - - - * - * * -
11 - - - - - - - - - - -
Enter orientation of boat:
1. Horizontal
2. Vertical
> 1
Horizontal orientation confirmed.
Enter top/left (X,Y) coordinate of boat:
(X and Y can be 1..10)
2
6
Coordinates confirmed.
Place Aircraft Carrier on board!
1 2 3 4 5 6 7 8 9 10 11
1 - - - - - - - - - - -
2 - - - - - - - - - - -
3 - - - - - - - - - - -
4 - - - - - - - - * - -
5 - - - - - - - - * - -
6 - * * * * - - - * - -
7 - - - - - - - - - - -
8 - - - - - - * - - - -
9 - - - - - - * - - - -
10 - - - - - - * - * * -
11 - - - - - - - - - - -
Enter orientation of boat:
1. Horizontal
2. Vertical
>
Note:
Only thing is in the 10’th row you can place only one boat.
The rest that are changed are highlighted with bold letters.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.