So im making a battleship game to help me learn the language, but im currently stuck on allowing the player to input custom ship positions onto the grid. *Some issues on the copy and paste with indenting*
#include <iostream>
#include <ctime>
#include <random>
#include <string>
//Set up Variables
//Set up Menu
//Set Up AI Grid
//Set up Player Grid
//Set up Playerships
usingnamespace std;
constint row = 10; // Sets up Grid*
constint collum = 10; // Sets up grid
constchar water = 247; // Water on grid
constchar hit = 'x'; //Displyed when a ship is hit
constchar Pship = 's'; //Displayed where a player places a ship
int maxship = 5; //Sets Max # of ships on AI board
int matrix[row][collum]; //*
constint shipnum = 5;
void clear() // Clears the grid
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < collum; j++)
{
matrix[i][j] = 0;
}
}
}
void show() //Displays the Players grid
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < collum; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int numship() //Tells the player how many AI ships are left
{
int c = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < collum; j++)
{
if (matrix[i][j] == 3) //
c++;
}
}
return c;
}
void setships() //Sets up the ships randomly for the AI player (Sets AI ships to 3 on the array/ AI Grid)
{
int s = 0;
while (s < maxship)
{
int x = rand() % row;
int y = rand() % collum;
if (matrix[x][y] != 3)
{
s++;
matrix[x][y] = 3;
}
}
}
bool attack(int x, int y) //Allows the player to fire on X,Y Cords of their choice
{
if (matrix[x][y] == 3)
{
matrix[x][y] = 2;
returntrue;
}
returnfalse;
}
int main()
{
bool Quit = 0; //If Quit = 1 it will exit the program
//Player Selection Menu
Begin:
cout << "1: Play Game \n""2: Quit Game \n""3: Game Credits \n""4: How To Play \n";
int Select; //Variable to allow player to select a option
cout << "Please Enter a Number from the Options: ";
cin >> Select;
if (Select == 1)
{
cout << "\n""\n""\n";
srand(time(NULL));
clear();
show();
cout << "------------------------" << endl;
setships(); // *
show(); // Shows AI Board (Testing Feature only)*
int pos1, pos2;
while (1)
{
cout << "Please input Location (X then Y):"; cin >> pos1 >> pos2; // Asking player where to "fire"
if (attack(pos1, pos2)) //If the hit is succesful
cout << "Hit succesful" << endl;
else
cout << "Hit Failed" << endl; //If there is no hit
cout << "Remaining Ships: " << numship() << endl;
}
system("pause");
return 0;
}
}
are you asking to let the player set their starting positions?
just copy setships, and replace the randoms with user input that you validate to be in range.
int main()
{
srand(time(nullptr));
for (bool quit = false; !quit; )
{
cout << "1: Play Game\n""2: Quit Game\n""3: Game Credits\n""4: How To Play\n";
int select;
cout << "Please enter a number from the options: ";
if (!(cin >> select)) {
cin.clear();
cin.ignore(99999, '\n');
select = -1; // force default case in switch stmt
}
switch (select)
{
case 1:
play_game();
break;
case 2:
quit = true;
break;
case 3:
show_credits();
break;
case 4:
show_instructions();
break;
default:
cout << "\nBad input. Try again\n\n";
}
}
}