Question

In: Computer Science

You will extend last week's workshop activity by adding a Player class. Each player has a...

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.

  • Pokemon.java - Class definition (completed)
  • Player.java - Class definition
  • PokemonField.java - Contains main() method
  1. Complete the Player class with the following specifications:

    (1) add the following three private fields

    • String name
    • int level
    • ArrayList pokemons

    (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

    • getName()
    • getLevel()
    • getPokemons()
    • setLevel(int level)
    • addPokemon(Pokemon p)
    • removePokemon(Pokemon p)
    • printStat()
  2. 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*/

}   
}

Solutions

Expert Solution

/*******************************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 :)


Related Solutions

Extend the custom unorderedLinkedList class by adding the following operation: Find and delete the node with...
Extend the custom unorderedLinkedList class by adding the following operation: Find and delete the node with the smallest info in the list. Delete only the first occurrence and traverse the list only once. The function's prototype is deleteSmallest() use this code #include <ctime> #include <iostream> #include <random> #include "unorderedLinkedList.h" using namespace std; int main() { default_random_engine e(1918); uniform_int_distribution<int> u(10, 99); int size = 12; unorderedLinkedList ul; while (ul.getnelts() < size) { int elt = u(e); ul.append(elt); ul.show(); cout << endl;...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
Extend the program by adding rules for the following family relationships (add more facts as you...
Extend the program by adding rules for the following family relationships (add more facts as you need ). Rules father(X.Y) > Father (z,w) where X is Y's father father(X,Y):>male(X),Parent(X, Y). brother(X, Y): where X is Y's brother brother(X,Y):-male().praent(X,Y). Brother (y.w) sister(X,Y):-famale(X),praent(X, Y). sister(X, Y): where X is Y's sister Sister(w.y) son(X, Y)> son(X,Y):-male(X), praent(Y,X). where X is Y's son Son(y.z) daughter(X, Y) :- Daughter (w,x) where X is Y's daughter daughter(X,Y):-famale(X), praent(Y.X). where X and Y are sibling Sibling(X, Y):-praent(X,Y),...
In this assignment, which continues to build on last week's assignment, you will create a mind...
In this assignment, which continues to build on last week's assignment, you will create a mind map identifying the steps in the training process for a CRM program to help you understand the important role training plays in an organization. It further helps you understand the importance of training as you explore how training is designed, delivered, and measured for success I need more info on what a CRM is and Which training evaluation methods would you recommend to assess...
Below is a game between player A and player B. Each player has two possible strategies:...
Below is a game between player A and player B. Each player has two possible strategies: 1 or 2. The payoffs for each combination of strategies between A and B are in the bracket. For example, if A plays 1 and B plays 1, the payoff for A is -3, and the payoff for B is -2. Player B Strategy 1 Strategy 2 Player A Strategy 1 (-3,-2) (10,0) Strategy 2 (0,8) (0,0) How many pure strategy Nash equilibria does...
Below is a game between player A and player B. Each player has two possible strategies:...
Below is a game between player A and player B. Each player has two possible strategies: 1 or 2. The payoffs for each combination of strategies between A and B are in the bracket. For example, if A plays 1 and B plays 1, the payoff for A is 1, and the payoff for B is 0. Player B Strategy 1 Strategy 2 Player A Strategy 1 (1,0) (0,1) Strategy 2 (0,1) (1,0) How many pure strategy Nash equilibria does...
This week, you will use two of the data sets that were posted during last week's...
This week, you will use two of the data sets that were posted during last week's discussion, as follows: 1) Refer to the data set that you posted last week (high temperatures for your area during the month of June 2019) and 2) Refer to the data set that one of your classmates posted last week (high temperatures for their area during the month of June 2019). Use these data sets to test the claim that the average high temperature...
This week, you will use two of the data sets that were posted during last week's...
This week, you will use two of the data sets that were posted during last week's discussion, as follows: 1) Refer to the data set that you posted last week (high temperatures for your area during the month of June 2019) and 2) Refer to the data set that one of your classmates posted last week (high temperatures for their area during the month of June 2019). Use these data sets to test the claim that the average high temperature...
Use the same organization as in previous week's projects. Last week, you completed the strengths, weaknesses,...
Use the same organization as in previous week's projects. Last week, you completed the strengths, weaknesses, opportunities, and threats (SWOT) analysis for your organization. Hopefully, you have begun researching potential social causes, issues, or nonprofits for your company to adopt. As you consider your options, select three or four possible candidates (social causes) and evaluate whether the alternatives you come across fit with your company's mission, vision, and ethical framework, as well as any existing social responsibility efforts. Finally, select...
For each audit activity, identify the audit procedure. Each activity has one answer, but the audit...
For each audit activity, identify the audit procedure. Each activity has one answer, but the audit procedures can be used more than once.       -       A.       B.       C.       D.       E.       F.       G.       H.       I.       J.    Review lease agreements for capital leases.       -       A.       B.       C.       D.      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT