Question: This was some code that was written for me for a game called battlship where their must be 4 small ships (2 spaces), 3 medium
This was some code that was written for me for a game called battlship where their must be 4 small ships (2 spaces), 3 medium size ships (3 spaces), 2 large size ships ( 4 spaces) and 1 very large ship of 6 spaces in length. To where the user must play against some form of an AI bot, which can be either a dumb random function bot or one that has some noticibal intelligance that the user can play against. I would greatly appreciate it if anyone could help with getting this code to work. Thank you.
#include
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]; //Ignore player 0, just using player's 1 & 2
enum DIRECTION {HORIZONTAL,VERTICAL}; struct PLACESHIPS { DIRECTION direction; SHIP shipType; };
bool gameRunning = false;
//Functions void LoadShips(); void ResetBoard(); void DrawBoard(int); PLACESHIPS UserInputShipPlacement(); bool UserInputAttack(int&,int&,int); bool GameOverCheck(int);
int main() { LoadShips(); ResetBoard();
//"PLACE SHIPS" phase of game //Loop through each player... for (int aplyr=1; aplyr<3; ++aplyr) { //Loop through each ship type to place for (int thisShip=0; thisShip //Combine user data with "this ship" data aShip.shipType.length = ship[thisShip].length; aShip.shipType.name = ship[thisShip].name; //Add the FIRST grid point to the current player's game board player[aplyr].grid[aShip.shipType.onGrid[0].X][aShip.shipType.onGrid[0].Y] = isSHIP; //Determine ALL grid points based on length and direction for (int i=1; i //Add the REMAINING grid points to our current players game board player[aplyr].grid[aShip.shipType.onGrid[i].X][aShip.shipType.onGrid[i].Y] = isSHIP; } //Loop back through each ship type } //Loop back through each player } //FINISHED WITH "PLACE SHIPS" PHASE //Ready to play the game gameRunning = true; int thisPlayer = 1; do { //Because we are ATTACKING now, the //opposite players board is the display board int enemyPlayer; if (thisPlayer == 1) enemyPlayer = 2; if (thisPlayer == 2) enemyPlayer = 1; system("cls"); DrawBoard(enemyPlayer); //Get attack coords from this player bool goodInput = false; int x,y; while (goodInput == false) { goodInput = UserInputAttack(x,y,thisPlayer); } //Check board; if a ship is there, set as HIT.. otherwise MISS 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; //Check to see if the game is over //If 0 is returned, nobody has won yet int aWin = GameOverCheck(enemyPlayer); if (aWin != 0) { gameRunning = false; break; } //Alternate between each player as we loop back around thisPlayer = (thisPlayer == 1) ? 2 : 1; } while (gameRunning); system("cls"); cout << " CONGRATULATIONS!!! PLAYER " << thisPlayer<< " HAS WON THE GAME! "; system("pause"); return 0; } bool GameOverCheck(int enemyPLAYER) { bool winner = true; //Loop through enemy board for (int w=0; w 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; //Using this as a bad return tmp.shipType.onGrid[0].X = -1; //Get 3 integers from user 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; //Good data tmp.direction = (DIRECTION)d; tmp.shipType.onGrid[0].X = x; tmp.shipType.onGrid[0].Y = y; return tmp; } void LoadShips() { //Sets the default data for the ships //we plan to include in the game //IMPORTANT!! > MUST MATCH SHIP_TYPES -Default=5 (0-4) 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() { //Loop through each player for (int plyr=1; plyr<3; ++plyr) { //For each grid point, set contents to 'water' for (int w=0; w void DrawBoard(int thisPlayer) { //Draws the board for a player (thisPlayer) cout << "PLAYER " << thisPlayer << "'s GAME BOARD "; cout << "---------------------- "; //Loop through top row (board_width) and number columns cout << " "; for (int w=0; w //Loop through each grid point and display to console for (int h=0; h
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
