Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.70 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /* The game is played in a series of rounds. The game board is a 2D array with
  4. * a set number of rows and columns. The array contains Objects of type GamePiece.
  5. * There are four types of GamePieces: Player- the user, Troll- the villain, Treasure- the
  6. * goal, and EmptyPiece- represents cells that are not one of the previous types.
  7. * The player's goal is to reach the treasure without encountering the Troll.
  8. * The Troll's goal is to intercept the player before the player reaches the treasure.
  9. * The player starts in the upper left-hand corner- position 0,0.
  10. * The treasure is located at the lower right-hand corner- position 7, 9.
  11. * The Troll starts at a random position in the board that is not
  12. * the player's starting position or the treasure's position.
  13. * The player makes a move one step in one of four directions: up, down, left, right.
  14. * If the player is on a border and the requested move would take the player off the
  15. * board, the move is ignored- i.e. the player does not move.
  16. * The Troll knows the player's position and after the player moves, the Troll will
  17. * move towards the player. If the Troll's next move reaches the player, the Troll
  18. * wins. If the player reaches the treasure and the Troll also reaches the treasure
  19. * in the same round, the player wins.
  20. * The player starts with 160 life points. Each move the player makes costs 10 life points.
  21. * This deduction occurs for all player moves- even if the player attempts to move off the board.
  22. * If a player's life level drops to 0 or less, the player is not alive an cannot move.
  23. */
  24.  
  25. public class TrollGame {
  26. //constants for this version of the game
  27. private static final int INIT_PLAYER_POINTS = 160;
  28. private static final int PLAYER_POINTS_DEC = -10;
  29. private static final int TREASURE_POINTS = 500;
  30. private static final int ROWS = 8;
  31. private static final int COLS = 10;
  32.  
  33. // random number generator
  34. private Random rand;
  35.  
  36. // the game board, a 2D array of GamePieces
  37. private GamePiece[][] gameBoard;
  38.  
  39. // variables to keep track of the locations of player, troll
  40. private int curPlayerRow, curPlayerCol;
  41. private int curTrollRow, curTrollCol;
  42.  
  43. // the player's status
  44. private boolean playerWins;
  45. private boolean playerLoses;
  46.  
  47. private int lifePoints;
  48.  
  49.  
  50. /* Constructor that uses an unseeded instance of the random number generator.
  51. * Calls initBoard.
  52. */
  53. public TrollGame () {
  54.  
  55. Random rand = new Random();
  56. gameBoard=initBoard(ROWS,COLS,rand);
  57.  
  58. }
  59.  
  60. /* Constructor that uses a seeded instance of the random number generator.
  61. * Calls initBoard.
  62. */
  63. public TrollGame (int seed){
  64.  
  65. Random rand = new Random(seed);
  66. gameBoard=initBoard(ROWS,COLS,rand);
  67.  
  68. }
  69.  
  70. /*
  71. * Manages a player move. This is a multi-step process that involves:
  72. * 1- if the player is alive:
  73. * - determine the direction of the user's input.
  74. * - check that the user move is valid. If not, player does not change position,
  75. * if valid, adjust player position accordingly.
  76. * - adjust player life level- regardless of validity of move.
  77. * 2- calculate troll's move given the player's new position.
  78. * 3- check if troll has same position as player.if so, player loses. Adjust the GamePieces
  79. * so that the board is displayed properly, i.e. the player is gone and is replaced by the troll.
  80. * 4- check if the player reached the treasure. If so, the player wins. Adjust the GamePieces
  81. * so that the board is displayed properly, i.e. the player now appears in the treasure position.
  82. * Also, update the troll's last position.
  83. * 5- If neither 3 nor 4 above apply, then update the player and trollpositions.
  84. * Adjust the GamePieces so that the board is displayed properly, i.e. the player and troll appear
  85. * in their new positions.
  86. */
  87. public void movePlayer(String direction) {
  88. if (playerAlive(curPlayerRow,curPlayerCol)) {
  89. if (curPlayerRow==0 && direction == "u") {
  90. curPlayerRow=curPlayerRow;
  91. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);
  92.  
  93. }
  94. else if (curPlayerRow==ROWS-1 && direction == "d") {
  95. curPlayerRow=curPlayerRow;
  96. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);
  97. }
  98. else if (curPlayerCol==0 && direction == "l") {
  99. curPlayerCol=curPlayerCol;
  100. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);
  101. }
  102. else if (curPlayerCol==COLS-1 && direction == "r") {
  103. curPlayerCol=curPlayerCol;
  104. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);
  105. }
  106. else {
  107. if (direction == "u") {overwritePositionWithEmpty(curPlayerRow,curPlayerCol);
  108. curPlayerRow--;
  109. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);}
  110. else if (direction == "d") {overwritePositionWithEmpty(curPlayerRow,curPlayerCol);
  111. curPlayerRow++;
  112. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);}
  113. else if (direction == "l") {overwritePositionWithEmpty(curPlayerRow,curPlayerCol);
  114. curPlayerCol--;
  115. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);}
  116. else if (direction == "r") {overwritePositionWithEmpty(curPlayerRow,curPlayerCol);
  117. curPlayerCol++;
  118. adjustPlayerLifeLevel(curPlayerRow,curPlayerCol);}
  119. }
  120. }
  121.  
  122. if (playerFoundTreasure(curPlayerRow,curPlayerCol)) {
  123. curPlayerCol=COLS-1;
  124. curPlayerRow=ROWS-1;
  125. playerWins=true;
  126. overwritePosition(curPlayerRow,curPlayerCol,ROWS-1,COLS-1);}
  127. else if (curPlayerCol==curTrollCol && curPlayerRow==curTrollRow)
  128. {playerLoses=true;
  129. overwritePosition(curPlayerRow,curPlayerCol,curTrollRow,curTrollCol);}
  130.  
  131. calcNewTrollCoordinates(curPlayerRow,curPlayerCol,curTrollRow,curTrollCol);
  132.  
  133.  
  134. }
  135.  
  136. /* Returns true if the player wins, false otherwise. */
  137. public boolean playerWins(){
  138. if (playerFoundTreasure(curPlayerRow, curPlayerCol))
  139. {playerWins=true;}
  140. return playerWins;
  141. }
  142.  
  143. /* Returns true if the player loses, false otherwise. */
  144. public boolean playerLoses(){
  145. if (!(playerAlive(curPlayerRow, curPlayerCol))) {playerLoses=true;};
  146. return playerLoses;
  147. }
  148.  
  149. /* Returns the number of treasure points. */
  150. public int getTreasurePoints(){
  151.  
  152. return TREASURE_POINTS;
  153. }
  154.  
  155. /* Resets the game variables and game board (call initBoard).
  156. * Does NOT change the random number generator instance.
  157. */
  158. public void resetGame(){
  159. gameBoard=initBoard(ROWS,COLS,rand);
  160. }
  161.  
  162. /* Returns a String version of the game. */
  163. public String getGameStr() {
  164. StringBuilder outStr = new StringBuilder();
  165. for(int i=0;i<ROWS;i++) {
  166. for(int j=0;j<COLS;j++)
  167. outStr.append("|").append(gameBoard[i][j].show());
  168. outStr.append("|").append(System.getProperty("line.separator"));
  169. }
  170. return outStr.toString();
  171. }
  172.  
  173. /** private methods below **/
  174.  
  175. /* Creates the game board array with rows and cols. The player starts in the upper left-hand corner.
  176. * The treasure is at the lower right-hand corner. The rest of the cells are empty pieces.
  177. * The player starts with INIT_PLAYER_POINTS life points. The treasure is initialized to
  178. * TREASURE_POINTS. The Troll position is determined at random by calling the getRandTrollRow
  179. * and getRandTrollCol methods. This method returns the initialized array.
  180. */
  181. private GamePiece[][] initBoard(int rows, int cols, Random rand) {
  182. playerWins=false;
  183. playerLoses=false;
  184. curPlayerRow=0;
  185. curPlayerCol=0;
  186. //GamePiece.updateLifePoints(INIT_PLAYER_POINTS);
  187.  
  188. GamePiece[][] gameBoard = new GamePiece[rows][cols];
  189.  
  190. do {curTrollRow=getRandTrollRow(rand, rows);
  191. curTrollCol=getRandTrollCol(rand, cols);}
  192. while ((!(curTrollRow==curPlayerRow && curTrollCol==curPlayerCol)) && (!(curTrollRow==rows-1 && curTrollCol==cols-1)));
  193.  
  194. gameBoard[0][0]=new Player();
  195. gameBoard[curTrollRow][curTrollCol]=new Troll();
  196. gameBoard[rows-1][cols-1]=new Treasure();
  197.  
  198. for (int i=0; i<rows-1; i++) {
  199. for (int j=0; j<cols-1; j++) {
  200. if (!(i==0 && j==0) || (!(i==rows-1 && j==cols-1))|| (!(i==curTrollRow && j==curTrollCol)));
  201. {gameBoard[i][j]=new EmptyPiece();}
  202. }
  203. }
  204.  
  205. return gameBoard;
  206. }
  207.  
  208. /* Returns true if the player is alive, false otherwise.*/
  209. private boolean playerAlive(int curPlayerRow, int curPlayerCol){
  210.  
  211. if (lifePoints>0 && !(this.curPlayerRow==this.curTrollRow && this.curPlayerCol==this.curTrollCol)){
  212. return true;}
  213.  
  214. else {
  215. return false;}
  216. }
  217.  
  218. /* Adjusts the player's life level by the amount PLAYER_POINTS_DEC. */
  219. private void adjustPlayerLifeLevel(int curPlayerRow, int curPlayerCol){
  220. lifePoints-=PLAYER_POINTS_DEC;
  221. }
  222.  
  223. /* Returns true if the player row and column passed in equals
  224. the treasure row and column. */
  225. private boolean playerFoundTreasure(int playerRow, int playerCol){
  226.  
  227. if (playerRow==ROWS-1 && playerCol==COLS-1)
  228. {lifePoints+=TREASURE_POINTS;
  229. return true;}
  230. else return false;
  231.  
  232. }
  233.  
  234. /* Returns a random number in [1,numRows-1] */
  235. private int getRandTrollRow(Random rand, int numRows){
  236.  
  237. int randomRow=rand.nextInt(numRows-1);
  238.  
  239. return randomRow;
  240. }
  241.  
  242. /* Returns a random number in [1,numCols-1] */
  243. private int getRandTrollCol(Random rand, int numCols){
  244.  
  245. int randomCol=rand.nextInt(numCols-1);
  246.  
  247. return randomCol;
  248. }
  249.  
  250. /*
  251. * This method calculates the direction the troll will move to get as close to the player as possible.
  252. * The player and current troll positions are passed in. The method chooses a move in one direction
  253. * that will bring it closer to the player. The method returns an int array [row, col], where row is the
  254. * index of the verical position and col is the index of the horizontal position in the game board.
  255. * The strategy of calculating the new troll position is to minimize the distance between the troll and player.
  256. * This can be done by using the Manhattan, or city block, distance between the two pieces.
  257. * Determine the horizontal distance and then the vertical distance. The troll will want to move to
  258. * decrease the greater of the two distances. Note that the direction is also important. The sign of
  259. * the difference can indicate the direction to move in. Asume the player is never out of bounds.
  260. * Note: the player should loose if they move onto the troll's position. The troll should detect this
  261. * situation and not make a move.
  262. */
  263. private int[] calcNewTrollCoordinates(int playerRow, int playerCol, int trollRow, int trollCol){
  264.  
  265. /* int[][] newPos = new int[ROWS][COLS];
  266.  
  267. if ((trollRow-playerRow) < (trollCol-playerCol)) {
  268. newPos={[trollRow-1][trollCol]};}
  269.  
  270. else if ((playerRow-trollRow) < (playerCol-trollCol)) {
  271. newPos={[trollRow+1][trollCol]};}
  272.  
  273. else if ((playerRow-trollRow) > (playerCol-trollCol)) {
  274. newPos={[trollRow][trollCol+1]};}*/
  275.  
  276. return null;
  277. }
  278.  
  279. // The following three methods may be helpful when adjusting the GamePieces in the movePlayer method. */
  280.  
  281. /* Overwrite the GamePiece at the coordinates passed in with an empty GamePiece. */
  282. private void overwritePositionWithEmpty(int row, int col){
  283.  
  284. gameBoard[row][col]=new EmptyPiece();
  285.  
  286. }
  287.  
  288. /* Overwrite the GamePiece at the new coordinates with the GamePiece at the
  289. old coordinates. Place a new EmptyPiece at the old coordinates. */
  290. private void overwritePosition(int oldRow, int oldCol, int newRow, int newCol){
  291.  
  292. if (playerWins()) {
  293. overwritePositionWithEmpty(oldRow,oldCol);
  294. newRow=ROWS-1;
  295. newCol=COLS-1;
  296. gameBoard[newRow][newCol]=new Player();}
  297.  
  298. else if (curPlayerRow==curTrollRow && curPlayerCol==curTrollCol)
  299. {gameBoard[newRow][newCol]= new Troll();
  300. overwritePositionWithEmpty(oldRow,oldCol);}
  301. }
  302.  
  303. /* Swap the position of the GamePiece at the current position with the
  304. * GamePiece at the new position. */
  305. private void swapPosition(int curRow, int curCol, int newRow, int newCol){
  306.  
  307. gameBoard[newRow][newCol]=gameBoard[curRow][curCol];
  308. overwritePositionWithEmpty(curRow,curCol);
  309. }
  310.  
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement