Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class TrollGame
  4. {
  5. //constants for this version of the game
  6. private static final int INIT_PLAYER_POINTS = 160;
  7. private static final int PLAYER_POINTS_DEC = -10;
  8. private static final int TREASURE_POINTS = 500;
  9. private static final int ROWS = 8;
  10. private static final int COLS = 10;
  11. // random number generator
  12. private Random rand;
  13. // the game board, a 2D array of GamePieces
  14. private GamePiece[][] gameBoard;
  15. // variables to keep track of the locations of player, troll
  16. private int curPlayerRow, curPlayerCol;
  17. private int curTrollRow, curTrollCol;
  18. // the player's status
  19. private boolean playerWins;
  20. private boolean playerLoses;
  21.  
  22. GamePiece player=new Player();
  23. GamePiece treasure=new Treasure();
  24.  
  25.  
  26. /* Constructor that uses an unseeded instance of the random number generator.
  27. * Calls initBoard.
  28. */
  29. public TrollGame()
  30. {
  31. rand = new Random();
  32. initBoard(ROWS, COLS, rand);
  33.  
  34. }
  35.  
  36. /* Constructor that uses a seeded instance of the random number generator.
  37. * Calls initBoard.
  38. */
  39. public TrollGame(int seed)
  40. {
  41.  
  42. rand = new Random(seed);
  43. initBoard(ROWS, COLS, rand);
  44.  
  45. }
  46.  
  47. public void movePlayer(String direction)
  48. {
  49.  
  50. int vertical = 0;
  51. int horizontal = 0;
  52.  
  53. switch (direction){
  54. case "u": {
  55. if (curPlayerRow != 0)
  56. curPlayerRow--;
  57. break;}
  58. case "d": {
  59. if (curPlayerRow != ROWS)
  60. vertical = 1;
  61. break;}
  62. case "l": {
  63. if (curPlayerCol != 0)
  64. horizontal = -1;
  65. break;}
  66. case "r": {
  67. if (curPlayerCol != COLS)
  68. horizontal = 1;
  69. break;}
  70. }
  71.  
  72. int oldPlayerRow = curPlayerRow;
  73. int oldPlayerCol = curPlayerCol;
  74. curPlayerRow += vertical;
  75. curPlayerCol += horizontal;
  76. adjustPlayerLifeLevel(curPlayerRow, curPlayerCol);
  77.  
  78. if(playerFoundTreasure(curPlayerRow, curTrollCol))
  79. {
  80. playerWins = true;
  81. overwritePosition(oldPlayerRow, oldPlayerCol, curPlayerRow, curPlayerCol);}
  82. else{
  83. swapPosition(oldPlayerRow, oldPlayerCol, curPlayerRow, curPlayerCol);}
  84.  
  85. int[] newPosition = calcNewTrollCoordinates(curPlayerRow, curPlayerCol, curTrollRow, curTrollCol);
  86. swapPosition(curTrollRow, curPlayerCol, newPosition[0], newPosition[1]);
  87. curTrollRow = newPosition[0];
  88. curPlayerCol = newPosition[1];
  89.  
  90. }
  91.  
  92. /* Returns true if the player wins, false otherwise. */
  93. public boolean playerWins()
  94. {
  95. return playerWins;
  96. }
  97.  
  98. /* Returns true if the player loses, false otherwise. */
  99. public boolean playerLoses()
  100. {
  101. return (curPlayerRow == curTrollRow && curPlayerCol == curTrollCol);
  102. }
  103.  
  104. /* Returns the number of treasure points. */
  105. public int getTreasurePoints()
  106. {
  107. return TREASURE_POINTS;
  108. }
  109.  
  110. /* Resets the game variables and game board (call initBoard).
  111. * Does NOT change the random number generator instance.
  112. */
  113. public void resetGame()
  114. {
  115. initBoard(ROWS, COLS, rand);
  116. }
  117.  
  118. /* Returns a String version of the game. */
  119. public String getGameStr()
  120. {
  121. StringBuilder outStr = new StringBuilder();
  122. for (int i = 0; i < ROWS; i++)
  123. {
  124. for (int j = 0; j < COLS; j++)
  125. outStr.append("|").append(gameBoard[i][j].show());
  126. outStr.append("|").append(System.getProperty("line.separator"));
  127. }
  128. return outStr.toString();
  129. }
  130.  
  131. private GamePiece[][] initBoard(int rows, int cols, Random rand)
  132. {
  133. curPlayerRow = 0;
  134. curPlayerCol = 0;
  135. curTrollRow = 0;
  136. curPlayerCol = 0;
  137. playerWins = false;
  138. playerLoses = false;
  139. gameBoard = new GamePiece[ROWS][COLS];
  140. curTrollRow = getRandTrollRow(rand, ROWS);
  141. curTrollCol = getRandTrollCol(rand, COLS);
  142.  
  143. for (int i = 0; i < ROWS; i++){
  144. for (int j = 0; j < COLS; j++){
  145. gameBoard[i][j] = new EmptyPiece();}
  146. }
  147.  
  148. gameBoard[0][0] = player;
  149. gameBoard[7][9] = treasure;
  150. gameBoard[curTrollRow][curTrollCol] = new Troll();
  151.  
  152. player.updateLifePoints(INIT_PLAYER_POINTS);
  153. treasure.updateLifePoints(TREASURE_POINTS);
  154.  
  155. return gameBoard;
  156. }
  157.  
  158. /* Returns true if the player is alive, false otherwise.*/
  159. private boolean playerAlive(int curPlayerRow, int curPlayerCol)
  160. {
  161. return gameBoard[curPlayerRow][curPlayerCol].isAlive();
  162. }
  163.  
  164. /* Adjusts the player's life level by the amount PLAYER_POINTS_DEC. */
  165. private void adjustPlayerLifeLevel(int curPlayerRow, int curPlayerCol)
  166. {
  167. player.updateLifePoints(PLAYER_POINTS_DEC);
  168. }
  169.  
  170. /* Returns true if the player row and column passed in equals
  171. the treasure row and column. */
  172. private boolean playerFoundTreasure(int playerRow, int playerCol)
  173. {
  174. return (playerRow == ROWS-1 && playerCol == COLS-1);
  175. }
  176.  
  177. /* Returns a random number in [1,numRows-1] */
  178. private int getRandTrollRow(Random rand, int numRows)
  179. {
  180. return rand.nextInt(ROWS-1)+1;
  181. }
  182.  
  183. /* Returns a random number in [1,numCols-1] */
  184. private int getRandTrollCol(Random rand, int numCols)
  185. {
  186. return rand.nextInt(COLS-1)+1;
  187. }
  188.  
  189. private int[] calcNewTrollCoordinates(int playerRow, int playerCol, int trollRow, int trollCol)
  190. {
  191. int[] newPos = new int[2];
  192.  
  193. if (playerRow == trollRow && playerCol == trollCol){
  194. newPos[0]=trollRow;
  195. newPos[1]=trollCol;}
  196. else if (Math.abs(playerRow - trollRow) < Math.abs(playerCol - trollCol)){
  197. if (playerCol > trollCol){
  198. newPos[0]=trollRow;
  199. newPos[1]=trollCol + 1;}
  200. else {
  201. newPos[0]=trollRow;
  202. newPos[1]=trollCol - 1;}
  203. }
  204. else {
  205. if (playerRow > trollRow){
  206. newPos[0]=trollRow + 1;
  207. newPos[1]=trollCol;}
  208. else {
  209. newPos[0]=trollRow - 1;
  210. newPos[1]=trollCol;}
  211. }
  212. return newPos;
  213.  
  214. }
  215.  
  216. // The following three methods may be helpful when adjusting the GamePieces in the movePlayer method. */
  217.  
  218. /* Overwrite the GamePiece at the coordinates passed in with an empty GamePiece. */
  219. private void overwritePositionWithEmpty(int row, int col)
  220. {
  221. gameBoard[row][col] = new EmptyPiece();
  222. }
  223.  
  224. /* Overwrite the GamePiece at the new coordinates with the GamePiece at the
  225. old coordinates. Place a new EmptyPiece at the old coordinates. */
  226. private void overwritePosition(int oldRow, int oldCol, int newRow, int newCol)
  227. {
  228. gameBoard[newRow][newCol] = gameBoard[oldRow][oldCol];
  229. gameBoard[oldRow][oldCol] = new EmptyPiece();
  230. }
  231.  
  232. /* Swap the position of the GamePiece at the current position with the
  233. * GamePiece at the new position. */
  234. private void swapPosition(int curRow, int curCol, int newRow, int newCol)
  235. {
  236. GamePiece temp = gameBoard[curRow][curCol];
  237. gameBoard[curRow][curCol] = gameBoard[newRow][newCol];
  238. gameBoard[newRow][newCol] = temp;
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement