Reputation: 33
So I created a game where player one and player two can play a game where they answer math questions, basically they go one after another for 5 rounds, so if one player answers the question right first they "Win" and if they both answer it right it is a "tie". Kind of a weird way to explain a weird game, but that's not my issue. I already finished the game inside of a class and it is functional and fine, my problem is that I need the players to play 5 games, and for it to tell who won the game the most, ex: "Player 1( or player 2, or draw) has won {0} times!" I have tried a lot of different things, but its all not working. Here is my code:
static void Main(string[] args)
{
string ID = "";
bool gOver = false;
Console.WriteLine("ID 1 ");
ID = Console.ReadLine();
MathGame p1 = new MathGame(1, ID);
Console.WriteLine();
Console.WriteLine("ID 2");
ID = Console.ReadLine();
MathGame p2 = new MathGame(2, ID);
Console.WriteLine();
while (!gOver)
{
MathGame.prblm();
while (!p1.Game() && !p2.Game())
gOver = true;
}
}
To reiterate; I'd like to make the game loop 5 times and tell me who won the most. I feel like my mistake is simple, make its just where I'm tired. Thanks for any and all help, this website is very helpful.
Upvotes: 0
Views: 1418
Reputation: 693
You need to wrap your game in a for loop, not a while. Then when the while loop (of your game) ends you should check who won and tally it. After the for loop you should have counters to display then. There are many ways to "tally" but the easiest would be to add a variable for each player and increment when they win.
const in GAME_COUNT_TO_PLAY = 5;
for(int i = 0; i < GAME_COUNT_TO_PLAY; i++)
{
MathGame.prblm();
while (!p1.Game() && !p2.Game())
{
//Keep track of score here, incriment some counter for winner
//e.g. if(p1.Win) p1Count++;
}
}
After the for loop you can check for who won.
if(p1Count > p2Count)
Display("Player 1 Wins!");
else if(p1Count < p2Count)
Display("Player 2 Wins!")
else
Display("Draw!");
Upvotes: 1