Question: Please in the below code fix the spots that say fix. If you could keep the other code the same I would appreciate it because
Please in the below code fix the spots that say fix. If you could keep the other code the same I would appreciate it because it relies on other code. Thanks!!
__________________________________________________________-
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 enter 1 to " + player1.getPokemons().size() + " or -1 to stop"); index1 = scnr.nextInt(); System.out.println(player2.getName() + " choose a pokemon to fight enter 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 enter 1 to " + player1.getPokemons().size() + " or -1 to stop"); index1 = scnr.nextInt(); System.out.println(player2.getName() + " choose a pokemon to fight enter 1 to " + player2.getPokemons().size() + " or -1 to stop"); index2 = scnr.nextInt(); } } }
__________________________________________________________________
import java.util.ArrayList; public class Player { /*FIXME(1) Add three fields: name, level, pokemons*/ private String name; private int level; private ArrayList
/*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 name) { this.name = name; this.level = 0; this.pokemons = new ArrayList
public int getLevel() { return level; } /*FIXME (5) Define the accessor for pokemons*/
public ArrayList
/*FIXME(7) Complete this method by adding p into the Pokemon ArrayList*/ public void addPokemon(Pokemon p){ this.pokemons.add(p); }
/*FIXME(8) Complete this method by removing p from the Pokemon ArrayList*/ public void removePokemon(Pokemon p){ pokemons.remove(p); }
public void printStat(){ System.out.println(" Player: " + 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: You can call the printStat method in Pokemon class*/
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
