Exemplo n.º 1
0
        /// <summary>
        ///     Checks response from the player.
        /// </summary>
        /// <returns>Response of the player.</returns>
        private PlayerResponse CheckResponse()
        {
            var response = new PlayerResponse();

            while (true)
            {
                var line = ReadLineFromCurrentPlayer();
                while (string.IsNullOrEmpty(line))
                {
                    line = ReadLineFromCurrentPlayer();
                }
                var orderTokens = line.Split(" ");
                if (orderTokens[0] == CommunicationConstants.ScoreOrder)
                {
                    response.Order = PlayerAction.Score;
                    return(response);
                }

                if (orderTokens[0] == CommunicationConstants.KeepOrder)
                {
                    response.Order = PlayerAction.Keep;
                    var dices = new List <Dice>();
                    for (var i = 1; i < orderTokens.Length; i++)
                    {
                        var diceIndex = -1;
                        if (!int.TryParse(orderTokens[i], out diceIndex))
                        {
                            InformPlayer(game.CurrentPlayer, CommunicationConstants.FailureInfo);
                            response.Order = PlayerAction.Invalid;
                            return(response);
                        }

                        if (diceIndex < 1 || diceIndex > 6)
                        {
                            InformPlayer(game.CurrentPlayer, CommunicationConstants.FailureInfo);
                            response.Order = PlayerAction.Invalid;
                            return(response);
                        }

                        dices.Add(game.CurrentPlayer.State.Dices[diceIndex - 1]);
                    }

                    response.Dices = dices;
                    return(response);
                }

                InformPlayer(game.CurrentPlayer, CommunicationConstants.FailureInfo);
                response.Order = PlayerAction.Invalid;
                return(response);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Runs the game.
        /// </summary>
        private void Run()
        {
            InformStartGame();
            while (!game.IsWon)
            {
                InformStartTurn();
                while (true)
                {
                    var rollResult = Logic.Reroll(game.CurrentPlayer);
                    InformDicesRolled(rollResult, game.CurrentPlayer.State);
                    if (rollResult == GameActionResult.Failure)
                    {
                        break;
                    }
                    var responseValid = false;
                    var response      = new PlayerResponse();
                    while (!responseValid)
                    {
                        response = CheckResponse();
                        if (response.Order == PlayerAction.Score)
                        {
                            var scoreResult = Logic.ScoreCurrentTurn(game.CurrentPlayer);
                            if (scoreResult == GameActionResult.Success)
                            {
                                responseValid = true;
                            }
                            else
                            {
                                InformPlayer(game.CurrentPlayer, CommunicationConstants.FailureInfo);
                                InformPlayer(game.CurrentPlayer, "-- Cant score current kept dices.");
                            }
                        }
                        else if (response.Order == PlayerAction.Keep)
                        {
                            var keepResult = Logic.KeepDices(game.CurrentPlayer.State, response.Dices);
                            if (keepResult == GameActionResult.Success)
                            {
                                responseValid = true;
                            }
                            else
                            {
                                InformPlayer(game.CurrentPlayer, CommunicationConstants.FailureInfo);
                                InformPlayer(game.CurrentPlayer, "-- Cant keep chosen dices.");
                            }
                        }
                    }

                    InformPlayer(game.CurrentPlayer, CommunicationConstants.SuccessInfo);
                    // player scored succesfully, end turn
                    if (response.Order == PlayerAction.Score)
                    {
                        break;
                    }
                }

                InformEndTurn();
                game.NextPlayer();
            }

            InformEndGame();
        }