示例#1
0
        private double tryMove(int[,] board, Move move)
        {
            var input = new BasicMLData (Board.SIZE * Board.SIZE);
            int index = 0;

            for (int x = 0; x < Board.SIZE; x++) {
                for (int y = 0; y < Board.SIZE; y++) {
                    if (board [x, y] == aXon.TicTacToe.Game.TicTacToe.NOUGHTS) {
                        input [index] = -1;
                    } else if (board [x, y] == aXon.TicTacToe.Game.TicTacToe.CROSSES) {
                        input [index] = 1;
                    } else if (board [x, y] == aXon.TicTacToe.Game.TicTacToe.EMPTY) {
                        input [index] = 0;
                    }

                    if ((x == move.x) && (y == move.y)) {
                        input [index] = -1;
                    }

                    index++;
                }
            }
            //var input = new BasicMLData(Board.SIZE*Board.SIZE);

            IMLData output = this.network.Compute (input);
            return output [0];
        }
示例#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)
        {
            Board.printBoard (board);
            // ask for move
            Console.WriteLine (string.Format ("You are playing: {0}, select your move.", aXon.TicTacToe.Game.TicTacToe.resolvePiece (color)));
            Console.Write ("X position? ");
            int x = getCoord ();
            Console.Write ("Y position? ");
            int y = getCoord ();

            return new Move (x, y, color);
        }
示例#3
0
 /**
  * Gets this player's next move. It is always the next available square.
  *
  * @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 player
  *            <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 for this player.
  */
 public Move getMove(int[,] board, Move prev, int player)
 {
     for (byte x = 0; x < Board.SIZE; x++) {
         for (byte y = 0; y < Board.SIZE; y++) {
             Move m = new Move (x, y, player);
             if (Board.isEmpty (board, m)) {
                 return m;
             }
         }
     }
     throw new Exception ("I'm just looking for the first "
     + "empty square and I can't move!");
 }
示例#4
0
        /**
         * Gets this player's next move. It is always the next available square.
         *
         * @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 player
         *            <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 for this player.
         */
        public Move getMove(int[,] board, Move prev, int player)
        {
            Random rand = new Random ();

            for (;;) {
                int x = (int)(rand.NextDouble () * Board.SIZE);
                int y = (int)(rand.NextDouble () * Board.SIZE);
                Move m = new Move (x, y, player);
                if (Board.isEmpty (board, m)) {
                    return m;
                }
            }
        }
示例#5
0
        public Move getMove(int[,] board, Move prev, int color)
        {
            Move bestMove = null;
            double bestScore = double.MinValue;

            for (int x = 0; x < Board.SIZE; x++) {
                for (int y = 0; y < Board.SIZE; y++) {
                    Move move = new Move ((int)x, (int)y, color);
                    if (Board.isEmpty (board, move)) {
                        double d = tryMove (board, move);
                        if ((d > bestScore) || (bestMove == null)) {
                            bestScore = d;
                            bestMove = move;
                        }
                    }
                }
            }

            return bestMove;
        }
示例#6
0
        private Move canWin(int[,]board, int color)
        {
            Move move1, move2, move3, move;
            for (int i = 0; i < Board.SIZE; i++) {
                move1 = new Move ((int)0, (int)i, color);
                move2 = new Move ((int)1, (int)i, color);
                move3 = new Move ((int)2, (int)i, color);
                move = findWin (board, move1, move2, move3, color);
                if (move != null) {
                    return move;
                }
            }

            for (int i = 0; i < Board.SIZE; i++) {
                move1 = new Move ((int)i, (int)0, color);
                move2 = new Move ((int)i, (int)1, color);
                move3 = new Move ((int)i, (int)2, color);
                move = findWin (board, move1, move2, move3, color);
                if (move != null) {
                    return move;
                }
            }

            move1 = new Move ((int)0, (int)0, color);
            move2 = new Move ((int)1, (int)1, color);
            move3 = new Move ((int)2, (int)2, color);
            move = findWin (board, move1, move2, move3, color);
            if (move != null) {
                return move;
            }

            move1 = new Move ((int)2, (int)0, color);
            move2 = new Move ((int)0, (int)0, color);
            move3 = new Move ((int)0, (int)2, color);
            move = findWin (board, move1, move2, move3, color);
            if (move != null) {
                return move;
            }

            return null;
        }
示例#7
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 ();
        }
