You are to design a Java program to implement a simulation of a game involving characters that can attack each other (you wont actually play the game -- instead your program will output what would happen if a series of actions occurred in the game). In the full version of the game, there will be a mix of types of characters (basic characters, warriors, aliens, etc.) and each type has unique abilities and properties. But it is always easier to break a large project up into smaller steps, so the instructions for the project have been divided into 2 steps. In step 1, you will get the game working with only basic characters (no warriors, aliens, etc.). After you have that working, in step 2 you will add the other character types and all of their unique functionality. (NOTE: you could develop the program all in one big step, but it will probably be much easier if you create it in 2 steps).

Step 1: Get the game working with only basic characters (no warriors, aliens, etc.)

Design and code a program that simulates a game being played where characters perform certain actions. The main parts of the program are as follows:

  • The program will read a text file called characterInfo.txt that contains information about characters and it will create objects for those characters and store them in an array or ArrayList.
  • The program will read from another text file called characterActions.txt that contains a series of actions that the characters should perform. For each action, the program should use the character objects to perform those actions and output a related message.

Following is an example of input files that could be provided to the program and the output that would be produced by the program:

(NOTE: the format of the data in the following file is: character type,character name,character attack strength)

characterInfo.txt
Basic,Sue,15
Basic,Tom,12

(NOTE: the format of the following file is: name,action (if there is a 3rd data item it is the character being attacked)

characterActions.txt
Sue,speak
Tom,display health
Sue,attack,Tom
Tom,display health

Output of Running Program that reads 2 files above

Sue says hi my name is Sue
Tom's health level is 100
Sue attacks Tom
Tom's health level is 85

Following are the specifications for the basic game characters for part 1:

A basic Character:

  • Has a name (such as Sue in the example above)
  • Has an attack strength value (such as 15 for Sue in the example above). This value will determine how much damage the character will inflict on another character if they attack them.
  • Has a health rating that starts at 100 and is reduced if the character is attacked by another character (in the example above, Toms health is originally 100 but is reduced to 85 after Sue attacks). If an attack would cause a health rating to go below 0, the health rating should just be 0 (instead of going negative). When the health rating reaches 0, the character is dead.
  • Can speak. When they speak, they say hi my name is < their name> (in the example above, Sue says hi my name is Sue). IF THE CHARACTER IS DEAD and they are instructed to speak, they just say silence (if Sue attacked Tom repeatedly until Toms was dead and then the instruction said Tom,speak, the output would be Tom says silence).

Instructions for part 1:

  • In jGrasp, create one class to represent the basic game characters that will be created
  • In jGrasp, create one class with main method that will contain the main program that will run. The program will:
    • Read the lines of the characterInfo.txt file, create objects for each character, and add the objects to an ArrayList of characters. (Hint: it may be easiest to read the entire line into one String using a Scanners nextLine() method, and then call methods such as indexOf , split, substring, and/or Integer.parseInt to break the String into the individual compenents in the line such as the name and attack points).
    • Read the lines of the characterActions.txt file and call methods on the objects to perform those actions (and output messages as shown in the example program output above)
  • In jGrasp (or Notepad or other text editor), create 2 text files called characterInfo.txt and characterActions.txt and copy the lines from the example files above into those text files.
  • Run your program using those text files. If you dont get the output shown in the example above, debug your code until you get that output.
  • Add more characters and instructions in the text files to test that all of the functionality works correctly (for example, make sure that after a character is dead they say silence if they are instructed to speak.

HINT: The toughest part about this project is figuring out write code so that one character can attack another character. Dont forget that an object can be passed as an argument into a method called on another object. For example, in a banking program where money could be transferred between accounts, there could be a class called Account with a method:

public void transfer(Account otherAccount, double amount) {
this.withdraw(amount);
otherAccount.deposit(amount);
}

Then the following code could be written:

Account myFirstAccount = new Account();
Account mySecondAcount = new Account();
myFirstAccount.transfer(mySecondAcount, 500);

Step 2: Get the full version of the game working with additional types of characters

In this step you will need to write additional code so that the game simulation works with additional types of characters.

Following is an example of input files that could be provided to the program and the output that would be produced by the program:

characterInfo.txt
Basic,Sue,15
Basic,Tom,12
Wizard,Harry Potter,20
Alien,Khan,30
Character,Bill,8
Warrior,Hercules,35
characterActions.txt
Sue,speak
Tom,display health
Sue,attack,Tom
Tom,display health
Harry Potter,find healing potion
Hercules,attack,Harry Potter
Harry Potter,display health
Harry Potter,use healing potion
Harry Potter,display health
Hercules,find weapon
Khan,speak
Hercules,speak
Hercules,attack,Khan
Khan,display health
Hercules,find weapon
Hercules,attack,Khan
Khan,display health
Khan,speak
Hercules,attack,Khan
Khan,display health
Khan,speak

Output of Running Program that reads 2 files above

Sue says hi my name is Sue
Tom's health level is 100
Sue attacks Tom
Tom's health level is 85
Harry Potter collects a healing potion
Hercules attacks Harry Potter
Harry Potter's health level is 65
Harry Potter uses healing potion
Harry Potter's health level is 100
Hercules collects a weapon
Khan says Ig Khan. TlhIngan maH
Hercules says I'm Hercules and I'm ready to fight
Hercules attacks Khan
Khan's health level is 100
Hercules collects a weapon
Hercules attacks Khan
Khan's health level is 45
Khan says Ig Khan. TlhIngan maH
Hercules attacks Khan
Khan's health level is 0
Khan says ...silence...

Following are the specifications for the other types of characters in the game.

  • A Warrior:
    • Has all of the features of a basic character (name, attack strength, etc.)
    • Has a count of the number of weapons he has (initially he has 0 weapons).
    • When he finds a weapon, he collects it (which increases his count of weapons by 1). See Hercules above.
    • When he speaks (and is alive), he says Im and Im ready to fight. (see Hercules in the example above)
  • A Wizard:
    • Has all of the features of a basic character (name, attack strength, etc.)
    • Has a count of the number of healing potions he has (initially he has 0 healing potions).
    • When he finds a healing potion, he collects it (which increases his count of healing potions by 1)
    • When he speaks (and is alive), he says Im . Expecto Patronum. (see Harry Potter in Example above)
  • An Alien:
    • Has all of the features of a basic character (name, attack strength, etc.)
    • Has a count of the number of healing potions he has (initially he has 0 healing potions).
    • When he speaks (and is alive), he says Ig < his name>. TlhIngan maH. (see Khan in Example above)
    • After he is attacked, if his health is still at least 50, he will automatically self-heal to full health of 100. In the example above, when Hercules first attacks the alien Khan, he inflicts 45 units of damage so Khans health is reduced to 55 (but since Khans health is greater than 50, he recovers to full health of 100). The 2nd time Hercules attacks Khan, he inflicts 55 units of damage so Khans health is reduced to 45 (this time Khans health is below 50 so he does not self-heal).

Instructions for part 2:

  • In jGrasp, write code to implement the characters described above.
  • In jGrasp, copy the lines from the example text files above into your characterInfo.txt and characterActions.txt files.
  • Run your program using those text files. If you dont get the output shown in the example above, debug your code until you get that output.
  • Add more characters and instructions in the text files to test that all of the functionality works correctly (for example, if a wizard doesnt have any healing potions he cant heal himself).

Following are some additional requirements for the project:

  • You must use inheritance (at least one class should extend another class). You will probably have more than one class extend a class.
  • All member variables in all classes must be private or protected (not public)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.