Question 1: Battle Game (50 points) For this question, you will write a number o
ID: 3813612 • Letter: Q
Question
Question 1: Battle Game (50 points) For this question, you will write a number of classes to create a battle game between a player and a monster. Your code for this question will go in multiple a files. We strongly recommend that you complete all the warm-up questions before starting this problem. Note that in addition to the required methods below, you are free to add as many other private methods as you want. (a) Character Class Character.java represents a character in our battle game. The monster and the player will both be instances of the Character class, each with their own attributes. The Character class should contain the following (private attributes A string name A double attack value A double maximum health value A double current health value A int number of wins in the battle game Here are the required public methods for this class. Note that you will also have to add getters and setters for the instance attributes as needed 1) A constructor The constructor for the Character class takes one String, two doubles, and one int as input. These parameters represent the name, attack value, maximum health, and number of wins in the battle game for the character, in that order. Note that the current health of a new character is the same as the maximum health 2) The toString method This method returns a String consisting of the character's name and current health. Format the String in any way you want. This method will be very handy for debugging your code, and will be used during the battle game to keep track of the health of each characterExplanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package BattleGame;
import java.util.Random;
/**
*
* @author Namburi Ramesh
*/
public class Character {
private String name;
private double attack;
private double maxHealth;
private double curHealth;
private int wins;
public Character(String name, double attack, double maxHealth, int wins) {
this.name = name;
this.attack = attack;
this.maxHealth = maxHealth;
this.wins = wins;
curHealth=maxHealth;
}
@Override
public String toString() {
return "name=" + name + ", Current Health=" + curHealth;
}
public void print(){
System.out.println("Name: "+name);
System.out.println("Health: "+curHealth);
System.out.println("Attack: "+attack);
System.out.println("Number of wins: "+wins);
}
public double calcAttack(){
//Generate a random number between 0.3 and 0.7
Random r=new Random();
double i=r.nextDouble()%0.4;
double rand=i+0.3;
double attack1=attack*rand;//calculate attack
return attack1;
}
public void takeDamage(double damage){
curHealth=curHealth-damage;
}
public void increaseWins(){
wins++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAttack() {
return attack;
}
public void setAttack(double attack) {
this.attack = attack;
}
public double getMaxHealth() {
return maxHealth;
}
public void setMaxHealth(double maxHealth) {
this.maxHealth = maxHealth;
}
public double getCurHealth() {
return curHealth;
}
public void setCurHealth(double curHealth) {
this.curHealth = curHealth;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package BattleGame;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author Namburi Ramesh
*/
public class FileIO {
public static Character readCharacter(String file){
Character c=null;
try{
File f=new File(file);
BufferedReader s=new BufferedReader(new FileReader(f)); //create a buffered reader
c=new Character(s.readLine(), Double.valueOf(s.readLine()), Double.valueOf(s.readLine()), Integer.valueOf(s.readLine())); //create a character instance
}catch(FileNotFoundException e){
throw new IllegalArgumentException("File not found");
}catch(IOException e){
throw new IllegalArgumentException("IO Error");
}
return c;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package BattleGame;
import java.util.Scanner;
/**
*
* @author Namburi Ramesh
*/
public class BattleGame {
public static void main(String args[]){
BattleGame b=new BattleGame();
b.playGame();
}
public void playGame(){
//create two characters
Character player=FileIO.readCharacter("C:\Users\Namburi Ramesh\Desktop\player.txt");
Character monster=FileIO.readCharacter("C:\Users\Namburi Ramesh\Desktop\monster.txt");
//print charaters info
player.print();
monster.print();
//create a scanner for user input
Scanner s=new Scanner(System.in);
while(player.getCurHealth()>0 || monster.getCurHealth()>0){
System.out.println("Enter a command");
String com=s.nextLine();
if(com.equalsIgnoreCase("attack")){
doAttack(player,monster);
doAttack(monster,player);
}else if(com.equalsIgnoreCase("quit")){
System.out.println("goodbye!");
break;
}else{
System.out.println("Command is wrong. Enter 'attack' or 'quit'");
}
}
if(player.getCurHealth()<=0 && monster.getCurHealth()<=0){
}else if(player.getCurHealth()<=0){
System.out.println(monster.getName()+" wins");
monster.increaseWins();
}else if(monster.getCurHealth()<=0){
System.out.println(player.getName()+" wins");
player.increaseWins();
}
}
private void doAttack(Character c1, Character c2) {
double dam=c1.calcAttack();
System.out.println(c1.getName()+" attacks for "+dam+" damage");
c2.takeDamage(dam);
System.out.println(c2.getName()+" takes "+dam+" damage");
System.out.println(c2.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.