C++ Battleship Adjacent Check: I\'m having trouble writing code to check if when
ID: 3680064 • Letter: C
Question
C++ Battleship Adjacent Check:
I'm having trouble writing code to check if when a ship is placed if it is adjacent to another ship. My issue comes up when checking any ship that is located on an edge of the board because you can't check outside the bounds of the board. I know the size of the ship and the orientation, true=vertical and false=horizontal as well as the x,y of the test spot. x,y represents the top left of the ship. this is what I have so far, any help would be appreciated.
//check if ship will intersect another ship
for(int i=0; i<size;++i){
if( (orientation==true && game_board[x+i][y]!='0') || (orientation==false && game_board[x][y+i]!='0') ){return false;}
int check_spot_x=0;
int check_spot_y=0;
if(check_spot_x>=0 && check_spot_y>=0){
//vertical ship checks
if(orientation==true){
if(y>0 && x>0){
//check this check
if( game_board[x][y+size]!='0' || game_board[x][y-1]!='0' ){return false;}
}
else if(y>0 && x==0){}
else if(x>0 && y==0){}
//horizontal ship checks
}else{
if(y>0 && x>0){
if( game_board[x][y+size]!='0' || game_board[x][y-1]!='0' ){return false;}
}
else if(y>0 && x==0){}
else if(x>0 && y==0){}
}
}
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
const int BOARD_WIDTH = 15;
const int BOARD_HEIGHT = 10;
const int SHIP_TYPES = 5;
const char isWATER = 247;
const char isHIT = 'X';
const char isSHIP = 'S';
const char isMISS = '0';
struct POINT
{
int X;
int Y;
};
struct SHIP {
string name;
int length;
POINT onGrid[5];
bool hitFlag[5];
}ship[SHIP_TYPES];
struct PLAYER
{
char grid[BOARD_WIDTH][BOARD_HEIGHT];
}player[3];
enum DIRECTION {HORIZONTAL,VERTICAL};
struct PLACESHIPS
{
DIRECTION direction;
SHIP shipType;
};
bool gameRunning = false;
void LoadShips();
void ResetBoard();
void DrawBoard(int);
PLACESHIPS UserInputShipPlacement();
bool UserInputAttack(int&,int&,int);
bool GameOverCheck(int);
int main()
{
LoadShips();
ResetBoard();
for (int aplyr=1; aplyr<3; ++aplyr)
{
for (int thisShip=0; thisShip<SHIP_TYPES; ++thisShip)
{
system("cls");
DrawBoard(aplyr);
cout << " ";
cout << "INSTRUCTIONS (Player " << aplyr << ") ";
cout << "You are about to place your ships. Format should be: ";
cout << "Facing (0:Horizontal,1:Vertical), X (top-row) coords, Y (left-side) coords ";
cout << "Example: 0 7 2 This would place a ship beginning at X:7 Y:2 going horizontal ";
cout << "Ship to place: " << ship[thisShip].name << " which has a length of " << ship[thisShip].length << " ";
cout << "Where do you want it placed? ";
PLACESHIPS aShip;
aShip.shipType.onGrid[0].X = -1;
while (aShip.shipType.onGrid[0].X == -1)
{
aShip = UserInputShipPlacement();
}
aShip.shipType.length = ship[thisShip].length;
aShip.shipType.name = ship[thisShip].name;
player[aplyr].grid[aShip.shipType.onGrid[0].X][aShip.shipType.onGrid[0].Y] = isSHIP;
for (int i=1; i<aShip.shipType.length; ++i)
{
if (aShip.direction == HORIZONTAL){
aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X+1;
aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y; }
if (aShip.direction == VERTICAL){
aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y+1;
aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X; }
player[aplyr].grid[aShip.shipType.onGrid[i].X][aShip.shipType.onGrid[i].Y] = isSHIP;
}
}
}
gameRunning = true;
int thisPlayer = 1;
do {
int enemyPlayer;
if (thisPlayer == 1) enemyPlayer = 2;
if (thisPlayer == 2) enemyPlayer = 1;
system("cls");
DrawBoard(enemyPlayer);
bool goodInput = false;
int x,y;
while (goodInput == false) {
goodInput = UserInputAttack(x,y,thisPlayer);
}
if (player[enemyPlayer].grid[x][y] == isSHIP) player[enemyPlayer].grid[x][y] = isHIT;
if (player[enemyPlayer].grid[x][y] == isWATER) player[enemyPlayer].grid[x][y] = isMISS;
int aWin = GameOverCheck(enemyPlayer);
if (aWin != 0) {
gameRunning = false;
break;
}
thisPlayer = (thisPlayer == 1) ? 2 : 1;
} while (gameRunning);
system("cls");
cout << "WIN! ";
system("pause");
return 0;
}
bool GameOverCheck(int enemyPLAYER)
{
bool winner = true;
for (int w=0; w<BOARD_WIDTH; ++w){
for (int h=0; h<BOARD_HEIGHT; ++h){
if (player[enemyPLAYER].grid[w][h] = isSHIP)
{
winner = false;
return winner;
}
}}
return winner;
}
bool UserInputAttack(int& x, int& y, int theplayer)
{
cout << " PLAYER " << theplayer << ", ENTER COORDINATES TO ATTACK: ";
bool goodInput = false;
cin >> x >> y;
if (x<0 || x>=BOARD_WIDTH) return goodInput;
if (y<0 || y>=BOARD_HEIGHT) return goodInput;
goodInput = true;
return goodInput;
}
PLACESHIPS UserInputShipPlacement()
{
int d, x, y;
PLACESHIPS tmp;
tmp.shipType.onGrid[0].X = -1;
cin >> d >> x >> y;
if (d!=0 && d!=1) return tmp;
if (x<0 || x>=BOARD_WIDTH) return tmp;
if (y<0 || y>=BOARD_HEIGHT) return tmp;
tmp.direction = (DIRECTION)d;
tmp.shipType.onGrid[0].X = x;
tmp.shipType.onGrid[0].Y = y;
return tmp;
}
void LoadShips()
{
ship[0].name = "Cruiser"; ship[0].length = 2;
ship[1].name = "Frigate"; ship[1].length = 3;
ship[2].name = "Submarine"; ship[2].length = 3;
ship[3].name = "Escort"; ship[3].length = 4;
ship[4].name = "Battleship"; ship[4].length = 5;
}
void ResetBoard()
{
for (int plyr=1; plyr<3; ++plyr)
{
for (int w=0; w<BOARD_WIDTH; ++w){
for (int h=0; h<BOARD_HEIGHT; ++h){
player[plyr].grid[w][h] = isWATER;
}}
}
}
void DrawBoard(int thisPlayer)
{
cout << "PLAYER " << thisPlayer << "'s GAME BOARD ";
cout << " ";
for (int w=0; w<BOARD_WIDTH; ++w) {
if (w < 10)
cout << w << " ";
else if (w >= 10)
cout << w << " ";
}
cout << " ";
for (int h=0; h<BOARD_HEIGHT; ++h){
for (int w=0; w<BOARD_WIDTH; ++w){
if (w==0) cout << h << " ";
if (w<10 && w==0) cout << " ";
if (gameRunning == false) cout << player[thisPlayer].grid[w][h] << " ";
if (gameRunning == true && player[thisPlayer].grid[w][h] != isSHIP)
{cout << player[thisPlayer].grid[w][h] << " ";}
else if (gameRunning == true && player[thisPlayer].grid[w][h] == isSHIP)
{cout << isWATER << " ";}
if (w == BOARD_WIDTH-1) cout << " ";
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.