UNKNOWN //************************************** // Name: Battleship // Description:This code is for a personal project I did based on a school assignment. It plays a game of Battleship. When you compile it, the program asks you how many enemy ships you want and how big you want them to be. Ships are generated randomly and never occupy the same spot. There is commented out code for diagonal ship orientation; to implement it you would have to change line 116 to "ort = rand()%4". Play the game by inputting row and column coordinates. // By: Frank Tan // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.13783/lngWId.3/qx/vb/scripts/ShowCode.htm //for details. //************************************** /**************************************** battleShip.cpp simulates a game of Battleship against a computer that randomly generates enemies based on user inputs. ****************************************/ #include <cstdlib> #include <iostream> #include <ctime> using namespace std; const int ROWS = 10; const int COLS = 10; void printBoard(char board[][COLS]); void generateShip(char board[][COLS], int size); void checkBoard(char board[][COLS], bool &victory); int main(void) { srand(time(NULL)); char board[ROWS][COLS], playerBoard[ROWS][COLS]; int sizeShip, nShips; /*******Initialize boards*******/ for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = '.'; playerBoard[i][j] = '.'; } } /*******Get nShips******/ cout << "\nLet's play Battleship! You get to choose the difficulty of the game.\n\n"; do { cout << "How many enemy battleships do you want? You can't have more than 5: "; cin >> nShips; } while (nShips < 1 || nShips > 5); /*******Get each ship's size and generate it*******/ for (int i = 1; i <= nShips; i++) { do { cout << "Size of enemy battleship #" << i << " (at least 2, up to 5): "; cin >> sizeShip; } while (sizeShip < 2 || sizeShip > 5); generateShip(board, sizeShip); } /*******The rest of the main function is gameplay*******/ int nBombs = 50, iGuess, jGuess; bool victory = false; if (nShips != 1) cout << "\nThe board is below. There are " << nShips << " enemy battleships hidden here.\n"; else cout << "\nThe board is below. There is " << nShips << " enemy battleship hidden here.\n"; cout << "Input \"i\" and \"j\" (\"i\" is the row and \"j\" is the column) to decide where to fire your bombs.\n" << "For example, if you wanted to fire in the top right corner, type \"1 10\" (row 1 column 10).\n" << "You have 50 bombs. Make your first move now. Good luck.\n"; /*******Each turn, check if the player still has bombs left or if the player has won*******/ for (int n = 1; n <= nBombs && !victory; n++) { printBoard(playerBoard); cout << "\nBomb #" << n << ", i and j: "; cin >> iGuess; cin >> jGuess; if (board[iGuess - 1][jGuess - 1] == 'S') { cout << "You made a hit!\n"; board[iGuess - 1][jGuess - 1] = '*'; playerBoard[iGuess - 1][jGuess - 1] = '*'; } else if (board[iGuess - 1][jGuess - 1] == '.') { cout << "You missed.\n"; board[iGuess - 1][jGuess - 1] = 'o'; playerBoard[iGuess - 1][jGuess - 1] = 'o'; } victory = true; checkBoard(board, victory); } /*******Endgame results*******/ cout << "The final board:\n"; printBoard(board); cout << "A \".\" is open sea.\n" << "An \"*\" is a hit on an enemy battleship.\n" << "An \"S\" is a surviving enemy battleship.\n" << "An \"o\" is a missed bomb.\n\n"; if (victory) cout << "Congratulations, you sunk all the enemy battleships!\n\n"; else cout << "You didn't sink every battleship. Please try again!\n\n"; return 0; } void printBoard(char board[][COLS]) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { cout << board[i][j]; } cout << endl; } return; } void generateShip(char board[][COLS], int size) { int iStart,jStart,ort; /*******Generate random start point (and orientation) on a point that doesn't already have a ship on it*******/ Randomize: do { iStart = rand()%ROWS; jStart = rand()%COLS; ort = rand()%2; } while (board[iStart][jStart] == 'S'); /*******Case 0: Horizontal orientation*******/ if (ort == 0) { /*******Make sure the ship won't go off the board*******/ while (jStart + size >= COLS) { jStart = rand()%COLS; } /*******Check to make sure other ships aren't in the way*******/ for (int j = jStart; j < jStart + size; j++) { if (board[iStart][j] == 'S') goto Randomize; } /*******If the above checks out, generate the ship*******/ for (int j = jStart; j < jStart + size; j++) { board[iStart][j] = 'S'; } } /*******Case 1: Vertical orientation*******/ if (ort == 1) { while (iStart + size >= ROWS) { iStart = rand()%ROWS; } for (int i = iStart; i < iStart + size; i++) { if (board[i][jStart] == 'S') goto Randomize; } for (int i = iStart; i < iStart + size; i++) { board[i][jStart] = 'S'; } } /* *******Case 2: Diagonal orientation: top left to bottom right******* if (ort == 2) { while (iStart + size >= ROWS || jStart + size >= COLS) { iStart = rand()%ROWS; jStart = rand()%COLS; } for (int i = iStart; i < iStart + size; i++) { if (board[i][i] == 'S') goto Randomize; } for (int i = iStart; i < iStart + size; i++) { board[i][i] = 'S'; } } *******Case 3: Diagonal orientation: top right to bottom left******* if (ort == 3) { while (iStart + size >= ROWS || jStart - size <= 0) { iStart = rand()%ROWS; jStart = rand()%COLS; } for (int i = iStart; i < iStart + size; i++) { if (board[i][COLS - i] == 'S') goto Randomize; } for (int i = iStart; i < iStart + size; i++) { board[i][COLS - i] = 'S'; } } */ return; } void checkBoard(char board[][COLS], bool &victory) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (board[i][j] == 'S') victory = false; } } }