Pokemon Unite Friend Tracker

洛雨听花發表於2024-12-05

Data Structures: Assignment 2 Pokemon Unite Friend Tracker

AnDe 20241-IntroductionA MOBA recently got released in China called Pokemon Unite. As you enjoy playing various MOBA-style gamesyou decide to give it a try. While playing, you’ve decided you want to track your skills and progression in

comparison to your friends. To accomplish this, you decide to build a back-end system for adding, removingand maintaining them. The idea is to organise your friends so that you can search for individuals, search forplayers who play certain Pokemon, determine rankings based arrays and linked lists. Deciding that the mostimportant factor for ordering your friends is theiraccount level, you build a binary search tree (BST) structure, using the level (actually represented via a

method, see Section 4) as the key.In this assignment we will build a BST structure, with each node representing a friend. Each friend nodecontains information about that player, including their username and level, along with attached datastructures for the Pokemon they play (single linked-list) and ribbons(ArrayList). In accordance with the rules ofBSTs, each friend has a arent node and two child nodes, left and right. From any one node, all nodes to theleft are less (lower level) and all nodes to the right are greater (higher level). Due to this, searching for higheror lower-levelled players is, on average, a O(log n) process. This assignment consists of a number of parts.Part A - you will setup the basic class structure, ensuring that the test suite is able to run withouerror.

  • Part B - you will implement the basic structures needed by Account to hold multiple Ribbon andPokemon objects.
  • Part C - you will create your BST-based friend database.Part D - you will improve the efficiency of your tree by implementing AVL balancing.You are free to add your own methods and fields to any of the classes in the Database package, but do notchange any existing method prototypes or field definitions. A testing suite has been provided for you to testthe functionality of your classes. These tests will be used to mark your assignment, but with altered values.

This means that you cannot hard-code answers to pass the tests. It is suggested that you complete theassignment in 1 the order outlined in the following sections. Many ofthe later-stage classes relyon the correct implementation of their dependencies.Importing into eclipseThe Assignment has been provided as an eclipse project. You just need to import the project into an existingworkspace.Make sure that your Java JDK has been set, as well as the two jar files that you need for junit to function. Thiscan be found in:Project -> Properties -> Java -> Build Path -> Libraries.You might have to add the JAR files to the Classpath (see figure below). The jar files have been provided withinthe project; there is no need to download any other version and doing so may impact the testing environment.2 Part A

f you run the testing suite, you will be lovingly presented with many errors. Your first task is to complete theclass implementations that the tests expect (including instance variables and basic methods) to remove allerrors from the testing classes.

2.1 Pokemon

The Pokemon class represents one playable Pokemon and includes general information: the name, role(represented as an enum: speedster, all-rounder, attacker, defender, supporter) and number of obtainableribbons. In addition, it holds a reference to another Pokemon object. This will be importantin section 3 where you will make a single-linked list of Pokemon. The Pokemon class requires the followinginstance variables:

Total: 5

2.3 AccountThe Account class represents a user and, more generally, a tree node. Most importantly when using as a treenode, the class must have a key on which the tree can be ordered. In our case, it is a double named key. Thiskey is a simple function based on the combination of a user'susername and level. As levels are whole numbersand likely not unique, a simple method (see calculateKey 代寫Pokemon Unite Friend Tracker snippet below) is used to combine the two valuesinto one key whilst preserving the level. For example, imaginethat the hashcode for username “abc” is 1234and the user's level is 3. We do not want to simply add the hash to the level as that would not preserve thelevel and would lead to incorrect rankings. Instead, we calculate 1234=10000 to get 0:1234. This can then beadded to the level. As the usernames must be unique, our node keys are now also unique and the user level ispreserved.

Test Marks

Total: 5

3 Part BIn this section you will complete the PokemonList single linked-list for storing playable Pokemon objects.

3.1 PokemonList

The PokemonList class provides a set of methods used to find Pokemon objects that have been linked to forma single-linked list as shown in the image below. The head is a reference to the first Pokemon node, and eachPokemon stores a reference to the next Pokemon, or null if the Pokemon is at the end.The PokemonList class requires only one instance variable:public Pokemon headThere are a number of methods that you must complete to receive marks for this section. They can becompleted in any order. Your tasks for each method are outlined in the following sections.

3.1.1

void addPokemon(Pokemon pokemon)This method should add the provided pokemon to the end of your linked list. It should search for the first

