Question: Create a Gameboard class that will hold and manage a 2D array of tiles that will represent the game state. To initialize the game board,
Create a Gameboard class that will hold and manage a 2D array of tiles that will represent the game state. To initialize the game board, data will be read in from a text file. The text file has the following format. The first row contains two integers that specify how many rows and columns are in the rectangular game board. The rest of the file gives the initial state of the game board. It has the same format as the output that we have been generating to this point: Y is the amulet, @ is the player, and so on. The Gameboard class, for this phase, only needs one instance variable of type Tile[][]. Write a constructor that accepts a file name as a parameter (a String). Open that file, read the contents, and initialize your two dimensional array of tiles based on the file. It can be assumed that the file will be in the same folder as your code. Write a toString() method that returns the game board as a multi-line String. The first line should give the current health of the player, and the remaining lines should show the board. See the examples below. You can test your class with TestPhase4.java. You should get the output shown below. Make sure the supplied test data files (phase4GameBoard1.txt and phase4GameBoard2.txt) are in the same folder as the rest of your files. Health: 100
#######
#^Y.t.#
#^h@.T#
#.h..h#
#######
Health: 100
#############
#^Y.t.......#
#^h..T..^...#
#.h..h.....@#
#############
Phase 5: The Game
Extend the Gameboard class to play a simple game with the game board we have created. Add:
. Static variables to define the four possible directions. You can set these to whatever values you like:
public static final int UP
public static final int DOWN
public static final int LEFT
public static final int RIGHT
Two boolean instance variables: one indicating whether or not the game is over, and one indicating whether or not the player has won the game. Add accessor methods getWon() and getDone() for these instance variables.
void doRound(int): This method contains all the logic to control one move in the game. The parameter specifies the direction of the move: up, down, left or right. This will be a fairly large method.
If the player tries to move into a wall, the player should not move.
If the player moves into an empty space, the player should move to that space.
If the player moves onto a space where there is an Item, call getEffect() on the item, and change the users hit points appropriately with changeHP(). If the item is the Amulet of Yendor (Y), the game is won. Set the boolean variables appropriately.
If the player attempts to move into a space occupied by a different combatant:
-The player attacks that combatant. Use doAttack() to generate how many hit points the attack removes from the combatant. If the other combatant is reduced to 0 or fewer hit points, that combatant is removed from the board, and the player moves into that square.
-If the combatant is not at 0 or fewer hit points, the player does not move, and the combatant attacks back, reducing the health of the player. If the player is reduced to 0 or fewer hit points, the player dies and the game is over.
-Print out to the console the number of hit points both combatants have after the attacks are complete.
-Print out to the console [enemy type] is vanquished! when a combatant is reduced to 0 or fewer hit points.
You can test your class with TestPhase5.java. Sample output is shown below, which shows fighting a combatant, and setting off a trap.
Welcome to NetHack, 1020 edition.
Health: 100
#############
#^Y.t.......#
#^h..T..^.^t#
#.h..h.....@#
############# Which way would you like to move?
Valid commands are up, down, left, right.
up
Player attacks the Troll for 8
Troll attacks the player for 3
Player health:97 Troll health 2
Health: 97
#############
#^Y.t.......#
#^h..T..^.^t#
#.h..h.....@#
#############
Which way would you like to move?
Valid commands are up, down, left, right.
up
Player attacks the Troll for 7
Troll is vanquished!
Health: 97
#############
#^Y.t.......#
#^h..T..^.^@#
#.h..h......#
############# Which way would you like to move?
Valid commands are up, down, left, right.
left
You set off a trap!
Health: 92
#############
#^Y.t.......#
#^h..T..^.@.#
#.h..h......#
#############
Which way would you like to move?
Valid commands are up, down, left, right.
left
Health: 92
#############
#^Y.t.......#
#^h..T..^@..#
#.h..h......#
############# Which way would you like to move?
Valid commands are up, down, left, right.
TestPhase4 is below
import java.io.IOException;
public class TestPhase4 {
public static void main(String[] args) throws IOException { Gameboard board = new Gameboard("phase4GameBoard1.txt"); System.out.println(board);
board = new Gameboard("phase4GameBoard2.txt"); System.out.println(board); }
}
TestPhase5 is below
import java.util.Scanner;
public class TestPhase5 {
public static void main(String[] args) { Gameboard board = new Gameboard("phase5GameBoard.txt"); System.out.println("Welcome to NetHack, 1020 edition.");
Scanner keyboard = new Scanner(System.in); String input; while (!board.getDone()) { System.out.println(board); System.out.println("Which way would you like to move?"); System.out.println("Valid commands are up, down, left, right."); input = keyboard.nextLine(); if (input.equals("up") || input.equals("u")) board.doRound(Gameboard.UP); if (input.equals("down") || input.equals("d")) board.doRound(Gameboard.DOWN); if (input.equals("left") || input.equals("l")) board.doRound(Gameboard.LEFT); if (input.equals("right") || input.equals("r")) board.doRound(Gameboard.RIGHT); } keyboard.close(); if (board.getWon()) System.out.println("You win!"); else System.out.println("You were vanquished."); } }
My Tile class is below
public abstract class Tile { public String symbol; public boolean passable; private Content tile; public Tile(String symbol, boolean passable){ this.symbol = symbol; this.passable = passable; } public Tile(String symbol, boolean passable, Content tile){ this.symbol = symbol; this.passable = passable; this.tile = tile; } public String getSymbol(){ if(tile != null){ return tile.getSymbol(); } else{ return symbol; } } public boolean isPassable(){ if(passable = true){ } return passable; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
