/// <summary> /// Begins the game and creates the players. /// </summary> public void Run() { // Get players and initialize them with deck and opponents players = startMenu.Execute(); foreach (IBasePlayer player in players) { // Assign the deck to each player so they could pull cards player.CurrentDeck = cardController; // Make sure each player gets aware of the opponents player.Opponents = players.Where(p => p.PlayerName != player.PlayerName); // Attach a callback delegate to each player to enable callback when cardswapping happens player.CardExchangeAnnouncementCallback += AnnotatePlayersOfCardExchange; } IBasePlayer winner = null; while (winner == null) { for (int current = 0; current < players.Count(); current++) { IBasePlayer currentPlayer = players.ToArray()[current]; // If player has 0 cards on hand and deck has 0 cards left then skip to next player // continue will continue with next value of the for loop if (currentPlayer.CardsLeftOnHand == 0 && currentPlayer.CurrentDeck.CardsLeft == 0) { continue; } currentPlayer.Play(); } if (AllPlayersOutOfCards(players)) { winner = players.OrderByDescending(p => p.OnTheTable.Count).First(); Console.WriteLine($"Winner is: {winner.PlayerName}"); Console.WriteLine($"OnTheHand: {winner.CardsLeftOnHand} cards"); Console.WriteLine($"OnTheTable: {winner.OnTheTable.Count} cards"); foreach (var player in players.OrderByDescending(p => p.OnTheTable.Count)) { Console.WriteLine($"Player: {player.PlayerName}, {player.OnTheTable.Count} on the table, {player.CardsLeftOnHand} on the hand."); } } } }
/// <summary> /// Begins the game and creates the players. /// </summary> public void Run() { // Get players and initialize them with deck and opponents players = sm.Execute(); foreach (IBasePlayer player in players) { // Assign the deck to each player so they could pull cards player.CurrentDeck = deck; // Make sure each player gets aware of the opponents player.Opponents = players.Where(p => p.PlayerName != player.PlayerName); // Attach a callback delegate to each player to enable callback when cardswapping happens player.CardExchangeAnnouncement += AnnotatePlayersOfCardExchange; } IBasePlayer winner = null; while (winner == null) { for (int current = 0; current < players.Count(); current++) { //TODO! Test for empty deck //TODO! If empty deck, test for different player amount of cards in OnTheTable IBasePlayer currentPlayer = players.ToArray()[current]; if (!currentPlayer.Play()) { Console.WriteLine($"player {currentPlayer} is out"); players = players.Where(p => p.GetType().Name != currentPlayer.GetType().Name); current--; } if (players.Count() == 1) { winner = players.ToArray()[0]; break; } } } Console.WriteLine($"Winner is {winner}"); }