Ejemplo n.º 1
0
        void aiWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            AIAttentionRequiredResult aiGameAttentionRequired = (AIAttentionRequiredResult)e.Argument;
            GameState state = TicTacToeHost.Instance.GetGameState(aiGameAttentionRequired.GameId, aiGameAttentionRequired.PlayerId);
            int[,] aiBoard = new int[3, 3];
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    aiBoard[x, y] = state.GameBoard[x][y] == null ? 0 : state.GameBoard[x][y].Value;
                }
            }
            GameBoard ai = new GameBoard(aiBoard);
            Move aiMove = ai.GetMove(1);
            Games.Move gameMove = new Games.Move();
            gameMove.GameId = aiGameAttentionRequired.GameId;
            gameMove.PlayerId = aiGameAttentionRequired.PlayerId;
            gameMove.OriginX = aiMove.OriginX;
            gameMove.OriginY = aiMove.OriginY;
            gameMove.X = aiMove.X;
            gameMove.Y = aiMove.Y;

            var moveReuslt = TicTacToeHost.Instance.Move(gameMove);
            if (moveReuslt != MoveResult.Valid)
            {
                //using(IGameDataService dataService = new GameDataService())
                //{
                //    Match match = dataService.GetMatch(null, aiGameAttentionRequired.GameId);
                //    Player tttdPlayer = dataService.GetPlayer(aiGameAttentionRequired.PlayerId);
                //    CentralServerSession session = dataService.GetCentralServerSession(null, null, aiGameAttentionRequired.GameId);

                //    dataService.EndGame(aiGameAttentionRequired.GameId, match.PlayerOneId == aiGameAttentionRequired.PlayerId ? match.PlayerTwoId : match.PlayerOneId);

                //    MoveRequest challengeRequest = new MoveRequest();
                //    challengeRequest.GameId = session.CentralServerGameId.Value;
                //    challengeRequest.PlayerName = tttdPlayer.PlayerName;
                //    challengeRequest.X = 0;
                //    challengeRequest.Y = 0;
                //    challengeRequest.Flags = CentralServerCommunicationChannel.GetStatus(StatusFlag.AcceptLoss);
                //    CentralServerCommunicationChannel.Instance.PostMove(challengeRequest, match.CurrentGameId.Value, match.MatchId);
                //}
            }

            using (IGameDataService gameDataService = new GameDataService())
            {
                AIGame aiGame = gameDataService.GetAIGame(aiGameAttentionRequired.GameId, aiGameAttentionRequired.PlayerId);
                gameDataService.Attach(aiGame);
                aiGame.EvaluatingMove = false;
                gameDataService.Save();
            }
        }