Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

WILL RATE 5 STARS IF ANSWERED TODAY Objective: Write a class that reads a text f

ID: 3760254 • Letter: W

Question

WILL RATE 5 STARS IF ANSWERED TODAY

Objective:

Write a class that reads a text file and uses that in an rpg game.

First download the LabFiles

Put the driver in your project

DO NOT ALTER THE DRIVER!

Put the text file in your Project directory. This should be one directory up from your source

Write a class file called RPGPlayer

Attributes of this class are

Name – the name of the player

HP – corresponding to the number of hit points. This is assumed to be a whole number. Also it can be less than 0

Strength – corresponding to how much damage the player inflicts after each hit. This is assumed to be a whole number

Speed – the speed at which a player can move. This can be in fractions such as 3.5.

Weapon – is the name of the weapon that the player wields

Create the following constructors

Default – sets all attributes to a default value

One that has assigns each of the attributes to a given value

One that takes in only a string and then that string reads the values from a file.

Accessors and Mutators for each variable

MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!

Create the following MethodsreadPlayerFile – this only takes in a string and then assigns each of the player’s attributes based on the values found in the file. These values can be in any order but will have the protocol of <attribute> <value>

Example

Name Bob
HP 100
Strength 150
Speed 4.5
Weapon Dead Rat on a Stick

writePlayerFile – this also takes in a string and will print each of the values to a file. This must also follow the protocol of above

printInfo – This prints out all the stats about the player

Example Dialog:

Simple PVP Game

Player 1

The player Travis the Immortal!

HP: 100

Strength: 15

Speed: 2.0

Weapon: Board with a nail

Player 2

The player Bob

HP: 100

Strength: 10

Speed: 5.5

Weapon: Rat on a Stick

Travis the Immortal! attacks with Board with a nail

Bob now has 85 HP

Bob attacks with Rat on a Stick

Travis the Immortal! now has 90 HP

Travis the Immortal! attacks with Board with a nail

Bob now has 70 HP

Bob attacks with Rat on a Stick

Travis the Immortal! now has 80 HP

Travis the Immortal! attacks with Board with a nail

Bob now has 55 HP

Bob attacks with Rat on a Stick

Travis the Immortal! now has 70 HP

Travis the Immortal! attacks with Board with a nail

Bob now has 40 HP

Bob attacks with Rat on a Stick

Travis the Immortal! now has 60 HP

Travis the Immortal! attacks with Board with a nail

Bob now has 25 HP

Bob attacks with Rat on a Stick

Travis the Immortal! now has 50 HP

Travis the Immortal! attacks with Board with a nail

Bob now has 10 HP

Bob attacks with Rat on a Stick

Travis the Immortal! now has 40 HP

Travis the Immortal! attacks with Board with a nail

Bob now has -5 HP

The battle is over!

Travis the Immortal! has won!

The player was cloned after the battle and has the following stats

The player   Travis the Immortal!

HP: 40

Strength: 15

Speed: 2.0

Weapon:   Board with a nail

The player Travis the Immortal! HP: 40 Strength: 15 Speed: 2.0 Weapon: Board with a nail

LabFile (DO NOT ALTER)


public class SimplePVPGame {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println("Simple PVP Game");
       RPGPlayer p1 = new RPGPlayer("player1.txt");
       RPGPlayer p2 = new RPGPlayer("Bob", 100, 10, 5.5,"Rat on a Stick");
      
       System.out.println("Player 1");
       p1.printInfo();
       System.out.println();
       System.out.println("Player 2");
       p2.printInfo();
      
       boolean gameOver = false;
       boolean isPlayer1sTurn = true;
       while(gameOver == false)
       {
           //The winning condition
           if(p1.getHP()<=0||p2.getHP()<=0)
           {
               System.out.println("The battle is over!");
               if(p1.getHP()<=0)
                   System.out.println(p2.getName()+" has won!");
               else
                   System.out.println(p1.getName()+" has won!");
               break;
           }
           else if(isPlayer1sTurn)
           {
               Turn(p1,p2);
               isPlayer1sTurn = false;
           }
           else
           {
               Turn(p2,p1);
               isPlayer1sTurn = true;
           }
           System.out.println();
       }
       p1.writePlayerFile("results.txt");
      
