public TicTacToeGameImpl()
        {
            game             = new TicTacToeGame();
            currentGameState = GameStateFactory <TicTacToeGameStateImpl> .GetNewGameState();

            currentPlayerIndex = 1;
            opponent           = new RandomPlayer();
            //opponent = new MinimaxPlayer(new Heuristic[] { HeuristicFactory.GetHeuristicByName("ticTacToeHeuristic", 1, null) }, 6);
            playerGameStates = new List <TicTacToeGameStateImpl>();
            statistics       = new TicTacToeGameStatistics();
        }
Exemple #2
0
        private void OpenScoringWindow(SessionConfigParams configParams)
        {
            Window w = new ScoringWindow();
            ScoringWindowViewModel vm = ContainerHelper.Container.Resolve <ScoringWindowViewModel>();
            GameState gs = GameStateFactory.GetNewGameState(configParams);

            vm.SetCurrentGameState(gs);
            w.DataContext = vm;
            w.Show();
            NavToStartSession();
        }
        public HashSet <OpponentMove> GetPossibleOpponentMovesForGameState(GameState state)
        {
            HashSet <OpponentMove> possibleOpponentMoves = new HashSet <OpponentMove>();
            TicTacToeGameStateImpl ticTacToeState        = (TicTacToeGameStateImpl)state;

            for (byte i = 0; i < 3; ++i)
            {
                for (byte j = 0; j < 3; ++j)
                {
                    if (ticTacToeState.GetSquare(i, j) == 0)
                    {
                        TicTacToeGameStateImpl newState = GameStateFactory <TicTacToeGameStateImpl> .GetNewGameState(ticTacToeState);

                        newState.SetSquare(i, j, (byte)(GetNextPlayerIndex((byte)GetCurrentPlayerIndex()) + 1));
                        possibleOpponentMoves.Add(new OpponentMove(newState, 1));
                        playerGameStates.Add(newState);
                    }
                }
            }
            return(possibleOpponentMoves);
        }
Exemple #4
0
        public GameState LoadGame(string name)
        {
            SessionConfigParams session = SessionSerialization.LoadSession(_repo, name);
            GameState           gs      = GameStateFactory.GetNewGameState(session);

            List <GameSaveParams> gameData = JsonConvert.DeserializeObject <List <GameSaveParams> >(File.ReadAllText(ConfigurationManager.AppSettings["game_save_config"] + name));

            Dictionary <string, List <List <Question> > > table = new Dictionary <string, List <List <Question> > >();

            foreach (var entry in gameData)
            {
                table.Add(entry.TeamName, entry.Answers);
            }

            foreach (var scorer in gs.ActiveScorers)
            {
                foreach (var team in scorer.ScoringTeams)
                {
                    team.SetAllAnswers(table[team.Team.Name]);
                }
            }

            return(gs);
        }
        public HashSet <Player> PlayGameInView(HashSet <Player> players, GameView view, AutoResetEvent manualMoveAcceptBlock)
        {
            RestartGame();
            gameView = view;
            if (players.Count != 1)
            {
                throw new NotImplementedException("Tic tac toe is only for 2 players.");
            }
            Logger.LogInfo("Starting Tic Tac Toe game.");

            players.Add(opponent);

            Random rng = new Random();

            currentPlayerIndex        = (byte)rng.Next(2);
            startingPlayerIndex       = currentPlayerIndex;
            statistics.StartedPlaying = startingPlayerIndex == 0;
            Logger.LogInfo("Starting player is " + GetCurrentPlayer(players).GetType().Name + ".");

            Stopwatch gameTimer = Stopwatch.StartNew();

            do
            {
                if (manualMoveAcceptBlock != null)
                {
                    manualMoveAcceptBlock.WaitOne();
                }
                Player currentPlayer = GetCurrentPlayer(players);
                Logger.LogDebug(currentPlayer.GetType().Name + "'s move.");
                TicTacToeGameStateImpl currentGameState = (TicTacToeGameStateImpl)GetCurrentGameState();
                Stopwatch moveTimer = Stopwatch.StartNew();
                currentPlayer.PerformMove(this, currentGameState);
                moveTimer.Stop();
                if (currentPlayerIndex == 0)
                {
                    TicTacToeGameStateImpl startMove = GameStateFactory <TicTacToeGameStateImpl> .GetNewGameState(currentGameState);

                    TicTacToeGameStateImpl endMove = GameStateFactory <TicTacToeGameStateImpl> .GetNewGameState((TicTacToeGameStateImpl)GetCurrentGameState());

                    statistics.Moves.Add(new Tuple <GameState, GameState>(startMove, endMove));
                    statistics.MovesDurations.Add(moveTimer.Elapsed.TotalMilliseconds);
                }
                ReturnStatesToPool();
                ChangeToNextPlayer();
            }while (!game.IsGameFinished());
            gameTimer.Stop();
            statistics.GameDuration = gameTimer.Elapsed.TotalMilliseconds;

            byte winner = game.GetWinner();

            if (winner == 0)
            {
                statistics.Score = 0;
                Logger.LogInfo("Tic Tac Toe game ended in a tie.");
                return(new HashSet <Player>());
            }

            if ((winner == 1 && startingPlayerIndex == 0) ||
                (winner == 2 && startingPlayerIndex == 1))
            {
                statistics.Score = 1000;
                Logger.LogInfo("Tic Tac Toe game ended in a win");
                return(new HashSet <Player>()
                {
                    players.ElementAt(0)
                });
            }

            if ((winner == 1 && startingPlayerIndex == 1) ||
                (winner == 2 && startingPlayerIndex == 0))
            {
                statistics.Score = -1000;
                Logger.LogInfo("Tic Tac Toe game ended in a lose");
                return(new HashSet <Player>()
                {
                    players.ElementAt(1)
                });
            }

            return(new HashSet <Player>());
        }