Guest User

Untitled

a guest
Nov 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. import cs1.*;
  2.  
  3.  
  4. public class TreasureFinderGameDeluxe
  5. {
  6. public static void main(String[] args)
  7. {
  8.  
  9. /** Constants for readability/flexibility*******/
  10. final int TREASURE = 999;
  11. final int GUESSED = -2;
  12. boolean resume = false;
  13. int gamemode = 0;
  14. while (!resume) {
  15. System.out.print("Easy(0) Medium(1) Hard(2): ");
  16. gamemode = Keyboard.readInt();
  17. if (gamemode < 0 || gamemode > 2) {
  18. System.out.println("Please select a valid game mode");
  19. resume = false;
  20. }
  21. else {
  22. resume = true;
  23. }
  24. }
  25. final int GAMEMODE = gamemode;
  26. System.out.print("\f");
  27. int boardsize = 0;
  28. resume = false;
  29. while (!resume) {
  30. System.out.print("How large would you like the board to be? ");
  31. boardsize = Keyboard.readInt();
  32. if (boardsize <= 1) {
  33. System.out.println("Please select a board size larger than one");
  34. resume = false;
  35. }
  36. else {
  37. resume = true;
  38. }
  39. }
  40. final int BOARDSIZE = boardsize;
  41. final int CLOSE = -1;
  42. final int FAR = -3;
  43.  
  44. /** VARIABLES **********************************/
  45. int[][] board = new int[BOARDSIZE][BOARDSIZE];
  46.  
  47. int randRow = (int)(Math.random()*BOARDSIZE);
  48. int randCol = (int)(Math.random()*BOARDSIZE);
  49.  
  50. board[randRow][randCol] = TREASURE; //TREASURE PLACED AT RANDOM LOCATION, MARKED WITH 999
  51.  
  52. int guesses = 1; //Keeps track of # user guesses (they only get 3)
  53.  
  54. int row = 0;
  55. int col = 0; //Keeps track of user's row/col guess
  56. boolean winner = false; //Determines if user has won yet
  57. boolean close = false;
  58. boolean realNum = false;
  59. int howFar = 0;
  60. /** MAIN LOOP **********************************/
  61. while (guesses <= BOARDSIZE && winner == false)
  62. {
  63.  
  64. printBoard(board,false);
  65.  
  66. //GET GUESS row/col
  67. System.out.println("Guess #" + guesses);
  68. realNum = false;
  69. while (!realNum) {
  70. System.out.print("r: ");
  71. row = Keyboard.readInt();
  72. if (row >= BOARDSIZE || row < 0) {
  73. System.out.println("Please select a valid row");
  74. realNum = false;
  75. }
  76. else {
  77. realNum = true;
  78. }
  79. }
  80. realNum = false;
  81. while (!realNum) {
  82. System.out.print("c: ");
  83. col = Keyboard.readInt();
  84. if (col >= BOARDSIZE || col < 0) {
  85. System.out.println("Please select a valid column");
  86. realNum = false;
  87. }
  88. else {
  89. realNum = true;
  90. }
  91. }
  92. //IF CORRECT END GAME, OTHERWISE MARK SPOT GUESSED
  93. if (board[row][col] == TREASURE)
  94. winner = true;
  95. else
  96. {
  97. board[row][col] = GUESSED;
  98. guesses++;
  99. if (Math.abs(randCol - col) < Math.abs(randRow - row)) howFar = Math.abs(randRow - row);
  100. else howFar = Math.abs(randCol - col);
  101. if (howFar == 1 && GAMEMODE == 1) {
  102. board[row][col] = CLOSE;
  103. }
  104. else if (GAMEMODE == 0) {
  105. if (Math.abs(randCol - col) < Math.abs(randRow - row)) howFar = Math.abs(randRow - row);
  106. else howFar = Math.abs(randCol - col);
  107. board[row][col] = howFar;
  108. }
  109. }
  110.  
  111. } //END WHILE
  112. printBoard(board,true);
  113. if (winner && guesses == 1)
  114. System.out.println("\nCongratulations, you won in " + guesses + " guess!");
  115. else if (winner)
  116. System.out.println("\nYou won in " + guesses + " guesses!");
  117. else
  118. System.out.println("You lost!");
  119.  
  120.  
  121. }//END MAIN
  122.  
  123.  
  124. //HELPER METHOD TO REDUCE CODE IN MAIN
  125. //NOTICE - can't use constants here. We will discuss better game designs soon!
  126.  
  127. //Post-Condition: THE BOARD HAS NOT BEEN ALTERED IN ANY WAY!!!
  128. private static void printBoard(int[][] arr, boolean isEnd)
  129. {
  130. //PRINT BOARD
  131. System.out.print("\f");
  132.  
  133.  
  134.  
  135.  
  136. for (int r = 0; r < arr.length; r++)
  137. {
  138. for (int c = 0; c < arr[0].length; c++)
  139. {
  140. if (arr[r][c] == -2)
  141. System.out.print(" \t");
  142. else if (arr[r][c] == -1)
  143. System.out.print("CLOSE\t");
  144. else if (isEnd && arr[r][c] == 999)
  145. System.out.print("$$$$$\t");
  146. else if (arr[r][c] > 0 && arr[r][c] != 999)
  147. System.out.print(arr[r][c] + "\t");
  148. else
  149. System.out.print("(" + r + "," + c + ")\t");
  150.  
  151. }
  152. System.out.println();
  153. }
  154.  
  155. }
  156.  
  157.  
  158. }//END CLASS
Add Comment
Please, Sign In to add comment