I want to make the code so a user can manually input 5 different types of "ships"
e.g
Aircraft Carrier 5 long
Patrol Boat 4 long etc all different lengths
Ive already tried using a string and create 5 different classes but to no avail and now im stuck.
I want to input them on the function clear, but am not sure how. Any input/help is greatly appreciated
#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 setshipsuser() //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;
}
}
}
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)*
cout << "------------------------" << endl;
// setshipsuser();
// show();
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;
}
if (numship == 0)
cout << "Remaining Ships: " << numship() << endl;
cout << "You are out of ships!";
system("pause");
return 0;
}
}
Did you say you wanted to make a class for every ship they enter? That doesn't seem necessary if that's what you intend. You can make one class with all the variables needed.
Otherwise, you can input what the user wants like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <string>
int main()
{
std::string ships[5];
for (int input = 0; input < 5; input++)
{
std::cout << "Enter A Ship: ";
std::getline(std::cin, ships[input]);
std::cout << '\n';
}
//Output the string
std::cout << '\n';
for (int i = 0; i < 5; i++)
std::cout << ships[i] << '\n';
}
Then, if you want to use it in the function clear, simply give the variable to it as you've done for other variables on line 103/153