Beispiel #1
0
        /// <summary>
        /// Save Human player's most recent move ('X') and the Cpu player's next move ('O')
        /// </summary>
        /// <param name="move"></param>
        /// <returns></returns>
        public async Task <NextMove> PlayNextMove(PlayerMove move)
        {
            var newMove = new NextMove();

            try
            {
                // Save human player's move
                await _gameRepo.SaveMove(move);

                // Get saved board including player's latest move
                var currentGame = await _gameRepo.GetGame(move.GameId);

                var isPlayerWinner = IsPlayerWinner(currentGame, GamePlayer.Human);
                newMove.GameCompleted = (currentGame.Status != GameStatus.Incomplete.ToString());
                newMove.Winner        = currentGame.Winner;

                if (!isPlayerWinner)
                {
                    if (currentGame.Status == GameStatus.Incomplete.ToString())
                    {
                        var nextCpuMove = GetCpuMove(currentGame);
                        newMove.Cell  = nextCpuMove.Cell;
                        newMove.Value = nextCpuMove.Value;

                        // Save cpu player's move
                        await _gameRepo.SaveMove(nextCpuMove);

                        var isCpuWinner = IsPlayerWinner(currentGame, GamePlayer.Cpu);
                        newMove.GameCompleted = (currentGame.Status != GameStatus.Incomplete.ToString());
                        newMove.Winner        = currentGame.Winner;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(newMove);
        }