Beispiel #1
0
        public static void SaveToFile(string filename, OthelloState state)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream     stream    = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);

            formatter.Serialize(stream, state);
            stream.Close();
        }
Beispiel #2
0
        public static OthelloState LoadStateFromFile(string filename)
        {
            IFormatter   formatter = new BinaryFormatter();
            Stream       stream    = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            OthelloState state     = (OthelloState)formatter.Deserialize(stream);

            stream.Close();

            return(state);
        }
Beispiel #3
0
        /// <summary>
        /// Redo the first undone actions
        /// </summary>
        /// <returns>state of the board of the action</returns>
        public OthelloState Redo(OthelloState state)
        {
            if (this.actionToRedo.Count > 0)
            {
                OthelloState stateToRedo = this.actionToRedo.Pop();
                this.actionDone.Push(state);

                return(stateToRedo);
            }
            else
            {
                throw new StackEmptyException("no action to redo");
            }
        }
Beispiel #4
0
        }; // pattern for the eval method


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="agent">alphabeta agent</param>
        /// <param name="parent">parent node</param>
        /// <param name="op">operation</param>
        /// <param name="score">score to add, default 0</param>
        public Node(AlphaBetaAgentBG agent, Node parent, Vector2 op, int score = 0)
        {
            this.agent = agent;
            this.state = agent.IAGame.BoardState; // state of the board for the board
            this.op    = op;                      // operation made to arrive to this state

            this.score       = score;             //score is the number of point lost or won
            this.turnSkipped = false;
            this.parent      = parent;

            this.agent = agent;
            // get the state of the board for this child
            this.state = agent.IAGame.BoardState;
            this.score = 0; //initial score at the beginning of the game
        }
        //Implemented from the code example in the pdf of the IA course
        //the Tuple<int, int> contains in the following order: move X coordinate, move Y coordinate
        public Tuple <int, Vector2> alphabeta(Node root, int depth, int minOrMax, int parentValue)
        {
            // check if end of the depth or if the move is final
            if (depth == 0 || root.Final())
            {
                return(new Tuple <int, Vector2>(root.Eval(), null));
            }

            int optVal = minOrMax * -int.MaxValue;

            Vector2 optOp = null;

            OthelloState initialState = Game.Instance.BoardState; // get the state to come back to it

            var ops = root.Ops();

            // if there is no move available, add a skip turn move
            if (ops.Count == 0)
            {
                ops.Add(new Vector2(-1, -1));
            }

            foreach (Vector2 op in ops)
            {
                Game.Instance.LoadState(initialState); // load the initial state to compute the brother

                Node newNode = root.Apply(op);

                Tuple <int, Vector2> result = alphabeta(newNode, depth - 1, -1 * minOrMax, optVal);

                int val = result.Item1;

                if (val * minOrMax > optVal * minOrMax)
                {
                    optVal = val;
                    optOp  = op;

                    if (optVal * minOrMax > parentValue * minOrMax)
                    {
                        break;
                    }
                }
            }

            Game.Instance.LoadState(initialState); // load the initial state

            return(new Tuple <int, Vector2>(optVal, optOp));
        }
Beispiel #6
0
 /// <summary>
 /// do an action and stack it
 /// </summary>
 /// <param name="state">state of the board to stack</param>
 public void DoAction(OthelloState state)
 {
     this.actionDone.Push(state);
     this.actionToRedo.Clear();
 }