Ok I'm a total scrub at C++ and need all the direction I can get.
I'm tasked with creating a 2-player Battleships game and so far have this code with the grid and asking the players to enter their 1 space boat positions.
I'm stuck on the next step of actually starting to play the game and having each player guess the ship co-ordinates.
Any help to head me in the right direction will be much appreciated.
while (the number of pieces alive for player 1 is not 0 AND the number of pieces alive for player 2 is not 0)
{
display board for player 1
ask player 1 to enter guessed coordinates
if (there's a 'P' at said coordinates on player 2's board)
{
display message saying a boat was hit
remove the 'P' on player 2's board and put a 'X' instead
player2.numberOfPiecesAlive --;
}
else
{
display message saying it was a miss
}
exactly the same as above for player 2
}
check who has no boats left
display who's the winner
end of game.
I hope this is what you were looking for and that this helps in any way
while (the number of pieces alive for player 1 is not 0 AND the number of pieces alive for player 2 is not 0)
{
display board for player 1
ask player 1 to enter guessed coordinates
if (there's a 'P' at said coordinates on player 2's board)
{
display message saying a boat was hit
remove the 'P' on player 2's board and put a 'X' instead
player2.numberOfPiecesAlive --;
}
else
{
display message saying it was a miss
}
exactly the same as above for player 2
}
check who has no boats left
display who's the winner
end of game.
display message saying a boat was hit
remove the 'P' on player 2's board and put a 'X' instead
player2.numberOfPiecesAlive --;
this is what I mean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//player one just chose x and y
if (player2.pieceBoard[chosenY][chosenX] == 'P')
{
cout << "\nHit a boat!":
player1.otherBoard[chosenY][chosenX} = 'X';
player2.pieceBoard[chosenY][chosenX] == 'X'
player2.numberOfPiecesAlive -= 1;
}
else
{
cout << "\nYou missed all the boats.."
player1.otherBoard[chosenY][chosenX} = 'M'; //M for miss, or anything you like
}
Stuck on the last while part that determines if the game is still going or finished. I'm figuring the code is going to take the numberOfPiecesAlive for p1 and p2 and if it's greater than 0 then keep going.
I'm getting this error now as I'm sure my code is horribly incorrect.
-
bs2.cpp: At global scope:
bs2.cpp:198:1: error: expected unqualified-id before 'while'
while(p1.numberOfPiecesAlive =>0 AND p2.numberOfPiecesAlive =>0)
^~~~~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while(p1.numberOfPiecesAlive =>0 AND p2.numberOfPiecesAlive =>0)
{
//player one just chose x and y
if (player2.pieceBoard[chosenY][chosenX] == 'P')
{
cout << "\nHit a boat!":
player1.otherBoard[chosenY][chosenX} = 'X';
player2.pieceBoard[chosenY][chosenX] == 'X'
player2.numberOfPiecesAlive -= 1;
}
else
{
cout << "\nYou missed all the boats.."
player1.otherBoard[chosenY][chosenX} = 'M'; //M for miss, or anything you like
}
Sorry for the late reply, I was away for a few days.
Yes there is a problem with your while loop statement.
For logical connectors such as "and" "or" you don't simply use "AND" or "OR".
for "and" you use the operator "&&"
for "or" you use the operator "||"
so your while loop should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while(p1.numberOfPiecesAlive =>0 && p2.numberOfPiecesAlive =>0) //Replace and by &&
{
//player one just chose x and y
if (player2.pieceBoard[chosenY][chosenX] == 'P')
{
cout << "\nHit a boat!":
player1.otherBoard[chosenY][chosenX} = 'X';
player2.pieceBoard[chosenY][chosenX] == 'X'
player2.numberOfPiecesAlive -= 1;
}
else
{
cout << "\nYou missed all the boats.."
player1.otherBoard[chosenY][chosenX} = 'M'; //M for miss, or anything you like
}
also be aware of logic vs bit operations. & is bits, as in 0011 & 0000 is 0000, while x && y is a boolean true or false value. That is why the && is doubled, to differentiate between logical and bit operations. Do not confuse them as it will compile either way, but give very, very different answers!
Thanks for getting back to me guys, I made the suggested changes and now I'm getting these errors now when compiling with the above code that I changed.
1 2 3 4 5 6 7 8
bs2.cpp: In function 'player_t initPlayer(player_t)':
bs2.cpp:57:4: error: 'struct player_t' has no member named 'numberOfPiecesAlive'; did you mean 'numerOfPiecesAlive'?
p.numberOfPiecesAlive = NUMBER_OF_PIECES;
^~~~~~~~~~~~~~~~~~~
bs2.cpp: At global scope:
bs2.cpp:198:1: error: expected unqualified-id before 'while'while(p1.numberOfPiecesAlive =>0 && p2.numberOfPiecesAlive =>0) //Replace and by &&
^~~~~
bs2.cpp:57:4: error: 'struct player_t' has no member named 'numberOfPiecesAlive'; did you mean 'numerOfPiecesAlive'?
You probably made a typo either in the variable you used in your code or in your structure definition 'numberOfPiecesAlive'/'numerOfPiecesAlive' there's a b missing in "number" for the second one.
Can you post your code? there seems to be something wrong before your while loop.
struct player_t
{
std::string name;
char pieceBoard[GRID_SIZE][GRID_SIZE];
char otherBoard[GRID_SIZE][GRID_SIZE];
int numberOfPieces;
int numberOfPiecesAlive; //ADDED "B" to make "numer" into "number"
};
Your while function is out of any function. That might be why you're getting an error
while(p1.numberOfPiecesAlive =>0 && p2.numberOfPiecesAlive =>0)
{
//player one just chose x and y
if (player2.pieceBoard[chosenY][chosenX] == 'P')
{
cout << "\nHit a boat!":
player1.otherBoard[chosenY][chosenX} = 'X';
player2.pieceBoard[chosenY][chosenX] == 'X'
player2.numberOfPiecesAlive -= 1;
}
else
{
cout << "\nYou missed all the boats.."
player1.otherBoard[chosenY][chosenX} = 'M'; //M for miss, or anything you like
}
void function(agruments...)
{
while(p1.numberOfPiecesAlive =>0 && p2.numberOfPiecesAlive =>0) //Replace and by &&
{
//player one just chose x and y
if (player2.pieceBoard[chosenY][chosenX] == 'P')
{
cout << "\nHit a boat!":
player1.otherBoard[chosenY][chosenX} = 'X';
player2.pieceBoard[chosenY][chosenX] == 'X'
player2.numberOfPiecesAlive -= 1;
}
else
{
cout << "\nYou missed all the boats.."
player1.otherBoard[chosenY][chosenX} = 'M'; //M for miss, or anything you like
}
return;
}
or put it where it belongs in main:
1 2 3 4 5 6
int main()
{
...
//WHILE LOOP HERE
}
As a general rule you can't have statements such as loops outside of functions in a .cpp file. Okay you can have global variables and constants but those are generally not advised.
while(p1.numberOfPiecesAlive =>0 && p2.numberOfPiecesAlive =>0) //Replace and by &&
{
//player one just chose x and y
if (player2.pieceBoard[chosenY][chosenX] == 'P')
{
cout << "\nHit a boat!":
player1.otherBoard[chosenY][chosenX} = 'X';
player2.pieceBoard[chosenY][chosenX] == 'X'
player2.numberOfPiecesAlive -= 1;
}
else
{
cout << "\nYou missed all the boats.."
player1.otherBoard[chosenY][chosenX} = 'M'; //M for miss, or anything you like
}
return;
}
to
1 2 3 4 5 6 7 8 9
int main(){}
//While Loop Here
void initBoard(char board[][GRID_SIZE]){}
void displayBoard(char board[][GRID_SIZE]){}
void placePieces(std::string name, char board[][GRID_SIZE]){}
void saveGame(player_t p1, player_t p2){}
void loadGame(player_t *p1, player_t *p2){}
//Or here