Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

OK. Create a GUI for your game. You are free in designing it the way you like. You need to submit your code and a

OK.

Create a GUI for your game. You are free in designing it the way you like. You need to submit your code and a pdf of the different screen shots. Must be able to: 1 vs 1 2 vs 2 You have the freedom to add other formations.

//The game code//

- BattleRoyale.java package

package project3;

import project3.characters.Riot; import project3.characters.Carnage; import project3.characters.Spiderman; import project3.characters.Venom; import project3.characters.Character; import java.util.Random;

public class BattleRoyale {

// A method that counts the number of alive characters and return it. public static int numAlive(Character[] characters) { int count = 0; // initialize count to 0 // for loop over the array of characters for(int i=0;i { if(characters[i].isAlive()) // if the character is alive, incrementing count by 1 count++; } return count; //returning count } public static void main(String[] args) { // main method // creating an array that contains 4 characters Character[] characters = new Character[4]; // populating the array characters[0] = new Riot("Lethal Riot"); characters[1] = new Spiderman("Reloaded Spiderman"); characters[2] = new Venom("Night Venom"); characters[3] = new Carnage("Blood Carnage"); Random ran = new Random(); int attackerIdx, otherIdx; // display the characters at the start of battle System.out.println("Characters at the beginning Battle Royale: "); for(int i=0;i { System.out.println(characters[i]); } System.out.println(); // loop that continues until there is only 1 character alive while(numAlive(characters) > 1) { attackerIdx = ran.nextInt(characters.length); // randomly generate the attacker index // a loop that continues until the character at attacker index is alive while(!characters[attackerIdx].isAlive()) attackerIdx = ran.nextInt(characters.length); // randomly generating the other character index otherIdx = ran.nextInt(characters.length); // loop that continues until the character at other index is alive and the indices of attacker and other is not the same while((attackerIdx == otherIdx) || (!characters[otherIdx].isAlive())) { otherIdx = ran.nextInt(characters.length); } // displaying the details of who attacked whom System.out.println(characters[attackerIdx].getName()+" attacked "+characters[otherIdx].getName()); characters[otherIdx].hit(characters[attackerIdx].attack()); // attack // display the updated health of the character who was attacked System.out.println(characters[otherIdx].getName()+" health is: "+characters[otherIdx].getHealth()+" "); } // displaying the characters at the end of the battle System.out.println(" Characters at the end of Battle Royale: "); for(int i=0; i { System.out.println(characters[i]); } // displaying the winner for(int i=0; i { if(characters[i].isAlive()) { System.out.println(); System.out.println(characters[i].getName()+" is the WINNER!"); break; } } }//End of main.

} //End of BattleRoyale class.

- Character.java

package project3.characters;

public abstract class Character {

// The Name of the charcter protected String name; // The Strength of the charcter protected int strength; //The Health of the charcter protected int health;

// Instantiates a new Character public Character(String name) { this.name = name; }

// Gets name of the charcter public String getName() { return name; }

// Gets strength of the charcter. public int getStrength() { return strength; }

// Gets health of the charcter public int getHealth() { return health; }

// Attack int public abstract int attack();

//Hit / damage

public abstract void hit(int points);

// Is alive boolean public abstract boolean isAlive();

// printing @Override public String toString() { return "Name: " + name + ", Strength: " + strength + ", Health: " + health ; } }

- Riot.java

package project3.characters;

public class Riot extends Character {

public Riot(String name) { super(name); this.health = 100; this.strength = 20; // super powerful to attack }

@Override public int attack() { return strength; }

@Override public void hit(int points) { this.health -= points; }

@Override public boolean isAlive() { return health>=0; }

@Override public String toString() { return "[ Class: Riot, "+super.toString()+" ]"; } }

- Spiderman.java

ackage project3.characters;

public class Spiderman extends Character {

public Spiderman(String name) { super(name); this.health = 100; this.strength = 13; }

@Override public int attack() { return strength; }

@Override public void hit(int points) { this.health -= points; }

@Override public boolean isAlive() { return health>=0; }

@Override public String toString() { return "[ Class: Spiderman, "+super.toString()+" ]"; } }

- Venom.java

package project3.characters;

public class Venom extends Character {

public Venom(String name) { super(name); this.health = 100; this.strength = 15; // more powerful to attack }

@Override public int attack() { return strength; }

@Override public void hit(int points) { this.health -= points; }

@Override public boolean isAlive() { return health >= 0; }

@Override public String toString() { return "[ Class: Venom, "+super.toString()+" ]"; } }

- Carnage.java

package project3.characters;

public class Carnage extends Character {

public Carnage(String name) { super(name); this.health = 100; this.strength = 15; // more powerful to attack }

@Override public int attack() { return strength; }

@Override public void hit(int points) { this.health -= points; }

@Override public boolean isAlive() { return health >= 0; }

@Override public String toString() { return "[ Class: Carnage, "+super.toString()+" ]"; } }

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

Step: 3

blur-text-image

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

Lords Of Finance The Bankers Who Broke The World

Authors: Liaquat Ahamed

1st Edition

0143116800, 978-0143116806

Students also viewed these Databases questions