COMP3021 Java ProgrammingSpring 2019 Programming Assignment #1(Civilization – ASCII mode)(Deadline: 11:55 PM, 16 April 2019)Note: This is an individual assignment; all the work must be your own. Download the project skeleton athttps://course.cse.ust.hk/comp3021/assignments/assignment1/PA1.zip and finish the implementationsusing the skeleton.Zip your complete project directory to PA1.zip and submit it to CASS throughhttps://course.cse.ust.hk/cass/student/. Refer to http://cssystem.cse.ust.hk/UGuides/cass/index.html forthe details of uploading assignment using CASS. You can submit the assignment for as many times as you wish, we will only mark you latest submission. This PA accounts for 10% of the total course grade. Cheating/plagiarism will be caught and punished HEAVILY!University’s Honor code: http://tl.ust.hk/integrity/student-1.htmlUniversity’s Penalties for Cheating: http://tl.ust.hk/integrity/student-5.html2BackgroundCivilization is a turn based strategy game where the objective is to conquer other playerscities by developing our own resources and troops (reference). In this assignment, youwill complete a partial implementation of a simplified, ASCII-version civilization game.This game involves two human players (according to the configuration in theplayers.txt file that comes with the skeleton). In each turn, players are able to performtasks on cities through their ministers. Each human player has a set of cities under his/hergovernance. The human player will have a list of ministers to carry out the various taskson the cities. Some of the possible actions are: collect tax, collect science and productionpoints, build improvements (banks, roads, universities) and recruit troops, see theselectAndPerformAction() method in the GameEngine.java file for the list ofpossible actions. Each minister has different intelligence, experience and leadershipvalues, these values are stored in the instance variables of the minister object. Thesevalues affect how much gold, science and production collected. A minister of the playercan do one single task in each turn. If the player has 3 ministers, the player will be ableto perform three tasks per turn.Gold, science points, and production points are the main resources of the game. Theplayer can spend these resources to perform various tasks in the game, such as buildingbanks, roads, universities, upgrading technologies, and recruiting troops. Using therecruited troops, players can attack adjacent cities to conquer them. The game will endwhen one player has conquered all cities on the map.Please play with the supplied executable file on a Windows platform to see how the gamecould be played.Getting StartedImporting the skeleton code1. Download the skeleton code from the course website and unzip the file2. In IntelliJ, go to File > New > Project From Existing Sources...3. Choose the root directory of the skeleton code4. Click Next, make sure the project format is .idea5. When prompted to choose JDK version, choose JDK 106. Click finish3The List and Map interfacesWe use the List and Map interfaces extensively to store references to game objects. TheList interface defines the set of methods you can do on a List (ArrayList, LinkedList, etc).Whereas the Map interface defines the methods of a Map (key, value data structure suchas HashMap).You can refer to the following documentations on the methods of these interfaces:● List: https://docs.oracle.com/javase/10/docs/api/java/util/List.html● Map: https://docs.oracle.com/javase/10/docs/api/java/util/Map.htmlYou will need the following methods for this assignment:● List: size(), add(), get()● Map: put(), get()Since List and Map are interfaces, they can’t be instantiated. Instead, you can assign aclass that implements the interface. For example:List list = new ArrayList();Map map = new HashMap();The T, K, V letters above are generic datatypes. See the following examples for the detailsof replacing these generic datatypes with a real existing datatype.If you wish the ArrayList to store a particular type of elements, you will need to create anArrayList and indicate the type of elements being stored. For example, the followingstatement:List players=new ArrayList();creates an empty ArrayList, players, which contains player reference as the elements.To add new player to the ArrayList, we can do:// Create a player object with name being “Alex”, and the// initial gold being 100 unitsPlayer player = new Player(“Alex”,100,100,100);// add the “player” to the “players” ArrayListPlayers.add(player);4A Map on the other hand acts as a “dictionary”. For each element in the Map, there aretwo fields, one is the Key (K), the other field is the Value (V). The lines below create aMap and each element of the Map consists of a String and a Double type data. Mind thatthe K, and V are generic datatypes, so they can be any non-primitive data types (butcannot be primitive).Map fruitPrices=new HashMap();After creating the Map, we add the prices of fruits to the Map fruitPrices:fruitPrices.put(Apple,3.5);fruitPrices.put(Pineapple,35.0);The fruit name Strings (“Apple” and “Pineapple”) will become the Keys of the two entries,and if you wish to retrieve the Value field of “Pineapple”, you simply do this:Double price=fruitPrices.get(“Pineapple”);Then the variable, price, will hold the price of the “Pineapple”, which is 35.0. Asyou can see, Map provides you an extremely convenient way of retrieving data using amore meaningful index (a String representing the name of a fruit here, instead of themeaningless index numbers 0,1,2,…n-1 in the case of an array).To Use ArrayList and Map, you will need to have the following line in your Java file:import java.util.*;5UML Diagram NotationThe UML diagrams used in this document is generated by IntelliJ (Ctrl + Alt + Shift + U toenter UML diagram edition mode). The notations used by IntelliJ is slightly different thanthe standard notations we discussed during our lectures. We will illustrate the differenceusing the Sample.java class UML diagram below:The character “c” in the blue circle on the top row indicates that this is a class, thecharacter “f” in the yellow circle indicates this is a field (i.e. variable), and the character“m” in the red circle indicates this is a “method”. The hollow dot at the left of the variable“a” indicates that “a” has the “default” access right, the locked red lock at the left of thevariable “b” indicates that “b” has the “protected” “private” access right, the gray key atthe left of the variable “c” indicates that “c” has the “private” “protected” access right. Theopened lock at the left of the variable “d” indicates that “d” has the “public” access right.For a method or a constructor, the input type is shown in the parentheses and the outputtype is shown at the right of the method name.6TasksYour tasks are to complete all the TODO items in the skeleton code. A detaileddescription of what you need to do is given in the Javadoc comments above eachTODO. In IntelliJ, you can go to View > Tool Windows > TODO to jump to eachTODO in the project.More information about the TODO tasks are given below.The TODOs in the GameEngine class (GameEngine.java)You need to complete the following methods:public boolean isGameOver() {/*** Determine if the game is over by checking if there is only one player with atleast one city** @return true if game is over, false otherwise*/}public Player getWinner() {/*** Get the the only player with at least one city** @return*/}7The TODOs in the GameMap class (GameMap.java)You need to complete the following methods:public void loadMap(String filename) throws IOException {/*** Load the map from a text file* The outline of the map format is given in the assignment description** You should instantiate cityLocations and connectedCities here** @param filename* @throws IOException*/}This functions reads the content of map.txt and stores the map information inside thesetwo data structures Map cityLocations and Map>connectedCities.cityLocation is an associative array where the key is a unique ID of a city and the valueis the location.connectedCities is an associative array where the key is a unique ID of a city and thevalue is a list of neighboring city IDs.8The map text file is structured as follows:48 // comment: this is the width of the text-mode game board12 // comment: this is the height of the text-mode game board2 // comment: this is the number of cities in the game0 16,4 // comment: this is the cityID,(x,y)-coordinates of the city1 32,4// comment: this is another city, cityID==10 1,2 // comment: this is the city with cityId==0, its neighboring cityIds1 0,3 // comment: this is the city with cityId==1, and neighboring cityIdspublic void loadPlayers(String filename) throws IOException {/*** Loads player data from text file* The outline of the player file format is given in the assignment description* You should instantiate the member variable players here** @param filename* @throws IOException*/}This method reads the content of players.txt and store player information in players list.The structure of the players text file is as follows:2 // number of playersBlue 100 100 100 2 3 // playerName, gold, science, production, cities, ministers1 SF 300 50 500 // cityId,name,population,troops,crop yields3 LA 500 100 600 // cityId,name,population,troops,crop yieldsEconomist 80 100 60 // ministerType,intelligence,experience,leadershipWarGeneral 100 80 60 // ministerType,intelligence,experience,leadershipWarGeneral 60 80 100// ministerType,intelligence,experience,leadership// note the blank line between each playerRed 100 100 100 2 2 // Player 2, the “Red” player and its information90 BJ 200 0 1002 HK 500 0 800Economist 80 80 80Scientist 100 100 100public List getAllCities() {/*** @return list of all cities from every player*/}public Cell getCityLocation(City city) {/*** @param city* @return Cell object representing the citys location on the game map*/�The TODOs in the Player Class(Player.java)The player class contains the cities, ministers, technologies of the player as well as gold,production and science points. Please complete the following methods:public Player(String name, int gold, int science, int production) { technologies.add(new WarTech()); technologies.add(new TradingTech()); technologies.add(new ManufacturingTech());/*** Initializes member variables.** @param name* @param gold* @param science* @param production*/ // TODO: complete initialization of member variables using the parameters}public boolean hasCity(City city) {/*** @param city* @return true if the given city belongs to this player*/}public boolean hasAnyCity() {/*** Hint: use a method in the List class** @return true if this player has at least one city*/}11public boolean hasReadyMinister() {/*** @return true if the player has at least one ready minister*/}The TODOs in the City Class (City.java)In addition to completing unfinished methods, you need to declare themember variables according to the UML diagram. The lock symbol indicatesthat the variable is private and the pin symbol indicates final variables.public void addBank() {/*** Adds number of banks by one*/}public void addRoad() {/*** Adds number of roads by one*/}public void addUniversity() {/*** Adds number of universities by one*/}12public void addTroops(int increment) {/*** Adds number of troops by specified increment.* If the increment is negative, invoke decreaseTroops() within this methodinstead.** @param increment*/}public void decreaseTroops(int decrement) {/*** Decreases number of troops by the amount specified* Caps the number of troops at 0** @param decrement*/}public void improveCrops(int addition) {/*** Add to crop yields the amount specified** @param addition*/}public int getExcessCrops() {/*** Calculates excess crops.** @return crop yields - population - 2 * troops*/}13@Overridepublic boolean equals(Object o) {/*** Checks whether two cities are equal* Two cities are equal when they have the same id* Return false if Object o is not an instance of City** @param o object to be compared* @return result of equality check*/}public void growAtTurnEnd() {/*** Increases population by increment = round(excess crops * 0.5)* If the increment turns out to be negative, leave population unchanged* Print a line that says:* Turn end: [city name]s population has grown by [increment]* e.g. Turn end: HKs population has grown by 50*/}�public void invokeRandomEvent(double rand) {/*** Invoke a random event at the end of turn.* There are two types of events, a disaster and baby boom.* A disaster happens when rand * A baby boom happens when rand > 0.4 AND rand population by 1.5.* Otherwise the population is left unchanged* Print a message in the following format when each event happens* Random event: A disaster in [city name] has happened, population wasreduced significantly* Random event: A baby boom in [city name] has happened, population wasincreased significantly** @param rand random number between 0 and 1 supplied by the function caller*/}public Cost getBankCost() {/*** Cost of building a bank* gold = production points = (# of banks + 1) * 400** @return Cost object encapsulating the gold and production costs*/}public Cost getRoadCost() {/*** Cost of building a road* gold = production points = (# of roads + 1) * 100** @return Cost object encapsulating the gold and production costs*/}15public Cost getUniversityCost() {/*** Cost of building a university* gold = production points = (# of universities + 1) * 1500** @return Cost object encapsulating the gold and production costs*/}The TODOs in the pa1.ministers package (in the ministersfolder)You need to complete the implementation of the Minister abstract class (Minister.java)and its subclasses. The Minister abstract class defines the general behavior of a ministerwhen performing tasks. The subclasses (WarGeneral, Scientist, Economist) introducesome task-specific perks and bonuses which are implemented as method overriding.Since actions in the game are done through ministers, these classes will contain most ofyourCOMP3021作业代做、ASCII mode作业代写、代做Java课程设计作业、Java程序语言作业调试 代写R语言程 work.17The TODOs in the Minister Abstract Class (Minister.java)public boolean isReady() {/*** @return Whether or not the minister is ready*/}public void beginTurn() {/*** Changes isReady to true*/}public void endTurn() {/*** Changes isReady to false*/}public int collectTax(City city) {/*** Collect gold from a city* amount collected = (city population + minister experience + minister leadership)* (# of banks + 1) * 0.2** @param city to collect gold from* @return amount of gold collected*/}18public int collectSciencePoints(City city) {/*** Collect science points from a city* amount collected = (city population + minister experience + ministerintelligence) * (# of universities + 1) * 0.2** @param city to collect science points from* @return amount of science points collected*/}public int collectProductionPoints(City city) {/*** Collect production points from a city* amount collected = (city population + minister intelligence + minister leadership)* (# of roads + 1) * 0.2** @param city to collect production points from* @return amount of production points collected*/}public void buildBank(Player player, City city) throws TooPoorException {/*** Build a bank in the city* 1. Get the cost of building a bank, with applied minister discount* 2. Check whether player has enough gold and production points* 3. If not, throw an exception* 4. Subtract the cost from the players gold and production point* 5. Add number of bank in the city by one* HINT:* - the Cost class has a getDiscountedCost() method* - the Minister class has a getImprovementDiscountRate() method, to obtain the* discount rate of building a bank** @param player19* @param city* @throws TooPoorException*/}public void buildRoad(Player player, City city) throws TooPoorException {/*** Build a road in the city* 1. Get the cost of building a road, with applied minister discount* 2. Check whether player has enough gold and production points* 3. If not, throw an exception* 4. Subtract the cost from the players gold and production point* 5. Add number of road in the city by one* * HINT:* - the Cost class has a getDiscountedCost() method* - the Minister class has a getImprovementDiscountRate() method, to obtain the* discount rate of building a road** @param player* @param city* @throws TooPoorException*/}public void buildUniversity(Player player, City city) throws TooPoorException {/*** Build a university in the city* 1. Get the cost of building a university, with applied minister discount* 2. Check whether player has enough gold and production points* 3. If not, throw an exception* 4. Subtract the cost from the players gold and production point* 5. Add number of university in the city by one* * HINT:* - the Cost class has a getDiscountedCost() method* - the Minister class has a getImprovementDiscountRate() method, to obtain the* discount rate of building a university** @param player* @param city* @throws TooPoorException*/}public void attackCity(City attacker, City defender, int attackingTroops,List technologyList) {/*** Attack a target city* Attacking city loses troops equal to min(# of troops attacking, # of troops in thedefending city)* Defending city loses round(0.8 * # of attacking troops * product of tech attackbonuses)* * This method is overridden in the WarGeneral class** Print the following messages:* [attacker city name] loses [number of troops lost by attacker] troops whileattacking* [defender city name] loses [number of troops lost by defender] troops whiledefending21** @param attacker attacking city* @param defender defending city* @param attackingTroops number of troops deployed for the attack* @param technologyList technologies owned by the attacking player*/}public void improveCropYield(Player player, City city) throwsTooPoorException {/*** Improve the crop yields in a city* Improve Crop yields by 50 for 500 golds* If the player does not have enough gold, throw an exception* Else decrease 500 golds from the player and improves crops by 50* * This method is overridden in the Economist class** @param player* @param city* @throws TooPoorException*/}public void recruitTroops(Player player, City city) throws TooPoorException {/*** Recruit troops to be stationed in a city* Recruit 50 troops for 500 golds* If the player does not have enough gold, throw an exception* * Overridden in WarGeneral class** @param player* @param city* @throws TooPoorException*/}22public void upgradeTech(Player player, Technology technology) throwsTooPoorException {/*** Upgrades tech* 1. Get the cost of upgrading tech, with applied minister discount* 2. Check whether player has enough gold, production, and science points* 3. If not, throw an exception* 4. Subtract the costs from the players balance* 5. Add level of technology by one* * HINT:* - the Cost class has a getDiscounterCost() method* - the Minister class has a getTechDiscountRate() method** @param player* @param technology* @throws TooPoorException*/}public void spyOnNeighbors(City city, GameMap map) {/*** This method is called when you want to spy on your neighbors.* * Print the information of a Citys neighbors* * HINT:* - GameMap class has a getNeighboringCities() method* - City class overrides the toString() method which returns the* String representation of a city** @param city* @param map*/}23The TODOs in the Economist class (Economist.java)public Economist(int intelligence, int experience, int leadership) {/*** Call the superclass constructor* @param intelligence* @param experience* @param leadership*/}@Overridepublic double getImprovementDiscountRate() {/*** @return improvement discount rate equals to 1 - (intelligence + experience) /1500*/}@Overridepublic int collectTax(City city) {/*** @param city to collect gold from* @return 1.5 times the amount collected by other types of minister*/}24@Overridepublic void improveCropYield(Player player, City city) throwsTooPoorException {/*** Economists get a bonus when doing crops improvements* Crop improvement still costs 500 gold* Crop is improved by 50 + round((intelligence + experience + leadership) * 0.2)* If the player does not have enough gold, throw an exception* Else decrease 500 golds from the player and improves crops by the calculatedamount** @param player* @param city* @throws TooPoorException*/}@Overridepublic String toString() {/*** Example string representation:* Economist | intelligence: 100 | experience: 100 | leadership: 100 | READY -when isReady() == true* Economist | intelligence: 100 | experience: 100 | leadership: 100 | DONE -when isReady() == false** @return string representation of this object*/}25The TODOs in the Scientist class (Scientist.java)public Scientist(int intelligence, int experience, int leadership) {/*** Calls the superclass constructor** @param intelligence* @param experience* @param leadership*/}@Overridepublic double getTechDiscountRate() {/*** @return tech discount rate equals to 1 - (intelligence + experience) / 1500*/}@Overridepublic int collectSciencePoints(City city) {/*** @param city to collect science points from* @return 1.5 times the amount collected by other types of minister*/}26@Overridepublic String toString() {/*** Example string representation:* Scientist | intelligence: 100 | experience: 100 | leadership: 100 | READY -when isReady() == true* Scientist | intelligence: 100 | experience: 100 | leadership: 100 | DONE - whenisReady() == false** @return string representation of this object*/}The TODOs in the WarGeneral class (WarGeneral.java)public WarGeneral(int intelligence, int experience, int leadership) {/*** Calls the superclass constructor** @param intelligence* @param experience* @param leadership*/}@Overridepublic int collectProductionPoints(City city) {/*** @param city to collect production points from* @return 1.5 times the amount collected by other types of minister*/}27@Overridepublic void recruitTroops(Player player, City city) throws TooPoorException {/*** Recruits more troops than superclass implementation,* troops added to city = 50 + round(leadership * 0.2).* Recruitment still costs 500 golds. Throw an exception* when player does not have enough gold.** @param player* @param city* @throws TooPoorException*/}@Overridepublic void attackCity(City attacker, City defender, int troops, ListtechnologyList) {/*** WarGeneral gets attack bonus when attacking a city.* bonus_multiplier = 1 + (intelligence + experience + leadership) / 1500* * Attacking city loses troops equal to min(# of troops attacking, # of troops in thedefending city)* Defending city loses round(bonus_multiplier * # of attacking troops * product oftech attack bonuses)* * [attacker city name] loses [number of troops lost by attacker] troops whileattacking* [defender city name] loses [number of troops lost by defender] troops whiledefending** @param attacker attacking city* @param defender defending city* @param troops number of troops deployed for the attack* @param technologyList*/}28@Overridepublic String toString() {/*** Example string representation:* WarGeneral | intelligence: 100 | experience: 100 | leadership: 100 | READY -when isReady() == true* WarGeneral | intelligence: 100 | experience: 100 | leadership: 100 | DONE -when isReady() == false** @return string representation of this object*/}The TODOs in the pa1.technologies package (in thetechnologies folder)The technology package contains classes represent the technologies owned by a playerand they provide bonuses to various aspects of the game. They are structured similarlyto the minister classes with one abstract class and many specific subclasses with differentbehaviors. You need to complete the implementation of these subclasses.29The TODOs in the TradingTech class (TradingTech.java)@Overridepublic double getGoldBonus() {/*** @return gold bonus equal to 1 + (level * 0.5);*/}@Overridepublic Cost getUpgradeCost() {/*** Upgrade costs:* gold = production = science = (current level + 1) * 1000;** @return upgrade costs*/}@Overridepublic String toString() {/*** Example string representation:* TradingTech | level: 1 | gold bonus: 1.50** @return String representing this object*/}30The TODOs in the ManufacturingTech class(ManufacturingTech.java)@Overridepublic double getProductionBonus() {/*** @return production bonus equal to 1 + current level * 0.5*/}@Overridepublic Cost getUpgradeCost() {/*** Upgrade costs:* gold = production = (current level + 1) * 1000;* science = 0** @return upgrade costs*/}@Overridepublic String toString() {/*** Example string representation:* ManufacturingTech | level: 1 | production bonus: 1.50** @return String representing this object*/}31The TODOs in the WarTech class (WarTech.java)@Overridepublic double getAttackBonus() {/*** @return attack bonus equal to 1 + level * 0.5*/}@Overridepublic Cost getUpgradeCost() {/*** Upgrade costs:* gold = science = (current level + 1) * 1000;* production = 0** @return upgrade costs*/}@Overridepublic String toString() {/*** Example string representation:* WarTech | level: 1 | attack bonus: 1.50** @return String representing this object*/}32Additional ClassesThese additional classes help encapsulate data. The Cell class represent an x, ylocation on the game map. The Cost class encapsulates gold, production and sciencepoints costs for performing a task.The TODOs in the Cell class (Cell.java)Implement this class from scratch according to the UML diagram33The TODOs in the Cost class (Cost.java)public Cost getDiscountedCost(double rate) {/*** Get a new Cost object with applied discount rate** @param rate discount rate* @return Discounted Cost = round(rate * current cost)*/}@Overridepublic boolean equals(Object obj) {/*** Two Cost objects are equal if their* gold costs AND production costs AND science costs* are equal* * Return false if obj is not an instance of Cost** @param obj* @return*/}34ExceptionsYou also need to implement a custom exception class, TooPoorException. Thisexception will be thrown when the player does not have enough resources to complete atask. You will need to throw this exception in your implementation of the minister classes.TooPoorException.java@Overridepublic String getMessage() {/*** Constructs an error message in the form:* need (%d golds, %d production points, %d science points), have (%d golds,%d production points, %d science points)** @return*/}35Running the executable fileAn executable file for the PA could be downloaded from the PA1 link at the course web.You could use it to verify the correctness of your finished PA. To run the executable fileunder Windows (we only have Windows machine to compile it :( ), first you have to makesure the two .txt files (map.txt and players.txt files) are in the same directory as theexecutable file. Then Open a Windows command window, adjust the width of thecommand window as indicate below for a better display:Then issue the command:ASCII_civilization.exe36After you are done with the PACongratulations! Hope you have enjoyed it! Now you can Zip your complete projectdirectory to PA1.zip and submit this file to the CASS link shown on page 1. You cansubmit for as many times as you wish before the deadline, but we will only mark you latestsubmission.Late Submission PolicyThere will be a penalty of -1 point (out of a maximum 100 points) for every minute yousubmit the PA late. If you submit your PA1 at 00:30am on 17 April 2019 (Wednesday),there will be a penalty of -35 points for your assignment. The lowest score you may getfrom the assignment is zero. If you submit your PA1 at 2:35am on 17 April 2019 or later,you will have zero for the PA1. So be sure to submit the finished PA1 as early as possible.转自:http://ass.3daixie.com/2019041423054213.html