available slot, and appropriately set the previous pokemon's next variable. All pokemon must be unique, soyou should check that the same pokemon has not already beenadded. Note that the tests require that the provided Pokemon object is added, not a copy. If the PokemonList headvariable is null, head should be updated to refer to the new pokemon. If the provided Pokemon object is null,an IllegalArgumentException should be thrown.3.1.2Pokemon getPokemon(String name)getPokemon should traverse the linked list to find a pokemon with a matching name. If the pokemon cannotbe found, the method should return null. If the name provided is null, the method should throw anllegalArgumentException.

3.1.3

void removePokemon(String name) | void removePokemon(Pokemon pokemon)There are two overloaded removePokemon methods with one taking as an argument a String, the other aPokemon. Both methods should search the linked list for the target pokemon and remove it from the list. Youshould appropriately set the previous node's next variable or set the head variable, if applicable. Both methodsshould throw an IllegalArgumentException if their argument is null.

3.1.4

Total: 15Figure 1 – initial BST structure

The BinaryTree class requires only one instance variable:public Account rootThere are a number of methods that you must complete to receive marks for this section. They can becompleted in any order. Your tasks for each method are outlined in the following sections. Remember that youcan add any other methods you require, but do not modify existing method signatures.

4.1.1 boolean beFriend(Account friend)The beFriend method takes as an argument a new Account to add to your database. Adhering to the rules ofBSTs, you should traverse the tree and find the correct position to add your new friend. You must also correctset the left, right and parent variables as applicable.Note that the tests require that you add the provided Account object, not a copy. If the Account key is alreadypresent inthe tree, this method should return false. If the Account argument is null, this method should throwan IllegalArgumentException. As an example, adding an Account with key 6 into the Figure above results in thetree shown in Figure 2 below.

Figure 2 – BST with an account added with key value of 64.1.2 boolean removeFriend(Account friend)

The removeFriend method takes as an argument an Account to remove from your database. This methodshould search the tree for the target friend and remove them. This should be achieved by removing allreferences to the Account and updating the left, right and parent values as applicable. removeFriend shouldreturn true if the friend is successfully removed, false if not found or some other error case. If the friend objectis null, an IllegalArgumentException should be thrown. As an example, removing the Account with key 4 fromFigure 1 above. Results are shown in Figure 3 below.

Figure 3 – initial BST with node removed containing key value of 4

4.1.3 int countBetterPlayers(Account reference)The countBetterPlayers method takes as an argument an Account from which you should search for playerswith higher rank. This method should search from the reference account and increment a counter of betterplayers to return. You should return the number of better players, 0 if there are none.Note that a greater key value does not necessarily equal a higher level. If the Account argument is null, thismethod should throw an IllegalArgumentException. As an example, using User with key 7 from Figure 1 above,this method should return 5.

4.1.4 int countWorsePlayers(Account reference)

The countWorsePlayers method takes as an argument an Account from which you should search for playerswith lower rank. This method should search from the reference account and increment a counter of worseplayers to return. You should return the number of worse players, 0 if there are none.Note that a lower key value does not necessarily equal a lower level. If the User argument is null, thismethod should throw an IllegalArgumentException.As an example, using User with key 7 from Figure 1, this

method should return 6.4.1.5 Account highestRibbonScore()The highestRibbonScore method should search the tree and return the player who has the highest ribbon

score. If there are no users with ribbons, this method should return null.Ribbon score is calculated by adding the ribbons together with the following points:GREEN = 1, BLUE = 2, GOLD = 3.Hint: You may want to add a method in the Account class for determining ribbon points of an account.4.1.6 void addPokemon(String username, Pokemon pokemon)The addPokemon method takes two arguments, a String username and Pokemon pokemon.You should search your database for a matching user and add the new pokemon to their PokemonList. Youshould also check that they do not already have that pokemon in their collection. If either argument is null, thismethod should throw an IllegalArgumentException.4.1.7 void addRibbon(String username, Ribbon ribbon)

The addRibbon method takes two arguments, a String username and Ribbon ribbon. You should search yourdatabase for a matching user and add the new ribbon to their ribbons. You should also check that they do notalready have the ribbon to be added and that they do not already have all available ribbons for the pokemon.If either argument is null, this method should throw an IllegalArgumentException.

4.1.8 void levelUp(String username)The levelUp method takes as an argument a String username that you should use to search for the matchinguser in the database. You should then increment that user's level by one. If this breaches any BST rules youshould make the necessary adjustments to the tree. As anexample, Figure 4 shows an invalid tree after a levelup and Figure 5 shows the correct alteration. If the username argument is null, this method should throw anIllegalArgumentException.

相關文章