In: Computer Science
You will extend last week's workshop activity by adding a Player class. Each player has a number of Pokemons, and the player can select one of his or her Pokemon to play against another player's Pokenmon. The player who lost the battle lost his or her Pokemon to the other player.
You will be working with three.java files in this zyLab.
Complete the Player class with the following specifications:
(1) add the following three private fields
(2) add a constructor, which takes one parameter to initialize the name field. The level is initialized as 0. The constructor also creates the ArrayList.
(3) add each of the following public member methods
Complete the PokemonField.java
PokemonField.java
import java.util.Random;
import java.util.Scanner;
public class PokemonField {
public static void main(String[] args) {
Player player1 = new Player("dragonslayer");
Player player2 = new Player("voldemort");
Random random = new Random();
initialize(player1, player2);
player1.printStat();
player2.printStat();
play(random, player1, player2);
System.out.println("Game over");
player1.printStat();
player2.printStat();
}
public static boolean fight(Random random, Pokemon p1, Pokemon
p2){
double prob = (double)p1.getAttackPoints() / (p1.getAttackPoints()
+ p2.getAttackPoints());
while(p1.getHitPoints() > 0 && p2.getHitPoints() > 0)
{
if(random.nextDouble() < prob) {
p2.setHitPoints(Math.max(p2.getHitPoints()- p1.getAttackPoints(),
0));
}
else{
p1.setHitPoints(Math.max(p1.getHitPoints() - p2.getAttackPoints(),
0));
}
}
if(p1.getHitPoints() > 0){
System.out.println(p1.getName() + " won!");
return true;
}
else {
System.out.println(p2.getName() + " won!");
return false;
}
}
public static void initialize(Player player1, Player player2)
{
String[] pokemonNames1 = {"Butterfree", "Ivysaur", "Pikachu",
"Squirtle"};
int[] pokemonMaxHps1 = {10, 15, 20, 30};
int[] pokemonAps1 = {1, 2, 2, 3};
String[] pokemonNames2=
{"Jigglypuff","Oddish","Gloom","Psyduck","Machop"};
int[] pokemonMaxHps2 = {12, 18, 25, 27, 32};
int[] pokemonAps2 = {1, 1, 2, 2, 3};
for(int i = 0; i < pokemonNames1.length; i++) {
/*FIX(1) create a Pokemon object with the following three
arguments:pokemonNames1[i], pokemonMaxHps1[i], pokemonAps1[i]
Add the Pokemon object to the Pokemon array list in player1
Increment player1's level by 1*/
}
for(int i = 0; i < pokemonNames2.length; i++) {
/*FIX(2) create a Pokemon object with the following three
arguments:pokemonNames2[i], pokemonMaxHps2[i], pokemonAps2[i]
Add the Pokemon object to the Pokemon array list in player2
Increment player2's level by 1*/
}
}
public static void play(Random random, Player player1, Player
player2) {
Pokemon p1, p2;
int index1, index2;
Scanner scnr = new Scanner(System.in);
boolean result;
System.out.println("Let's play!");
System.out.println(player1.getName() + " choose a pokemon to
fight\nenter 1 to " + player1.getPokemons().size() + " or -1 to
stop");
index1 = scnr.nextInt();
System.out.println(player2.getName() + " choose a pokemon to
fight\nenter 1 to " + player2.getPokemons().size() + " or -1 to
stop");
index2 = scnr.nextInt();
while(index1 != -1 && index2 != -1) {
p1 = player1.getPokemons().get(index1-1);
p2 = player2.getPokemons().get(index2-1);
result = fight(random, p1, p2);
if(result) {
/*FIXME(3) p1 won, reset p2, remove p2 from player2, and add p2 to
player 1, increment player1's level by 1*/
}
else {
/*FIXME(4) p2 won, reset p1, remove p1 from player1, and add p1 to
player 2, increment player2's level by 1*/
}
System.out.println(player1.getName() + " choose a pokemon to
fight\nenter 1 to " + player1.getPokemons().size() + " or -1 to
stop");
index1 = scnr.nextInt();
System.out.println(player2.getName() + " choose a pokemon to
fight\nenter 1 to " + player2.getPokemons().size() + " or -1 to
stop");
index2 = scnr.nextInt();
}
}
}
Player.java
import java.util.ArrayList;
public class Player {
/*FIXME(1) Add three fields: name, level, pokemons*/
/*FIXME(2) Add a constructor, which takes one parameter: String
n.
Initialize name to n, level to 0, and create an ArrayList for
pokemons*/
/*FIXME (3) Define the accessor for name*/
/*FIXME (4) Define the accessor for level*/
/*FIXME (5) Define the accessor for pokemons*/
/*FIXME (6) Define the mutator for level*/
/*FIXME(7) Complete this method by adding p into the Pokemon
ArrayList*/
public void addPokemon(Pokemon p){
}
/*FIXME(8) Complete this method by removing p from the Pokemon
ArrayList*/
public void removePokemon(Pokemon p){
}
public void printStat(){
System.out.println("\nPlayer: " + name + " at level " + level + ",
has " + pokemons.size() + " pokemons");
/*FIXME(9) Complete this method by printing the stat of each
pokemon in the Pokemon ArrayList.
Each pokemon's stat is printed in an individual line. Hint: loop
through the ArrayList and call the printStat method of Pokemon
class*/
}
}
Pokemon.java
public class Pokemon {
private String name;
private int maxHp;
private int hitPoints;
private int attackPoints;
public Pokemon(String name, int maxHp, int ap) {
this.name = name;
this.maxHp = maxHp;
hitPoints = maxHp;
attackPoints = ap;
}
public String getName(){
return name;
}
public int getHitPoints() {
return hitPoints;
}
public int getAttackPoints() {
return attackPoints;
}
public void setHitPoints(int hp) {
hitPoints = hp;
}
public void setAttackPoints(int ap) {
attackPoints = ap;
}
public void reset() {
hitPoints = maxHp;
}
public void printStat() {
System.out.println(name + " has " + hitPoints + " hit points and "
+ attackPoints + " attack points");
}
}
/*FIXME(2) Add a constructor, which takes one parameter: String
n.
Initialize name to n, level to 0, and create an ArrayList for
pokemons*/
/*FIXME (3) Define the accessor for name*/
/*FIXME (4) Define the accessor for level*/
/*FIXME (5) Define the accessor for pokemons*/
/*FIXME (6) Define the mutator for level*/
/*FIXME(7) Complete this method by adding p into the Pokemon
ArrayList*/
public void addPokemon(Pokemon p){
}
/*FIXME(8) Complete this method by removing p from the Pokemon
ArrayList*/
public void removePokemon(Pokemon p){
}
public void printStat(){
System.out.println("\nPlayer: " + name + " at level " + level + ",
has " + pokemons.size() + " pokemons");
/*FIXME(9) Complete this method by printing the stat of each
pokemon in the Pokemon ArrayList.
Each pokemon's stat is printed in an individual line. Hint: loop
through the ArrayList and call the printStat method of Pokemon
class*/
}
}
/*******************************PokemonField.java*******************************/
import java.util.Random;
import java.util.Scanner;
public class PokemonField {
public static void main(String[] args) {
Player player1 = new
Player("dragonslayer");
Player player2 = new
Player("voldemort");
Random random = new Random();
initialize(player1,
player2);
player1.printStat();
player2.printStat();
play(random, player1, player2);
System.out.println("Game over");
player1.printStat();
player2.printStat();
}
public static boolean fight(Random random, Pokemon
p1, Pokemon p2) {
double prob = (double)
p1.getAttackPoints() / (p1.getAttackPoints() +
p2.getAttackPoints());
while (p1.getHitPoints() > 0
&& p2.getHitPoints() > 0) {
if
(random.nextDouble() < prob) {
p2.setHitPoints(Math.max(p2.getHitPoints() -
p1.getAttackPoints(), 0));
} else {
p1.setHitPoints(Math.max(p1.getHitPoints() -
p2.getAttackPoints(), 0));
}
}
if (p1.getHitPoints() > 0)
{
System.out.println(p1.getName() + " won!");
return
true;
} else {
System.out.println(p2.getName() + " won!");
return
false;
}
}
public static void initialize(Player player1,
Player player2) {
String[] pokemonNames1 = {
"Butterfree", "Ivysaur", "Pikachu", "Squirtle" };
int[] pokemonMaxHps1 = { 10, 15,
20, 30 };
int[] pokemonAps1 = { 1, 2, 2, 3
};
String[] pokemonNames2 = {
"Jigglypuff", "Oddish", "Gloom", "Psyduck", "Machop" };
int[] pokemonMaxHps2 = { 12, 18,
25, 27, 32 };
int[] pokemonAps2 = { 1, 1, 2, 2, 3
};
for (int i = 0; i <
pokemonNames1.length; i++) {
/*
* FIX(1) create
a Pokemon object with the following three
*
arguments:pokemonNames1[i], pokemonMaxHps1[i], pokemonAps1[i] Add
the Pokemon
* object to the
Pokemon array list in player1 Increment player1's level by 1
*/
player1.addPokemon(new Pokemon(pokemonNames1[i], pokemonMaxHps1[i],
pokemonAps1[i]));
}
for (int i = 0; i <
pokemonNames2.length; i++) {
/*
* FIX(2) create
a Pokemon object with the following three
*
arguments:pokemonNames2[i], pokemonMaxHps2[i], pokemonAps2[i] Add
the Pokemon
* object to the
Pokemon array list in player2 Increment player2's level by 1
*/
player2.addPokemon(new Pokemon(pokemonNames2[i], pokemonMaxHps2[i],
pokemonAps2[i]));
}
}
public static void play(Random random, Player
player1, Player player2) {
Pokemon p1, p2;
int index1, index2;
Scanner scnr = new
Scanner(System.in);
boolean result;
System.out.println("Let's
play!");
System.out.println(player1.getName() + " choose a pokemon to
fight\nenter 1 to " + player1.getPokemons().size()
+ " or -1 to stop");
index1 = scnr.nextInt();
System.out.println(player2.getName() + " choose a pokemon to
fight\nenter 1 to " + player2.getPokemons().size()
+ " or -1 to stop");
index2 = scnr.nextInt();
while (index1 != -1 &&
index2 != -1) {
p1 =
player1.getPokemons().get(index1 - 1);
p2 =
player2.getPokemons().get(index2 - 1);
result =
fight(random, p1, p2);
if (result)
{
/*
* FIXME(3) p1 won, reset p2, remove p2 from
player2, and add p2 to player 1,
* increment player1's level by 1
*/
p2.setHitPoints(0);
player2.removePokemon(p2);
player1.addPokemon(p2);
player1.setLevel(player1.getLevel() + 1);
} else
{
/*
* FIXME(4) p2 won, reset p1, remove p1 from
player1, and add p1 to player 2,
* increment player2's level by 1
*/
p1.setHitPoints(0);
player1.removePokemon(p1);
player2.addPokemon(p1);
player2.setLevel(player2.getLevel() + 1);
}
System.out.println(player1.getName() + " choose a pokemon to
fight\nenter 1 to "
+
player1.getPokemons().size() + " or -1 to stop");
index1 =
scnr.nextInt();
System.out.println(player2.getName() + " choose a pokemon to
fight\nenter 1 to "
+
player2.getPokemons().size() + " or -1 to stop");
index2 =
scnr.nextInt();
}
}
}
/***********************************Pokemon.java**********************************/
public class Pokemon {
private String name;
private int maxHp;
private int hitPoints;
private int attackPoints;
public Pokemon(String name, int maxHp, int ap)
{
this.name = name;
this.maxHp = maxHp;
hitPoints = maxHp;
attackPoints = ap;
}
public String getName() {
return name;
}
public int getHitPoints() {
return hitPoints;
}
public int getAttackPoints() {
return attackPoints;
}
public void setHitPoints(int hp) {
hitPoints = hp;
}
public void setAttackPoints(int ap) {
attackPoints = ap;
}
public void reset() {
hitPoints = maxHp;
}
public void printStat() {
System.out.println(name + " has " +
hitPoints + " hit points and " + attackPoints + " attack
points");
}
@Override
public boolean equals(Object obj) {
Pokemon other = (Pokemon)
obj;
if
(this.name.equalsIgnoreCase(other.name)) {
return
true;
} else {
return
false;
}
}
}
/********************************Player.java***************************/
import java.util.ArrayList;
import java.util.Iterator;
public class Player {
/* FIXME(1) Add three fields: name, level, pokemons */
private String name;
private int level;
private ArrayList<Pokemon> pokemons;
/*
* FIXME(2) Add a constructor, which takes one
parameter: String n. Initialize
* name to n, level to 0, and create an ArrayList for
pokemons
*/
public Player(String n) {
this.name = n;
this.level = 0;
this.pokemons = new
ArrayList<>();
}
/* FIXME (3) Define the accessor for name */
public String getName() {
return name;
}
/* FIXME (4) Define the accessor for level */
public int getLevel() {
return level;
}
/* FIXME (5) Define the accessor for pokemons */
public ArrayList<Pokemon> getPokemons()
{
return pokemons;
}
/* FIXME (6) Define the mutator for level */
public void setLevel(int level) {
this.level = level;
}
/* FIXME(7) Complete this method by adding p into
the Pokemon ArrayList */
public void addPokemon(Pokemon p) {
pokemons.add(p);
}
/* FIXME(8) Complete this method by removing p from
the Pokemon ArrayList */
public void removePokemon(Pokemon p) {
Iterator<Pokemon> ip =
pokemons.iterator();
while (ip.hasNext()) {
Pokemon pokemon = ip.next();
if (pokemon.equals(pokemon)) {
ip.remove();
}
}
}
public void printStat() {
System.out.println("\nPlayer: " +
name + " at level " + level + ", has " + pokemons.size() + "
pokemons");
/*
* FIXME(9) Complete this method by
printing the stat of each pokemon in the
* Pokemon ArrayList. Each pokemon's
stat is printed in an individual line.
* Hint: loop through the ArrayList
and call the printStat method of Pokemon
* class
*/
for (Pokemon pokemon : pokemons) {
pokemon.printStat();
}
}
}
/*************************************output**********************/
Player: dragonslayer at level 0, has 4 pokemons
Butterfree has 10 hit points and 1 attack points
Ivysaur has 15 hit points and 2 attack points
Pikachu has 20 hit points and 2 attack points
Squirtle has 30 hit points and 3 attack points
Player: voldemort at level 0, has 5 pokemons
Jigglypuff has 12 hit points and 1 attack points
Oddish has 18 hit points and 1 attack points
Gloom has 25 hit points and 2 attack points
Psyduck has 27 hit points and 2 attack points
Machop has 32 hit points and 3 attack points
Let's play!
dragonslayer choose a pokemon to fight
enter 1 to 4 or -1 to stop
1
voldemort choose a pokemon to fight
enter 1 to 5 or -1 to stop
3
Gloom won!
dragonslayer choose a pokemon to fight
enter 1 to 0 or -1 to stop
-1
voldemort choose a pokemon to fight
enter 1 to 6 or -1 to stop
-1
Game over
Player: dragonslayer at level 0, has 0 pokemons
Player: voldemort at level 1, has 6 pokemons
Jigglypuff has 12 hit points and 1 attack points
Oddish has 18 hit points and 1 attack points
Gloom has 24 hit points and 2 attack points
Psyduck has 27 hit points and 2 attack points
Machop has 32 hit points and 3 attack points
Butterfree has 0 hit points and 1 attack points
Please let me know if you have any doubt or modify the answer, Thanks :)