Beispiel #1
0
 // Use this for initialization
 void Start()
 {
     uInt      = gSceneController.GetInstance() as IUserInterface;
     query     = gSceneController.GetInstance() as IGameStatus;
     judge     = gSceneController.GetInstance() as IJudgeEvent;
     myFactory = DiskFactory.GetInstance();
 }
Beispiel #2
0
        /// <summary>
        /// Runs a game using the Default Policy until a terminal node is reached
        /// The children are then cleared and the terminal value returned
        /// </summary>
        /// <param name="startStatus">the game Status to simulate from</param>
        /// <returns>the value from the reached terminal node</returns>
        private int Simulate(IGameStatus startStatus, int depth = -1)
        {
            startStatus.Visited = true;

            if (startStatus.IsTerminal)
            {
                return(startStatus.Value);
            }

            IGameStatus curr      = startStatus;
            int         currDepth = 0;

            while (!curr.IsTerminal)
            {
                curr = curr.Moves.ElementAt(random.Next(0, curr.Moves.Count()));

                if (currDepth == depth)
                {
                    break;
                }
            }
            startStatus.Moves = null;

            return(curr.Value);
        }
Beispiel #3
0
 public void AddUseruserGameStatus(IGameStatus input)
 {
     if (!userGameStatus.Contains(input))
     {
         userGameStatus.Add(input);
     }
 }
 public GameStatusUpdateService(IGameStatus gameStatus,
                                IEnumerable <IPlayer> players)
 {
     Status         = gameStatus;
     Status.Players = players;
     InitializePlayers();
 }
Beispiel #5
0
        protected int Minimax(IGameStatus state, bool isMax)
        {
            if (state.IsTerminal)
            {
                return(state.Value);
            }

            if (isMax)
            {
                int value = int.MinValue;
                foreach (IGameStatus move in state.Moves)
                {
                    value = Math.Max(value, Minimax(move, false));
                }
                return(value);
            }
            else
            {
                int value = int.MaxValue;
                foreach (IGameStatus move in state.Moves)
                {
                    value = Math.Min(value, Minimax(move, true));
                }
                return(value);
            }
        }
Beispiel #6
0
        /// <summary>
        /// The Monte Carlo Tree Search Method
        /// Peforms 1 playout of: SELECTION, EXPANSION, SIMULATION, BACKPROPAGATION phases
        /// </summary>
        /// <param name="curr">the current node in the algorithm</param>
        /// <param name="isMax">whether it is the maximizers turn or the minimizers turn</param>
        /// <returns>the terminal value from the simulation phase of the playout</returns>
        private int MonteCarlo(IGameStatus curr, bool isMax)
        {
            if (curr.IsTerminal)
            {
                UpdateStatus(curr, isMax, curr.Value);
                return(curr.Value);
            }

            if (!curr.Moves.All(x => x.Visited))
            {
                IGameStatus child = Expand(curr);

                int value = Simulate(child);

                UpdateStatus(child, !isMax, value);
                UpdateStatus(curr, isMax, value);
            }

            double[] uct    = curr.Moves.Select(x => UCT(x, curr.Simulations)).ToArray();
            double   maxUCT = uct.Max();

            var bestMoves = curr.Moves.Where((x, index) => uct[index] == maxUCT);

            var pickedMove = bestMoves.ElementAt(random.Next(0, bestMoves.Count()));

            int termValue = MonteCarlo(pickedMove, !isMax);

            UpdateStatus(curr, isMax, termValue);

            return(termValue);
        }
Beispiel #7
0
        /// <summary>
        /// Returns a non-visited child from the given Status using
        /// the Default Policy
        /// </summary>
        /// <param name="Status">the game Status to expand upon</param>
        /// <returns>a non-visited child of the given Status</returns>
        private IGameStatus Expand(IGameStatus status)
        {
            //select all children who are not visited
            var unvisitedMoves = status.Moves.Where(s => !s.Visited).ToList();

            //return a random unvisited child of the given state
            return(unvisitedMoves[random.Next(unvisitedMoves.Count)]);
        }
    void Start()
    {
        addAction    = SceneController.getInstance() as IAddAction;
        gameStatusOp = SceneController.getInstance() as IGameStatus;

        whichPatrol  = getwhichPatrol();
        runningAfter = false;
    }
Beispiel #9
0
        private void CheckGameStatus(IGameStatus gameStatus)
        {
            if (Players.All(x => x.CurrentThreat >= 50))
            {
                gameStatus.IsPlayerDefeat = true;
            }

            TriggerEffectsForAllCardsInPlay <ICardInPlay, IDuringCheckGameStatus>();
        }
            public void DuringCheckGameStatus(IGameStatus gameStatus)
            {
                if (gameStatus.Game.QuestArea.ActiveQuest == null || gameStatus.Game.QuestArea.ActiveQuest.Card.Id != source.Id)
                {
                    return;
                }

                var ungoliantsSpawn = gameStatus.Game.VictoryDisplay.Cards.Where(x => x.Title == "Ungoliant's Spawn").FirstOrDefault() as IEnemyCard;

                gameStatus.IsPlayerVictory = (ungoliantsSpawn != null);
            }
