|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package net.hoo2.auth.dsproject.snake;
-
- import java.util.*;
-
- /**
- * @class HeuristicPlayer
- * @brief Represent a Heuristic Player in the Game
- *
- * The players are playing in a round-robin sequence and we keep track
- * for each one of them their playing order, score and place on the board.
- * This kind of player, is a cheater. He can control the dice. Not fair dude.
- *
- * @author Christos Choutouridis AEM:8997
- * @email cchoutou@ece.auth.gr
- */
- public class HeuristicPlayer
- extends Player {
-
- /** @name Constructors */
- /** @{ */
- /** Default doing nothing constructor */
- public HeuristicPlayer() {
- super ();
- path = new ArrayList<Integer[]>();
- }
- /**
- * @brief The main constructor
- *
- * This creates a player for the game
- * @param playerId The player's to create
- * @param name The name of the player
- * @param board Reference to the board the player will play on.
- */
- HeuristicPlayer (int playerId, String name) {
- super (playerId, name);
- path = new ArrayList<Integer[]>();
- }
- /* @} */
-
- /** @name Get/Set interface */
- /** @{ */
- ArrayList<Integer[]> getPath() { return path; }
- void setPath (ArrayList<Integer[]> path) {
- this.path = path;
- }
- /** @} */
-
- /**
- * Override dice functionality for the player
- * @return As this is called from the game only to select playing order
- * we cheat and return 1
- */
- @Override
- int dice () {
- return 1;
- }
-
- /**
- * Override get the next tile after the user's move
- * @param tile The initial tile
- * @return The tile after the move
- */
- @Override
- int getNextMove (int tile) {
- Map<Integer, Double> moves = new HashMap<Integer, Double>();
- double max = Double.NEGATIVE_INFINITY;
- double ev = Double.NEGATIVE_INFINITY;
- int roll = 0;
-
- // Evaluate each possible dice result and find the better one
- for (int r=1 ; r<=6 ; ++r) {
- moves.put (new Integer(r), evaluate (tile, r));
- if ((ev = moves.get(r)) > max) {
- max = ev;
- roll = r;
- }
- }
- // Do the move and get the move data
- Integer[] move_data = Arrays.stream(move (tile, roll, true))
- .boxed()
- .toArray(Integer[]::new);
- // Store the move data
- path.add(move_data);
- return tile + roll; // return the new tile position
- }
-
- /**
- * The Heuristic statistics version
- * @param verbose Flag to select the verbosity
- * @param sum Flag to select if we need to print a summarize of the user hystory
- */
- @Override
- void statistics (boolean verbose, boolean sum) {
- if (sum) {
- // If we run the summarize
- int nSnakes =0;
- int nLadders =0;
- int nRedApples =0;
- int nBlackApples =0;
-
- // Calculate frequencies
- for (int i=0 ; i<path.size() ; ++i) {
- nSnakes += path.get(i)[MOVE_SNAKES_IDX];
- nLadders+= path.get(i)[MOVE_LADDERS_IDX];
- nRedApples += path.get(i)[MOVE_RED_APPLES_IDX];
- nBlackApples += path.get(i)[MOVE_BLACK_APPLES_IDX];
- }
- // Print the results
- System.out.println("");
- System.out.println("*** Statistics for " + name + " ***");
- System.out.println(" Number of Snake bites : " + nSnakes);
- System.out.println(" Number of Ladders used : " + nLadders);
- System.out.println(" Number of Red Apples eaten : " + nRedApples);
- System.out.println(" Number of Black Apples eaten: " + nBlackApples);
-
- }
- else
- // Call the base version
- super.statistics(verbose, sum);
- }
-
-
- /**
- * The main evaluation function
- * @param tile The current tile of the player
- * @param roll the roll to check
- * @return The evaluation of the roll
- */
- private double evaluate (int tile, int roll) {
- int[] check = new int[MOVE_DATA_SIZE];
- check = move(tile, roll, false);
-
- return 0.65*check[MOVE_STEPS_IDX] + 0.35*check[MOVE_POINTS_IDX];
- }
-
- /** @name Data members package access only */
- /** @{ */
- private ArrayList<Integer[]> path; /**< Players history as required */
- /** @} */
- }
|