Example #1
0
            public State   s1; // New state

            public Transition(State s, Actione a, double Reward, State s1)
            {
                this.s      = s;
                this.a      = a;
                this.Reward = Reward;
                this.s1     = s1;
            }
 /// <summary>
 /// Check if an action is legal in a given state
 /// </summary>
 public override bool IsLegalAction(Actione action, State s)
 {
     if ((Players)s.Board[action.ID, RowNum - 2] != Players.NoPlayer)
     {
         return(false);
     }
     return(true);
 }
 /// <summary>
 /// Check if an action is legal in the current game state
 /// </summary>
 public override bool IsLegalAction(Actione action)
 {
     if (circleList[action.ID, RowNum - 2] != Players.NoPlayer)
     {
         return(false);
     }
     return(true);
 }
        /// <summary>
        /// Register the action inside the given state, and it is done by given player
        /// </summary>
        public override void RegisterAction(State s, Actione a, Players player)
        {
            int addY = a.ID / ColNum;
            int addX = a.ID % ColNum;

            if ((Players)s.Board[addX, addY] == Players.NoPlayer)
            {
                s.Board[addX, addY] = (double)CurrTurn;
            }
        }
        /// <summary>
        /// Is the action legal in the given state
        /// </summary>
        public override bool IsLegalAction(Actione action, State s)
        {
            int addY = action.ID / ColNum;
            int addX = action.ID % ColNum;

            if ((Players)s.Board[addX, addY] == Players.NoPlayer)
            {
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Check if an action is legal in the current game state
        /// </summary>
        public override bool IsLegalAction(Actione action)
        {
            int addY = action.ID / ColNum;
            int addX = action.ID % ColNum;

            if (Tiles[addX, addY] == Players.NoPlayer)
            {
                return(true);
            }
            return(false);
        }
 public override void RegisterAction(State s, Actione a, Players player)
 {
     // Add the circle to the two dimensional array for checking the wins
     for (int y = 0; y < RowNum - 1; y++)
     {
         if ((Players)s.Board[a.ID, y] == Players.NoPlayer)
         {
             s.Board[a.ID, y] = (double)player;
             break;
         }
     }
 }
        /// <summary>
        /// Execute the action and pass to the next turn
        /// </summary>
        public override void DoAction(Actione action)
        {
            int addY = action.ID / ColNum;
            int addX = action.ID % ColNum;

            if (Tiles[addX, addY] == Players.NoPlayer)
            {
                Tiles[addX, addY] = CurrTurn;
            }

            NextTurn();
        }
Example #9
0
        /// <summary>
        /// Return an action according to an epsilon greedy policy.
        /// Means either best or random.
        /// </summary>
        /// <param name="epsilon">The chance of the action being random</param>
        /// <param name="state"></param>
        protected Actione TakeEpsilonGreedyAction(double epsilon, State state, Random rand)
        {
            Actione action;

            if (rand.NextDouble() <= epsilon)
            {
                action = new Actione(rand.Next(Control.ActionNum));
            }
            else
            {
                action = getMaxAction(Control, state, false);
            }
            Control.DoAction(action);


            return(action);
        }
Example #10
0
        /// <summary>
        /// Execute a move by the given bot
        /// </summary>
        /// <param name="against">A bot, if null then takes random action</param>
        protected void BotMove(Bot against)
        {
            Actione botAction;

            if (!Control.IsTerminalState())
            {
                if (against != null)
                {
                    against.TakeAction(Control, Control.GetState());
                }
                else // Find a random legal action
                {
                    botAction = new Actione(rand.Next(Control.ActionNum));
                    while (!Control.IsLegalAction(botAction))
                    {
                        botAction = new Actione(rand.Next(Control.ActionNum));
                    }
                    Control.DoAction(botAction);
                }
            }
        }
Example #11
0
 public override void DoAction(Actione action)
 {
     throw new NotImplementedException();
 }
Example #12
0
 public override bool IsLegalAction(Actione action)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Execute the action and pass to the next turn
 /// </summary>
 public override void DoAction(Actione action)
 {
     QuickAdd(action.ID);
 }
Example #14
0
        /// <summary>
        /// A wrapper function to the recursive function, that returns the best action
        /// </summary>
        /// <param name="control">Control that the state is in</param>
        /// <param name="s">The current control state</param>
        /// <param name="alpha">Has to be int.MinValue</param>
        /// <param name="beta">Has to be int.MaxVakue</param>
        /// <param name="player">The player that is maximizing</param>
        private Actione BestMove(GameControlBase control, State s, double alpha, double beta, Players player)
        {
            // This is needed in order to register the next action as the opponent's
            Players opponent;

            if (player == Players.Player1)
            {
                opponent = Players.Player2;
            }
            else
            {
                opponent = Players.Player1;
            }

            int    maxID     = 0;
            double maxReward = -1000000;
            double Reward    = 0;

            bool    allSame = true; // Are all the rewards the same?
            Actione botAction;

            State currState = (State)s.Clone(); // Clone the state because it is a reference, so save the original state

            for (int i = 0; i < control.ActionNum; i++)
            {
                if (control.IsLegalAction(new Actione(i), currState))
                {
                    // Make the action as the player
                    s.Copy(currState);
                    control.RegisterAction(currState, new Actione(i), player);
                    if (control.IsTerminalState(currState)) // If its terminal, reward is the reward that the control returns
                    {
                        Reward = control.GetReward(player, currState);
                    }
                    else // If not terminal, return the minimax recursive function return
                    {
                        Reward = 0.9 * GetMaxRewardRec(control, currState, false, opponent, alpha, beta, 1, MaxDepth);
                    }
                    if (Reward != maxReward && i != 0) // If the reward changed along the call
                    {
                        allSame = false;
                    }
                    if (Reward > maxReward)
                    {
                        maxID     = i;
                        maxReward = Reward;
                    }
                    // Alpha beta pruning
                    alpha = Math.Max(alpha, maxReward);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
            }
            if (!allSame)
            {
                return(new Actione(maxID));
            }
            else // If all rewards were the same, do a random action
            {
                botAction = new Actione(rand.Next(control.ActionNum));
                while (!control.IsLegalAction(botAction))
                {
                    botAction = new Actione(rand.Next(control.ActionNum));
                }
                return(botAction);
            }
        }
Example #15
0
        /// <summary>
        /// Take the best legal action (according to Bot's policy) in the given game state.
        /// </summary>
        public void TakeAction(GameControlBase control, State state)
        {
            Actione action = getMaxAction(control, state, true);

            control.DoAction(action);
        }