public Game(Player[] players, Narrator narrator) { this.players = players; this.narrator = narrator; board = new Board(); dice = new Dice(); }
public void TakeTurn(Board board, Dice dice, Narrator narrator) { int roll; bool playerGetsAnotherGo; do { // Roll the dice roll = dice.Roll(); narrator.SayRoll(this, roll); // Move the player that many spaces forward on the board // Take into account bounce-back position += roll; // If player over-shoots the final square, they bounce backwards if (position > board.Size) { int bounce = position - board.Size; position = board.Size - bounce; narrator.SayBounce(this); } narrator.SaySquareLandedOn(this); // Landed on ladder? if (board.PlayerIsOnLadder(this, out Ladder ladder)) { narrator.SayLandedOnLadder(this); position = ladder.To; narrator.SaySquareLandedOn(this); } // Landed on snake? if (board.PlayerIsOnSnake(this, out Snake snake)) { narrator.SayLandedOnSnake(this); position = snake.To; narrator.SaySquareLandedOn(this); } // Player ends if they have landed on the final square of the board if (board.PlayerIsOnFinalSquare(this)) { break; } // Player will get another go if they rolled a six playerGetsAnotherGo = false; if (roll == 6) { narrator.SayAnotherGo(this); playerGetsAnotherGo = true; } }while (playerGetsAnotherGo); }
static void Main(string[] args) { ConsoleColor[] colours = { ConsoleColor.Green, ConsoleColor.Red, ConsoleColor.Yellow, ConsoleColor.Cyan, ConsoleColor.Magenta, ConsoleColor.Gray }; Console.Write("How many players? "); int numberOfPlayers = int.Parse(Console.ReadLine()); Player[] players = new Player[numberOfPlayers]; for (int p = 1; p <= numberOfPlayers; p++) { Console.Write($"Enter the name of player {p}: "); string name = Console.ReadLine(); Console.Write($"Enter the colour for player {p}: "); ConsoleColor colour = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), Console.ReadLine(), true); players[p - 1] = new Player(name, colour); } Narrator narrator = new Narrator(); Game game = new Game(players, narrator); while (true) { game.SelectFirstPlayer(); while (true) { Console.ReadLine(); game.CurrentPlayer.TakeTurn(game.Board, game.Dice, narrator); if (game.Winner != null) { break; } game.SelectNextPlayer(); } narrator.SayWinner(game.Winner); Console.WriteLine(); Console.Write("Play again? (y/n) "); bool playAgain = Console.ReadLine().ToLower() == "y"; if (playAgain == false) { break; } game.Reset(); } }