/// <summary> /// Plays the next turn in the game. /// </summary> private void NextTurn() { totalTurns++; // the play area represents the area on the "table" where all cards would // normally be in play. An ICollection is used to serve as a type of card // pool since the winner of the round takes all cards. ICollection <Card> playArea = new List <Card>(); // each player plays the card on top of their deck Card p1Card = Player1.Deck.Dequeue(); Card p2Card = Player2.Deck.Dequeue(); // the cards are then added to the play area playArea.Add(p1Card); playArea.Add(p2Card); // check to see if war has been triggered // this comparison is a while loop because the war condition can be triggered // a number of times in sequence. Doing it this way demands that the final // cards drawn for each player be different before doing the final comparison // to see who won the round. while (p1Card.Value == p2Card.Value) { Console.WriteLine(SEPERATOR); Console.WriteLine("WAR!!!"); // make sure each player has enough cards for war. // if you don't have enough cards, you can't go to war // and you automatically lose. // similar enough to real life to make sense - you can't go to war without proper supplies. // Since the GameOver() method checks to see if any of the players is out of cards, // the player without enough cards for war has their deck discarded which forces an end to the game. if (!PlayerHasEnoughCardsForWar(Player1)) { Player1.Deck.Clear(); Console.WriteLine($"{Player1.Name} doesn't have enough cards for war.\n{Player2.Name} wins!"); break; } if (!PlayerHasEnoughCardsForWar(Player2)) { Player2.Deck.Clear(); Console.WriteLine($"{Player2.Name} doesn't have enough cards for war.\n{Player1.Name} wins!"); break; } // the rules of war dictate that each user must add one card face down // and then add another face up and the second card drawn in that sequence // is the one used for determining the winner of war and the round // this operates the same way as beginning a turn, including adding each card // drawn to the play area for collection by the winner playArea.Add(Player1.Deck.Dequeue()); playArea.Add(Player2.Deck.Dequeue()); p1Card = Player1.Deck.Dequeue(); p2Card = Player2.Deck.Dequeue(); playArea.Add(p1Card); playArea.Add(p2Card); } // determine winner and assign cards in play area to winner if (p1Card.Value > p2Card.Value) { Console.WriteLine(SEPERATOR); Console.WriteLine($"{Player1.Name} wins the round!"); Player1.AddCardsToDeck(playArea); } else if (p1Card.Value < p2Card.Value) { Console.WriteLine(SEPERATOR); Console.WriteLine($"{Player2.Name} wins the round!"); Player2.AddCardsToDeck(playArea); } Console.WriteLine($"{Player1.Name} played: " + p1Card); Console.WriteLine($"{Player2.Name} played: " + p2Card); Console.WriteLine($"Turns taken: {totalTurns}"); playArea.Clear(); // A game of War without shuffling each player's deck every so often can run indefinitely (or seem to). // Some variants of the classic game suggest that a player must shuffle their deck at various // intervals or when they have used all of their cards before using the cards they have won // since the last time they shuffled. // Since a previously run simulation has taken upwards of 3.2 million turns, a good middle ground // is to shuffle on a specified interval. This greatly improves the speed of a game. // Most of the games that use this logic end well before 1000 turns. if (totalTurns % SHUFFLE_INTERVAL == 0) { ShufflePlayerDeck(Player1); ShufflePlayerDeck(Player2); } }