Hey guys! I've started working on some coding aspects of my Pong game and started off with setting up the game to determine when to end the game when only looking at the number of points each player has and then use that information to determine which player won.
Right now I have a while loop set up to check to see how many points each player has and if at the end of the loop one of the two players has 10 points (this is also the number of points required to win) the program exits the loop and tells you which player won. I have followed this up with an if/else check to determine the winner.
I've run the check a few different times with various conditions and when I have it set in a way that would make player 1 (player 1's score is stored as p1score) it works just as it should and displays "Player 1 wins!" at the end of the program. However, as soon as I change the conditions to make player 2 win (player 2's score is stored as p2score) it doesn't do what it needs to and still outputs "Player 1 wins!" even though it clearly shouldn't.
That being said, I've been able to narrow the bug down to the if/else statements but I can't for the life of me figure out what is wrong. The only way I have been able to get it to display "Player 2 wins!" when I have it set up to where they should is if I comment out everything from the first if to the else that is outside of the while loop. I'm almost certain whatever is wrong is something that is so plainly easy to spot it is painful but I just can't see it...
I've included the code with a set to where player 2 should win. I'm sure there is some cleaner way to write this but at the moment I am more focused on why my if/else statements aren't working they way they should.
Any help or insight would be greatly appreciated.
//Pong Win loop
#include <iostream>
using namespace std;
int main() {
// your code goes here
int p1score = 0;
int p2score = 0;
bool win = false;
while (win = false){
p1score++;
p2score = p2score + 2;
if (p1score = 10){
win = true;
}
else if (p2score = 10){
win = true;
}
else{
win = false;
}
}
if(p1score = 10){
std::cout<<"Player 1 wins!"<<endl;
}
else if (p2score = 10){
std::cout<<"Player 2 wins!"<<endl;
}
return 0;
}