c++ program Solitaire Battleship: I've posted in the pub/prog4 folder: AssignmentBase.cpp solution.o Since we aren't using file i/o for this assignment, solution.o will be executable by default. Refer to previous assignments for instructions on copying down the provided files. You only have to write the bodies for the functions: displayBoard calculateAttack checkGameOver I have provided: main initBoard For this assignment you can change any code you want, I will only grade you on output. I don't mind if you change the functions or simply don't use functions, or you use vectors instead of arrays. I would suggest that functions and arrays are the easiest / most intuitive way to approach the problem, but you don't have to use my functions if you don't want to. I would recommend against modifying initBoard as you will get points off if your game board is not the same as the solution's, even if the program is conceptually correct. The conceptual idea: The user will first enter an integer value which will be used to seed the random number generator, this will ensure the generation of a reliable board. (Every time you start a new program and enter 1253 for the seed value, you should get the same ship layout). the 2-dimensional waterState array represents a game board, which has a certain number (6) of horizontal or vertical boats of length 5 on it. Initially all board spaces are set by initBoard to either W_UNK_BOARD or W_UNK_EMPTY. These values should be displayed by displayBoard to the user as #. (Both are hidden). The user will input a 2-value location to fire an attack. The attack centered on the square will expose and destroy everything on the vertical and horizontal line (this may be easier to explain by running the solution program). The game is over when either all ships are exposed (destroyed) or when the user exhausts the maximum number of turns (10).
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!\n";
}
else{
cout << "You Lose!\n";
}
}
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!
c++ program:
#include <iostream>
#include <string>
using namespace std;
const int BOARD_WIDTH = 15;
const int BOARD_HEIGHT = 10;
const int SHIP_TYPES = 5;
const char isWATER = 247;
const char isHIT = 'X';
const char isSHIP = 'S';
const char isMISS = '0';
struct POINT {
int X;
int Y;
};
struct SHIP {
string name;
int length;
POINT onGrid[5];
bool hitFlag[5];
}ship[SHIP_TYPES];
struct PLAYER {
char
grid[BOARD_WIDTH][BOARD_HEIGHT];
}player[3];
enum DIRECTION {HORIZONTAL,VERTICAL};
struct PLACESHIPS {
DIRECTION
direction;
SHIP shipType;
};
bool gameRunning = false;
//Functions
void LoadShips();
void ResetBoard();
void DrawBoard(int);
PLACESHIPS UserInputShipPlacement();
bool UserInputAttack(int&,int&,int);
bool GameOverCheck(int);
int main()
{
LoadShips();
ResetBoard();
for (int aplyr=1;
aplyr<3; ++aplyr)
{
for (int thisShip=0; thisShip<SHIP_TYPES; ++thisShip)
{
system("cls");
DrawBoard(aplyr);
cout << "\n";
cout << "INSTRUCTIONS (Player " << aplyr <<
")\n\n";
cout << "You are about to place your ships. Format should
be:\n";
cout << "Facing (0:Horizontal,1:Vertical), X (top-row)
coords, Y (left-side) coords\n";
cout << "Example: 0 7 2 This would place a
ship beginning at X:7 Y:2 going horizontal\n\n";
cout << "Ship to place: " << ship[thisShip].name
<< " which has a length of " << ship[thisShip].length
<< "\n";
cout << "Where do you want it placed? ";
PLACESHIPS aShip;
aShip.shipType.onGrid[0].X = -1;
while (aShip.shipType.onGrid[0].X == -1)
{
aShip = UserInputShipPlacement();
}
aShip.shipType.length = ship[thisShip].length;
aShip.shipType.name = ship[thisShip].name;
player[aplyr].grid[aShip.shipType.onGrid[0].X][aShip.shipType.onGrid[0].Y]
= isSHIP;
for (int i=1; i<aShip.shipType.length; ++i)
{
if (aShip.direction == HORIZONTAL){
aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X+1;
aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y; }
if (aShip.direction == VERTICAL){
aShip.shipType.onGrid[i].Y = aShip.shipType.onGrid[i-1].Y+1;
aShip.shipType.onGrid[i].X = aShip.shipType.onGrid[i-1].X; }
player[aplyr].grid[aShip.shipType.onGrid[i].X][aShip.shipType.onGrid[i].Y]
= isSHIP;
}
}
}
gameRunning =
true;
int thisPlayer =
1;
do {
int enemyPlayer;
if (thisPlayer == 1) enemyPlayer = 2;
if (thisPlayer == 2) enemyPlayer = 1;
system("cls");
DrawBoard(enemyPlayer);
bool goodInput = false;
int x,y;
while (goodInput == false) {
goodInput = UserInputAttack(x,y,thisPlayer);
}
if (player[enemyPlayer].grid[x][y] == isSHIP)
player[enemyPlayer].grid[x][y] = isHIT;
if (player[enemyPlayer].grid[x][y] == isWATER)
player[enemyPlayer].grid[x][y] = isMISS;
int aWin = GameOverCheck(enemyPlayer);
if (aWin != 0) {
gameRunning = false;
break;
}
thisPlayer = (thisPlayer == 1) ? 2 : 1;
} while
(gameRunning);
system("cls");
cout <<
"\n\nCONGRATULATIONS!!! PLAYER " << thisPlayer << " HAS
WON THE GAME!\n\n\n\n";
system("pause");
return 0;
}
bool GameOverCheck(int enemyPLAYER)
{
bool winner =
true;
for (int w=0;
w<BOARD_WIDTH; ++w){
for (int h=0; h<BOARD_HEIGHT; ++h){
//If any ships remain, game is NOT over
if (player[enemyPLAYER].grid[w][h] = isSHIP)
{
winner = false;
return winner;
}
}}
return winner;
}
bool UserInputAttack(int& x, int& y, int theplayer)
{
cout << "\nPLAYER
" << theplayer << ", ENTER COORDINATES TO ATTACK:
";
bool goodInput =
false;
cin >> x >>
y;
if (x<0 ||
x>=BOARD_WIDTH) return goodInput;
if (y<0 ||
y>=BOARD_HEIGHT) return goodInput;
goodInput = true;
return goodInput;
}
PLACESHIPS UserInputShipPlacement()
{
int d, x, y;
PLACESHIPS tmp;
tmp.shipType.onGrid[0].X
= -1;
cin >> d >>
x >> y;
if (d!=0 &&
d!=1) return tmp;
if (x<0 ||
x>=BOARD_WIDTH) return tmp;
if (y<0 ||
y>=BOARD_HEIGHT) return tmp;
tmp.direction =
(DIRECTION)d;
tmp.shipType.onGrid[0].X
= x;
tmp.shipType.onGrid[0].Y
= y;
return tmp;
}
void LoadShips()
{
ship[0].name =
"Cruiser"; ship[0].length = 2;
ship[1].name =
"Frigate"; ship[1].length = 3;
ship[2].name =
"Submarine"; ship[2].length = 3;
ship[3].name = "Escort";
ship[3].length = 4;
ship[4].name =
"Battleship"; ship[4].length = 5;
}
void ResetBoard()
{
for (int plyr=1;
plyr<3; ++plyr)
{
for (int w=0; w<BOARD_WIDTH; ++w){
for (int h=0; h<BOARD_HEIGHT; ++h){
player[plyr].grid[w][h] = isWATER;
}}
}
}
void DrawBoard(int thisPlayer)
{
cout <<
"PLAYER " << thisPlayer << "'s GAME BOARD\n";
cout <<
"----------------------\n";
cout <<
" ";
for (int w=0;
w<BOARD_WIDTH; ++w) {
if (w < 10)
cout << w << " ";
else if (w >= 10)
cout << w << " ";
}
cout << "\n";
for (int h=0;
h<BOARD_HEIGHT; ++h){
for (int w=0; w<BOARD_WIDTH; ++w){
if (w==0) cout << h << " ";
if (w<10 && w==0) cout << " ";
if (gameRunning == false) cout <<
player[thisPlayer].grid[w][h] << " ";
if (gameRunning == true && player[thisPlayer].grid[w][h] !=
isSHIP)
{cout << player[thisPlayer].grid[w][h] << " ";}
else if (gameRunning == true &&
player[thisPlayer].grid[w][h] == isSHIP)
{cout << isWATER << " ";}
if (w == BOARD_WIDTH-1) cout << "\n";
}
}
}
c++ program Solitaire Battleship: I've posted in the pub/prog4 folder: AssignmentBase.cpp solution.o Since we aren't using...
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...
In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...
Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...
Similar to the Yahtzee project you completed earlier, in order to really understand the artificial intelligence and how to improve it; you must first have a good grasp on the game, its concepts, and its rules. For your first assignment, download the linked file below. This is a .cpp file of the game Tic-Tac-Toe. Tic-Tac-Toe Unzip the file, and run the game. Play a few games and begin to analyze the artificial intelligence that is currently programmed. Then, review the...
I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...
I have a question about C++. I have to code the 8 queens puzzle without using any recursive methods. Every time the program runs, it will randomly place 8 queens throughout the board. I got that working, but the issue is the board is suppose to be filled with asterisks and the queens are suppose to be represented with a Q. I have put zeros instead of asterisks and ones instead of Qs, i tried to fix it but it...
I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...
In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...
Program in C computer language In this program you will be mimicking a game of Battleship. Your game board is a line of 10 slots. You have to implement 3 methods. The following methods must be implemented: void setBoard(int *) This method sets up the human’s game board. The method prompts the user for 2 slots to place the “ship”. The program should ensure that the slots are consecutive. In other words, your ship cannot be placed at slot 2...
Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...