示例#8
0
文件: Game.cs 项目: tmassey/mtos
        // main guts of the game
        private Player play(Move prev)
        {
            for (;;) {
                // new board here else players can alter the game board as they
                // wish!
                int[,] b = Board.copy (this._board);
                Move move = this._players [this._player].getMove (b, prev,
                                this._player);

                if (move == null) {
                    throw new Exception ("Player supplied null move");
                }

                // check square is empty
                if (!Board.isEmpty (this._board, move)) {
                    Console.WriteLine ("That square is not empty");
                    play (prev);
                }

                // place piece
                if (!Board.placePiece (this._board, move)) {
                    Console.WriteLine ("Illegal Move - try again");
                    play (prev);
                }

                if (Board.isWinner (this._board, this._player)) {
                    return this._players [this._player];
                }

                // change player
                this._player = TicTacToe.reverse (this._player);

                if (Board.isFull (this._board)) {
                    return null;
                }

                prev = move;
            }
        }
示例#9
0
        public Move getMove(int[,] board, Move prev, int color)
        {
            if (Board.isFull (board)) {
                return null;
            }

            Move result = null;
            int other = aXon.TicTacToe.Game.TicTacToe.reverse (color);

            if (result == null) {
                result = canWin (board, color);
            }

            if (result == null) {
                result = canWin (board, other);
            }

            // Step 1: Is the center open
            if (result == null) {
                result = centerOpen (board, color);
            }

            // Step 2: Is the corner open
            if (result == null) {
                result = cornerOpen (board, color);
            }

            // Step 3: Move somewhere
            if (result == null) {
                result = moveSomewhere (board, color);
            }

            if (result.color () != color) {
                result = new Move (result.x, result.y, color);
            }
            if (result == null)
                Console.Write ("Null result");
            return result;
        }
示例#10
0
文件: Board.cs 项目: tmassey/mtos
 /**
  * Determines if the square the specified <code>Move</code> takes place in
  * is empty or not.
  *
  * @param move
  *            <code>Move</code> to check position of.
  * @return <code>bool</code>, true if the square is empty.
  */
 public static bool isEmpty(int[,] board, Move move)
 {
     return (board [move.x, move.y] == TicTacToe.EMPTY);
 }
示例#11
0
文件: Board.cs 项目: tmassey/mtos
 /**
  * Does the move specified in <code>m</code> on <code>board</code>.
  *
  * @param board
  *            <code>int[,]</code> of board to perform move on.
  * @param m
  *            <code>Move</code> to perform.
  * @return <code>bool</code> of whether the move succeeded or not.
  * @throws IllegalStateException
  *             If the move is not legal.
  */
 public static bool placePiece(int[,] board, Move m)
 {
     if (board [m.x, m.y] != TicTacToe.EMPTY) {
         throw new Exception ("Tried to play a piece on "
         + "top of another at (" + m.x + ", " + m.y + ")");
     } else {
         board [m.x, m.y] = m.color ();
         return true;
     }
 }
示例#12
0
        private Move findWin(int[,] board, Move move1,
		                      Move move2, Move move3, int color)
        {
            if ((board [move1.x, move1.y] == color)
                && (board [move2.x, move2.y] == color)
                && (board [move3.x, move3.y] == aXon.TicTacToe.Game.TicTacToe.EMPTY)) {
                return move3;
            } else if ((board [move2.x, move2.y] == color)
                       && (board [move3.x, move3.y] == color)
                       && (board [move1.x, move1.y] == aXon.TicTacToe.Game.TicTacToe.EMPTY)) {
                return move1;
            } else if ((board [move1.x, move1.y] == color)
                       && (board [move3.x, move3.y] == color)
                       && (board [move2.x, move2.y] == aXon.TicTacToe.Game.TicTacToe.EMPTY)) {
                return move2;
            } else {
                return null;
            }
        }
示例#13
0
        private Move cornerOpen(int[,] board, int color)
        {
            Move corner1 = new Move ((int)0, (int)0, color);
            Move corner2 = new Move ((int)2, (int)2, color);
            Move corner3 = new Move ((int)2, (int)0, color);
            Move corner4 = new Move ((int)0, (int)2, color);

            if (Board.isEmpty (board, corner1)) {
                return corner1;
            } else if (Board.isEmpty (board, corner2)) {
                return corner2;
            } else if (Board.isEmpty (board, corner3)) {
                return corner3;
            } else if (Board.isEmpty (board, corner4)) {
                return corner4;
            } else {
                return null;
            }
        }
示例#14
0
 private Move centerOpen(int[,] board, int color)
 {
     Move center = new Move ((int)1, (int)1, color);
     if (Board.isEmpty (board, center)) {
         return center;
     } else {
         return null;
     }
 }
示例#15
0
文件: Node.cs 项目: tmassey/mtos
 /**
  * Creates a node with the specified board position, reached by the
  * specified move.
  *
  * @param board
  *            <code>Board</code> of the board position of this node.
  * @param move
  *            <code>Move</code> last move that was taken to reach this
  *            board position.
  */
 public Node(int[,] board, Move move)
 {
     this._board = board;
     this._move = move;
 }
示例#16
0
文件: Node.cs 项目: 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;
        }