Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need java help cannot seem to be getting this Clash of Clans type game to work. I keep running into invalid on my swithc statement

Need java help cannot seem to be getting this Clash of Clans type game to work. I keep running into invalid on my swithc statement so I think it has something to do with my txt. file

Program is as follows

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 random = new Random();

Scanner read = new Scanner(new File("clash.txt"));

ArrayList < Fighter > fighters = new ArrayList < Fighter > ();

while (read.hasNext()) { Fighter f = null; String name = read.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("Invalid "); break; } f.name = name; f.level = read.nextInt(); f.hp = read.nextInt(); f.damage = read.nextInt(); f.style = read.next(); fighters.add(f); }

for (int i = 0; i < 200; i++) {

int randomNumber1 = random.nextInt((fighters.size() - 1) + 1 - 0) + 0; int randomNumber2 = random.nextInt((fighters.size() - 1) + 1 - 0) + 0;

System.out.println(""); Battle(fighters.get(randomNumber1), fighters.get(randomNumber2)); System.out.println("");

}

System.out.println(fighters); }

public static void Battle(Fighter f1, Fighter f2) { f1.fight(f2); f2.fight(f1); }

}

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); }

}

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); }

}

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);

f.hp = f.hp - (this.damage + this.level);

}

}

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);

f.hp = f.hp - (this.damage * 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);

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);

if (this.hp < 20 && this.hp > 0) { this.hp = this.hp + 5; //heals } 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);

if (this.hp < 20) { f.hp = f.hp - 10; //rage when in low life }

f.hp = f.hp - (this.damage + this.level); }

}

Clash.txt

Barbarian 1 10 1 Melee

Barbarian 5 20 3 Melee

Wizard 6 10 10 Ranged

Wizard 9 20 17 Ranged

Archer 2 10 2 Ranged

Archer 4 15 3 Ranged

PEKKA 3 30 5 Melee

PEKKA 6 60 8 Melee

Dragon 5 62 4 Ranged

Dragon 9 90 6 Ranged

Below is the full program requirment if you have any futher questions:

1. In this program, you are going to simulate Clash of Clans battle. You need to create a class for each of the following fighter types:

a. Barbarian

b. Wizard

c. Archer

d. PEKKA

e. Dragon

You should also have a parent for each of these class types.

You are to read in a single file (that you create) containing the fighter type, the level, hit points, damage points, and how they fight. You should have at least 2 of each fighter type in the file.

The parent class should have at least one method called fight(). All children should override this method. In the fight method, the name, level, hit points and damage inflicted should appear along with how they fight.

Once the fighter objects are populated, you are to put them all into a single array or ArrayList using the parents type. Then, randomly pick two from the list and fight. Your program should display the information as well as keep track of how many hit points are left after the battle.

The program should repeat this process 200 times and then you should print out all the fighters with their corresponding values.

Specifics:

a. Create 5 classes that extend a parent class

b. Create a file that contains information about each fighter have at least 10 entries

c. Read in the file and populate the correct fighter with the information given

d. Put all instances of the classes into a single array or ArrayList

. e. Randomly choose two instances from the array (ArrayList) and print out the result of the battle

f. Repeat steps e-f 200 times.

g. Print out the final totals at the end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

draft a research report or dissertation;

Answered: 1 week ago

Question

How reliable is your assessment on PetroChinas financial statements

Answered: 1 week ago