package edu.columbia.cs.cs1007.checkers;
import java.util.ArrayList;
import java.util.Random;
import edu.columbia.cs.cs1007.checkers.Constants.PIECE_COLOR;
import edu.columbia.cs.cs1007.checkers.Constants.PLAYER_STRATEGY;
/**
* Implementation of a checkers player.
* @author Julia Stoyanovich (jds2109@columbia.edu)
* COMS 1007, Summer 2009.
*
*/
public class Player {
private PIECE_COLOR _color;
private PLAYER_STRATEGY _strategy;
private Board _board;
/**
* Constructor.
* @param color
* @param strategy
*/
public Player(PIECE_COLOR color, PLAYER_STRATEGY strategy) {
_color = color;
_strategy = strategy;
}
/**
* Constructor that defaults to the "choose first available move" strategy.
* @param color
*/
public Player(PIECE_COLOR color) {
this(color, PLAYER_STRATEGY.FIRST);
}
/**
* Store a reference to the checkers board.
* This method does not create an instance of the board!
* @param theBoard
*/
public void setBoard(Board theBoard) {
_board = theBoard;
}
/**
* Accessor.
* @return color of the pieces with which the player plays
*/
public PIECE_COLOR getColor() {
return _color;
}
/**
* Attempt to make a move according to the strategy.
* @return string representation of the move, if one is available,
* an empty string if no available moves are left for player.
*/
public String makeAMove() {
String res="";
ArrayList<Move> possibleMoves = _board.getAllPossibleMoves(_color);
if (possibleMoves.size() > 0) {
int chosenMove;
if (_strategy == PLAYER_STRATEGY.FIRST) {
// make the first available move
chosenMove = 0;
} else {
// see how many possible moves there are,
// and choose one at random
Random rand = new Random();
chosenMove = rand.nextInt(possibleMoves.size());
}
_board.makeAMove(possibleMoves.get(chosenMove));
res = possibleMoves.get(chosenMove).toString();
}
return res;
}
}
|