Example #1
0
        /// <inheritdoc/>
        public BoringToeMoveResponse PlayerMove(long gameId, BoringToeMoveRequest request)
        {
            Player responsePlayer = null;

            Player     player = FindPlayerInDatabase(request.PlayerId, ErrorCode.PLAYER_NOT_EXISTS);
            ITicTacToe game   = FindGameInDatabase(gameId, ErrorCode.GAME_NOT_EXISTS);

            try
            {
                responsePlayer = game.PlayerMove(player, new Coordinate(request.XCoord, request.YCoord));
            }
            catch (TicTacToeGameOverException tttgoe)
            {
                return(GenerateGameOverWinnerResponseData(tttgoe.Player, game.GetGrid()));
            }
            catch (PlayerMovementException)
            {
                return(GenerateRepeatUserResponseData(player, game.GetGrid()));
            }
            catch (GameOverException)
            {
                return(GenerateGameOverNoWinnerResponseData(game.GetGrid()));
            }

            return(GenerateOkNextPlayerResponseData(responsePlayer, game.GetGrid()));
        }
Example #2
0
        /// <summary>
        /// Executes main thread
        /// </summary>
        public void Run()
        {
            ShowWelcome();
            ConfigurePlayers();

            try
            {
                while (true)
                {
                    Console.WriteLine(_game.GetGrid().StringGrid() + "\n");
                    try
                    {
                        currentPlayer = _game.PlayerMove(currentPlayer, GetPlayersCoordinates(currentPlayer));
                    } catch (PlayerMovementException pme)
                    {
                        if (pme.ErrorCode == ErrorCode.MOVEMENT_ERROR_MUST_RETRY)
                        {
                            Console.WriteLine(pme.Message);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            } catch (UserCancelException)
            {
                Console.WriteLine("User Quits");
            } catch (TicTacToeGameOverException tttgoe)
            {
                Console.WriteLine(tttgoe.Message);
            } catch (GameOverException goe)
            {
                Console.WriteLine(goe.Message);
            }
        }