/** * @file RandomPlayer.java * @author Kyle Burke <paithanq@gmail.com> * @date * * @brief A Combinatorial Game player who always chooses a uniformly random move. * */ //package something; import java.lang.*; import java.io.*; import java.util.*; public class RandomPlayer<G extends CombinatorialGame> extends Player { //instance variables //constants //constructors /** * Creates a random player. */ public RandomPlayer() { //do nothing } //public methods public G getMove(CombinatorialGame position, int playerId) { Random randomGenerator = new Random(); Collection<G> optionCollection = (Collection<G>) position.getOptions(playerId); if (optionCollection.size() == 0) { throw new NoSuchElementException("No moves of " + position + " exist for " + position.getPlayerName(playerId) + ". Tried to call Player.getMove()!"); } Object[] possibleOptions = optionCollection.toArray(); return (G) (possibleOptions[randomGenerator.nextInt(possibleOptions.length)]); } /** * Returns a string version of this board. */ public String toString() { return "Random Al"; } } //end of RandomPlayer.java