Develop code that makes essential use of a two-d array


Project Assignment: Troll Game

Overview

In this project you will complete the implementation of the Troll Game. The game is played on a 2-dimensional board with 8 rows and 10 columns. Each cell in the board contains a GamePiece object. There are four types (subclasses) of GamePieces: Player- the user, Troll- the villain, Treasure- the goal, and EmptyPiece- represents cells that are not one of the previous types.

At the start of each game, the player occupies the upper left-hand corner- position 0,0. The treasure is located at the lower right-hand corner- position 7, 9. The treasure does not change its position. The troll starts at a random position in the board that is not the player's starting position or the treasure's position. The player's goal is to reach the treasure without encountering the troll. The troll's goal is to intercept the player before the player reaches the treasure.

The game is played in a series of rounds where the player moves first and then the troll moves. The player's move is input by the user. The player makes a move one step in one of four directions: up, down, left, right. If the player is on a border and the requested move would take the player off the board, the move is ignored- i.e. the player does not move. The troll knows the player's position and after the player moves, the troll will move towards the player. If the troll's next move reaches the player, the troll wins, unless the player has reached the treasure. If the player reaches the treasure and the troll also reaches the treasure in the same round, the player wins. The treasure has life points which are awarded to the user if the player wins.

Each move the player makes comes with a cost. The player starts with 160 life points. Each move the player makes costs 10 life points. This deduction occurs for all player moves- even if the player attempts to move off the board (assume this would be a mistake). If a player's life level drops to 0 or less, the player is not alive and cannot move.

Learning Goals

1. Develop code that makes essential use of a 2D array.
2. Create derived classes from an abstract superclass.
3. Use seeded random number generation.
4. Gain experience dealing with program state.
5. Gain experience implementing "skeleton" code.
6. Develop methods that use private methods as "helper" methods.
7. Be able to write a class definition that works according to a specification.
8. Gain experience developing code in an incremental fashion.
9. Gain experience using a debugger.
10. Gain experience with testing code by using JUnit tests.

The Project - What you need to do

Your work is focused on two areas of the code: creating the GamePiece subclasses and implementing the methods in the TrollGame class.The specification for implementing the code is given in the comments above each method in the TrollGame class. You must write code wherever you see the comment // TODO: implement this method

1. Complete the GamePiece subclasses. Do this first.

Create the Player, Troll, Treasure classes as subclasses of GamePiece. Since GamePiece is an abstract class, the subclasses must provide implementations for the abstract methods in GamePiece. This allows the maintenance of the life points do be done in the superclass, while the subclasses implement the abstract methods in their own, special way.

Player: type: "Player", show: "P". Troll: type: "Troll", show: "T".
Treasure: type: "Treasure", show: "$".
EmptyPiece: type: "Empty", show: " ". (provided as an example).

2. Complete the TrollGame class.

Member variables declared: There are several static final member variables that serve as constants. Additionally, there are four integer member variables, two boolean member variables, a Random class variable, and a 2D array, the gameBoard. The member variables hold the state of the game at any moment in time.

Step 1: Implement the constructors

Implement both constructors. They both initialize the integer variables to 0, and the boolean variables to false.

1) One constructor initializes an unseeded instance of Random.

2) The other constructor initializes a seeded version of Random using the seed parameter passed in.

3) In both constructors, the gameBoard is initialized by a call to initBoard, which you implement next.

Step 2: Implement the initBoard method.

This method initializes the gameBoard.

1) Create a 2D array of type GamePiece using the rows and cols parameters.

2) Generate a random row and column for placement of the Troll. These coordinates are asigned to the member variables curTrollRow and curTrollCol, respectively. The coordinate values are generated by calling the getRandTrollRow and getRandTrollCol methods (see Step 4 for how to complete these methods).

3) After determining the Troll location, loop through the gameBoard

a) initializing position 0,0 to a Player instance, passing in the correct

INIT_PLAYER_POINTS.

b) Initialize position 7,9 to be an instance of Treasure, again, passing in the TREASURE_POINTS to its constructor. Make sure you initialize the troll's location with an instance of the Troll class. All other positions should be initialized to instances of the EmptyPiece class.

4) Finally, the array is returned. The calling method will assign it to the member variable gameBoard.

Step 3: Complete the getRandTrollRow and getRandTrollCol methods

These methods return valid index numbers generated at random that are not 0,0 or 7,9 as these spaces are taken by the Player and Treasure, respectively.

Valid indexes in a 2D array are row values in [0, rows-1] and column values in [0, cols-1].
Again, the Player and Treasure row and column values are not returned by these methods.

Step 4: Implement the movePlayer helper methods.

The movePlayer method makes use of these helper methods:

playerAlive adjustPlayerLifeLevel calcNewTrollCoordinates playerFoundTreasure overwritePositionWithEmpty overwritePosition swapPosition

Implement the first four methods so that you can call them from the movePlayer method. The movePlayer method does most of the management of the game. It makes use of many private helper methods you will implement. This makes its code simpler and easier to debug. It takes as a parameter the user's input String- a single letter, "u", "d", "l", "r", for up, down, left, right- indicating the direction of the user's move. The user moves the Player object on the gameBoard.

1) First, the Player has to be alive for a move to occur. A player starts with 160 life points- the value of the constant INIT_PLAYER_POINTS. Remember that each time a player moves, PLAYER_POINTS_DEC are deducted from the player's life points. A player is not alive if it has zero or fewer life points. A dead player cannot move. A call to the playerAlive helper method will determine if the player is alive.

