Fill This Form To Receive Instant Help
Homework answers / question archive / I have this task that I need help with in Java and I also have some files that belong to this task
I have this task that I need help with in Java and I also have some files that belong to this task.
Rogue- “Things”
Read the description of Rogue 101. How would you abstract "things" from the description of the Rogue game?
? Write down 5 properties or methods that you think must be part of an interface that abstracts "things" on the game board and a short justification for each of them. Write the answer in Svar.md.
Do not proceed until you have written down your answer under problem 1.1 in Svar.md and done add-commit-push in git. You can go back and change the answer later. The score is based on the last answer uploaded.
1.2) IItem.java
Open the IItem interface. Compare the method declarations in the interface with the 5 properties you wrote down in 1.1 and with the description of the game from Rogue.
? Write a short text in Svar.md that describes how IItem abstracts at least 5 properties of game elements of the type "thing" in a Rogue game. Write the answer in Svar.md.
? - Even though you may have written down other properties than what is in IItem, it does not mean that any of the parts are wrong. For example, every thing has a position, but in our code it is the game map that keeps track of it, not the thing itself. With a different design, it could just as well be a feature in IItem.
Tip: If you want to change answers you have already pushed, you are free to do so. Only use a descriptive commit message, e.g. "Improved task 1.1 after group leader explained abstraction." It is the latest version before the deadline that counts.
1.3) Carrot.java
The Carrot class implements the IItem interface and represents a Carrot "thing" on the game map. An object of the type Carrot is thus in a way both an abstraction of a real carrot, and of a game object from Rogue. Open the Carrot class and see how it implements the methods from IItem.
? ItemTest: testHandleDamage
What properties of a real carrot are found in the abstract Carrot class, and what properties have a carrot that is not found in the Carrot class?
? List 3 characteristics from problem 1.2 that the Carrot class implements, and describe how it implements them. Write the answer in Svar.md.
? List 1 property of a real carrot that is represented in the Carrot class and 1 some is not. Write the answer in Svar.md.
The Carrot :: handleDamage () method is not properly implemented. We imagine that carrots are damaged when a Rabbit eats on it. Rabbit informs the carrot how much it eats by calling Carrot :: handleDamage () and Carrot's health decreases accordingly
(This notation refers to the handleDamage () method in the Carrot class.)
You can run this test alone by right-clicking on the method name → Run As → JUnit Test).
? Implement handleDamage (). Check if it works by running CarrotTest and IItemTest: testHandleDamage.
Tip: In Eclipse, you can see the documentation of a method (eg handleDamage (), which is documented in IItem) by letting the mouse pointer rest over the method name. When implementing handleDamage (), you will also find a small triangle in the margin that you can press to go to the interface.
1.4) Game objects
What other classes does IItem implement?
? List all the classes that implement this interface. Write the answer in Svar.md.
Tip: right-click on IItem and select Open Type Hierarchy to get a list of references to the IItem declaration.
(In IntelliJ it is called “Find usage”)
1.5) Gold.java
Now you are going to expand the support for game objects to also be able to represent gold.
? Create a class Gold.java that implements the IItem interface in the same folder as Carrot.java.
There are tests in IItemTest.java for gold that require the class Gold to have the symbol 'G'. (see how the other types IItem have implemented SYMBOL and do the same.)
The tests in IItemTest.java will not work yet, we will continue working with those tests in Task 3.
But make sure you use the correct symbol in the Gold class otherwise you will have problems in Problem 3.
To implement the methods, it may be useful for you to look at how the other classes of game objects from 1.4 implement them.
add-commit-push
Protip: default methods do not need to be implemented by sub-classes, unless you want other functionality than that offered by the default implementation.
Check that the file Gold.java is in your online repository in the same folder as Carrot.java BEFORE you move on. It will save you trouble and bugs later.
Tip: You can choose more or less random values ??for max health and defense - so far we have not thought about whether gold can be damaged or attacked. For getSize (), you should set it to be larger than other items so that it is visible on the map (it is the largest thing that is displayed / picked up).
Exercise 2 - The Rabbit
In Exercise 1, you became familiar with the IItem interface and what the different methods are used for. We will continue to look at the interfaces for the game objects, and in this assignment we will look at IActor and examples of an "actor" in our game.
Remember that you can always check out the documentation in the links at the top of this file if you find it difficult to understand how these interface pieces are connected.
2.1) IActor.java
Look at IActor.java in the rogue101.objects package.
Note that IActor extends another interface.
? Which interface does IActor expand, and what does this mean for classes that will implement IActor? Write a short and concrete answer in Svar.md.
(Difficult? We remind you once again of the documentation links at the top of this file).
2.2) doTurn ()
Look at Rabbit.java. How does Rabbit decide which way to go in doTurn ()?
? Write a short and concrete answer in Svar.md.
2.3) getPossibleMoves ()
A natural question an IActor can ask the map (via IGameView) is "What opportunities do I have to move?". The GameMap :: getPossibleMoves method responds to this by returning a list of directions that a role is allowed to go. See an example of using this in Rabbit :: performMove (). For now, getPossibleMoves only returns a list with the direction 'EAST'.
? Implement the GameMap :: getPossibleMoves method.
? GameMapTest: testGetPossibleMoves should pass when you finish this task.
? Is this similar to something you have done on a previous weekly assignment?
Tip: A GameMap :: canGo method already exists.
2.4) Find carrot
In doTurn (), Rabbit moves if it has not already used up the trip to eat something. As you can see, the move is not very smart - what if there is a carrot right next to the rabbit?
The doTurn () method takes an IGameView argument, which is a relatively large interface. Get to know IGameView.
How can an IActor use IGameView to retrieve information about its environment and perform actions that affect other elements of the game?
You do not have to write the answer in the answer file, but note that your understanding of IGameView will have a lot to say for the rest of the submission.
Tip: If you are in Rabbit then you can ctrl / cmd-click on IGameView to jump there.
? Make Rabbit (a little) smarter by seeing if there is a carrot in one of the free spaces next to it on the board, and go there if it does.
? RabbitTest should pass when you finish this task.
Task 3 - The object factory
In this exercise, you will look at how IItems are created and added to the board. You will expand the game to support the gold objects from Task 1 and change the code so that it follows an important object-oriented design principle.
3.1 ItemFactory.java
To create new objects of a class in Java, we must call the constructor of the class. If we know we need a carrot we call new Carrot (), and if we know we need a spider we call new Spider (). When the game elements are to be built, it is therefore important to get the right constructor.
To solve this without messing with our code with references to specific classes and symbols, we use a well-known Design Pattern called Factory Pattern. A Design Pattern is a standardized way of solving a problem that constantly arises when programming object-oriented - regardless of programming language. They mean that you do not have to reinvent the wheel, and also make it easier for others to understand what you have done as they have often seen the pattern before. (Feel free to check out the popular book Design Patterns)
Factory Pattern is about having a method in a "Factory" class that knows which constructor to choose depending on the argument it gets. In our case, we have an ItemFactory class that does this.
? Which symbol represents a Player object and which represents Dust? Write the answer in Svar.md and in which class and method you found the answer.
The factory is missing a choice to add Dust.
? IItemTest :: testItemFactoryCreatesDust ()
? IItemTest :: testItemFactoryCreatesGold ()
? Add factory support to create Dust objects.
? Add factory support to create Gold objects.
? When you have completed this task, all the tests in IItemTest must pass.`
3.2 S.O.L.I.D.
SOLID is an abbreviation for five principles that make object-oriented code understandable, flexible and easy to maintain. The first principle - the principle of Single Responsibility - states that:
"Each class should have a single responsibility and that responsibility should only lie with that class."
If we want to change the symbol to e.g. Rabbit from 'R' to 'r' throughout the program so we had to make changes in more than one class.
? Why did we have to change more than one class to change the symbol of Rabbit? Write the answer in Svar.md.
When we have to change several classes to make one change means that we have broken the first principle in SOLID. The reason this is not good is that if Rolf who does not know the code should make this change in 3 years, and only make the change in one of the classes (how could he have known that he had to change several places?) Then it would be a program error.
? Change the code in ItemFactory so that it does not contain information about which symbol belongs to which class. (Note that with contains, we mean that the symbol is hard-coded in the class).
? Why is the Single Responsibility issue now fixed? Write a short explanation in Svar.md.
3.3 Gold.java
In this task you will add support for game objects of the type Gold.
You must have done problem 1 and previous parts of problem 3 to be able to solve this problem.
Open IItemTest in the inf101.rogue101.objects package.
? Add a new object of type Gold in the IItemTest :: getTestData () method following the same pattern as for the other objects. Run the tests.
? Find the file level1.txt in resources / inf101 / rouge101 / map / maps, open it and replace some of the symbols with the gold symbol you selected in Task 1. Save the file. Run the program. The gold symbols should appear on the screen where you put them on the map.
Task 4 - A smarter map
4.1 getNeighbourhood
Another question that IActors can ask the map is what is in the area around it. For this we have a method GameMap :: getNeighbourhood which takes a location and an integer dist anse as an argument, and returns all the locations within dist steps from the location.
For example. if a role is on a loc, and
ask for the neighboring field with dist = 1, then the 8 fields around the loc should be returned.
ask about the neighboring field with dist = 2, then the 8 locations around the loc should be returned together with the 16 locations which are a step further out.
etc.
? Implement the GameMap :: getNeighbourhood method.
? GameMapTest :: testGetNeighbourhoodCardinality
? GameMapTest :: testGetNeighbourhoodEdgeCardinality
4.2 A better getNeighbourhood
When a role wonders which locations are nearby, it is not interested in locations with walls.
? Improve the GameMap :: getNeighbourhood method so that it does not return locations that are walls.
? GameMapTest :: testGetNeighbourhoodDoesNotReturnWall
4.3 Sort Neighborhood
Go to IGameMap :: getNeighbourhood and sort the list of locations before returning it. IList :: sort needs a Comparator , here you can use LocationComparator which takes one location and compares the distances to two other locations.
You can use: Collections.sort (neighborhood, new LocationComparator (loc));
? Improve the GameMap :: getNeighbourhood method so that it returns locations in sorted order.
? GameMapTest :: testGetNeighbourhoodSorted
4.4 getReachable
Sometimes a location can be close but difficult to reach because other IItem is in the way. You will find the locations that are possible to reach at dist number of steps.
This can be difficult and we do not expect everyone to get to this task.
You can do the rest of the tasks even if this task is not completed.
? Implement the GameMap: getReachable method so that it returns the locations that can be accessed in the dist number of steps.
Tip: To find the locations that can be reached in 1 step, you may be able to reuse something from
getPossibleMoves ()
Is it a good idea to have a help method List expand (List found) that adds all the locations you can reach with an extra step?
? GameMapTest :: testGetReachableDoesNotWalkPastWalls
Please find the answer using this link
https://drive.google.com/file/d/1ddgjxUrJQ7l77cPGCOTW-Jphrp-wnqnK/view?usp=sharing