Menu

This is a code that simulates a pokemon battle. Make sure you’re not allowing PokemonTrainers to have too many Pokemon, you’re not allowing Pokemon to have too many Moves, etc. Essentially you should …

Question

This is a code that simulates a pokemon battle. Make sure you’re…

This is a code that simulates a pokemon battle.
Make sure you’re not allowing PokemonTrainers to have too many Pokemon, you’re not allowing Pokemon to have too many Moves, etc. Essentially you should allow to users to make their pokemon, and battle until there is one winner.

Finishing up the simulation:

  • A PokemonTrainer can only have one active Pokemon at a time.
  • When the active Pokemon faints, the PokemonTrainer will use their next Pokemon.
  • When all of a PokemonTrainer’s Pokemon have fainted, the game is over.
  • Allow each PokemonTrainer to take turns choosing a Move for their current Pokemon to use on the opponent Pokemon until one PokemonTrainer loses

You should NOT write in a new file, edit WITHIN PokemonSimulation.java to satisfy the requirements of simulating a pokemon battle. Essentially, allow a user to create two trainers to battle until one wins.

PokemonSimulation.java

public class PokemonSimulation {
    private PokemonImages images = new PokemonImages();
    public void run() {
        System.out.println("Welcome to Pokemon Simulator!");
        System.out.println("Set up first Pokemon Trainer: ");
        String name = readLine("Trainer, what is your name? ");
        System.out.println("Hello " + name + "!");
        System.out.println();
        PokemonTrainer trainer1 = new PokemonTrainer(name);
        trainerSetUp(trainer1);
        System.out.println();
        System.out.println("Set up second Pokemon Trainer: ");
        String name2 = readLine("Trainer 2, what is your name? ");
        System.out.println("Hello " + name2 + "!");
        System.out.println();
        PokemonTrainer trainer2 = new PokemonTrainer(name2);
        trainerSetUp(trainer2);
    }
    private void trainerSetUp(PokemonTrainer t) {
        boolean addNewPokemon = true;
        int pokeNum = 2;

        for (int i = 0; i < pokeNum; i++) {
            if (i == 0) {
                readLine("Choose your first pokemon: ");
                String pokeName = readLine("Enter the name of your pokemon: ");
                Pokemon p = new Pokemon(pokeName);
                String pokeImage = images.getPokemonImage(pokeName);
                System.out.println("You chose:n " + pokeImage);
                System.out.println(pokeName + " (Health: 100/100)");

                int maxMoves = 4;
                for (int y = 0; y < maxMoves; y++) {
                    System.out.println();
                    String addMove = readLine("Would you like to teach " + pokeName + " a new move? (yes/no) ");
                    if (addMove.equals("yes")) {
                        String moveName = readLine("Enter the name of the move: ");
                        String damage = readLine("How much damage does this move do? (Max damage = 25) ");
                        System.out.println(pokeName + " learned " + moveName + " (" + damage + " Damage)!");
                    } else if (addMove.equals("no")) {
                        y = maxMoves;
                    }
                }
                System.out.println(pokeName + " has learned all of their moves");
 
            } else {
                readLine("Choose your second pokemon: ");
                String pokeName = readLine("Enter the name of your pokemon: ");
                Pokemon p = new Pokemon(pokeName);
                String pokeImage = images.getPokemonImage(pokeName);
                System.out.println("You chose:n " + pokeImage);
                System.out.println(pokeName + " (Health: 100/100)");

                int maxMoves = 4;
                for (int m = 0; m < maxMoves; m++) {
                    System.out.println();
                    String addMove = readLine("Would you like to teach " + pokeName + " a new move? (yes/no) ");
                    if (addMove.equals("yes")) {
                        String moveName = readLine("Enter the name of the move: ");
                        String damage = readLine("How much damage does this move do? (Max damage = 25) ");
                        System.out.println(pokeName + " learned " + moveName + " (" + damage + " Damage)!");
                    } else if (addMove.equals("no")) {
                        m = maxMoves;
                        System.out.println(pokeName + " has learned all of their moves");
                    }
                }
            }
        }
    }
}

 

You will need the below code for compilation, DO NOT edit the below code

 

PokemonTrainer.java

import java.util.ArrayList;

public class PokemonTrainer {
	
	// private constants
    private static final int MAX_POKEMON = 2;
    
    // attributes
    private String name;
    private ArrayList<Pokemon> pokemons = new ArrayList<Pokemon>();
    
	public PokemonTrainer(String name) {
		this.name = name;
	}
	
	/**
	 * Adds Pokemon p to the PokemonTrainer's collection of Pokemon.
	 * a player is only allowed MAX_POKEMON pokemons, so it returns true if a pokemon
	 * has been added and false otherwise
	 * @return true if a pokemon has been succesfully added
	 * @return false if a pokemon cannot be added
	 */
	public boolean addPokemon(Pokemon p) {
		if (pokemons.size() < MAX_POKEMON) {
			pokemons.add(p);
			return true;
		}
		return false;
	}

