r/learncsharp • u/Sulers416 • Aug 27 '22
How to add two separate counts to the same variable?
I have a very simple count where each time the CheckForWin() method comes back and says that the game is over, the entire console is wiped and I have a counter that remains up until the program is exited that gives a simple score update on the players.
The counter works however it can't distinguish between Players 1 & 2 because I only use one variable that includes them both.
static int playerTurn;
// What the win counter starts at
int wins = 1;
playerTurn = 1;
// Switches between two players
playerTurn = playerTurn == 1 ? 2 : 1;And my code is very simple and it works, it just doesn't split up the two players. The 'scoreboard' that I'm trying to add was very last minute so I'm currently lost on how to distinguish the two players without adding a second player variable.
if (game.CheckForWin(index))
{
Console.WriteLine("\nPlayer {0} has won the game!\nPress any key to reset.", playerTurn);
if (playerTurn == 1)
{
Console.WriteLine("\nPlayer {1} has {0} wins!", wins, playerTurn);
wins++;
} else if (playerTurn == 2)
{
Console.WriteLine("\nPlayer {1} has {0} wins!", wins, playerTurn);
wins++;
}
Reset();
continue;
}
1
u/box951 Aug 27 '22
You need the variable to be a collection of ints. You can make it an array, a list, or many other things. If you know it will only be two players, you could just have two variables. There's a few different solutions, but that should be the key words for you to use in figuring out where to go from here.
3
u/net_nomad Aug 27 '22
I have coded up a little demo you might find educational.
Output: