A java snake game for A.U.TH. Data structures class
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.4 KiB

  1. package net.hoo2.auth.dsproject.snake;
  2. import java.util.*;
  3. /**
  4. * @class HeuristicPlayer
  5. * @brief Represent a Heuristic Player in the Game
  6. *
  7. * The players are playing in a round-robin sequence and we keep track
  8. * for each one of them their playing order, score and place on the board.
  9. * This kind of player, is a cheater. He can control the dice. Not fair dude.
  10. *
  11. * @author Christos Choutouridis AEM:8997
  12. * @email cchoutou@ece.auth.gr
  13. */
  14. public class HeuristicPlayer
  15. extends Player {
  16. /** @name Constructors */
  17. /** @{ */
  18. /** Default doing nothing constructor */
  19. public HeuristicPlayer() {
  20. super ();
  21. path = new ArrayList<Integer[]>();
  22. }
  23. /**
  24. * @brief The main constructor
  25. *
  26. * This creates a player for the game
  27. * @param playerId The player's to create
  28. * @param name The name of the player
  29. * @param board Reference to the board the player will play on.
  30. */
  31. HeuristicPlayer (int playerId, String name) {
  32. super (playerId, name);
  33. path = new ArrayList<Integer[]>();
  34. }
  35. /* @} */
  36. /** @name Get/Set interface */
  37. /** @{ */
  38. ArrayList<Integer[]> getPath() { return path; }
  39. void setPath (ArrayList<Integer[]> path) {
  40. this.path = path;
  41. }
  42. /** @} */
  43. /**
  44. * Override dice functionality for the player
  45. * @return As this is called from the game only to select playing order
  46. * we cheat and return 1
  47. */
  48. @Override
  49. int dice () {
  50. return 1;
  51. }
  52. /**
  53. * Override get the next tile after the user's move
  54. * @param tile The initial tile
  55. * @return The tile after the move
  56. */
  57. @Override
  58. int getNextMove (int tile) {
  59. Map<Integer, Double> moves = new HashMap<Integer, Double>();
  60. double max = Double.NEGATIVE_INFINITY;
  61. double ev = Double.NEGATIVE_INFINITY;
  62. int roll = 0;
  63. // Evaluate each possible dice result and find the better one
  64. for (int r=1 ; r<=6 ; ++r) {
  65. moves.put (new Integer(r), evaluate (tile, r));
  66. if ((ev = moves.get(r)) > max) {
  67. max = ev;
  68. roll = r;
  69. }
  70. }
  71. // Do the move and get the move data
  72. Integer[] move_data = Arrays.stream(move (tile, roll, true))
  73. .boxed()
  74. .toArray(Integer[]::new);
  75. // Store the move data
  76. path.add(move_data);
  77. return tile + roll; // return the new tile position
  78. }
  79. /**
  80. * The Heuristic statistics version
  81. * @param verbose Flag to select the verbosity
  82. * @param sum Flag to select if we need to print a summarize of the user hystory
  83. */
  84. @Override
  85. void statistics (boolean verbose, boolean sum) {
  86. if (sum) {
  87. // If we run the summarize
  88. int nSnakes =0;
  89. int nLadders =0;
  90. int nRedApples =0;
  91. int nBlackApples =0;
  92. // Calculate frequencies
  93. for (int i=0 ; i<path.size() ; ++i) {
  94. nSnakes += path.get(i)[MOVE_SNAKES_IDX];
  95. nLadders+= path.get(i)[MOVE_LADDERS_IDX];
  96. nRedApples += path.get(i)[MOVE_RED_APPLES_IDX];
  97. nBlackApples += path.get(i)[MOVE_BLACK_APPLES_IDX];
  98. }
  99. // Print the results
  100. System.out.println("");
  101. System.out.println("*** Statistics for " + name + " ***");
  102. System.out.println(" Number of Snake bites : " + nSnakes);
  103. System.out.println(" Number of Ladders used : " + nLadders);
  104. System.out.println(" Number of Red Apples eaten : " + nRedApples);
  105. System.out.println(" Number of Black Apples eaten: " + nBlackApples);
  106. }
  107. else
  108. // Call the base version
  109. super.statistics(verbose, sum);
  110. }
  111. /**
  112. * The main evaluation function
  113. * @param tile The current tile of the player
  114. * @param roll the roll to check
  115. * @return The evaluation of the roll
  116. */
  117. private double evaluate (int tile, int roll) {
  118. int[] check = new int[MOVE_DATA_SIZE];
  119. check = move(tile, roll, false);
  120. return 0.65*check[MOVE_STEPS_IDX] + 0.35*check[MOVE_POINTS_IDX];
  121. }
  122. /** @name Data members package access only */
  123. /** @{ */
  124. private ArrayList<Integer[]> path; /**< Players history as required */
  125. /** @} */
  126. }