Need help in editing C++ code

Badshah
Help.zip

Grid.h

#ifndef grid_h #define grid_h #include"Game.h" void printBoardInput (string gameBoard[11][11], string Display1[11][11]); void printBoard (string GuessP1[11][11], string Board1[11][11],string Display1[11][11], int GuessP2[11][11]); string Board1[11][11] = { {" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, {"A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} }; // will display a "O" for guesses that didn't hit a ship. // will display a "X" for guesses that did. string GuessP1[11][11] = { {" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, {"A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} }; string Display1[11][11] = { {" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, {"A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} }; string Board2[11][11] = { {" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, {"A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} }; string Display2[11][11] = { {" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, {"A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}, {"J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} }; #endif /* grid_h */

__MACOSX/._Grid.h

Game.h

#ifndef Game_h #define Game_h #include<iostream> #include<vector> using namespace std; void endPrintBoard (string Board1[11][11], string Display1[11][11],string Board2[11][11], string Display2[11][11],string GuessP1[11][11]); bool stringInterpreter (vector < char >LocShips, int &row1, int &Column1); void setLocShips (string Board1[11][11], int ships, int shipLengths[5],int p1LocShipss[5][4], string Display1[11][11]); void setP2ShipLocs (string Board2[11][11], int p2LocShipss[5][4],int shipLengths[5]); bool ShipSunk (int shipLocs[5][4], bool isSunk[5], string board[11][11],int turn); bool ShipSunk (int shipLocs[5][4], bool isSunk[5], string board[11][11],int turn, int GuessP2[11][11]); bool chooseTile (string Board1[11][11], string Board2[11][11],string GuessP1[11][11], int p2LocShipss[5][4],bool p2isSunk[5], string Display1[11][11],int GuessP2[11][11]); void getP1ShipLocs (string Board1[11][11], int shipLengths[],int p1LocShipss[5][4], string Display1[11][11]); void BombingMostProbable (int GuessP2[11][11], int row, int col,int directions[4]); void smartComp (int GuessP2[11][11], bool p1isSunk[5],int shipLengths[5], int &row, int &col); void displayCPUGuess (int row, int col); bool BSAI (string Board1[11][11], int GuessP2[11][11],bool p1isSunk[5], int shipLengths[5],int p1LocShipss[5][4]); void setDisplayBoard (string board[11][11], int locs[5][4]); #endif /* Game_h */

__MACOSX/._Game.h

main.cpp