2) If the player is alive, the next step is to see that the proposed move is possible. For example, if a player object is in the cell 7,0, the move "d" would take the player off the board. If this is the case, the player object is not moved and stays at its current location. A deduction of PLAYER_POINTS_DEC are made if the move is or is not made. This is done by calling the helper method adjustPlayerLifeLevel, passing in the current location of the player.

It can be a good idea to keep the current player position separate from the "proposed" position. That way, the current and new positions are available if you need them. You can assign the new position to the current position at the end of the method.

3) Next calculate the Troll's move by checking if the Troll got the Player or if the Player reached the Treasure. To finish this part, see Step 5 to implement the calcNewTrollCoordinates method.

Remember that there can be a case where the player and troll positions are the same. A player may no longer be alive and can't move, or it may be on a border and tried to move off the board. It's also possible the player might move onto the troll! In these cases, the troll should not move.

Once the player and troll moves are known, it's time to see the result. The player loses if the troll is on the same cell. The playerLooses variable should be set to reflect this state. The positions of the objects also have to be adjusted so the game will display correctly to the user.

Step 5: Implement the calcNewTrollCoordinates method.

This method returns an int[] array where the first value is the row and the second value is the column of the Troll's next move.

How does the Troll move?

? It knows its current position and the player's new position (passed in as parameters to the calcNewTrollCoordinates method). It calculates the one step move that will bring it closer to the player.

? For example, if the player was located at 0,0 and the troll at 6,2, the row distance is 6 while the column distance is 2. The troll will choose to make a row move to decrease the larger distance.

? The troll knows which direction based on the sign of the difference. For example, if you subtract 0 from 6 you get a negative number. That would mean move up. A positive number would mean move down. Of course this depends on the order you do the subtraction. If the row and column distances are the same, either row or column moves will work.

Step 6: There are three helper method for moving objects around:

1. The overwritePositionWithEmpty method assigns a new EmptyPiece object to the row and column values passed in in the gameBoard. This overwrites whatever object was occupying that location in the gameboard.

2. The overwritePosition method overwrites the GamePiece at the new coordinates with the GamePiece at the old coordinates. Places a new EmptyPiece at the old coordinates.

3. The swapPosition method swaps the position of the GamePiece at the current position with the GamePiece at the new position.

These methods allow the player and troll to move around the board. There can be only one player and troll on the board at any time.

If the player loses, the player must be overwritten by the troll. If the troll moves, it's new location must contain the troll object and its previous location must contain an EmptyPiece.

If the player reaches the treasure position, determined by a call to the private helper method playerFoundTreasure without encountering the troll, the player wins. The playerWins variable must be set. The player must appear in the treasure position and the troll position must also be updated. Use the overwritePosition and swapPosition helper methods to do this.

Finally, if the player didn't lose or win, the game goes on. Make sure the new player and troll positions on the board are updated. Again, use the overwritePosition and swapPosition helper methods methods to do this. Also be sure the member variables that track the current player and troll positions are updated with the new moves.

Step 7: Implement the remaining methods.

1. For the playerWins, playerLoses, getTreasuePoints methods, return the variable (that holds the current state or value).

Step 8: Implement the resetGame method.

This method resets the member variables in preparation for a new game. It should use the same Random instance- do NOT make a new instance. It should call initBoard as well.

Testing and Development Notes

Implement them to support the movePlayer method. First get the player to move correctly before working on the rest of the method. Make sure when the player wins it reaches the treasure. Then add the troll moves.

Do not try to write all of the code at once. Write small amounts, compile, then test.

Use the TrollGameMain main method to test your code in addition to the JUnit tests. It is useful to see that your player is moving correctly by running the code as a user. The debugger is very useful to inspect the logic of your code and to see the values in the gameBoard and other variables.

Use the jGrasp debugger to develop method code. This is especially useful for methods with a loop and conditional statements. The debugger is the best way to develop method code. Unit tests do not allow you to troubleshoot your code inside a method. When you think your method works, run the JUnit tests we provide in jGrasp. We suggest you also run the code from the main class and interact with the program as a user.

Once your code passes all of the jGrasp tests we have provided, upload to Gradescope. If your code fails any tests in Gradescope, look at the test description to see what was being tested.

You can add a test to the JUnit test class. This allows you to set up specific input values and make method calls you need to debug.

The JUnit tests we provide are a subset of the tests that we will use to test your code in Gradescope. If your code passes all of the test we provide, it is likely, but not guaranteed that you will pass all of the tests in Gradescope.

You must read the assignment specification carefully -- if your submission is not passing a test, it is almost certainly because your submission doesn't match the requirements of the assignment.

Note that you may pass all the tests on your machine and still receive a 0 when you submit to Gradescope. This is likely to happen when you change the project or method names, or fail to correctly create the zip file.

Format your assignment according to the following formatting requirements:

1. The answer should be typed, double spaced, using Times New Roman font (size 12), with one-inch margins on all sides.

2. The response also include a cover page containing the title of the assignment, the student's name, the course title, and the date. The cover page is not included in the required page length.

3. Also Include a reference page. The Citations and references should follow APA format. The reference page is not included in the required page length.

Attachment:- Project-Troll-Game.rar

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Develop code that makes essential use of a two-d array
Reference No:- TGS02957778

Expected delivery within 24 Hours