	/**
	 * Returns true if all of the PokemonTrainer's Pokemon
	 * have fainted, false otherwise.
	 * @return true if all pokemon of a trainer has fainted
	 * @return false if all pokemon of a trainer haven't fainted 
	 
	 */
	public boolean hasLost() {
		for (Pokemon pokemon : pokemons) 
			if (!pokemon.hasFainted())
				return false;
		return true;
	}

	/**
	 * Returns the first Pokemon that has not yet fainted
	 * from this PokemonTrainer's collection of Pokemon.
	 * If every Pokemon has fainted, this method returns null.
	 * @return first pokemon that hasn't fainted in collection
	 * @return null if all pokemon have fainted 
	 */
	public Pokemon getNextPokemon() {
		for (Pokemon pokemon : pokemons) 
			if (!pokemon.hasFainted())
				return pokemon;
		return null;
	}
	public String toString() {
		return this.name;
	}
	
}

 

PokemonImages.java

import java.util.*;
import java.io.*;

// This is the PokemonImages class
// It acts as a dictionary to get ASCII art for a given pokemon name
// Feel free to explore this code, it uses FileReader and BufferedReader
// to read in Pokemon images from the "pokemonImages.txt" file.
// DO NOT CHANGE THIS CODE, the program may not work if you do.
public class PokemonImages {
	private HashMap<String, String> pokedex;
	private static final String IMAGE_FILE = "pokemonImages.txt";
	
	public PokemonImages()
	{
		pokedex = new HashMap<String, String>();
		loadImages(IMAGE_FILE);
	}
	
	public String getPokemonImage(String name)
	{		
		return pokedex.get(name.toLowerCase());
	}
	
	public String getPokemonImage(Pokemon pokemon)
	{
		String name = pokemon.getName().toLowerCase();
		return pokedex.get(name);
	}
	
	public void setPokemonImage(String pokemonName, String image)
	{
		if(pokemonName != null && image != null)
		{
			pokedex.put(pokemonName, image);
		}
	}
	
	private void loadImages(String filename)
	{
	    // Try to do the following code, but there may be errors
	    // when reading the file.
		try {
			
			// Create BufferedReader to read from the file where the 
			// ASCII Art is stored
			BufferedReader input = new BufferedReader(new FileReader(filename));
			String line = input.readLine();
			String currentImage = "";
			String currentPokemon = null;
			
			// Read each line of the file and add each pokemon name and image 
			// to the HashMap
			while(line != null)
			{
				// Comment line, we can skip it
				if(line.startsWith("$$"))
				{
					line = input.readLine();
					continue;
				}
				
				// The "##" indicates a new pokemon name
				// Store the old pokemon if there was one and start a new pokemon
				else if(line.startsWith("##"))
				{
					if(currentPokemon != null)
					{
						pokedex.put(currentPokemon, currentImage);
					}
					
					// Get the name of the new pokemon
					currentPokemon = line.substring(2).toLowerCase();
					currentImage = "";
				}
				
				else
				{
					currentImage += line + "n";
				}
				
				line = input.readLine();
			}
		
			// Fencepost situation
			// The last pokemon has not been added yet, add it to the HashMap
			if(currentPokemon != null)
			{
				pokedex.put(currentPokemon, currentImage);
			}
			
			input.close();
		} 
		
		// If there were any errors when reading the file
		// they will be handled by these `catch` clauses
		catch (FileNotFoundException e) {
			System.out.println("Couldn't open file: " + filename);
		} catch (IOException e) {
			System.out.println("There was an error while reading the file: " + filename);
			e.printStackTrace();
		}
	}
}

 

Pokemon.java

import java.util.ArrayList;

public class Pokemon
{
	/**
	 * instance variables
	 */
	String pokemonName;
	int health;
	ArrayList<Move> moves = new ArrayList<Move>();
	
	/**
	 * stores image
	 */
	String image;
	
	/**
	 * Sets pokemon name and health
	 */
	Pokemon(String name)
	{
		this.pokemonName = name;
		this.health = 100;
	}
	
	/**
	 * constructor with image
	 */
	Pokemon(String name, String image)
	{
		this(name);
		this.image = image;
	}

	/**
	 * Getter for the image
	 * @return image
	 */
	public String getImage() 
	{
		return image;
	}

	/**
	 * setter for image
	 */
	public void setImage(String image) 
	{
		this.image = image;
	}

	void addMoves(Move other)
	{
		moves.add(other);
		System.out.println("Move Added to collection");
	}