#include <iostream> #include <string> #include <time.h> #include <vector> #include <stdlib.h> #include "Game.h" #include "Grid.h" using namespace std; int main () { srand (unsigned (time (NULL))); int GuessP2[11][11] = { 0 }; int p1LocShip[5][4]; // array with player 1 ship locations [ r1 c1 r2 c2 ] int p2LocShip[5][4]; // array with player 2 ship locations [ r1 c1 r2 c2 ] int shipLengths[5] = { 5, 4, 3, 3, 2 }; // remember the length of each ship. // if true, the ship is still alive. bool p1isSunk[5] = { true, true, true, true, true }; bool p2isSunk[5] = { true, true, true, true, true }; // A B C S D // get the user's ship locations getP1ShipLocs (Board1, shipLengths, p1LocShip, Display1); // set the CPU's ship locations setP2ShipLocs (Board2, p2LocShip, shipLengths); // set the CPU display board based on the ship locations setDisplayBoard (Display2, p2LocShip); // begin play int turn = 1; // keep track of whose turn it is while (true) { if (turn == 1) { if (chooseTile (Board1, Board2, GuessP1, p2LocShip, p2isSunk, Display1, GuessP2)) { cout << "\n-----------------------------------------------------------------------------------------------------------------------\n"; cout << "| You Win! |"; endPrintBoard (Board1, Display1, Board2, Display2, GuessP1); return 0; } // end if (chooseTile(GuessP1, Board2, userGuess)) turn = 2; // CPU's turn } // end if (turn == 1) else { if (BSAI (Board1, GuessP2, p1isSunk, shipLengths, p1LocShip)) { cout << "\n-----------------------------------------------------------------------------------------------------------------------\n"; cout << "| You Lose! |"; endPrintBoard (Board1, Display1, Board2, Display2, GuessP1); return 0; } turn = 1; } } } void printBoardInput (string gameBoard[11][11], string Display1[11][11]) { for (int r = 0; r < 11; r++) { for (int c = 0; c < 11; c++) { if (r == 0) { cout << gameBoard[r][c] << " "; } else { if (gameBoard[r][c] == "S") { cout << Display1[r][c] << " | "; } else if (gameBoard[r][c] == " ") { cout << gameBoard[r][c] << " | "; } else { cout << gameBoard[r][c] << " | "; } } } cout << "\n -----------------------------------------\n"; } } void printBoard (string GuessP1[11][11], string Board1[11][11], string Display1[11][11], int GuessP2[11][11]) { cout << "-----------------------------------------------------------------------------------------------------------------------\n"; cout << "|" << " YOUR GUESSES YOUR SHIPS/CPU GUESSES " << "|\n"; cout << "-----------------------------------------------------------------------------------------------------------------------\n"; cout << "| |\n"; for (int r = 0; r < 11; r++) { cout << "|\t"; for (int c = 0; c < 11; c++) { if (r == 0) { cout << GuessP1[r][c] << " "; } // end if (r == 0) else { if ((GuessP1[r][c] == "X" || GuessP1[r][c] == "A" || GuessP1[r][c] == "B" || GuessP1[r][c] == "C" || GuessP1[r][c] == "S" || GuessP1[r][c] == "D") && c > 0) { cout << GuessP1[r][c] << " | "; } else if (GuessP1[r][c] == "O") { cout << GuessP1[r][c] << " | "; } // end else if (GuessP1[r][c] == "O") else if (GuessP1[r][c] == " ") { cout << GuessP1[r][c] << " | "; } else { cout << GuessP1[r][c] << " | "; } } } cout << "\t\t"; for (int c = 0; c < 11; c++) { if (r == 0) { cout << Board1[r][c] << " "; } else { if (Board1[r][c] == "S") { cout << Display1[r][c] << " | "; } else if (Board1[r][c] == "X") { if (GuessP2[r][c] == 2 || GuessP2[r][c] == 3) { cout << Display1[r][c] << " | "; } else { cout << Display1[r][c] << " | "; } } else if (Board1[r][c] == "O") { cout << Board1[r][c] << " | "; } else if (Board1[r][c] == " ") { cout << Board1[r][c] << " | "; } else { cout << Board1[r][c] << " | "; } } } if (r == 0) { cout << " |"; } // end if (r == 0) else { cout << " |"; } // end else: if (r == 0) cout << "\n| \t ----------------------------------------- \t\t ----------------------------------------- |\n"; } // end for (int r = 0; r < 11; r++) cout << "| |"; cout << "\n-----------------------------------------------------------------------------------------------------------------------\n"; } bool stringInterpreter (vector < char >LocShips, int &row1, int &Column1) { if (LocShips.size () < 2) { cout << " Mistakes were made. Try Again.\n Inputs must be a Letter(A - J) followed by a Number(1 - 10).\n\n"; return true; } if (LocShips[0] == 'A' || LocShips[0] == 'a') { row1 = 1; } else if (LocShips[0] == 'B' || LocShips[0] == 'b') { row1 = 2; } else if (LocShips[0] == 'C' || LocShips[0] == 'c') { row1 = 3; } else if (LocShips[0] == 'D' || LocShips[0] == 'd') { row1 = 4; } else if (LocShips[0] == 'E' || LocShips[0] == 'e') { row1 = 5; } else if (LocShips[0] == 'F' || LocShips[0] == 'f') { row1 = 6; } else if (LocShips[0] == 'G' || LocShips[0] == 'g') { row1 = 7; } else if (LocShips[0] == 'H' || LocShips[0] == 'h') { row1 = 8; } else if (LocShips[0] == 'I' || LocShips[0] == 'i') { row1 = 9; } else if (LocShips[0] == 'J' || LocShips[0] == 'j') { row1 = 10; } else { cout << " Mistakes were made. Try Again.\n Inputs must be a Letter(A-J) followed by a Number(1-10).\n\n"; return true; } if (LocShips.size () < 3) { if (LocShips[1] == '1') { Column1 = 1; } else if (LocShips[1] == '2') { Column1 = 2; } else if (LocShips[1] == '3') { Column1 = 3; } else if (LocShips[1] == '4') { Column1 = 4; } else if (LocShips[1] == '5') { Column1 = 5; } else if (LocShips[1] == '6') { Column1 = 6; } else if (LocShips[1] == '7') { Column1 = 7; } else if (LocShips[1] == '8') { Column1 = 8; } else if (LocShips[1] == '9') { Column1 = 9; } else { cout << " Mistakes were made.Try Again.\n Inputs must be a Letter(A - J) followed by a Number(1 - 10).\n\n"; return true; } } else if (LocShips.size () == 3) { if (LocShips[1] == '1' && LocShips[2] == '0') { Column1 = 10; } else { cout << " Mistakes were made.Try Again.\n Inputs must be a Letter(A - J) followed by a Number(1 - 10).\n\n"; return true; } } else { cout << " Mistakes were made.Try Again.\n Inputs must be a Letter(A - J) followed by a Number(1 - 10).\n\n"; return true; } return false; } void setLocShips (string Board1[11][11], int ships, int shipLengths[5], int p1LocShip[5][4], string Display1[11][11]) { string LocShips; int row1, Column1; int row2, Column2; int temp; vector < char >location1; vector < char >location2; bool secondString; bool shipsIntersected; static int p1Ships = 0; while (true) { LocShips = " "; cout << "Please place your " << shipLengths[ships] << "-tile ship: "; cin >> LocShips; secondString = false; for (int idx = 0; idx < LocShips.size (); idx++) { if (!secondString) { if (LocShips[idx] == '-') { secondString = true; } else { location1.push_back (LocShips[idx]); } } else { location2.push_back (LocShips[idx]); } } if (!secondString) { cout << " Input must be LetterNumber-LetterNumber.\n"; location1.clear (); location2.clear (); continue; } if (stringInterpreter (location1, row1, Column1)) { location1.clear (); location2.clear (); continue; } if (stringInterpreter (location2, row2, Column2)) { location1.clear (); location2.clear (); continue; } if (row1 != row2 && Column1 != Column2) { cout << " The ships are one dimensional. Try again.\n"; location1.clear (); location2.clear (); continue; } if (row1 == row2) { if (Column2 < Column1) { temp = Column1; Column1 = Column2; Column2 = temp; } // end if (Column2 < Column1) if ((Column2 - Column1 + 1) != shipLengths[ships]) { cout << " Your input was not the correct size. Try again.\n"; location1.clear (); // if it isn't, clear the vectors location2.clear (); continue; } } else { if (row2 < row1) { temp = row1; row1 = row2; row2 = temp; } if ((row2 - row1 + 1) != shipLengths[ships]) { cout << " Your input was not the correct size. Try again.\n"; location1.clear (); location2.clear (); continue; } } shipsIntersected = false; for (int r = row1; r <= row2; r++) { for (int c = Column1; c <= Column2; c++) { if (Board1[r][c] == "S") { shipsIntersected = true; cout << " Ships may not intersect. Try Again.\n"; location1.clear (); location2.clear (); break; } } if (shipsIntersected) { break; } } if (shipsIntersected) { continue; } break; } for (int r = row1; r <= row2; r++) { for (int c = Column1; c <= Column2; c++) { Board1[r][c] = "S"; } } string shipNames[5] = { "A", "B", "C", "S", "D" }; for (int r = row1; r <= row2; r++) { for (int c = Column1; c <= Column2; c++) { Display1[r][c] = shipNames[p1Ships]; } } int shipLocs[4] = { row1, Column1, row2, Column2 }; if (p1Ships < 5) { for (int i = 0; i < 4; i++) { p1LocShip[p1Ships][i] = shipLocs[i]; } // for (int i = 0; i < 4; i++) p1Ships++; } // if (p1Ships < 5) } void setP2ShipLocs (string Board2[11][11], int p2LocShip[5][4], int shipLengths[5]) { int max[] = { 6, 5, 4, 4, 3 }; bool isPlaced = false; int row = 0; int col = 0; enum direction { horizontal, vertical }; direction whichWay; for (int ships = 0; ships < 5; ships++) { while (!isPlaced) { isPlaced = true; whichWay = (rand () % 2 == 0) ? horizontal : vertical; if (whichWay == horizontal) { col = 1 + rand () % max[ships]; row = 1 + rand () % 10; for (int c = col; c < col + (11 - max[ships]); c++) { if (Board2[row][c] == "S") { isPlaced = false; break; } } } else { row = 1 + rand () % max[ships]; col = 1 + rand () % 10; for (int r = row; r < row + (11 - max[ships]); r++) { if (Board2[r][col] == "S") { isPlaced = false; break; } } } } isPlaced = false; if (whichWay == horizontal) { for (int c = 0; c < max[ships] - 1; c++) { Board2[row][col + c] = 'S'; } // end for (int c = 0; c < maxGuess - 1; c++) p2LocShip[ships][0] = row; p2LocShip[ships][1] = col; p2LocShip[ships][2] = row; p2LocShip[ships][3] = col + shipLengths[ships] - 1; } // end if (whichWay == horizontal) else { for (int r = 0; r < max[ships] - 1; r++) { Board2[row + r][col] = 'S'; } // end for (int r = 0; r < maxGuess - 1; r++) p2LocShip[ships][0] = row; p2LocShip[ships][1] = col; p2LocShip[ships][2] = row + shipLengths[ships] - 1; p2LocShip[ships][3] = col; } } } bool ShipSunk (int shipLocs[5][4], bool isSunk[5], string board[11][11], int turn, string GuessP1[11][11]) { // determine if the ship is sunk during player 1's turn bool shipSunk = true; for (int ship = 0; ship < 5; ship++) { if (isSunk[ship]) { shipSunk = true; for (int r = shipLocs[ship][0]; r <= shipLocs[ship][2]; r++) { for (int c = shipLocs[ship][1]; c <= shipLocs[ship][3]; c++) { if (board[r][c] == "S") { shipSunk = false; break; } } if (!shipSunk) { break; } // end if (!shipSunk) } // end for (int r = shipLocs[ship][0]; r <= shipLocs[ship][2]; r++) if (shipSunk) { isSunk[ship] = false; string shipNames[5] = { "A", "B", "C", "S", "D" }; for (int r = shipLocs[ship][0]; r <= shipLocs[ship][2]; r++) { for (int c = shipLocs[ship][1]; c <= shipLocs[ship][3]; c++) { GuessP1[r][c] = shipNames[ship]; } } if (turn == 1) { cout << " You sunk the computer's "; } // end if (turn == 1) else { cout << " The CPU sank your "; } // end else: if (turn == 1) switch (ship) { case 0: cout << "Aircraft Carrier!\n"; break; case 1: cout << "Battleship!\n"; break; case 2: cout << "Cruiser!\n"; break; case 3: cout << "Submarine!\n"; break; case 4: cout << "Destroyer!\n"; break; } // end switch (ship) return true; } // end if (shipSunk) } // end if (isSunk[ship]) } // end for (int ship = 0; ship < 5; ship++) return false; } // end bool ShipSunk(int shipLocs[5][4], bool isSunk[5], string board[11][11], int turn, string GuessP1[11][11]) bool ShipSunk (int shipLocs[5][4], bool isSunk[5], string board[11][11], int turn, int GuessP2[11][11]) { // determine if CPU's last guess sunk a ship. bool shipSunk = true; for (int ship = 0; ship < 5; ship++) { if (isSunk[ship]) { shipSunk = true; for (int r = shipLocs[ship][0]; r <= shipLocs[ship][2]; r++) { for (int c = shipLocs[ship][1]; c <= shipLocs[ship][3]; c++) { if (board[r][c] == "S") { shipSunk = false; break; } // end if board } // end for (int c = shipLocs[ship][1]; c <= shipLocs[ship][3]; c++) if (!shipSunk) { break; } // end if (!shipSunk) } // end for (int r = shipLocs[ship][0]; r <= shipLocs[ship][2]; r++) if (shipSunk) { // if shipsunk is true, the previous guess sunk the ship // update GuessP2 to indicate the sunk ship for (int r = shipLocs[ship][0]; r <= shipLocs[ship][2]; r++) { for (int c = shipLocs[ship][1]; c <= shipLocs[ship][3]; c++) { GuessP2[r][c] = 3; } // end for (int c = shipLocs[ship][1]; c <= shipLocs[ship][3]; c++) } // end for (int r = shipLocs[ship][0]; r <= shipLocs[ship][2]; r++) // update the ship's status isSunk[ship] = false; if (turn == 1) { cout << " You sunk the computer's "; } // end if (turn == 1) else { cout << " The CPU sank your "; } // end else: if (turn == 1) switch (ship) { case 0: cout << "Aircraft Carrier!\n"; break; case 1: cout << "Battleship!\n"; break; case 2: cout << "Cruiser!\n"; break; case 3: cout << "Submarine!\n"; break; case 4: cout << "Destroyer!\n"; break; } return true; } // end switch (ship) } // end if (isSunk[ship]){ } // end for (int ship = 0; ship < 5; ship++) return false; } // end bool ShipSunk(int shipLocs[5][4], bool isSunk[5], string board[11][11], int turn, int GuessP2[11][11]) bool isGameOver (bool isSunk[5]) { // check each ships status. // if a ship is alive, return false -- else return true; for (int i = 0; i < 5; i++) { if (isSunk[i]) return false; } return true; } // end bool isGameOver(bool isSunk[5]) bool chooseTile (string Board1[11][11], string Board2[11][11], string GuessP1[11][11], int p2LocShip[5][4], bool p2isSunk[5], string Display1[11][11], int GuessP2[11][11]) { // handle user's turn. // when the user sinks a ship, check to see if the ship was the last ship. // if the last ship was sunk, return true to alert main that the game is over. vector < char >userGuess; // vector to hold user input string userInput; // string for user guess int row; // user's row guess int col; // user's col guess bool tryAgain; // if their input is invalid -- true // print the game board printBoard (GuessP1, Board1, Display1, GuessP2); do { cout << " Where are we aiming our cannons: "; cin >> userInput; // this wouldn't have to be a char vector, // but the input for placing the user's ships does // so string interpreter was written to handle char vectors // ergo, char vector. for (int i = 0; i < userInput.size (); i++) { userGuess.push_back (userInput[i]); } // end for (int i = 0; i < userInput.size(); i++) tryAgain = stringInterpreter (userGuess, row, col); if (tryAgain) { userGuess.clear (); } // end if (tryAgain) } while (tryAgain); if (Board2[row][col] == "S") { cout << " You hit the CPU's ship!\n"; GuessP1[row][col] = "X"; Board2[row][col] = "X"; if (ShipSunk (p2LocShip, p2isSunk, Board2, 1, GuessP1)) { if (isGameOver (p2isSunk)) { return true; } } } else if (GuessP1[row][col] == "X" || GuessP1[row][col] == "O" || GuessP1[row][col] == "A" || GuessP1[row][col] == "B" || GuessP1[row][col] == "C" || GuessP1[row][col] == "S" || GuessP1[row][col] == "D") { cout << " You've already guessed this location. Try Again.\n"; if (chooseTile (Board1, Board2, GuessP1, p2LocShip, p2isSunk, Display1, GuessP2)) { return true; } } else { cout << " you missed :( \n"; GuessP1[row][col] = "O"; } // end else return false; } void getP1ShipLocs (string Board1[11][11], int shipLengths[], int p1LocShip[5][4], string Display1[11][11]) { for (int ships = 0; ships < 5; ships++) { cout << "\n-----------------------------------------------------------------------------------------------------------------------\n"; cout << "WELCOME TO BATTLESHIP \n"; cout << "-----------------------------------------------------------------------------------------------------------------------\n\n"; printBoardInput (Board1, Display1); setLocShips (Board1, ships, shipLengths, p1LocShip, Display1); system ("cls"); } } void BombingMostProbable (int GuessP2[11][11], int row, int col, int directions[4]) { int dir[4] = { 1, 1, 1, 1 }; // check North while ((GuessP2[row - dir[0]][col] == 0 || GuessP2[row - dir[0]][col] == 2) && row - dir[0] >= 1) { dir[0]++; } // end while ((GuessP2[row - dir[0]][col] == 0 || GuessP2[row - dir[0]][col] == 2) && row - dir[0] >= 1) // check South while ((GuessP2[row + dir[1]][col] == 0 || GuessP2[row + dir[1]][col] == 2) && row + dir[1] <= 10) { dir[1]++; } // end while ((GuessP2[row - dir[1]][col] == 0 || GuessP2[row - dir[1]][col] == 2) && row - dir[1] >= 1) // check East while ((GuessP2[row][col + dir[2]] == 0 || GuessP2[row][col + dir[2]] == 2) && col + dir[2] <= 10) { dir[2]++; } // end while ((GuessP2[row][col + dir[2]] == 0 || GuessP2[row][col + dir[2]] == 2) && col + dir[2] <= 10) // check West while ((GuessP2[row][col + dir[3]] == 0 || GuessP2[row][col + dir[3]] == 2) && col + dir[3] >= 1) { dir[3]++; } // end while ((GuessP2[row][col + dir[3]] == 0 || GuessP2[row][col + dir[3]] == 2) && col + dir[3] <= 10) int whichWay = 0; int maxValue = 0; int temp = 0; for (int j = 0; j < 4; j++) { maxValue = 0; for (int i = j; i < 4; i++) { if (dir[i] > maxValue) { maxValue = dir[i]; whichWay = i; } } temp = directions[j]; directions[j] = directions[whichWay]; directions[whichWay] = temp; temp = dir[j]; dir[j] = dir[whichWay]; dir[whichWay] = temp; } } // For void smartComp (int GuessP2[11][11], bool p1isSunk[5], int shipLengths[5], int &row, int &col) { int probabilityField[11][11] = { 0 }; bool possible; for (int ship = 0; ship < 5; ship++) { if (p1isSunk[ship]) { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10 - (shipLengths[ship] - 1); j++) { possible = true; for (int idx = 0; idx < shipLengths[ship]; idx++) { if (GuessP2[i][j + idx] == 1 || GuessP2[i][j + idx] == 3) { possible = false; break; } } if (possible) { for (int idx = 0; idx < shipLengths[ship] / 2; idx++) { probabilityField[i][j + idx] += idx + 1; probabilityField[i][j + shipLengths[ship] - 1 - idx] += idx + 1; } if (shipLengths[ship] % 2 == 1) { probabilityField[i][j + shipLengths[ship] / 2] += shipLengths[ship] / 2 + 1; } } } } } } // loop through each ship. for (int ship = 0; ship < 5; ship++) { if (p1isSunk[ship]) { // loop through each row. for (int i = 1; i <= 10 - (shipLengths[ship] - 1); i++) { for (int j = 1; j <= 10; j++) { possible = true; for (int idx = 0; idx < shipLengths[ship]; idx++) { if (GuessP2[i + idx][j] == 1 || GuessP2[i + idx][j] == 3) { possible = false; break; } // end if (GuessP2[i + idx][j] == 1 || GuessP2[i + idx][j] == 3) } // end for (int idx = 0; idx < shipLengths[ship]; idx++) if (possible) { // give the center of the possible ship location priority for (int idx = 0; idx < shipLengths[ship] / 2; idx++) { probabilityField[i + idx][j] += idx + 1; probabilityField[i + shipLengths[ship] - 1 - idx][j] += idx + 1; } // end for (int idx = 0; idx < shipLengths[ship]; idx++) if (shipLengths[ship] % 2 == 1) { probabilityField[i + shipLengths[ship] / 2][j] += shipLengths[ship] / 2 + 1; } // end if (shipLengths[ship] % 2 == 1) } // end if(possible) } // end for (int c = 1; c <= 10 - (shipLengths[ship] - 1); c++) } // end for (int r = 1; r <= 10; r++) // break; // uncomment to only worry about the largest possible ship } // end if (p1isSunk[ship]) } // end for (int ship = 0; ship < 5; ship++) // find the max value and randomly select one if there are more than one. vector < int >i_locs; vector < int >j_locs; // find the highest value int maxValue = 0; for (int i = 1; i < 11; i++) { for (int j = 1; j < 11; j++) { if (probabilityField[i][j] > maxValue && GuessP2[i][j] != 1 && GuessP2[i][j] != 2 && GuessP2[i][j] != 3) { maxValue = probabilityField[i][j]; } // end if (probabilityField[i][j] > maxValue && GuessP2[i][j] != 1 && GuessP2[i][j] != 2 && GuessP2[i][j] != 3) } // end for (int j = 1; j < 11; j++) } // end for (int i = 1; i < 11; i++) // find all the spots that have the highest value for (int i = 1; i < 11; i++) { for (int j = 1; j < 11; j++) { if (probabilityField[i][j] == maxValue && GuessP2[i][j] == 0) { i_locs.push_back (i); j_locs.push_back (j); } // end if (probabilityField[i][j] == maxValue && GuessP2[i][j] == 0) } // end for (int j = 1; j < 11; j++) } // end for (int i = 1; i < 11; i++) // randomly select one int randNum = rand () % i_locs.size (); row = i_locs.at (randNum); col = j_locs.at (randNum); } // end void smartComp(int GuessP2[11][11], bool p1isSunk[5], int shipLengths[5], int &row, int &col) void displayCPUGuess (int row, int col) { // interpret the guess into something the user will understand char letters[] = { ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' }; cout << "-----------------------------------------------------------------------------------------------------------------------\n"; cout << " The CPU guessed: " << letters[row] << col << "\n"; } // end void displayCPUGuess(int row, int col) bool BSAI (string Board1[11][11], int GuessP2[11][11], bool p1isSunk[5], int shipLengths[5], int p1LocShip[5][4]) { static bool Bombing = true; // state flag for remembering if the cpu is Bombing for a ship. static bool directionFound = false; // state flag for remembering if the cpu has found the orientation of the ship static bool bothDirections = false; // state flag for remembering if the cpu has found the orientation of the ship and checked both sides. static int directions[4] = { 1, 2, 3, 4 }; // for guessing the direction once a ship has been found. static int row = 0, col = 0; // for remembering the last cpu guess static int idx = 0; // for remembering how far away from the origin the cpu is now guessing. static bool findMostProbable = true; // for determining if you should calculate the most probable neighborhood shot static int whichWay = 0; // for keeping track of which neighborhoods have been shot at it. static int dRow = 0, dCol = 0; // for guessing at ship locations while no longer in hunt mode. *conserves original hit. static int pursuingDirection = 0; // for keeping track of the direction being pursued static int checkingDirection = 0; // for keeping track of the direction being checked. /* when the orientation is found, pursuing will equal checking */ if (Bombing) { for (int r = 0; r < 11; r++) { for (int c = 0; c < 11; c++) { if (GuessP2[r][c] == 2) { Bombing = false; directionFound = false; bothDirections = false; findMostProbable = true; for (int i = 0; i < 4; i++) { directions[i] = i + 1; } // end for (int i = 0; i < 4; i++) row = r; col = c; idx = 1; whichWay = 0; cout << " The CPU is moving on to the next found ship.\n"; if (BSAI (Board1, GuessP2, p1isSunk, shipLengths, p1LocShip)) { return true; } // end if (BSAI( Board1, GuessP2, p1isSunk, shipLengths, p1LocShip)) else { return false; } // end else: if (BSAI( Board1, GuessP2, p1isSunk, shipLengths, p1LocShip)) } // end if (GuessP2[r][c] == 2) } // end for (int c = 0; c < 11; c++) } // end for (int r = 0; r < 11; r++) // calculate the probability fields and choose a location to guess -- updating row and col by // pass by reference smartComp (GuessP2, p1isSunk, shipLengths, row, col); cout << "-----------------------------------------------------------------------------------------------------------------------\n"; cout << " The CPU is Bombing.\n"; displayCPUGuess (row, col); // display the cpu's guess if (Board1[row][col] == "S") { // the cpu hit a player's ship. cout << " The CPU hit your ship!\n"; Board1[row][col] = "X"; // update player 1's board GuessP2[row][col] = 2; // update the cpu's guessing matrix Bombing = false; // the cpu is no longer Bombing! idx = 1; // begin looking 1 away from the origin // the cpu hit a ship, so check if that ship got sunk. if (ShipSunk (p1LocShip, p1isSunk, Board1, 2, GuessP2)) { if (isGameOver (p1isSunk)) { return true; // the game is over. } // end if (isGameOver(p1isSunk)) // a ship was sunk, so reset the flags and begin Bombing Bombing = true; directionFound = false; bothDirections = false; findMostProbable = true; for (int i = 0; i < 4; i++) { directions[i] = i + 1; } // end for (int i = 0; i < 4; i++) } // end if (ShipSunk(p1LocShip, p1isSunk, Board1, 2)) } // end if ( Board1[row][col] == "S") else { // the cpu missed a player's ship cout << " The CPU missed your ship!\n"; Board1[row][col] = "O"; // update player 1's board GuessP2[row][col] = 1; // update the cpu's guessing matrix } // end else: if ( Board1[row][col] == "S") } // end if (Bombing) else { if (directionFound) { // keep guessing until you find a new // spot to guess. do { // North if (pursuingDirection == 1) { dRow = row - idx; dCol = col; } // end if (pursuingDirection == 1) // South else if (pursuingDirection == 2) { dRow = row + idx; dCol = col; } // end else if (pursuingDirection == 2) // East else if (pursuingDirection == 3) { dRow = row; dCol = col + idx; } // end else if (pursuingDirection == 3) // West else if (pursuingDirection == 4) { dRow = row; dCol = col - idx; } // end // end else if (pursuingDirection == 4) // skip over previous guesses that // hit a ship and the ship never sunk if (GuessP2[dRow][dCol] == 2) { idx++; } // end if (GuessP2[dRow][dCol] == 2) // if the direction is a dead end swap directions if (GuessP2[dRow][dCol] == 1 || GuessP2[dRow][dCol] == 3) { if (bothDirections) { cout << "-----------------------------------------------------------------------------------------------------------------------\n"; cout << " The CPU found multiple touching ships.\n"; directionFound = false; bothDirections = false; whichWay++; idx = 1; for (int i = 0; i < 4; i++) { directions[i] = i + 1; } // end for (int i = 0; i < 4; i++) if (BSAI (Board1, GuessP2, p1isSunk, shipLengths, p1LocShip)) { return true; } // end if (BSAI( Board1,GuessP2,p1isSunk,shipLengths,p1LocShip)) else { return false; } // end else: if (BSAI( Board1,GuessP2,p1isSunk,shipLengths,p1LocShip)) } // end if (bothDirections) else { bothDirections = true; if (pursuingDirection % 2 == 1) { pursuingDirection++; idx = 1; } // end if (pursuingDirection % 2 == 1) else { pursuingDirection--; idx = 1; } // end else: if (pursuingDirection % 2 == 1) } // end else: if (bothDirections) } // end if (GuessP2[dRow][dCol] == 1 || GuessP2[dRow][dCol] == 3) } while (GuessP2[dRow][dCol] != 0); cout << "-----------------------------------------------------------------------------------------------------------------------\n"; if (pursuingDirection == 1) { cout << " The CPU is pursuing North.\n"; } // end if (pursuingDirection == 1) else if (pursuingDirection == 2) { cout << " The CPU is pursuing South.\n"; } // end else if (pursuingDirection == 2) else if (pursuingDirection == 3) { cout << " The CPU is pursuing East.\n"; } // end else if (pursuingDirection == 3) else if (pursuingDirection == 4) { cout << " The CPU is pursuing West.\n"; } // end else if (pursuingDirection == 4) displayCPUGuess (dRow, dCol); // display the cpu's guess if (Board1[dRow][dCol] == "S") { // the cpu hit a player's ship. cout << " The CPU hit your ship!\n"; Board1[dRow][dCol] = "X"; // update player 1's board GuessP2[dRow][dCol] = 2; // update the cpu's guessing matrix idx++; // begin looking 1 away from the origin directionFound = true; // the cpu has discovered the orientation // the cpu hit a ship, so check if that ship got sunk. if (ShipSunk (p1LocShip, p1isSunk, Board1, 2, GuessP2)) { if (isGameOver (p1isSunk)) { return true; // the game is over. } // end if (isGameOver(p1isSunk)) // a ship was sunk, so reset the flags and begin Bombing Bombing = true; directionFound = false; bothDirections = false; findMostProbable = true; for (int i = 0; i < 4; i++) { directions[i] = i + 1; } // end for (int i = 0; i < 4; i++) } // end if (ShipSunk(p1LocShip, p1isSunk, Board1, 2)) } // end if ( Board1[dRow][dCol] == "S") else if (Board1[dRow][dCol] == " ") { // the cpu missed a player's ship cout << " The CPU missed your ship!\n"; Board1[dRow][dCol] = "O"; // update player 1's board GuessP2[dRow][dCol] = 1; // update the cpu's guessing matrix } // end else: if ( Board1[dRow][dCol] == "S") } // end if (directionFound) else { if (findMostProbable) { BombingMostProbable (GuessP2, row, col, directions); findMostProbable = false; whichWay = 0; } // end if (findMostProbable) do { checkingDirection = directions[whichWay]; // North if (checkingDirection == 1) { dRow = row - idx; dCol = col; } // end if (checkingDirection == 1) // South else if (checkingDirection == 2) { dRow = row + idx; dCol = col; } else if (checkingDirection == 3) { dRow = row; dCol = col + idx; } else if (checkingDirection == 4) { dRow = row; dCol = col - idx; } if (dRow >= 1 && dRow <= 10 && dCol >= 1 && dCol <= 10) { if (GuessP2[dRow][dCol] != 0) { whichWay++; } } else { whichWay++; } } while (GuessP2[dRow][dCol] != 0); cout << "-----------------------------------------------------------------------------------------------------------------------\n"; if (checkingDirection == 1) { cout << " The CPU is checking North.\n"; } else if (checkingDirection == 2) { cout << " The CPU is checking South.\n"; } // end else if (checkingDirection == 2) else if (checkingDirection == 3) { cout << " The CPU is checking East.\n"; } // end else if (checkingDirection == 3) else if (checkingDirection == 4) { cout << " The CPU is checking West.\n"; } // end else if (checkingDirection == 4) displayCPUGuess (dRow, dCol); // display the cpu's guess if (Board1[dRow][dCol] == "S") { cout << " The CPU hit your ship!\n"; cout << " The CPU has entered Destroyer Mode.\n"; Board1[dRow][dCol] = "X"; GuessP2[dRow][dCol] = 2; idx++; directionFound = true; pursuingDirection = checkingDirection; if (ShipSunk (p1LocShip, p1isSunk, Board1, 2, GuessP2)) { if (isGameOver (p1isSunk)) { return true; } Bombing = true; directionFound = false; bothDirections = false; findMostProbable = true; for (int i = 0; i < 4; i++) { directions[i] = i + 1; } } } else if (Board1[dRow][dCol] == " ") { cout << " The CPU missed your ship!\n"; Board1[dRow][dCol] = "O"; GuessP2[dRow][dCol] = 1; } } } return false; } void endPrintBoard (string Board1[11][11], string Display1[11][11], string Board2[11][11], string Display2[11][11], string GuessP1[11][11]) { cout << "-----------------------------------------------------------------------------------------------------------------------\n"; cout << "|" << " CPU Game Board Your Game Board " << "|\n"; cout << "-----------------------------------------------------------------------------------------------------------------------\n"; cout << "| |\n"; for (int r = 0; r < 11; r++) { cout << "|\t"; for (int c = 0; c < 11; c++) { if (r == 0) { cout << Board2[r][c] << " "; } else { if (Board2[r][c] == "S") { cout << Display2[r][c] << " | "; } // end if ( Board2[r][c] == "S" else if (Board2[r][c] == "X") { cout << Display2[r][c] << " | "; } // end else if ( Board2[r][c] == "X") else if (GuessP1[r][c] == "O") { cout << GuessP1[r][c] << " | "; } else if (Board2[r][c] == " ") { cout << Board2[r][c] << " | "; } else { cout << Board2[r][c] << " | "; } // end else } } cout << "\t\t"; for (int c = 0; c < 11; c++) { if (r == 0) { cout << Board1[r][c] << " "; } else { if (Board1[r][c] == "S") { cout << Display1[r][c] << " | "; } else if (Board1[r][c] == "X") { cout << Display1[r][c] << " | "; } else if (Board1[r][c] == "O") { cout << Board1[r][c] << " | "; } else if (Board1[r][c] == " ") { cout << Board1[r][c] << " | "; } else { cout << Board1[r][c] << " | "; } } } if (r == 0) { cout << " |"; } else { cout << " |"; } cout << "\n| \t ----------------------------------------- \t\t ----------------------------------------- |\n"; } // end for (int r = 0; r < 11; r++) cout << "| |"; cout << "\n-----------------------------------------------------------------------------------------------------------------------\n"; } void setDisplayBoard (string board[11][11], int locs[5][4]) { string shipNames[5] = { "A", "B", "C", "S", "D" }; for (int s = 0; s < 5; s++) { for (int r = locs[s][0]; r <= locs[s][2]; r++) { for (int c = locs[s][1]; c <= locs[s][3]; c++) { board[r][c] = shipNames[s]; } } } }

__MACOSX/._main.cpp