       RPGPlayer p1Clone = new RPGPlayer("results.txt");
       System.out.println();
       System.out.println("The player was cloned after the battle and has the following stats");
       p1Clone.printInfo();
   }
   public static void Turn(RPGPlayer player, RPGPlayer opponent)
   {
       //Try to run away!
       if(player.getHP()<10)
       {
           if(player.getSpeed()>opponent.getSpeed())
           {
               System.out.println(player.getName()+" has run away! And falling rocks killed "+opponent.getName());
               opponent.setHP(0);
           }
           else
           {
               System.out.println(player.getName()+" cannot run away "+opponent.getName()+" is too fast!");
           }
       }
       else//ATTACK!
       {
           System.out.println(player.getName() + " attacks with "+player.getWeapon());
           opponent.setHP(opponent.getHP()- player.getStrength());
           System.out.println(opponent.getName()+" now has "+opponent.getHP()+" HP");
       }
   }

}

TextFile:

Explanation / Answer

//RPGPlayer.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class RPGPlayer
{
   private String name;
   private int HP;
   private int strength;
   private double speed;
   private String weapon;  

  
   //default constructor
   public RPGPlayer()
   {
       name="";
       HP=0;
       strength=0;
       speed=0;
       weapon="";
   }  

   //parameter constructor
   public RPGPlayer(String name, int HP, int strength,
           double speed, String weapon)
   {
       this.name=name;
       this.HP=HP;
       this.strength=strength;
       this.speed=speed;
       this.weapon=weapon;
   }

   //The constructor takes string file name and reads file
   //date to results.txt
   public RPGPlayer(String string)
   {
       Scanner fileReader=null;
       try
       {
           fileReader=new Scanner(new File(string));

           while(fileReader.hasNext())
           {
           String attribute=fileReader.next();
           if(attribute.equals("Name"))
               setName(fileReader.nextLine());
           else if(attribute.equals("HP"))
               setHP(fileReader.nextInt());
           else if(attribute.equals("Strength"))
               setStrength(fileReader.nextInt());
           else if(attribute.equals("Speed"))
               setSpeed(fileReader.nextDouble());
           else if(attribute.equals("Weapon"))
               setWeapon(fileReader.nextLine());
           }

       }
       catch (FileNotFoundException e)
       {
           System.out.println(e);          
       }

       fileReader.close();
   }
   public void setName(String name)
   {
       this.name=name;
   }
   public void setHP(int HP)
   {
       this.HP=HP;
   }
   public void setStrength(int strength)
   {
       this.strength=strength ;
   }
   public void setSpeed(double speed)
   {
       this.speed=speed;
   }
   public void setWeapon(String weapon)
   {
       this.weapon=weapon;
   }

   public String getName()
   {
       return name;
   }
   public int getHP()
   {
       return HP;
   }
   public int getStrength()
   {
       return strength ;
   }
   public double getSpeed()
   {
       return speed;
   }
   public String getWeapon()
   {
       return weapon;
   }
   public void printInfo() {

       System.out.println("Name="+name);
       System.out.println("HP="+HP);
       System.out.println("Strength="+strength);
       System.out.println("Speed="+speed);
       System.out.println("Weapon="+weapon);
   }

   //The method takes string file name and writes file from
   //the results.txt
   public void writePlayerFile(String string) throws IOException
   {
       PrintWriter writter=null;
       try
       {
           //create an instacne of PrintWritter class
           writter=new PrintWriter(new File(string));
           //write attribute values to the file "results.txt"
           writter.println("Name "+getName());
           writter.println("HP "+getHP()+" ");
           writter.println("Strength "+getStrength()+" ");
           writter.println("Speed "+getSpeed()+" ");
           writter.println("Weapon "+getWeapon()+" ");
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e);          
       }
       writter.close();

   }

}

