Battleship Source Code
Battleship Source Code
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
#include <stdlib.h>
#include "ConsoleColors.h"
using namespace std;
using namespace eku;
//
//
//
//
//
//
// Function Prototypes:
// for printing the board to the console during player 1 ship input
void printBoardInput(string gameBoard[11][11], string p1Display[11][11]);
// for printing the game boards to the console during game play
void printBoard(string p1Guesses[11][11], string p1Board[11][11],
string p1Display[11][11], int Guesses[11][11]);
// for printing the game boards to the console at the end of the game: The Reveal
void endPrintBoard(string p1Board[11][11], string p1Display[11][11],
string p2Board[11][11], string p2Display[11][11],
string p1Guesses[11][11]);
p1Board[11][11] = {
" ", "1", "2", "3",
"A", " ", " ", " ",
"B", " ", " ", " ",
"C", " ", " ", " ",
"D", " ", " ", " ",
"E", " ", " ", " ",
"F", " ", " ", " ",
"G", " ", " ", " ",
"H", " ", " ", " ",
"I", " ", " ", " ",
"J", " ", " ", " ",
"4",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"5",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"6",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"7",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"8",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"9",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"10" },
" " },
" " },
" " },
" " },
" " },
" " },
" " },
" " },
" " },
" " }
"G",
"H",
"I",
"J",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
",
",
",
",
"
"
"
"
"
"
"
"
},
},
},
}
};
// p2Board will store the locations of the CPU ships
//
Will also store when a CPU ship gets hit with an "X"
string p2Board[11][11] = {
{ " ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" },
{ "A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
};
// p2Display will store the ship locations and the letters that
// correspond to the specific ship for the CPU.
//
A - Aircraft Carrier
//
B - Battleship
//
C - Cruiser
//
S - Submarine
//
D - Destroyer
string p2Display[11][11] = {
{ " ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" },
{ "A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
",
",
",
",
",
",
",
",
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
},
},
},
},
},
},
},
}
};
// p2Guesses will store the guesses of the computer
//
value
meaning
//
----------//
0
unvisited space
//
1
miss
//
2
Damaged Ship
//
3
Sunk Ship
int p2Guesses[11][11] = { 0 };
int
int
int
} // end if (turn == 1)
else{
// CPU will guess
//
CPU_Fire will return true if its guess
//
results in the user losing their last ship
if (BSAI(p1Board, p2Guesses, p1ShipStatus, shipLengths, p1ShipLocations)){
cout << "\n----------------------------------------------------------------------------------------------------------------------\n";
max[] = { 6, 5, 4, 4, 3 }; //
//
bool isPlaced = false;
//
int row = 0;
//
int col = 0;
//
// 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 && p2Guesses[i][j] != 1
&& p2Guesses[i][j] != 2 && p2Guesses[i][j] != 3){
maxValue = probabilityField[i][j];
} // end if (probabilityField[i][j] > maxValue && p2Guesses[i][j] != 1 &&
p2Guesses[i][j] != 2 && p2Guesses[i][j] != 3)
} // end for (int j = 1; j < 11; j++)
} // end for (int i = 1; i < 11; i++)
//######################################################################################
###########################//
// if you're hunting and there's a partially damaged ship,
// it means that you hit an adjacent ship while sinking the previous
// one, so now sink that adjacent ship before continuing to hunt.
for (int r = 0; r < 11; r++){
for (int c = 0; c < 11; c++){
if (p2Guesses[r][c] == 2){
Hunting = 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;
#endif
//_INC_EKU_IO_CONCOL