Esempio n. 1
0
        // Implementation of mimimax with alpha-beta pruning.
        private int minimaxAB(Node n, bool min, int a,
		                       int b)
        {
            int alpha = a;
            int beta = b;

            if (n.isLeaf ()) {
                return n.value (this._player);
            }

            Node child;

            if (min) {
                while (((child = n.getChild ()) != null) && !(alpha > beta)) {
                    int val = minimaxAB (child, false, alpha, beta);
                    if (val < beta) {
                        beta = val;
                    }
                }
                return beta;
            } else {
                while (((child = n.getChild ()) != null) && !(alpha > beta)) {
                    int val = minimaxAB (child, true, alpha, beta);
                    if (val > alpha) {
                        alpha = val;
                    }
                }
                return alpha;
            }
        }
Esempio n. 2
0
        /**
         * Gets the next move of this player.
         *
         * @param board
         *            <code>Board</code> representation of the current game state.
         * @param prev
         *            <code>Move</code> representing the previous move in the the
         *            game.
         * @param color
         *            <code>int</code> representing the pieces this
         *            <code>Player</code> is playing with. One of
         *            <code>TicTacToe.NOUGHTS</code> or
         *            <code>TicTacToe.CROSSES</code>
         * @return <code>Move</code> Next move of this player.
         */
        public Move getMove(int[,] board, Move prev, int color)
        {
            this._player = color;

            Node root = new Node (board, prev);
            int max = int.MinValue;

            Node child;
            Node bestNode = null;

            while ((child = root.getChild ()) != null) {
                int val = minimaxAB (child, true, int.MinValue,
                              int.MaxValue);
                if (val >= max) {
                    max = val;
                    bestNode = child;
                }
            }

            return bestNode.move ();
        }
Esempio n. 3
0
File: Node.cs Progetto: tmassey/mtos
 /**
  * Returns all the children of this node found so far. You probably won't
  * want to call this method until you're sure you're done with getChild().
  *
  * @return <code>Node[]</code> of all known children for this node.
  * @see getChild()
  */
 public Node[] children()
 {
     Node[] temp = new Node[this.childNum + 1];
     for (int i = 0; i < this.childNum + 1; i++) {
         temp [i] = this._children [i];
     }
     return temp;
 }
Esempio n. 4
0
File: Node.cs Progetto: tmassey/mtos
        /**
         * Gets the "next" child for this node, or <code>null</code> if all
         * children of this node have already been found.
         *
         * @return <code>Node</code> Representation of child (one move on) in the
         *         game tree.
         */
        public Node getChild()
        {
            bool firstColumn = true;

            for (int x = this._x; x < 3; x++) {
                int starter = 0;
                if (firstColumn) {
                    starter = this._y;
                }
                for (int y = starter; y < 3; y++) {
                    Move m = new Move (x, y, this.player ());
                    if (Board.isEmpty (this._board, m)) {
                        int[,] newPos = Board.copy (this._board);
                        Board.placePiece (newPos, m);
                        Node child = new Node (newPos, m);
                        this._children [this.childNum++] = child;
                        if (y >= (Board.SIZE - 1)) {
                            this._x = (int)(x + 1);
                            this._y = 0;
                        } else {
                            this._x = x;
                            this._y = (int)(y + 1);
                        }
                        this._leaf = false;
                        this._leafdef = true;
                        return child;
                    }
                }
                firstColumn = false;
            }

            this._leaf = true;
            this._leafdef = true;
            return null;
        }