Intro to Programming [C++ Language]: Write the bodies for the functions: -------
ID: 3688341 • Letter: I
Question
Intro to Programming [C++ Language]: Write the bodies for the functions:
--------------------------------------------------------------------------------------------------------------
#include
#include
using namespace std;
const int BOARDSIZE= 25;
const int BOATCOUNT = 6;
const int BOATLEN = 5;
const int W_UNK_BOAT = 0;
const int W_UNK_EMPTY = 1;
const int W_EXP_BOAT = 2;
const int W_EXP_EMPTY = 3;
void displayBoard(const int board[BOARDSIZE][BOARDSIZE]);
void calculateAttack(int board[BOARDSIZE][BOARDSIZE], int target[2]);
bool checkGameOver(const int board[BOARDSIZE][BOARDSIZE]);
void initBoard(int board[BOARDSIZE][BOARDSIZE]){
for(int r = 0; r < BOARDSIZE; r++){
for(int c = 0; c < BOARDSIZE; c++){
board[r][c] = W_UNK_EMPTY;
}
}
for (int i = 0; i < BOATCOUNT; i++){
int bx = rand() % BOARDSIZE;
int by = rand() % BOARDSIZE;
int horiz = rand() % 2;
int minbx, maxbx;
if(bx > BOARDSIZE / 2){
minbx = bx - BOATLEN;
maxbx = bx;
}
else{
minbx = bx;
maxbx = bx + BOATLEN;
}
for(int x = minbx; x < maxbx; x++){
if(horiz){
board[x][by] = W_UNK_BOAT;
}
else{
board[by][x] = W_UNK_BOAT;
}
}
}
}
int main(){
int waterState[BOARDSIZE][BOARDSIZE];
const int MAXTURNS = 10;
bool playingGame = true;
int target[2];
cout << "Enter a number for RND seed";
long sval;
cin >> sval;
srand(sval);
initBoard(waterState);
int turns = 0;
while(playingGame && turns < MAXTURNS){
displayBoard(waterState);
cout << "Enter a zero-indexed x y location to fire!";
cin >> target[0] >> target[1];
calculateAttack(waterState, target);
playingGame = checkGameOver(waterState);
turns = turns + 1;
}
if(!playingGame){
cout << "You Win! ";
}
else{
cout << "You Lose! ";
}
}
When you hit:
Enter a number for RND seed123 Enter a zero-indexed x y location to fire!12 12Explanation / Answer
public static void displayBoard(int board[BOARDSIZE][BOARDSIZE])
{
int cont = 0;
int orientation = 0;
int direction;
int x;
int y;
int emptySquare = 1;
while (!cont)
{
emptySquare = 1;
orientation = rand() % 2;
direction = rand() % 2;
x = ran() % BOARDSIZE;
y = ran() % BOARDSIZE;
//vertical
if (orientation)
{
//placed to the right
if (direction)
{
//both points are one the board
if (y + BOARDSIZE <= 7)
{
for (int i = y; i < y + BOARDSIZE; i++)
{
//square is already occupied
if (board[x][i] != 0)
{
emptySquare = 0;
}
}
//ship can be placed here
if (emptySquare)
{
for (int i = y; i < y + BOARDSIZE; i++)
{
board[x][i] = BOARDSIZE;
}
}
}
}
//placed to the left
else
{
if (y - BOARDSIZE>= 0)
{
for (int i = y; i > y - BOARDSIZE; i--)
{
//square is already occupied
if (board[x][i] != 0)
{
emptySquare = 0;
}
}
//ship can be placed here
if (emptySquare)
{
for (int i = y; i > y - BOARDSIZE; i--)
{
board[x][i] = BOARDSIZE;
}
}
}
}
}
//horizontal
if (!orientation)
{
//placed upward
if (!direction)
{
//both points are one the board
if (x - BOARDSIZE >= 0)
{
for (int i = x; i > x - BOARDSIZE; i--)
{
//square is already occupied
if (board[i][y] != 0)
{
emptySquare = 0;
}
}
//ship can be placed here
if (emptySquare)
{
for (int i = x; i > x - BOARDSIZE; i--)
{
board[i][y] = BOARDSIZE;
}
}
}
}
//placed downward
else
{
if (x + BOARDSIZE <= 7)
{
for (int i = x; i < x + BOARDSIZE; i++)
{
//square is already occupied
if (board[i][y] != 0)
{
emptySquare = 0;
}
}
//ship can be placed here
if (emptySquare)
{
for (int i = x; i < x + BOARDSIZE; i++)
{
board[i][y] = BOARDSIZE;
}
}
}
}
}
}
}
public boolean checkForGameOver(int board[BOARDSIZE][BOARDSIZE])
{
for (int row = 0; row < BOARDSIZE; row++)
{
for (int col = 0; col < BOARDSIZE; col++)
{
//if the board doesnt contain a ship or if it contains a ship but the position has already been located
if (board[row][col] != 0 && board[row][col] != -1)
{
return false;
}
}
}
return true;
}
public void calculateAttack(int board[BOARDSIZE][BOARDSIZE], int target[2])
{
//if the target was a miss
if (board[BOARDSIZE][BOARDSIZE] == 0)
{
board[BOARDSIZE][BOARDSIZE].SetConsoleTextAttribute(console,0);
board[BOARDSIZE][BOARDSIZE] = 0;
}
//if the target hit a minesweeper
if (board[BOARDSIZE][BOARDSIZE] == 2)
{
board[BOARDSIZE][BOARDSIZE].SetConsoleTextAttribute(console, 2);
board[BOARDSIZE][BOARDSIZE] = -1;
}
//if the target hit a frigate
if (board[BOARDSIZE][BOARDSIZE] == 3)
{
board[BOARDSIZE][BOARDSIZE].SetConsoleTextAttribute(console, 1);
board[BOARDSIZE][BOARDSIZE] = -1;
}
//if the target hit a cruiser
if (board[BOARDSIZE][BOARDSIZE] == 4)
{
board[BOARDSIZE][BOARDSIZE].SetConsoleTextAttribute(console, 4);
board[BOARDSIZE][BOARDSIZE] = -1;
}
//if the target hit a battleship
if (board[BOARDSIZE][BOARDSIZE] == 5)
{
board[BOARDSIZE][BOARDSIZE].SetConsoleTextAttribute(console, 14);
board[BOARDSIZE][BOARDSIZE] = -1;
}
if (board[BOARDSIZE][BOARDSIZE] == target[2])
{
board[BOARDSIZE][BOARDSIZE].SetConsoleTextAttribute(console, 5);
board[BOARDSIZE][BOARDSIZE] = -1;
}
if (checkForGameOver())
{
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.