Beispiel #11
0
        /// <summary>
        /// Updates the win & simulation count depending on the given arguments
        /// </summary>
        /// <param name="Status">the Status to update</param>
        /// <param name="isMax">if the Status is owed by the maximizer or minimizer</param>
        /// <param name="terminalValue">the terminal value we reached in the simulation phase</param>
        private void UpdateStatus(IGameStatus status, bool isMax, int terminalValue)
        {
            if ((isMax && terminalValue == 1) || (!isMax && terminalValue == -1)) //win game for current player
            {
                status.Wins++;
            }
            else if (terminalValue == 0)
            {
                status.Wins += 0.5;
            }

            status.Simulations++;
        }
Beispiel #12
0
            public void DuringCheckGameStatus(IGameStatus gameStatus)
            {
                if (gameStatus.Game.QuestArea.ActiveQuest == null || gameStatus.Game.QuestArea.ActiveQuest.Card.Id != source.Id)
                    return;

                var ungoliantsSpawn = gameStatus.Game.StagingArea.CardsInStagingArea.Where(x => x.Title == "Ungoliant's Spawn").FirstOrDefault() as IEnemyInPlay;
                if (ungoliantsSpawn != null)
                {
                    gameStatus.IsPlayerVictory = false;
                }
                else if (gameStatus.Game.QuestArea.ActiveQuest.Progress >= gameStatus.Game.QuestArea.ActiveQuest.Card.QuestPoints)
                {
                    gameStatus.IsPlayerVictory = true;
                }
            }
Beispiel #13
0
            public void DuringCheckGameStatus(IGameStatus gameStatus)
            {
                if (gameStatus.Game.QuestArea.ActiveQuest == null || gameStatus.Game.QuestArea.ActiveQuest.Card.Id != source.Id)
                {
                    return;
                }

                var ungoliantsSpawn = gameStatus.Game.StagingArea.CardsInStagingArea.Where(x => x.Title == "Ungoliant's Spawn").FirstOrDefault() as IEnemyInPlay;

                if (ungoliantsSpawn != null)
                {
                    gameStatus.IsPlayerVictory = false;
                }
                else if (gameStatus.Game.QuestArea.ActiveQuest.Progress >= gameStatus.Game.QuestArea.ActiveQuest.Card.QuestPoints)
                {
                    gameStatus.IsPlayerVictory = true;
                }
            }
        public void PlayNetworkGame()
        {
            List <PlayerProxy> playerProxies = new List <PlayerProxy>();

            playerProxies.Add(new TicTacToeAutomatedPlayerProxy("X"));
            playerProxies.Add(new NetworkPlayerProxy("O", new Uri("http://localhost:53554/TicTacToePlayer")));

            TicTacToeGame game = new TicTacToeGame(playerProxies);

            while (true)
            {
                IGameStatus status = game.ProcessGameStep();
                Debug.WriteLine(game.ToString());
                Debug.WriteLine("");

                if (status is GameOverStatus)
                {
                    string winner = ((GameOverStatus)status).NameOfWinner;
                    if (string.IsNullOrEmpty(winner))
                    {
                        Debug.WriteLine("The game has ended in a tie.");
                    }
                    else
                    {
                        Debug.WriteLine(winner + " has won the game.");
                    }
                    break;
                }
                else if (status is GameInProgressStatus)
                {
                    // Nothing to do
                }
                else
                {
                    throw new Exception("IGameStatus case not handled.");
                }
            }
        }
Beispiel #15
0
        public void StartNewGame()
        {
            int fieldSize;

            this.renderer.PrintMessage("Welcome to \"Battle Field game.\" Enter battle field size: n = ");

            do
            {
                fieldSize = this.userInterface.ReadInteger();

                if (fieldSize < MinFieldSize || MaxFieldSize < fieldSize)
                {
                    Console.WriteLine("Invalid input! Please enter a number between {0} and {1}", MinFieldSize, MaxFieldSize);
                }
                else
                {
                    break;
                }
            }while (true);

            this.gameStatus = GameStatus.GetInstance(fieldSize);
            this.Run();
        }
Beispiel #16
0
 public void SetUp()
 {
     _board            = new GameBoard();
     _gameStatus       = new GameStatus(_board);
     _gameRulesService = new GameRuleService(_gameStatus);
 }
 public GameRuleService(IGameStatus status)
 {
     _winningCordinateSets = new SquareWinningCordinateSetGenerator().GetWinningCordinateSetPermutations();
     _gameStatus           = status;
 }
            public void DuringCheckGameStatus(IGameStatus gameStatus)
            {
                if (gameStatus.Game.QuestArea.ActiveQuest == null || gameStatus.Game.QuestArea.ActiveQuest.Card.Id != source.Id)
                    return;

                var ungoliantsSpawn = gameStatus.Game.VictoryDisplay.Cards.Where(x => x.Title == "Ungoliant's Spawn").FirstOrDefault() as IEnemyCard;

                gameStatus.IsPlayerVictory = (ungoliantsSpawn != null);
            }
Beispiel #19
0
 private double UCT(IGameStatus child, double parentSims, double rate = 1.41421356237)
 {
     return((child.Simulations - child.Wins) / child.Simulations + rate * Math.Sqrt(Math.Log(parentSims) / child.Simulations));
 }