Open main menu

lensowiki β

Changes

Computer Science/61b/Projects/Network/player/MachinePlayer.java

2,049 bytes added, 07:32, 22 September 2007
no edit summary
{{code}}
/* MachinePlayer.java */

package player;

/**
* An implementation of an automatic Network player. Keeps track of moves
* made by both players. Can select a move for itself.
*/
public class MachinePlayer extends Player {

private int color;
private int opponent;
private int depth;
private Gameboard board;

public static final int SDEPTH = 2;

// Creates a machine player with the given color. Color is either 0 (black)
// or 1 (white). (White has the first move.)
public MachinePlayer(int color) {
this(color, SDEPTH);
}

// Creates a machine player with the given color and search depth. Color is
// either 0 (black) or 1 (white). (White has the first move.)
public MachinePlayer(int color, int searchDepth) {
this.color = color;
opponent = Gameboard.generateOpponent(color);
depth = searchDepth;
board = new Gameboard();
}

// Returns a new move by "this" player. Internally records the move (updates
// the internal game board) as a move by "this" player.
public Move chooseMove() {
Move winningmove = board.evalTree(color, depth);
board.performMove(color, winningmove);
return winningmove;
}

// If the Move m is legal, records the move as a move by the opponent
// (updates the internal game board) and returns true. If the move is
// illegal, returns false without modifying the internal state of "this"
// player. This method allows your opponents to inform you of their moves.
public boolean opponentMove(Move m) {
return board.performMove(opponent, m);
}

// If the Move m is legal, records the move as a move by "this" player
// (updates the internal game board) and returns true. If the move is
// illegal, returns false without modifying the internal state of "this"
// player. This method is used to help set up "Network problems" for your
// player to solve.
public boolean forceMove(Move m) {
return board.performMove(color, m);
}

public int boardScore() {
return board.winner();
}
}
1,277
edits