----------------------------------------------------------------------------------------------------------------------------

//Your test file

//SimplePVPGame.java

import java.io.IOException;

public class SimplePVPGame
{

   /**
   * @param args
   * @throws IOException
   */
   public static void main(String[] args) throws IOException
   {      
       System.out.println("Simple PVP Game");
       RPGPlayer p1 = new RPGPlayer("player1.txt");
       RPGPlayer p2 =
       new RPGPlayer("Bob", 100, 10, 5.5,
               "Rat on a Stick");

       System.out.println("Player 1");
       p1.printInfo();
       System.out.println();
       System.out.println("Player 2");
       p2.printInfo();

       boolean gameOver = false;
       boolean isPlayer1sTurn = true;
       while(gameOver == false)
       {
           //The winning condition
           if(p1.getHP()<=0||p2.getHP()<=0)
           {
               System.out.println("The battle is over!");
               if(p1.getHP()<=0)
                   System.out.println(p2.getName()+" has won!");
               else
                   System.out.println(p1.getName()+" has won!");
               break;
           }
           else if(isPlayer1sTurn)
           {
               Turn(p1,p2);
               isPlayer1sTurn = false;
           }
           else
           {
               Turn(p2,p1);
               isPlayer1sTurn = true;
           }
           System.out.println();
       }
      
      
       p1.writePlayerFile("results.txt");      
       RPGPlayer p1Clone = new RPGPlayer("results.txt");      
       System.out.println();
       System.out.println("The player was cloned after the battle and has the following stats");
       p1Clone.printInfo();
   }
   public static void Turn(RPGPlayer player, RPGPlayer opponent)
   {
       //Try to run away!
       if(player.getHP()<10)
       {
           if(player.getSpeed()>opponent.getSpeed())
           {
               System.out.println(player.getName()+" has run away! And falling rocks killed "+opponent.getName());
               opponent.setHP(0);
           }
           else
           {
               System.out.println(player.getName()+" cannot run away "+opponent.getName()+" is too fast!");
           }
       }
       else//ATTACK!
       {
           System.out.println(player.getName() + " attacks with "+player.getWeapon());
           opponent.setHP(opponent.getHP()- player.getStrength());
           System.out.println(opponent.getName()+" now has "+opponent.getHP()+" HP");
       }
   }

}

--------------------------------------------------------------------------------------------------------------------------------------------

Sample input file

player1.txt

Name Bob
HP 100
Strength 15
Speed 2.0
Weapon Board with a nail

--------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Simple PVP Game
Player 1
Name= Bob
HP=100
Strength=15
Speed=2.0
Weapon= Board with a nail

Player 2
Name=Bob
HP=100
Strength=10
Speed=5.5
Weapon=Rat on a Stick
Bob attacks with Board with a nail
Bob now has 85 HP

Bob attacks with Rat on a Stick
Bob now has 90 HP

Bob attacks with Board with a nail
Bob now has 70 HP

Bob attacks with Rat on a Stick
Bob now has 80 HP

Bob attacks with Board with a nail
Bob now has 55 HP

Bob attacks with Rat on a Stick
Bob now has 70 HP

Bob attacks with Board with a nail
Bob now has 40 HP

Bob attacks with Rat on a Stick
Bob now has 60 HP

Bob attacks with Board with a nail
Bob now has 25 HP

Bob attacks with Rat on a Stick
Bob now has 50 HP

Bob attacks with Board with a nail
Bob now has 10 HP

Bob attacks with Rat on a Stick
Bob now has 40 HP

Bob attacks with Board with a nail
Bob now has -5 HP

The battle is over!
Bob has won!

The player was cloned after the battle and has the following stats
Name= Bob
HP=40
Strength=15
Speed=2.0
Weapon= Board with a nail


--------------------------------------------------------------------------------------------------------------------------------------------

Output file results.txt

Name Bob
HP 40

Strength 15

Speed 2.0

Weapon Board with a nail