Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class TrollGame
- {
- //constants for this version of the game
- private static final int INIT_PLAYER_POINTS = 160;
- private static final int PLAYER_POINTS_DEC = -10;
- private static final int TREASURE_POINTS = 500;
- private static final int ROWS = 8;
- private static final int COLS = 10;
- // random number generator
- private Random rand;
- // the game board, a 2D array of GamePieces
- private GamePiece[][] gameBoard;
- // variables to keep track of the locations of player, troll
- private int curPlayerRow, curPlayerCol;
- private int curTrollRow, curTrollCol;
- // the player's status
- private boolean playerWins;
- private boolean playerLoses;
- GamePiece player=new Player();
- GamePiece treasure=new Treasure();
- /* Constructor that uses an unseeded instance of the random number generator.
- * Calls initBoard.
- */
- public TrollGame()
- {
- rand = new Random();
- initBoard(ROWS, COLS, rand);
- }
- /* Constructor that uses a seeded instance of the random number generator.
- * Calls initBoard.
- */
- public TrollGame(int seed)
- {
- rand = new Random(seed);
- initBoard(ROWS, COLS, rand);
- }
- public void movePlayer(String direction)
- {
- int vertical = 0;
- int horizontal = 0;
- switch (direction){
- case "u": {
- if (curPlayerRow != 0)
- curPlayerRow--;
- break;}
- case "d": {
- if (curPlayerRow != ROWS)
- vertical = 1;
- break;}
- case "l": {
- if (curPlayerCol != 0)
- horizontal = -1;
- break;}
- case "r": {
- if (curPlayerCol != COLS)
- horizontal = 1;
- break;}
- }
- int oldPlayerRow = curPlayerRow;
- int oldPlayerCol = curPlayerCol;
- curPlayerRow += vertical;
- curPlayerCol += horizontal;
- adjustPlayerLifeLevel(curPlayerRow, curPlayerCol);
- if(playerFoundTreasure(curPlayerRow, curTrollCol))
- {
- playerWins = true;
- overwritePosition(oldPlayerRow, oldPlayerCol, curPlayerRow, curPlayerCol);}
- else{
- swapPosition(oldPlayerRow, oldPlayerCol, curPlayerRow, curPlayerCol);}
- int[] newPosition = calcNewTrollCoordinates(curPlayerRow, curPlayerCol, curTrollRow, curTrollCol);
- swapPosition(curTrollRow, curPlayerCol, newPosition[0], newPosition[1]);
- curTrollRow = newPosition[0];
- curPlayerCol = newPosition[1];
- }
- /* Returns true if the player wins, false otherwise. */
- public boolean playerWins()
- {
- return playerWins;
- }
- /* Returns true if the player loses, false otherwise. */
- public boolean playerLoses()
- {
- return (curPlayerRow == curTrollRow && curPlayerCol == curTrollCol);
- }
- /* Returns the number of treasure points. */
- public int getTreasurePoints()
- {
- return TREASURE_POINTS;
- }
- /* Resets the game variables and game board (call initBoard).
- * Does NOT change the random number generator instance.
- */
- public void resetGame()
- {
- initBoard(ROWS, COLS, rand);
- }
- /* Returns a String version of the game. */
- public String getGameStr()
- {
- StringBuilder outStr = new StringBuilder();
- for (int i = 0; i < ROWS; i++)
- {
- for (int j = 0; j < COLS; j++)
- outStr.append("|").append(gameBoard[i][j].show());
- outStr.append("|").append(System.getProperty("line.separator"));
- }
- return outStr.toString();
- }
- private GamePiece[][] initBoard(int rows, int cols, Random rand)
- {
- curPlayerRow = 0;
- curPlayerCol = 0;
- curTrollRow = 0;
- curPlayerCol = 0;
- playerWins = false;
- playerLoses = false;
- gameBoard = new GamePiece[ROWS][COLS];
- curTrollRow = getRandTrollRow(rand, ROWS);
- curTrollCol = getRandTrollCol(rand, COLS);
- for (int i = 0; i < ROWS; i++){
- for (int j = 0; j < COLS; j++){
- gameBoard[i][j] = new EmptyPiece();}
- }
- gameBoard[0][0] = player;
- gameBoard[7][9] = treasure;
- gameBoard[curTrollRow][curTrollCol] = new Troll();
- player.updateLifePoints(INIT_PLAYER_POINTS);
- treasure.updateLifePoints(TREASURE_POINTS);
- return gameBoard;
- }
- /* Returns true if the player is alive, false otherwise.*/
- private boolean playerAlive(int curPlayerRow, int curPlayerCol)
- {
- return gameBoard[curPlayerRow][curPlayerCol].isAlive();
- }
- /* Adjusts the player's life level by the amount PLAYER_POINTS_DEC. */
- private void adjustPlayerLifeLevel(int curPlayerRow, int curPlayerCol)
- {
- player.updateLifePoints(PLAYER_POINTS_DEC);
- }
- /* Returns true if the player row and column passed in equals
- the treasure row and column. */
- private boolean playerFoundTreasure(int playerRow, int playerCol)
- {
- return (playerRow == ROWS-1 && playerCol == COLS-1);
- }
- /* Returns a random number in [1,numRows-1] */
- private int getRandTrollRow(Random rand, int numRows)
- {
- return rand.nextInt(ROWS-1)+1;
- }
- /* Returns a random number in [1,numCols-1] */
- private int getRandTrollCol(Random rand, int numCols)
- {
- return rand.nextInt(COLS-1)+1;
- }
- private int[] calcNewTrollCoordinates(int playerRow, int playerCol, int trollRow, int trollCol)
- {
- int[] newPos = new int[2];
- if (playerRow == trollRow && playerCol == trollCol){
- newPos[0]=trollRow;
- newPos[1]=trollCol;}
- else if (Math.abs(playerRow - trollRow) < Math.abs(playerCol - trollCol)){
- if (playerCol > trollCol){
- newPos[0]=trollRow;
- newPos[1]=trollCol + 1;}
- else {
- newPos[0]=trollRow;
- newPos[1]=trollCol - 1;}
- }
- else {
- if (playerRow > trollRow){
- newPos[0]=trollRow + 1;
- newPos[1]=trollCol;}
- else {
- newPos[0]=trollRow - 1;
- newPos[1]=trollCol;}
- }
- return newPos;
- }
- // The following three methods may be helpful when adjusting the GamePieces in the movePlayer method. */
- /* Overwrite the GamePiece at the coordinates passed in with an empty GamePiece. */
- private void overwritePositionWithEmpty(int row, int col)
- {
- gameBoard[row][col] = new EmptyPiece();
- }
- /* Overwrite the GamePiece at the new coordinates with the GamePiece at the
- old coordinates. Place a new EmptyPiece at the old coordinates. */
- private void overwritePosition(int oldRow, int oldCol, int newRow, int newCol)
- {
- gameBoard[newRow][newCol] = gameBoard[oldRow][oldCol];
- gameBoard[oldRow][oldCol] = new EmptyPiece();
- }
- /* Swap the position of the GamePiece at the current position with the
- * GamePiece at the new position. */
- private void swapPosition(int curRow, int curCol, int newRow, int newCol)
- {
- GamePiece temp = gameBoard[curRow][curCol];
- gameBoard[curRow][curCol] = gameBoard[newRow][newCol];
- gameBoard[newRow][newCol] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement