public static int GetUserInt(string prompt, int minNum, int maxNum, string tooLowMessage, string tooHighMessage) { int response = 0; bool isValid = false; Console.WriteLine(prompt); do { try { Console.ForegroundColor = ConsoleColor.Cyan; response = int.Parse(Console.ReadLine()); Console.ResetColor(); if (response < minNum) { MethodLibrary.WriteInColour(tooLowMessage, ConsoleColor.Red, writeLine: true); } else if (response > maxNum) { MethodLibrary.WriteInColour(tooHighMessage, ConsoleColor.Red, writeLine: true); } else { isValid = true; } } // end try catch (Exception) { MethodLibrary.WriteInColour("That's not a number. Please re-enter.", ConsoleColor.Red, writeLine: true); } // end catch } while (!isValid); return(response); } // end method
} // end method static List <Player> InitializePlayers(int totalPlayers, int humanPlayers) { List <Player> players = new List <Player>(); Random random = new Random(); for (int i = 0; i < totalPlayers; i++) { // add specified number of Human players, in random order: if (i < humanPlayers) { players.Insert(random.Next(players.Count + 1), new Player(Player.PlayerType.Human, MethodLibrary.GetUserString("Human, what is your name?"))); } // otherwise add Computer players, in random order: else { players.Insert(random.Next(players.Count + 1), new Player()); } } // end for for (int i = 0; i < totalPlayers; i++) { Console.Write($"Player {i + 1}: "); players[i].DisplayPlayer(); } // end for return(players); } // end InitializePlayers method
static void Main(string[] args) { const int MIN_PLAYERS = 2; const int MAX_PLAYERS = 8; string userResponse; do { int numPlayers = MethodLibrary.GetUserInt("How many players?", MIN_PLAYERS, MAX_PLAYERS, "You need more friends. Please enter a larger number.", "That's too many friends. Please enter a smaller number."); int humanPlayers = MethodLibrary.GetUserInt("And how many of those are human?", MIN_PLAYERS - 1, numPlayers, "You need at least one!", "That's more than the number of players. Please re-enter."); InitializeDrawPile(); List <Player> players = InitializePlayers(numPlayers, humanPlayers); // nursery list // draw pile -- when empty, reshuffle discard, or end game? // discard pile for (int i = 0; i < players.Count; i++) { players[i].TakeTurn(); bool gameOver = CheckIfWinner(players); } // end for userResponse = MethodLibrary.GetUserChoice("\nWanna play again?", "y", "n"); } while (userResponse.ToLower() == "y"); Console.WriteLine("Goodbye forever."); } // end method
} // end constructor public void DisplayPlayer() { MethodLibrary.WriteInColour(this.Name, ConsoleColor.Magenta); MethodLibrary.WriteInColour($" is a {Type} player. They have ", ConsoleColor.Yellow); MethodLibrary.WriteInColour($"{Hand.Count}", ConsoleColor.Magenta); MethodLibrary.WriteInColour(" cards in their hand and ", ConsoleColor.Yellow); MethodLibrary.WriteInColour($"{Stable.Count}", ConsoleColor.Magenta); MethodLibrary.WriteInColour(" in their stable.", ConsoleColor.Yellow, writeLine: true); } // end DisplayPlayer
static void Main(string[] args) { string userResponse; do { ExplodingUnicornsGame game = new ExplodingUnicornsGame(); game.PlayGame(); userResponse = MethodLibrary.GetUserChoice("\nWanna play again?", "y", "n"); } while (userResponse.ToLower() == "y"); Console.WriteLine("Goodbye forever."); } // end method
} // end InitializeNursery() private void CheckIfWinner(List <Player> player) { bool gameOver = false; if (player.Count > 5) { unicornsToWin = 6; } for (int i = 0; i < player.Count; i++) { if (player[i].GetNumberOfUnicorns() >= unicornsToWin) { gameOver = true; MethodLibrary.WriteInColour("THIS PLAYER HAS WON!", ConsoleColor.Magenta); // add player name. } // end if } // end for } // end CheckIfWinner()
} // end method public static string GetUserChoice(string prompt, string firstChoice, string secondChoice) { Console.WriteLine(prompt + $" ({firstChoice}/{secondChoice})"); Console.ForegroundColor = ConsoleColor.Cyan; string userResponse = Console.ReadLine(); Console.ResetColor(); while (userResponse.ToLower() != firstChoice && userResponse.ToLower() != secondChoice) { MethodLibrary.WriteInColour("That wasn't a valid choice. Please re-enter.", ConsoleColor.Red, writeLine: true); Console.ForegroundColor = ConsoleColor.Cyan; userResponse = Console.ReadLine(); Console.ResetColor(); } // end while invalid entry return(userResponse); } // end GetUserChoice method
public void PlayGame() { const int MIN_PLAYERS = 2; const int MAX_PLAYERS = 8; int numPlayers = MethodLibrary.GetUserInt("How many players?", MIN_PLAYERS, MAX_PLAYERS, "You need more friends. Please enter a larger number.", "That's too many friends. Please enter a smaller number."); int humanPlayers = MethodLibrary.GetUserInt("And how many of those are human?", MIN_PLAYERS - 1, numPlayers, "You need at least one!", "That's more than the number of players. Please re-enter."); InitializeDrawPile(); InitializePlayers(numPlayers, humanPlayers); // nursery list // draw pile -- when empty, reshuffle discard, or end game? // discard pile for (int i = 0; i < players.Count; i++) { players[i].TakeTurn(); CheckIfWinner(players); } // end for } // end method