The program you submit will contain 3 files. Please see the requirements below f
ID: 3913402 • Letter: T
Question
The program you submit will contain 3 files. Please see the requirements below for what is expected for these items.
A class called Player
A class called Enemy (or Opponent)
A class called Game which contains your main method
How the Program Works
Here is how the game works. A video sample of the game running is provided below.
When the game begins, it should create a Player instance. The Player instance will be randomly assigned values for hit points, strength, speed, and charm at the time it is created. The Player instance should also be assigned a name. You can prompt the user for the name or auto-generate one. This can all be done within the Constructor for this class.
The game should then print all of the statistics of the Player instance to the console so the user knows each of these values. You can make this task easy by implementing a toString() method in your Player class.
Once the Player is created you will create an Enemy (or Opponent) instance. The Enemy instance will be randomly assigned values for hit points, strength, speed, and charm at the time it is created. The Enemy instance should also be assigned a name. I would recommend auto-generating one. This can all be done within the Constructor for this class.
The user is then presented with a menu asking them if they would like to Battle the enemy, Run from the enemy, Talk to the enemy, or Quit the game.
Run - If the user chooses to run from the enemy they succeed and earn a point if their speed is greater than the enemy’s speed. If the player is slower or equal to the enemy in speed then they must Battle.
Talk - If the user chooses to talk to the enemy they succeed and earn a point if their charm is greater than the enemy’s charm. If the player is slower or equal to the enemy in charm then they must Battle.
Quit - The game immediately ends and statistics are printed to the screen indicating how many rounds were played and how many rounds were won by the player.
Battle - All battles occur using Rock, Paper, Scissors. The Player and Enemy should automatically choose their option (Rock, Paper, or Scissors). Do not prompt the user for a value. Battles are automatic. The Player wins any ties. If the Player wins you should subtract the Player’s strength from the Enemy’s hit points. If the Enemy wins you should subtract the Enemy’s strength from the Player’s hit points. The battle should continue until either the Player or the Enemy are at or below zero hit points.
If the Enemy wins, the Enemy’s information should be printed to the screen. You can make this task easy by implementing a toString() method in your Enemy class. The game should then follow the process as if the user had selected Quit from the menu.
If the Player wins the round (through battle or successfully running away or talking), then they should be presented with another new Enemy and the process starts over by presenting the user the menu asking them if they would like to Battle the enemy, Run from the enemy, Talk to the enemy, or Quit the game. Play should continue until the user selects Quit from the menu or Player reaches or goes below zero hit points.
UML Diagrams, Notes, Resources, and Discussion
Below is a simplified UML diagram for Classes to be used in this project. I have also attached a zip file with Java Docs for these classes to help guide you.
Please note that the UML does not show Accessors & Mutators (Get & Set methods). Your classes should implement these.
Note that the Player class has several methods that accept an Enemy object as a parameter and return a boolean (true or false). These methods are used to determine who wins in a given scenario. For example, if the Player chooses to run you can call the run method passing the Enemy as a parameter and this method will compare the speed of each object and return true if the Player is faster and false if the Player is slower. Please see the attached Java Documentation for more detail. You will need to unzip the Java Documentation folder and double-click on the file “index.html” to view it in a web browser.
The Battle method works similar to the talk and run methods but it will handle the Rock/Paper/Scissors battle and return true if the Player wins and false if they lose. The battle method only handles one round of R/P/S and therefore, the battle method may need to be called repeatedly until either the Player or the Enemy object reach zero hit points.
Please note that you can use the isDefeated() method to determine if a player has reached zero hit points as it returns true if they have zero or less hit points.
Again, more details on these methods can be found in the attached Java Documentation, in the UML diagram below, and in the attached demo video.
UML Diagram
Some Items for Discussion
This program is meant to drive a lot of discussion. Please use the Discussion Board as you work on this assignment. You should also make some observations as you are writing the code. Some questions to ask yourself might be as follows…
Why are the Player and Enemy classes so similar?
Where do you I put my print statements? In the driver or in the Player or Enemy classes?
What other designs might work for this program? Is the instructor’s approach a good design?
How might one auto-generate names?
Here's the info from the zip file:
B
battle(Enemy) - Method in class wk3game.Player
Used to determine who wins a battle and to reduce the hit points of the loser.
BattleGame - Class in wk3game
BattleGame() - Constructor for class wk3game.BattleGame
E
Enemy - Class in wk3game
Represents an Enemy for this game.
Enemy(String) - Constructor for class wk3game.Enemy
Creates a new enemy with the given name.
I
isDefeated() - Method in class wk3game.Enemy
Used to determine if the enemy is beaten.
isDefeated() - Method in class wk3game.Player
Used to determine if the player is beaten.
M
main(String[]) - Static method in class wk3game.BattleGame
P
Player - Class in wk3game
Represents a Player for this game.
Player(String) - Constructor for class wk3game.Player
Creates a new player with the given name.
R
run(Enemy) - Method in class wk3game.Player
Used to determine if the player can run from this situation.
T
talk(Enemy) - Method in class wk3game.Player
Used to determine if the player can talk their way out of this situation.
toString() - Method in class wk3game.Enemy
Returns a string representation of this instance.
toString() - Method in class wk3game.Player
Returns a string representation of this instance.
W
wk3game - package wk3game
Enemy Game Player - hitPoints int - strength : int - speed: int - charm int - name : String - hitPoints: int - strength : int idse speed: int n args: Strin - Use - charm int - name : String + Enemy(name : String) + isDefeated): boolean + toString0) String You may add as many helper" methods and variables as you feel is necessary to complete the program. The items listed here are the minimum + Player(name : String) + isDefeated() : boolean + toString0) String +battle(e : Enemy): boolearn +run(e: Enemy) : boolean +talk(e Enemy): boolean Not listed, but all instance variables should have getter and setter methods For example: I created a method to generate some random names for my characters Not listed, but all instance variables should have getter and setter methodsExplanation / Answer
import java.util.*;
public class BattleGame{
int round;
BattleGame()
{
round=0;
}
String[] nameset={"dan","john","jack","andrew","gwen"};
boolean endCard(boolean res)
{
if(res==true)
{
System.out.println("You Won! round"+this.round+" replay(y) quit(n):");
Scanner sc=new Scanner(System.in);
String cho=sc.next();
if(cho.toLowerCase().equals("y"))
return true;
else
return false;
}
else
{
System.out.println("You Lost"+this.round+" replay(y) quit(n):");
Scanner sc=new Scanner(System.in);
String cho=sc.next();
if(cho.toLowerCase().equals("y"))
return true;
else
return false;
}
}
void battle(Player p1)
{
Scanner sc=new Scanner(System.in);
Random rnd = new Random(System.currentTimeMillis());
Enemy e=new Enemy(nameset[rnd.nextInt(((nameset.length-1) - 0) + 1) + 0]);
System.out.println("your Hitpoints "+p1.getHitpoints()+" Speed: "+p1.getSpeed()+" Strength: "+p1.getStrength()+" charm: "+p1.getCharm());
System.out.println("1.run 2.talk 3.battle 4.quit your choice?");
int ch=sc.nextInt();
if(ch==1)
{
if(p1.run(e)==true)
{
this.endCard(true);
}
}
else if(ch==2)
{
if(p1.talk(e)==true)
{
this.endCard(true);
}
}
boolean flag=true;
while(flag)
{
flag=p1.battle(e,this);
}
if(e.isDefeated())
this.endCard(true);
else if(p1.isDefeated())
this.endCard(false);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
boolean flag=true;
Random rnd = new Random(System.currentTimeMillis());
System.out.println("enter (y) to enter your own name (n) for auto generated:");
String cs=sc.next();
cs=cs.toLowerCase();
Player p1;
BattleGame b=new BattleGame();
if(cs.equals("y"))
{ System.out.println("enter name:");
String name=sc.next();
p1=new Player(name);
}
else
{ int randnum=rnd.nextInt(((b.nameset.length-1) - 0) + 1) + 0;
p1=new Player(b.nameset[randnum]);
}
b.battle(p1);
}
}
import java.util.Random;
public class Enemy{
int hitpoints;
int charm;
int speed;
int strength;
String name;
String[] RPS={"rock","paper","scissor"};
Enemy(String name)
{
this.name=name;
int min=0,max=10;
Random rnd = new Random(System.currentTimeMillis());
this.setHitpoints(rnd.nextInt((max - min) + 1) + min);
this.setCharm(rnd.nextInt((max - min) + 1) + min);
this.setStrength(rnd.nextInt((max - min) + 1) + min);
this.setSpeed(rnd.nextInt((max - min) + 1) + min);
}
String battle()
{
int min=0;int max=this.RPS.length;
Random rnd = new Random(System.currentTimeMillis());
return RPS[rnd.nextInt((max - min) + 1) + min];
}
boolean isDefeated()
{
if(getHitpoints()<=0)
return true;
else
return false;
}
void setHitpoints(int n)
{
this.hitpoints=n;
}
int getHitpoints()
{
return this.hitpoints;
}
void setSpeed(int n)
{
this.speed=n;
}
int getSpeed()
{
return this.speed;
}
void setStrength(int n)
{
this.strength=n;
}
int getStrength()
{
return this.strength;
}
void setCharm(int n)
{
this.charm=n;
}
int getCharm()
{
return this.charm;
}
}
import java.util.Random;
public class Player{
int hitpoints;
int charm;
int speed;
int strength;
String name;
String[] RPS={"rock","paper","scissor"};
Player(String name)
{
this.name=name;
int min=0,max=10;
Random rnd = new Random(System.currentTimeMillis());
this.setHitpoints(rnd.nextInt((max - min) + 1) + min);
this.setCharm(rnd.nextInt((max - min) + 1) + min);
this.setStrength(rnd.nextInt((max - min) + 1) + min);
this.setSpeed(rnd.nextInt((max - min) + 1) + min);
}
boolean isDefeated()
{
if(getHitpoints()<=0)
return true;
else
return false;
}
boolean battle(Enemy e,BattleGame b)
{ int max=this.RPS.length-1;int min=0;
Random rnd = new Random(System.currentTimeMillis());
String pchoice=RPS[rnd.nextInt((max - min) + 1) + min];
String echoice=e.battle();
b.round+=1;
if(pchoice.equals("scissor")&&echoice.equals("paper"))
{
e.setHitpoints(e.getHitpoints()-this.getStrength());
if(e.isDefeated())
return false;
else
return true;
}
else if(echoice.equals("scissor")&&pchoice.equals("paper"))
{
this.setHitpoints(this.getHitpoints()-e.getStrength());
if(this.isDefeated())
return false;
else
return true;
}
else if(pchoice.equals("stone")&&echoice.equals("scissor"))
{
e.setHitpoints(e.getHitpoints()-this.getStrength());
if(e.isDefeated())
return false;
else
return true;
}
else if(echoice.equals("stone")&&pchoice.equals("scissor"))
{
this.setHitpoints(this.getHitpoints()-e.getStrength());
if(this.isDefeated())
return false;
else
return true;
}
else if(pchoice.equals("paper")&&echoice.equals("stone"))
{
e.setHitpoints(e.getHitpoints()-this.getStrength());
if(e.isDefeated())
return false;
else
return true;
}
else if(echoice.equals("paper")&&pchoice.equals("stone"))
{
this.setHitpoints(this.getHitpoints()-e.getStrength());
if(this.isDefeated())
return false;
else
return true;
}
else
return true;
}
boolean run(Enemy e)
{
if(e.speed<this.speed)
return true;
else return false;
}
boolean talk(Enemy e)
{
if(e.charm<this.charm)
return true;
else return false;
}
void setHitpoints(int n)
{
this.hitpoints=n;
}
int getHitpoints()
{
return this.hitpoints;
}
void setSpeed(int n)
{
this.speed=n;
}
int getSpeed()
{
return this.speed;
}
void setStrength(int n)
{
this.strength=n;
}
int getStrength()
{
return this.strength;
}
void setCharm(int n)
{
this.charm=n;
}
int getCharm()
{
return this.charm;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.