	/**
	 * Returns the name of the Pokemon
	 * @return pokemonName
	 */
	public String getName()
	{
		return this.pokemonName;
	}
	/**
	 * Returns how much health this Pokemon has
	 * @return int health
	 */
	public int getHealth()
	{
		return this.health;
	}
	/**
	 * Returns true if this Pokemon has fainted,
	 * False otherwise. A Pokemon faints when their health is
	 * less than or equal to 0
	 * @return true if fainted
	 * @return false if not fainted 
	 */
	public boolean hasFainted()
	{
		if(this.health <= 0)
		{
			return true;
		}
		else return false;
	}
	/**
	 * A Pokemon can only know 4 Moves. This method 
	 * returns true if this Pokemon can still learn more
	 * Moves, returns false otherwise.
	 * @return true if pokemon knows less than 4 moves
	 * @return false if pokemon knows 4 moves
	 */
	public boolean canLearnMoreMoves()
	{
		if(this.moves.size() < 4)
		{
			return true;
		}
		else return false; 
	}

	/**
	 * Adds the Move `move` to the collection of Moves
	 * that this Pokemon knows. Returns true if the Move was 
	 * successfully added. Returns false if the Pokemon already
	 * knew 4 Moves and couldn't learn a new one.
	 * @return true if move succesfully added
	 * @return false otherwise 
	 */
	public boolean learnMove(Move move)
	{
		if(this.canLearnMoreMoves())
		{
			addMoves(move);
			return true;
		}
		else return false;
	}

	/**
	 * Remove the Move `move` from this Pokemon's
	 * collection of Moves, if it's there.
	 * @param Move move
	 */
	public void forgetMove(Move move)
	{
		if (this.moves.remove(move))
			System.out.println("Move removedn");
	}

	/**
	 * Return a String containing the name and health
	 * of this Pokemon
	 * Also returns the image if it exists
	 * Example: "Pikachu (Health: 85 / 100)"
	 * @return String name + health + image(if exists) of pokemon
	 */
	public String toString()
	{
		return ((this.image != null)?this.image:"") + this.pokemonName + "(Health:" + this.health + "/100)";
	}
	
	
	/**
	 * Returns an ArrayList of all the Moves this Pokemon knows
	 */
	public ArrayList<Move> getMoves() 
	{
		return this.moves;
	}

	/**
	 * Returns true if the Pokemon knows this move (has this Move in its collection)
	 * false otherwise
	 * @return true if pokemon knows this move
	 * @return false otherwise
	 * @param Move move
	 */
	public boolean knowsMove(Move move) 
	{
		return this.moves.contains(move);
	}

	/**
	 * Returns true if the Pokemon knows a Move with 
	 * the name `moveName`, false otherwise
	 * @return true if pokemon knows move with movename
	 * @return false if else 
	 * @param String moveName
	 */
	public boolean knowsMove(String moveName)
	{
		for (Move move: moves) {
			if (move.getName().equalsIgnoreCase(moveName))
				return true;
		}
		return false;
	}

	/**
	 * Tries to perform the Move `move` on the Pokemon `other`
	 * A Pokemon can only use a Move if it knows the Move
	 * An attack should subtract the damage of the Move 
	 * from the health of the opponent.
	 * Returns true if the Pokemon knew the Move and used it
	 * Returns false otherwise.
	 * @param Pokemon opponent
	 * @param Move move
	 */
	public boolean attack(Pokemon opponent, Move move) 
	{

		if (knowsMove(move))
		{
			move = moves.get(moves.indexOf(move));
			opponent.health -= move.getDamage();
			return true;
		}
		return false;
	}

	/**
	 * Tries to perform a Move on a Pokemon
	 * A Pokemon can only use a Move if it knows the Move
	 * An attack should subtract the damage of the Move 
	 * from the health of the opponent.
	 * Returns true if the Pokemon knew the Move and used it
	 * Returns false otherwise.
	 * @param Pokemon opponent
	 * @param String moveName
	 */
	public boolean attack(Pokemon opponent, String moveName)	
	{
		for (Move move: moves) {
			if (move.getName().equalsIgnoreCase(moveName))
				return attack(opponent, move);
		}
		return false;
	}
	
}

 

Move.java

public class Move
{
    /**
     * Private constants to represent the max damage, the name, and the amount of damage
     */
    private static final int MAX_DAMAGE = 25;
    private String name;
    private int damage;
    
    public Move(String name, int damage) {
        this.name = name;
        this.damage = Math.min(damage, MAX_DAMAGE);
    }
    /**
     * This method returns the name of the move
     * @return string name
     */
    public String getName() {
        return name;
    }
    /**
     * This method returns the damage of the move
     * @return int damage
     */
    public int getDamage() {
        return damage;
    }
    /**
     * This method returns a String of the move
     * @return String name of move, + damage dealt.
     */
    public String toString() {
        return getName() + " (" + getDamage() + " Damage)";
    }
}

OR

Leave a Reply

Your email address will not be published. Required fields are marked *