C++ Program: Instructions: Sample template program (does not need to stay in thi
ID: 3686259 • Letter: C
Question
C++ Program:
Instructions:
Sample template program (does not need to stay in this form):
#include<iostream>
#include<cstdlib>
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! ";
}
}
Sample program run:
Lose:
Enter a number for RND seed5
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
Enter a zero-indexed x y location to fire!4
5
FIRING:4 5
####.####################
####.####################
####.####################
####.####################
####.####################
.........................
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
####.####################
Enter a zero-indexed x y location to fire!^C
cs125366@classes:~/prog4$ ./solution.o
Enter a number for RND seed4
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
#########################
Enter a zero-indexed x y location to fire!5
4
FIRING:5 4
#####.###################
#####.###################
#####.###################
#####.###################
........@....@...........
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####@###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
#####.###################
Enter a zero-indexed x y location to fire!6
4
FIRING:6 4
#####..##################
#####..##################
#####..##################
#####..##################
........@....@...........
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####@.##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
#####..##################
Enter a zero-indexed x y location to fire!7
3
FIRING:7 3
#####...#################
#####...#################
#####...#################
........@....@...........
........@....@...........
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####@..#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
#####...#################
Enter a zero-indexed x y location to fire!4
5
FIRING:4 5
####....#################
####....#################
####....#################
........@....@...........
........@....@...........
........@................
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
####@@..#################
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
####....#################
Enter a zero-indexed x y location to fire!2
3
FIRING:2 3
##.#....#################
##.#....#################
##.#....#################
........@....@...........
........@....@...........
........@................
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##@#@@..#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
Enter a zero-indexed x y location to fire!4
13
FIRING:4 13
##.#....#################
##.#....#################
##.#....#################
........@....@...........
........@....@...........
........@................
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
.@@@@@.............@.....
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
Enter a zero-indexed x y location to fire!2
3
FIRING:2 3
##.#....#################
##.#....#################
##.#....#################
........@....@...........
........@....@...........
........@................
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
.@@@@@.............@.....
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
Enter a zero-indexed x y location to fire!4
5
FIRING:4 5
##.#....#################
##.#....#################
##.#....#################
........@....@...........
........@....@...........
........@................
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
.@@@@@.............@.....
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
Enter a zero-indexed x y location to fire!6
7
FIRING:6 7
##.#....#################
##.#....#################
##.#....#################
........@....@...........
........@....@...........
........@................
##.#....#################
.........................
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
.@@@@@.............@.....
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
##.#....#################
Enter a zero-indexed x y location to fire!6
5
FIRING:6 5
You Lose!
Win:
You Win!
Explanation / Answer
Hi below i have written the sample battleship program in C++ for your reference,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.