コード例 #1
0
        private void UpdateGame(int gameId)
        {
            using (IGameDataService gameDataService = new GameDataService())
            {
                Game game = gameDataService.GetGame(gameId);
                GameState state = this.GetGameState(gameId, game.PlayerOneId);
                int gameWon = this.IsWon(state.GameBoard);
                if (gameWon == 1 || gameWon == -1)
                {
                    if (gameWon == 1)
                        gameDataService.EndGame(gameId, game.PlayerOneId);
                    else if (gameWon == -1)
                        gameDataService.EndGame(gameId, game.PlayerTwoId);
                }

                if (!game.DeathMatchMode)
                {
                    int ninthCheckPlayer = game.CurrentPlayerId == game.PlayerOneId ? 1 : -1;
                    bool canWinWithNinth = false;
                    for (int x = 0; x < 3; x++)
                    {
                        for (int y = 0; y < 3; y++)
                        {
                            if ((state.GameBoard[x][y] == null || state.GameBoard[x][y] == 0)
                                && this.CanWin(state.GameBoard, ninthCheckPlayer, x, y) == ninthCheckPlayer)
                            {
                                canWinWithNinth = true;
                                goto FoundEmpty;
                            }
                        }
                    }

                FoundEmpty:
                    if (!canWinWithNinth)
                    {
                        gameDataService.setToDeathMatch(game.GameId);
                    }
                }

                gameDataService.Save();
            }
            using (IGameDataService gameDataService = new GameDataService())
            {
                gameDataService.SwapPlayerTurn(gameId);
                gameDataService.Save();
            }
        }