Looking for some help to make this Java program grapichally All it really needs
ID: 3832100 • Letter: L
Question
Looking for some help to make this Java program grapichally
All it really needs to have is a battle button that make them battle and then display the results.
Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class main {
public static void main(String args[]) throws FileNotFoundException {
Random rand = new Random();
Scanner scan = new Scanner(new File("clash.txt"));
ArrayList < Fighter > fighters = new ArrayList < Fighter > ();
//used a switch statement for the scan as it seemed easier for this question error checking can be found at end of the statement
while (scan.hasNext()) {
Fighter f = null;
String name = scan.next();
switch (name) {
case "Barbarian":
f = new Barbarian();
break;
case "Archer":
f = new Archer();
break;
case "Wizard":
f = new Wizard();
break;
case "PEKKA":
f = new Pekka();
break;
case "Dragon":
f = new Dragon();
break;
default:
System.out.println("No file loaded ");
break;
}
f.name = name;
f.level = scan.nextInt();
f.hp = scan.nextInt();
f.damage = scan.nextInt();
f.style = scan.next();
fighters.add(f);
}
//random choice to fight
for (int i = 0; i < 200; i++) {
int randNumber1 = rand.nextInt((fighters.size() - 1) + 1 - 0) + 0;
int randNumber2 = rand.nextInt((fighters.size() - 1) + 1 - 0) + 0;
System.out.println("");
Battle(fighters.get(randNumber1), fighters.get(randNumber2));
System.out.println("");
}
System.out.println(fighters.toString().replace("[","").replace("]",""));
}
//battle program
public static void Battle(Fighter f1, Fighter f2) {
f1.fight(f2);
f2.fight(f1);
}
}
Fighter.java
abstract public class Fighter {
public String name;
int level;
int damage;
int hp;
String style;
abstract void fight(Fighter f);
public String toString() {
return String.format("(%s,%d,%d,%d,%s)", name, level, damage, hp, style);
}
}
Archer.java
public class Archer extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces
f.hp = f.hp - (this.damage * this.level);
}
}
Barbarian.java
public class Barbarian extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces
f.hp = f.hp - (this.damage + this.level);
}
}
Dragon.java
public class Dragon extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
f.hp = f.hp - (this.damage*3 + this.level);
}
}
Pekka.java
public class Pekka extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces
f.hp = f.hp - (this.damage + 2 * this.level);
}
}
Wizard.java
public class Wizard extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces add a little extra to spice it up
f.hp = f.hp - (this.damage*2 + this.level);
}
}
Clash.txt
Barbarian 1 100 1 Melee
Barbarian 3 200 2 Melee
Wizard 3 75 3 Ranged
Wizard 4 150 5 Ranged
Archer 2 100 1 Ranged
Archer 4 150 3 Ranged
PEKKA 2 300 2 Melee
PEKKA 3 600 3 Melee
Dragon 2 620 2 Ranged
Dragon 5 900 4 Ranged
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class main {
public static void main(String args[]) throws FileNotFoundException {
Random rand = new Random();
Scanner scan = new Scanner(new File("clash.txt"));
ArrayList < Fighter > fighters = new ArrayList < Fighter > ();
//used a switch statement for the scan as it seemed easier for this question error checking can be found at end of the statement
while (scan.hasNext()) {
Fighter f = null;
String name = scan.next();
switch (name) {
case "Barbarian":
f = new Barbarian();
break;
case "Archer":
f = new Archer();
break;
case "Wizard":
f = new Wizard();
break;
case "PEKKA":
f = new Pekka();
break;
case "Dragon":
f = new Dragon();
break;
default:
System.out.println("No file loaded ");
break;
}
f.name = name;
f.level = scan.nextInt();
f.hp = scan.nextInt();
f.damage = scan.nextInt();
f.style = scan.next();
fighters.add(f);
}
//random choice to fight
for (int i = 0; i < 200; i++) {
int randNumber1 = rand.nextInt((fighters.size() - 1) + 1 - 0) + 0;
int randNumber2 = rand.nextInt((fighters.size() - 1) + 1 - 0) + 0;
System.out.println("");
Battle(fighters.get(randNumber1), fighters.get(randNumber2));
System.out.println("");
}
System.out.println(fighters.toString().replace("[","").replace("]",""));
}
//battle program
public static void Battle(Fighter f1, Fighter f2) {
f1.fight(f2);
f2.fight(f1);
}
}
Fighter.java
abstract public class Fighter {
public String name;
int level;
int damage;
int hp;
String style;
abstract void fight(Fighter f);
public String toString() {
return String.format("(%s,%d,%d,%d,%s)", name, level, damage, hp, style);
}
}
Archer.java
public class Archer extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces
f.hp = f.hp - (this.damage * this.level);
}
}
Barbarian.java
public class Barbarian extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces
f.hp = f.hp - (this.damage + this.level);
}
}
Dragon.java
public class Dragon extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
f.hp = f.hp - (this.damage*3 + this.level);
}
}
Pekka.java
public class Pekka extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces
f.hp = f.hp - (this.damage + 2 * this.level);
}
}
Wizard.java
public class Wizard extends Fighter {
@Override
void fight(Fighter f) {
// TODO Auto-generated method stub
System.out.println(this.name + " " + this.damage + " " + this.hp + " " + this.level + " " + this.style);
//battle mechinaces add a little extra to spice it up
f.hp = f.hp - (this.damage*2 + this.level);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.