Revision: 71346
Updated Code
at March 5, 2017 14:36 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.InputMismatchException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import java.lang.InterruptedException; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); URL sidewaysBridgeURL = new URL ("http://oi68.tinypic.com/30w9ie8.jpg"); BufferedImage sidewaysBridge = ImageIO.read(sidewaysBridgeURL); URL pokeCenterURL = new URL ("http://oi66.tinypic.com/ip06le.jpg"); BufferedImage pokeCenter = ImageIO.read(pokeCenterURL); URL pokeMartURL = new URL ("http://oi68.tinypic.com/149vazd.jpg"); BufferedImage pokeMart = ImageIO.read(pokeMartURL); URL smallHorizontalFenceURL = new URL ("http://oi64.tinypic.com/30l29vs.jpg"); BufferedImage smallHorizontalFence = ImageIO.read(smallHorizontalFenceURL); URL smallVerticalFenceURL = new URL ("http://oi64.tinypic.com/30l29vs.jpg"); BufferedImage smallVerticalFence = ImageIO.read(smallVerticalFenceURL); URL mainBackWalk1URL = new URL("http://oi66.tinypic.com/2ebwl8w.jpg"); BufferedImage mainBackWalk1 = ImageIO.read(mainBackWalk1URL); URL mainBackWalk2URL = new URL("http://oi67.tinypic.com/96j3wm.jpg"); BufferedImage mainBackWalk2 = ImageIO.read(mainBackWalk2URL); URL mainFrontWalk1URL = new URL("http://oi68.tinypic.com/w0mhqb.jpg"); BufferedImage mainFrontWalk1 = ImageIO.read(mainFrontWalk1URL); URL mainFrontWalk2URL = new URL("http://oi67.tinypic.com/2m7vpsn.jpg"); BufferedImage mainFrontWalk2 = ImageIO.read(mainFrontWalk2URL); URL mainLeftWalk1URL = new URL("http://oi63.tinypic.com/34r7fgy.jpg"); BufferedImage mainLeftWalk1 = ImageIO.read(mainLeftWalk1URL); URL mainLeftWalk2URL = new URL("http://oi66.tinypic.com/207xffm.jpg"); BufferedImage mainLeftWalk2 = ImageIO.read(mainLeftWalk2URL); URL mainRightWalk1URL = new URL("http://oi68.tinypic.com/14dzsr5.jpg"); BufferedImage mainRightWalk1 = ImageIO.read(mainRightWalk1URL); URL mainRightWalk2URL = new URL("http://oi64.tinypic.com/2zjhv11.jpg"); BufferedImage mainRightWalk2 = ImageIO.read(mainRightWalk2URL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; int earnings = 0; boolean shiftActivated = false; static int round = 1; static String trainerName = ""; boolean hasReadFile = false; boolean displayMain = true; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; //-180 is to skip to the end int up = -318+tD; final int left = 15; int right = bridge.getWidth() - 15; int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(600-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -318+tD, this); for(int y = 1; y <= 28; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); } boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 600-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 600-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } for(int x = 0; x < 5; x++) { g.drawImage(sidewaysBridge, 67+sidewaysBridge.getWidth()*x -8*x, 620 +tD, this); } for(int x = 0; x < 21; x++) { g.drawImage(smallHorizontalFence, 69+8*x, 650+tD, this); } for(int x = 0; x < 3; x++) { g.drawImage(smallVerticalFence, 73, 648 - 10*x + tD, this); } //g.drawImage() g.drawImage(pokeMart, 80, 598+tD, this); g.drawImage(pokeCenter, 150, 590+tD, this); if(displayMain) { g.drawImage(main, x1, y1+tD, this); } } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488652418722.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException, InterruptedException { if(!hasReadFile) { readInitFile(); hasReadFile = true; } setFocusable(false); removeParty(oppParty); System.out.println("\nROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); int didWin = battle(myParty, oppParty, trainerName, myBag, true); resetParty(myParty); resetParty(oppParty); if(didWin == 1) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println("\nYour Party has been healed.\n"); System.out.println("\nRecord: " + wins + " - " + losses); round++; calcEarnings(true, 0); setFocusable(true); System.out.println("\nMove using the arrow keys."); } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { if(walk) { main = mainLeftWalk1; walk = false; } else { main = mainLeftWalk2; walk = true; } repaint(); if(x1 > 72 && x1 <235 && y1 > 650 && y1 < 683) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(x1-1 > left) { x1 = x1-1; repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { if(walk) { main = mainRightWalk1; walk = false; } else { main = mainRightWalk2; walk = true; } if(x1 > 50 && x1 <235 && y1 > 650 && y1 < 680) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(x1+1 < right) { x1 = x1+1; repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} } } if(e.getKeyCode() == KeyEvent.VK_UP) { if(walk) { main = mainBackWalk1; walk = false; } else { main = mainBackWalk2; walk = true; } if(x1 > 63 && x1 <235 && y1 > 650 && y1 < 683) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(y1-1 > up) { y1 = y1-1; if(x1 >= 178 && x1 <= 186 && y1 <= 651) { setFocusable(false); displayMain = false; repaint(); System.out.println("\nWelcome to the Poke Center!"); displayPokeCenter(); displayMain = true; setFocusable(true); System.out.println("\nMove using the arrow keys."); } if(x1 >= 108 && x1 <= 116 && y1 <= 651) { setFocusable(false); displayMain = false; repaint(); System.out.println("\nWelcome to the Poke Mart!"); displayPokeMart(); displayMain = true; setFocusable(true); System.out.println("\nMove using the arrow keys."); } repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) {} catch (IOException ex) {} } if(y1 -1 < -318 + tD) { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { if(walk) { main = mainFrontWalk1; walk = false; } else { main = mainFrontWalk2; walk = true; } if(x1 > 63 && x1 <235 && y1 > 650 && y1 < 683) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) {} catch (IOException ex) {} } } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public void checkForWild() throws FileNotFoundException, IOException, InterruptedException { if(y1 >= 718 && y1 <= 1000 && x1 >= 15 && x1 <= 72) { int toHaveWild = (int)(100.0 * Math.random() + 1); if(toHaveWild == 1) { setUpWildPokemon(); } } } public void setUpWildPokemon() throws FileNotFoundException, IOException, InterruptedException { setFocusable(false); if(!hasReadFile) { readInitFile(); hasReadFile = true; } int maxLevel = 0; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getLevel() > maxLevel) { maxLevel = myParty[pSlot].getLevel(); } } } int level = 3 + (int)(Math.random() * ((maxLevel - 3) + 1)); int adder = -1 + (int)(Math.random() * 3); level += adder; int HPMultiplier = 3 + (int)(Math.random() * ((5 - 3) + 1)); int HPAdder = -5 + (int)(Math.random() * ((5 - -5) + 1)); int HP = level * HPMultiplier + HPAdder; determineWildPokemonTier(level, HP); setFocusable(true); System.out.println("\nMove using the arrow keys."); } public void determineWildPokemonTier(int level, int HP) throws FileNotFoundException, IOException, InterruptedException { int tier = 0; double randTierNumber = Math.random(); randTierNumber = (int)randTierNumber * 100 +1; int moves = 0; double randMovesNumber = Math.random(); randMovesNumber = (int)randMovesNumber * 100 +1; if(level <= 10) { tier = 1; randTierNumber = (int)randTierNumber * 100000 +1; if(randTierNumber == 50000) { tier = 4; } if(randMovesNumber <= 50) { moves = 1; } if(randMovesNumber > 50 && randMovesNumber <= 80) { moves = 2; } if(randMovesNumber > 80 && randMovesNumber <= 95) { moves = 3; } if(randMovesNumber > 95) { moves = 4; } } if(level >= 11 && level <= 20) { if(randTierNumber <= 45) { tier = 1; } if(randTierNumber > 45 && randTierNumber <= 90) { tier = 2; } if(randTierNumber > 90) { tier = 3; } randTierNumber = (int)randTierNumber * 10000 +1; if(randTierNumber == 5000) { tier = 4; } if(randMovesNumber <= 10) { moves = 1; } if(randMovesNumber > 10 && randMovesNumber <= 45) { moves = 2; } if(randMovesNumber > 45 && randMovesNumber <= 85) { moves = 3; } if(randMovesNumber > 85) { moves = 4; } } if(level >= 21 && level <= 30) { if(randTierNumber <= 25) { tier = 1; } if(randTierNumber > 25 && randTierNumber <= 70) { tier = 2; } if(randTierNumber > 70) { tier = 3; } randTierNumber = (int)randTierNumber * 5000 +1; if(randTierNumber == 2500) { tier = 4; } if(randMovesNumber <= 20) { moves = 2; } if(randMovesNumber > 20 && randMovesNumber <= 45) { moves = 3; } if(randMovesNumber > 45) { moves = 4; } } if(level >= 31) { if(randTierNumber <= 25) { tier = 2; } if(randTierNumber > 25) { tier = 3; } randTierNumber = (int)randTierNumber * 1000 +1; if(randTierNumber == 500) { tier = 4; } if(randMovesNumber <= 25) { moves = 3; } if(randMovesNumber > 25) { moves = 4; } } if(tier == 4) { level = 60; } determineWildMovesTier(tier, moves, level, HP); } public void determineWildMovesTier(int tier, int moves, int level, int HP) throws FileNotFoundException, IOException, InterruptedException { ArrayList<Integer> wildMoves = new ArrayList<Integer>(); if(tier == 1) { for(int x = 0; x < moves; x++) { double randMovesNumber = Math.random(); randMovesNumber = (int)(randMovesNumber * 100 +1); if(randMovesNumber <= 70) { wildMoves.add(1); } if(randMovesNumber > 70) { wildMoves.add(2); } } } if(tier == 2) { for(int x = 0; x < moves; x++) { double randMovesNumber = Math.random(); randMovesNumber = (int)(randMovesNumber * 100 +1); if(randMovesNumber <= 30) { wildMoves.add(1); } if(randMovesNumber > 30 && randMovesNumber <= 70) { wildMoves.add(2); } if(randMovesNumber > 70) { wildMoves.add(3); } } } if(tier == 3) { for(int x = 0; x < moves; x++) { double randMovesNumber = Math.random(); randMovesNumber = (int)(randMovesNumber * 100 +1); if(randMovesNumber <= 20) { wildMoves.add(1); } if(randMovesNumber > 20 && randMovesNumber <= 50) { wildMoves.add(2); } if(randMovesNumber > 50) { wildMoves.add(3); } } } if(tier == 4) { for(int x = 0; x < moves; x++) { wildMoves.add(3); } } createWildPokemon(tier, wildMoves, level, HP); } public void createWildPokemon(int tier, ArrayList<Integer> wildMoves, int level, int HP) throws FileNotFoundException, IOException, InterruptedException { boolean foundPokemon = false; String line = ""; String name = ""; ArrayList<String> types = new ArrayList<String>(); Pokemon wildPokemon = null; while(!foundPokemon) { URL url = new URL("http://m.uploadedit.com/ba3s/1488652544248.txt"); Scanner sc = new Scanner(url.openStream()).useDelimiter(","); int lineNumber = (int)(273 * Math.random() +1); if(lineNumber % 2 == 0) { lineNumber++; } for(int i = 0; i < lineNumber; i++) { sc.nextLine(); } line = sc.nextLine(); line = ",".concat(line); Scanner sc2 = new Scanner(line).useDelimiter(","); if(Integer.parseInt(sc2.next()) == tier) { name = sc2.next(); while(sc2.hasNext()) { String type = sc2.next(); if(type != "/" || type != "," || type != "/,") { types.add(type); } else { wildPokemon = new Pokemon(name, HP, level, types.get(0)); foundPokemon = true; } } wildPokemon = new Pokemon(name, HP, level, types.get(0)); foundPokemon = true; } } if(foundPokemon) { createWildMoves(tier, types, wildMoves, wildPokemon); } } public void createWildMoves(int tier, ArrayList<String> types, ArrayList<Integer> wildMoves, Pokemon wildPokemon) throws FileNotFoundException, IOException, InterruptedException { for(int x = 0; x < wildMoves.size(); x++) { String moveType = determineWildMoveType(types); ArrayList< String> lines = new ArrayList<String>() ; boolean foundMove = false; String line = ""; int tryCount = 0; URL url = new URL("http://m.uploadedit.com/ba3s/1488652222901.txt"); Scanner sc = new Scanner(url.openStream()).useDelimiter(","); for(int y = 0; y <= 329; y= y+2) { sc.nextLine(); String s = sc.nextLine(); lines.add(s); } while(!foundMove) { int lineNumber = (int)(lines.size() * Math.random()); line = lines.get(lineNumber); line = ",".concat(line); Scanner sc2 = new Scanner(line).useDelimiter(","); String posMoveType = sc2.next(); if(posMoveType.equals(moveType)) { int posTier = sc2.nextInt(); if(tryCount == 2000) { System.out.println("Recalculating..."); posTier -= 1; wildMoves.set(x, wildMoves.get(x)-1); } if(tryCount == 4000) { posTier += 2; wildMoves.set(x, wildMoves.get(x)+2); } if(tryCount == 6000) { moveType = determineWildMoveType(types); posTier -= 1; wildMoves.set(x, wildMoves.get(x)-1); tryCount = 0; } if(posTier == wildMoves.get(x)) { String name = sc2.next(); int PP = sc2.nextInt(); double damageMultiplier = sc2.nextDouble(); int damage = (int)Math.round(damageMultiplier * wildPokemon.getLevel()); int accuracy = sc2.nextInt(); int speed = sc2.nextInt(); Move newMove = new Move(name, PP, damage, accuracy, speed, moveType); wildPokemon.addMove(newMove, x); foundMove = true; } tryCount++; } } } initiateWildBattle(wildPokemon); } public void initiateWildBattle(Pokemon wildPokemon) throws IOException, FileNotFoundException, InterruptedException { if(!hasReadFile) { readInitFile(); } resetParty(oppParty); oppParty[0] = wildPokemon; int result = battle(myParty, oppParty, trainerName, myBag, false); if(result == 1) { calcEarnings(false, wildPokemon.getLevel()); } resetParty(oppParty); } public static String determineWildMoveType(ArrayList<String> types) { int n = types.size(); double total = 0; for(int i = 0; i < n; i++) { total += Math.pow(2,i); } double magicNum = 100/total; double ogMN = magicNum; int randNum = (int)(100.0 * Math.random() + 1); double sum = 0; for(int i = 0; i < n; i++) { if(randNum <= magicNum) { System.out.println("\n"+types.get(n - i -1)+"\n"); return types.get(n - i -1); } else { magicNum += Math.pow(2,i+1)*ogMN; } } return types.get(0); } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.nextLine()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println("\nMove using the arrow keys."); } public void readInitFile() throws FileNotFoundException, MalformedURLException, IOException { for(int x = 0; x < 100; x++) { Item masterBall = new Ball("Master Ball", 100000, 0); Item ultraBall = new Ball("Ultra Ball", 50, 0); Item greatBall = new Ball("Great Ball", 40, 0); Item pokeBall = new Ball("Poke Ball", 30, 0); myBag.add(masterBall); myBag.add(ultraBall); myBag.add(greatBall); myBag.add(pokeBall); } File f = new File("/Users/bensauberman/Desktop/PokemonSprites/MyPokeMonFile.txt"); Scanner fr = new Scanner(f).useDelimiter(","); //URL url = new URL("http://m.uploadedit.com/ba3s/1488652275683.txt"); //Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); String info = fr.nextLine(); addMove(info, myParty); } if(vL.charAt(0) == ('/')) { fr.nextLine(); } } } public int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488652045394.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return lineCounter; } if(vL.charAt(0) != ('|') ) { fr.nextLine(); lineCounter++; } } return lineCounter; } public static void printParty(Pokemon party[], boolean showNumbers) { if(showNumbers) { System.out.println("Pokemon HP Level Type"); } else { System.out.println("Pokemon HP Level Type"); } for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = 0; if(showNumbers) { spaceCount = (16 - party[x].getName().length()); } else { spaceCount = (14 - party[x].getName().length()); } StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } if(showNumbers) { System.out.print(x+1 + ": " + party[x].getName() + spaces + party[x].getnHP()); } else { System.out.print(party[x].getName() + spaces + party[x].getnHP()); } if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().toUpperCase().equals(pName.toUpperCase())){ party[pSlot] = newPokemon; break; } } } public void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().toUpperCase().equals(pName.toUpperCase())) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public int battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, boolean isTrainer) throws FileNotFoundException, IOException, InterruptedException { if(isTrainer) { System.out.println("\n\nWelcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); } if(!isTrainer) { System.out.println("A wild pokemon appeared."); System.out.println("The pokemon is a level " + oppParty[0].getLevel() + " " + oppParty[0].getName()); } Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println("\n" + trainerName + "'s Party: "); printParty(myParty, false); System.out.println("\nOpponent's Party: "); printParty(oppParty, false); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println("\nWhat would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } if(mSlot == -1) { validMove = false; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } System.out.println("That Pokemon is already in battle."); } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty, oppParty, isTrainer); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option.\n"); } } if(userChoice == 4) { boolean didRun = run(isTrainer); if(didRun) { validMove = true; return 2; } } } //OPPONENTS OPTIONS if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return 1; //WIN } boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println("\n---------------------------"); System.out.println("The opponent sends out " + oppPokemon); System.out.println("---------------------------"); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println("\nThe opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, isTrainer); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return 0; //LOSE } return 1; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, boolean isTrainer) throws FileNotFoundException, IOException, InterruptedException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().toUpperCase().equals(myPokemon.getMoves()[x].getName().toUpperCase())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().toUpperCase().equals(oppPokemon.getMoves()[x].getName().toUpperCase())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, isTrainer); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, isTrainer); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, isTrainer); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, isTrainer); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, isTrainer); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, isTrainer); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, boolean isTrainer) throws FileNotFoundException, IOException, InterruptedException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); System.out.println("---------------------------"); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon, isTrainer); Thread.sleep(750); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } System.out.println("---------------------------"); Thread.sleep(750); return true; } } else { System.out.println("---------------------------"); System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); System.out.println("---------------------------"); Thread.sleep(750); return true; } System.out.println("---------------------------"); Thread.sleep(750); return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty, true); boolean isValid = false; while(!isValid) { System.out.println("\nWhich Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); System.out.println(); printParty(myParty, true); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().toUpperCase().equals(requested.toUpperCase()) || Integer.parseInt(requested)-1 == x) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().toUpperCase().equals(requested.toUpperCase()) && myPokemon.getnHP() > 0) { return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println("\nThe opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println("\nPick a move. Press '0' to Exit."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = -1; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(requestedMoveWord.equals("0")) { return -1; } if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); int ogEarnings = earnings; earnings = earnings/2; System.out.println("You have been sent to the Poke Center to reheal.\n Your Earnings have" + " been cut from " + ogEarnings + " to " + earnings); x1 = 182; y1 = 651; main = mainFrontWalk1; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); System.out.println("4: Run Away."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); try { userChoice = s.nextInt(); } catch(InputMismatchException e) { System.out.println("Please enter a valid option."); break; } if(userChoice < 1 || userChoice > 4) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon, boolean isTrainer) throws InterruptedException { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); int divider = 1; if(!isTrainer) { divider = 2; } attPokemon.addEXP(((defPokemon.getLevel() * 10)+5) / divider); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); Thread.sleep(1300); System.out.println("---------------------------"); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); Thread.sleep(1300); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP() + "\n"); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); Thread.sleep(2000); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); System.out.println("---------------------------"); } public void calcEarnings(boolean isTrainer, int level) { int ogEarnings = earnings; if(isTrainer) { int r = (int)(Math.random() * 30 + 1); earnings += r * 10 * round; } else { int r = (int)(Math.random() * 20 + 1); earnings += r * level; } System.out.println("\nEarnings increased from " + ogEarnings + " to " + earnings +"\n"); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println("\nDefeated Opponent's Party:"); printParty(oppParty, false); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println("\n1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.toUpperCase().equals("Add/Replace Pokemon".toUpperCase())) { userChoice = 1; } if(userChoiceWord.toUpperCase().equals("Receive Random Item".toUpperCase())) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty, true); } if(userChoice == 2) { rewardChosen = takeItem(myBag); } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[], boolean isTrainer) { boolean hasTaken = false; while(!hasTaken) { if(!isTrainer) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[0]; System.out.println(oppParty[0].getName() + " has been successfully added to your party."); return true; } } return replacePokemon(0, myParty, oppParty); } else { printParty
Revision: 71345
Updated Code
at March 5, 2017 14:34 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.InputMismatchException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import java.lang.InterruptedException; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); URL sidewaysBridgeURL = new URL ("http://oi68.tinypic.com/30w9ie8.jpg"); BufferedImage sidewaysBridge = ImageIO.read(sidewaysBridgeURL); URL pokeCenterURL = new URL ("http://oi66.tinypic.com/ip06le.jpg"); BufferedImage pokeCenter = ImageIO.read(pokeCenterURL); URL pokeMartURL = new URL ("http://oi68.tinypic.com/149vazd.jpg"); BufferedImage pokeMart = ImageIO.read(pokeMartURL); URL smallHorizontalFenceURL = new URL ("http://oi64.tinypic.com/30l29vs.jpg"); BufferedImage smallHorizontalFence = ImageIO.read(smallHorizontalFenceURL); URL smallVerticalFenceURL = new URL ("http://oi64.tinypic.com/30l29vs.jpg"); BufferedImage smallVerticalFence = ImageIO.read(smallVerticalFenceURL); URL mainBackWalk1URL = new URL("http://oi66.tinypic.com/2ebwl8w.jpg"); BufferedImage mainBackWalk1 = ImageIO.read(mainBackWalk1URL); URL mainBackWalk2URL = new URL("http://oi67.tinypic.com/96j3wm.jpg"); BufferedImage mainBackWalk2 = ImageIO.read(mainBackWalk2URL); URL mainFrontWalk1URL = new URL("http://oi68.tinypic.com/w0mhqb.jpg"); BufferedImage mainFrontWalk1 = ImageIO.read(mainFrontWalk1URL); URL mainFrontWalk2URL = new URL("http://oi67.tinypic.com/2m7vpsn.jpg"); BufferedImage mainFrontWalk2 = ImageIO.read(mainFrontWalk2URL); URL mainLeftWalk1URL = new URL("http://oi63.tinypic.com/34r7fgy.jpg"); BufferedImage mainLeftWalk1 = ImageIO.read(mainLeftWalk1URL); URL mainLeftWalk2URL = new URL("http://oi66.tinypic.com/207xffm.jpg"); BufferedImage mainLeftWalk2 = ImageIO.read(mainLeftWalk2URL); URL mainRightWalk1URL = new URL("http://oi68.tinypic.com/14dzsr5.jpg"); BufferedImage mainRightWalk1 = ImageIO.read(mainRightWalk1URL); URL mainRightWalk2URL = new URL("http://oi64.tinypic.com/2zjhv11.jpg"); BufferedImage mainRightWalk2 = ImageIO.read(mainRightWalk2URL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; int earnings = 0; boolean shiftActivated = false; static int round = 1; static String trainerName = ""; boolean hasReadFile = false; boolean displayMain = true; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; //-180 is to skip to the end int up = -318+tD; final int left = 15; int right = bridge.getWidth() - 15; int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(600-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -318+tD, this); for(int y = 1; y <= 28; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); } boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 600-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 600-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } for(int x = 0; x < 5; x++) { g.drawImage(sidewaysBridge, 67+sidewaysBridge.getWidth()*x -8*x, 620 +tD, this); } for(int x = 0; x < 21; x++) { g.drawImage(smallHorizontalFence, 69+8*x, 650+tD, this); } for(int x = 0; x < 3; x++) { g.drawImage(smallVerticalFence, 73, 648 - 10*x + tD, this); } //g.drawImage() g.drawImage(pokeMart, 80, 598+tD, this); g.drawImage(pokeCenter, 150, 590+tD, this); if(displayMain) { g.drawImage(main, x1, y1+tD, this); } } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488652418722.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException, InterruptedException { if(!hasReadFile) { readInitFile(); hasReadFile = true; } setFocusable(false); removeParty(oppParty); System.out.println("\nROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); int didWin = battle(myParty, oppParty, trainerName, myBag, true); resetParty(myParty); resetParty(oppParty); if(didWin == 1) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println("\nYour Party has been healed.\n"); System.out.println("\nRecord: " + wins + " - " + losses); round++; calcEarnings(true, 0); setFocusable(true); System.out.println("\nMove using the arrow keys."); } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { if(walk) { main = mainLeftWalk1; walk = false; } else { main = mainLeftWalk2; walk = true; } repaint(); if(x1 > 72 && x1 <235 && y1 > 650 && y1 < 683) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(x1-1 > left) { x1 = x1-1; repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { if(walk) { main = mainRightWalk1; walk = false; } else { main = mainRightWalk2; walk = true; } if(x1 > 50 && x1 <235 && y1 > 650 && y1 < 680) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(x1+1 < right) { x1 = x1+1; repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} } } if(e.getKeyCode() == KeyEvent.VK_UP) { if(walk) { main = mainBackWalk1; walk = false; } else { main = mainBackWalk2; walk = true; } if(x1 > 63 && x1 <235 && y1 > 650 && y1 < 683) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(y1-1 > up) { y1 = y1-1; if(x1 >= 178 && x1 <= 186 && y1 <= 651) { setFocusable(false); displayMain = false; repaint(); System.out.println("\nWelcome to the Poke Center!"); displayPokeCenter(); displayMain = true; setFocusable(true); System.out.println("\nMove using the arrow keys."); } if(x1 >= 108 && x1 <= 116 && y1 <= 651) { setFocusable(false); displayMain = false; repaint(); System.out.println("\nWelcome to the Poke Mart!"); displayPokeMart(); displayMain = true; setFocusable(true); System.out.println("\nMove using the arrow keys."); } repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) {} catch (IOException ex) {} } if(y1 -1 < -318 + tD) { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { if(walk) { main = mainFrontWalk1; walk = false; } else { main = mainFrontWalk2; walk = true; } if(x1 > 63 && x1 <235 && y1 > 650 && y1 < 683) { right = 235; down = 677; up = 650; } else { right = bridge.getWidth() - 15; down = 780; up = -318+tD; } if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForWild(); } catch (FileNotFoundException ex) {} catch (IOException ex) {} catch (InterruptedException ex) {} try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) {} catch (IOException ex) {} } } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public void checkForWild() throws FileNotFoundException, IOException, InterruptedException { if(y1 >= 718 && y1 <= 1000 && x1 >= 15 && x1 <= 72) { int toHaveWild = (int)(100.0 * Math.random() + 1); if(toHaveWild == 1) { setUpWildPokemon(); } } } public void setUpWildPokemon() throws FileNotFoundException, IOException, InterruptedException { setFocusable(false); if(!hasReadFile) { readInitFile(); hasReadFile = true; } int maxLevel = 0; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getLevel() > maxLevel) { maxLevel = myParty[pSlot].getLevel(); } } } int level = 3 + (int)(Math.random() * ((maxLevel - 3) + 1)); int adder = -1 + (int)(Math.random() * 3); level += adder; int HPMultiplier = 3 + (int)(Math.random() * ((5 - 3) + 1)); int HPAdder = -5 + (int)(Math.random() * ((5 - -5) + 1)); int HP = level * HPMultiplier + HPAdder; determineWildPokemonTier(level, HP); setFocusable(true); System.out.println("\nMove using the arrow keys."); } public void determineWildPokemonTier(int level, int HP) throws FileNotFoundException, IOException, InterruptedException { int tier = 0; double randTierNumber = Math.random(); randTierNumber = (int)randTierNumber * 100 +1; int moves = 0; double randMovesNumber = Math.random(); randMovesNumber = (int)randMovesNumber * 100 +1; if(level <= 10) { tier = 1; randTierNumber = (int)randTierNumber * 100000 +1; if(randTierNumber == 50000) { tier = 4; } if(randMovesNumber <= 50) { moves = 1; } if(randMovesNumber > 50 && randMovesNumber <= 80) { moves = 2; } if(randMovesNumber > 80 && randMovesNumber <= 95) { moves = 3; } if(randMovesNumber > 95) { moves = 4; } } if(level >= 11 && level <= 20) { if(randTierNumber <= 45) { tier = 1; } if(randTierNumber > 45 && randTierNumber <= 90) { tier = 2; } if(randTierNumber > 90) { tier = 3; } randTierNumber = (int)randTierNumber * 10000 +1; if(randTierNumber == 5000) { tier = 4; } if(randMovesNumber <= 10) { moves = 1; } if(randMovesNumber > 10 && randMovesNumber <= 45) { moves = 2; } if(randMovesNumber > 45 && randMovesNumber <= 85) { moves = 3; } if(randMovesNumber > 85) { moves = 4; } } if(level >= 21 && level <= 30) { if(randTierNumber <= 25) { tier = 1; } if(randTierNumber > 25 && randTierNumber <= 70) { tier = 2; } if(randTierNumber > 70) { tier = 3; } randTierNumber = (int)randTierNumber * 5000 +1; if(randTierNumber == 2500) { tier = 4; } if(randMovesNumber <= 20) { moves = 2; } if(randMovesNumber > 20 && randMovesNumber <= 45) { moves = 3; } if(randMovesNumber > 45) { moves = 4; } } if(level >= 31) { if(randTierNumber <= 25) { tier = 2; } if(randTierNumber > 25) { tier = 3; } randTierNumber = (int)randTierNumber * 1000 +1; if(randTierNumber == 500) { tier = 4; } if(randMovesNumber <= 25) { moves = 3; } if(randMovesNumber > 25) { moves = 4; } } if(tier == 4) { level = 60; } determineWildMovesTier(tier, moves, level, HP); } public void determineWildMovesTier(int tier, int moves, int level, int HP) throws FileNotFoundException, IOException, InterruptedException { ArrayList<Integer> wildMoves = new ArrayList<Integer>(); if(tier == 1) { for(int x = 0; x < moves; x++) { double randMovesNumber = Math.random(); randMovesNumber = (int)(randMovesNumber * 100 +1); if(randMovesNumber <= 70) { wildMoves.add(1); } if(randMovesNumber > 70) { wildMoves.add(2); } } } if(tier == 2) { for(int x = 0; x < moves; x++) { double randMovesNumber = Math.random(); randMovesNumber = (int)(randMovesNumber * 100 +1); if(randMovesNumber <= 30) { wildMoves.add(1); } if(randMovesNumber > 30 && randMovesNumber <= 70) { wildMoves.add(2); } if(randMovesNumber > 70) { wildMoves.add(3); } } } if(tier == 3) { for(int x = 0; x < moves; x++) { double randMovesNumber = Math.random(); randMovesNumber = (int)(randMovesNumber * 100 +1); if(randMovesNumber <= 20) { wildMoves.add(1); } if(randMovesNumber > 20 && randMovesNumber <= 50) { wildMoves.add(2); } if(randMovesNumber > 50) { wildMoves.add(3); } } } if(tier == 4) { for(int x = 0; x < moves; x++) { wildMoves.add(3); } } createWildPokemon(tier, wildMoves, level, HP); } public void createWildPokemon(int tier, ArrayList<Integer> wildMoves, int level, int HP) throws FileNotFoundException, IOException, InterruptedException { boolean foundPokemon = false; String line = ""; String name = ""; ArrayList<String> types = new ArrayList<String>(); Pokemon wildPokemon = null; while(!foundPokemon) { URL url = new URL("http://m.uploadedit.com/ba3s/1488652544248.txt"); Scanner sc = new Scanner(url.openStream()).useDelimiter(","); int lineNumber = (int)(273 * Math.random() +1); if(lineNumber % 2 == 0) { lineNumber++; } for(int i = 0; i < lineNumber; i++) { sc.nextLine(); } line = sc.nextLine(); line = ",".concat(line); Scanner sc2 = new Scanner(line).useDelimiter(","); if(Integer.parseInt(sc2.next()) == tier) { name = sc2.next(); while(sc2.hasNext()) { String type = sc2.next(); if(type != "/" || type != "," || type != "/,") { types.add(type); } else { wildPokemon = new Pokemon(name, HP, level, types.get(0)); foundPokemon = true; } } wildPokemon = new Pokemon(name, HP, level, types.get(0)); foundPokemon = true; } } if(foundPokemon) { createWildMoves(tier, types, wildMoves, wildPokemon); } } public void createWildMoves(int tier, ArrayList<String> types, ArrayList<Integer> wildMoves, Pokemon wildPokemon) throws FileNotFoundException, IOException, InterruptedException { for(int x = 0; x < wildMoves.size(); x++) { String moveType = determineWildMoveType(types); ArrayList< String> lines = new ArrayList<String>() ; boolean foundMove = false; String line = ""; int tryCount = 0; URL url = new URL("http://m.uploadedit.com/ba3s/1488652222901.txt"); Scanner sc = new Scanner(url.openStream()).useDelimiter(","); for(int y = 0; y <= 329; y= y+2) { sc.nextLine(); String s = sc.nextLine(); lines.add(s); } while(!foundMove) { int lineNumber = (int)(lines.size() * Math.random()); line = lines.get(lineNumber); line = ",".concat(line); Scanner sc2 = new Scanner(line).useDelimiter(","); String posMoveType = sc2.next(); if(posMoveType.equals(moveType)) { int posTier = sc2.nextInt(); if(tryCount == 2000) { System.out.println("Recalculating..."); posTier -= 1; wildMoves.set(x, wildMoves.get(x)-1); } if(tryCount == 4000) { posTier += 2; wildMoves.set(x, wildMoves.get(x)+2); } if(tryCount == 6000) { moveType = determineWildMoveType(types); posTier -= 1; wildMoves.set(x, wildMoves.get(x)-1); tryCount = 0; } if(posTier == wildMoves.get(x)) { String name = sc2.next(); int PP = sc2.nextInt(); double damageMultiplier = sc2.nextDouble(); int damage = (int)Math.round(damageMultiplier * wildPokemon.getLevel()); int accuracy = sc2.nextInt(); int speed = sc2.nextInt(); Move newMove = new Move(name, PP, damage, accuracy, speed, moveType); wildPokemon.addMove(newMove, x); foundMove = true; } tryCount++; } } } initiateWildBattle(wildPokemon); } public void initiateWildBattle(Pokemon wildPokemon) throws IOException, FileNotFoundException, InterruptedException { if(!hasReadFile) { readInitFile(); } resetParty(oppParty); oppParty[0] = wildPokemon; int result = battle(myParty, oppParty, trainerName, myBag, false); if(result == 1) { calcEarnings(false, wildPokemon.getLevel()); } resetParty(oppParty); } public static String determineWildMoveType(ArrayList<String> types) { int n = types.size(); double total = 0; for(int i = 0; i < n; i++) { total += Math.pow(2,i); } double magicNum = 100/total; double ogMN = magicNum; int randNum = (int)(100.0 * Math.random() + 1); double sum = 0; for(int i = 0; i < n; i++) { if(randNum <= magicNum) { System.out.println("\n"+types.get(n - i -1)+"\n"); return types.get(n - i -1); } else { magicNum += Math.pow(2,i+1)*ogMN; } } return types.get(0); } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.nextLine()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println("\nMove using the arrow keys."); } public void readInitFile() throws FileNotFoundException, MalformedURLException, IOException { for(int x = 0; x < 100; x++) { Item masterBall = new Ball("Master Ball", 100000, 0); Item ultraBall = new Ball("Ultra Ball", 50, 0); Item greatBall = new Ball("Great Ball", 40, 0); Item pokeBall = new Ball("Poke Ball", 30, 0); myBag.add(masterBall); myBag.add(ultraBall); myBag.add(greatBall); myBag.add(pokeBall); } File f = new File("/Users/bensauberman/Desktop/PokemonSprites/MyPokeMonFile.txt"); Scanner fr = new Scanner(f).useDelimiter(","); //URL url = new URL("http://m.uploadedit.com/ba3s/1488652275683.txt"); //Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); String info = fr.nextLine(); addMove(info, myParty); } if(vL.charAt(0) == ('/')) { fr.nextLine(); } } } public int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488652045394.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return lineCounter; } if(vL.charAt(0) != ('|') ) { fr.nextLine(); lineCounter++; } } return lineCounter; } public static void printParty(Pokemon party[], boolean showNumbers) { if(showNumbers) { System.out.println("Pokemon HP Level Type"); } else { System.out.println("Pokemon HP Level Type"); } for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = 0; if(showNumbers) { spaceCount = (16 - party[x].getName().length()); } else { spaceCount = (14 - party[x].getName().length()); } StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } if(showNumbers) { System.out.print(x+1 + ": " + party[x].getName() + spaces + party[x].getnHP()); } else { System.out.print(party[x].getName() + spaces + party[x].getnHP()); } if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().toUpperCase().equals(pName.toUpperCase())){ party[pSlot] = newPokemon; break; } } } public void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().toUpperCase().equals(pName.toUpperCase())) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public int battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, boolean isTrainer) throws FileNotFoundException, IOException, InterruptedException { if(isTrainer) { System.out.println("\n\nWelcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); } if(!isTrainer) { System.out.println("A wild pokemon appeared."); System.out.println("The pokemon is a level " + oppParty[0].getLevel() + " " + oppParty[0].getName()); } Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println("\n" + trainerName + "'s Party: "); printParty(myParty, false); System.out.println("\nOpponent's Party: "); printParty(oppParty, false); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println("\nWhat would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } if(mSlot == -1) { validMove = false; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } System.out.println("That Pokemon is already in battle."); } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty, oppParty, isTrainer); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option.\n"); } } if(userChoice == 4) { boolean didRun = run(isTrainer); if(didRun) { validMove = true; return 2; } } } //OPPONENTS OPTIONS if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return 1; //WIN } boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println("\n---------------------------"); System.out.println("The opponent sends out " + oppPokemon); System.out.println("---------------------------"); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println("\nThe opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, isTrainer); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return 0; //LOSE } return 1; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, boolean isTrainer) throws FileNotFoundException, IOException, InterruptedException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().toUpperCase().equals(myPokemon.getMoves()[x].getName().toUpperCase())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().toUpperCase().equals(oppPokemon.getMoves()[x].getName().toUpperCase())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, isTrainer); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, isTrainer); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, isTrainer); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, isTrainer); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, isTrainer); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, isTrainer); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, boolean isTrainer) throws FileNotFoundException, IOException, InterruptedException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); System.out.println("---------------------------"); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon, isTrainer); Thread.sleep(750); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } System.out.println("---------------------------"); Thread.sleep(750); return true; } } else { System.out.println("---------------------------"); System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); System.out.println("---------------------------"); Thread.sleep(750); return true; } System.out.println("---------------------------"); Thread.sleep(750); return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty, true); boolean isValid = false; while(!isValid) { System.out.println("\nWhich Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); System.out.println(); printParty(myParty, true); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().toUpperCase().equals(requested.toUpperCase()) || Integer.parseInt(requested)-1 == x) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().toUpperCase().equals(requested.toUpperCase()) && myPokemon.getnHP() > 0) { return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println("\nThe opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println("\nPick a move. Press '0' to Exit."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = -1; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(requestedMoveWord.equals("0")) { return -1; } if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); int ogEarnings = earnings; earnings = earnings/2; System.out.println("You have been sent to the Poke Center to reheal.\n Your Earnings have" + " been cut from " + ogEarnings + " to " + earnings); x1 = 182; y1 = 651; main = mainFrontWalk1; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); System.out.println("4: Run Away."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); try { userChoice = s.nextInt(); } catch(InputMismatchException e) { System.out.println("Please enter a valid option."); break; } if(userChoice < 1 || userChoice > 4) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon, boolean isTrainer) throws InterruptedException { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); int divider = 1; if(!isTrainer) { divider = 2; } attPokemon.addEXP(((defPokemon.getLevel() * 10)+5) / divider); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); Thread.sleep(1300); System.out.println("---------------------------"); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); Thread.sleep(1300); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP() + "\n"); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); Thread.sleep(2000); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); System.out.println("---------------------------"); } public void calcEarnings(boolean isTrainer, int level) { int ogEarnings = earnings; if(isTrainer) { int r = (int)(Math.random() * 30 + 1); earnings += r * 10 * round; } else { int r = (int)(Math.random() * 20 + 1); earnings += r * level; } System.out.println("\nEarnings increased from " + ogEarnings + " to " + earnings +"\n"); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println("\nDefeated Opponent's Party:"); printParty(oppParty, false); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println("\n1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.toUpperCase().equals("Add/Replace Pokemon".toUpperCase())) { userChoice = 1; } if(userChoiceWord.toUpperCase().equals("Receive Random Item".toUpperCase())) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty, true); } if(userChoice == 2) { rewardChosen = takeItem(myBag); } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[], boolean isTrainer) { boolean hasTaken = false; while(!hasTaken) { if(!isTrainer) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[0]; System.out.println(oppParty[0].getName() + " has been successfully added to your party."); return true; } } return replacePokemon(0, myParty, oppParty); } else { printParty
Revision: 71344
Updated Code
at March 1, 2017 04:56 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.InputMismatchException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488233373757.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } } } else { e = null; } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.nextLine()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488231227545.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[], boolean showNumbers) { if(showNumbers) { System.out.println("Pokemon HP Level Type"); } else { System.out.println("Pokemon HP Level Type"); } for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = 0; if(showNumbers) { spaceCount = (16 - party[x].getName().length()); } else { spaceCount = (14 - party[x].getName().length()); } StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } if(showNumbers) { System.out.print(x+1 + ": " + party[x].getName() + spaces + party[x].getnHP()); } else { System.out.print(party[x].getName() + spaces + party[x].getnHP()); } if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().toUpperCase().equals(pName.toUpperCase())){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().toUpperCase().equals(pName.toUpperCase())) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException, IOException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty, false); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty, false); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } System.out.println("That Pokemon is already in battle."); } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException, IOException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().toUpperCase().equals(myPokemon.getMoves()[x].getName().toUpperCase())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().toUpperCase().equals(oppPokemon.getMoves()[x].getName().toUpperCase())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException, IOException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); System.out.println("---------------------------"); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } System.out.println("---------------------------"); return true; } } else { System.out.println("---------------------------"); System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); System.out.println("---------------------------"); return true; } System.out.println("---------------------------"); return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty, true); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); System.out.println(); printParty(myParty, true); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().toUpperCase().equals(requested.toUpperCase()) || Integer.parseInt(requested)-1 == x) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().toUpperCase().equals(requested.toUpperCase()) && myPokemon.getnHP() > 0) { return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); try { userChoice = s.nextInt(); } catch(InputMismatchException e) { System.out.println("Please enter a valid option."); break; } if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println("---------------------------"); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); System.out.println("---------------------------"); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty, false); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.toUpperCase().equals("Add/Replace Pokemon".toUpperCase())) { userChoice = 1; } if(userChoiceWord.toUpperCase().equals("Receive Random Item".toUpperCase())) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { printParty(oppParty, true); System.out.println(); System.out.println("Enter the name or number of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().toUpperCase().equals(desiredName.toUpperCase()) || Integer.parseInt(desiredName)-1 == x) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().toUpperCase().equals(desiredName.toUpperCase())) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty, false); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); printParty(myParty, true); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().toUpperCase().equals(desiredChange.toUpperCase())) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty, true); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().toUpperCase().equals(pokemon.toUpperCase())) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if((myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) && (myParty[pSlot].getnHP() != 0)) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty, true); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().toUpperCase().equals(pokemon.toUpperCase())) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { boolean validNumber = false; int i = 0; while(!validNumber) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); String userNumberWord = sc.next(); try { i = Integer.parseInt(userNumberWord); if(i > 0 && i < 101) { validNumber = true; } } catch(NumberFormatException nfe) { } } if(i < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(i >= 45 && i < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(i >= 60 && i < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(i >= 70 && i < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(i >= 95 && i <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change th
Revision: 71343
Updated Code
at February 28, 2017 23:47 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488233373757.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } } } else { e = null; } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488231227545.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().toUpperCase().equals(pName.toUpperCase())){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().toUpperCase().equals(pName.toUpperCase())) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException, IOException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException, IOException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().toUpperCase().equals(myPokemon.getMoves()[x].getName().toUpperCase())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().toUpperCase().equals(oppPokemon.getMoves()[x].getName().toUpperCase())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException, IOException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().toUpperCase().equals(requested.toUpperCase())) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().toUpperCase().equals(requested.toUpperCase()) && myPokemon.getnHP() > 0) { System.out.println("That Pokemon is already in battle."); return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.toUpperCase().equals("Add/Replace Pokemon".toUpperCase())) { userChoice = 1; } if(userChoiceWord.toUpperCase().equals("Receive Random Item".toUpperCase())) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().toUpperCase().equals(desiredName.toUpperCase())) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().toUpperCase().equals(desiredName.toUpperCase())) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().toUpperCase().equals(desiredChange.toUpperCase())) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().toUpperCase().equals(pokemon.toUpperCase())) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if((myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) && (myParty[pSlot].getnHP() != 0)) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().toUpperCase().equals(pokemon.toUpperCase())) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { boolean validNumber = false; while(!validNumber) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); String userNumberWord = sc.next(); try { int i = Integer.parseInt(userNumberWord); validNumber = true; } catch(NumberFormatException nfe) { validNumber = false; } } int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Trainer { private BufferedImage pic; private int x = 0; private int addX = 0; public Trainer(String picDirectory) throws IOException { URL openingURL = new URL (picDirectory); pic = ImageIO.read(openingURL); } public void setPic(String newDirectory) throws IOException { URL openingURL = new URL (newDirectory); pic = ImageIO.read(openingURL); } public BufferedImage getPic() { return pic; } public void setX(int x) { this.x = x; } public void addX(int x) { this.x += x; this.addX += x; } public int getAddX() { return addX; } public int getX() { return x; } }
Revision: 71342
Updated Code
at February 28, 2017 23:39 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488233373757.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } } } else { e = null; } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488231227545.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException, IOException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException, IOException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException, IOException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().toUpperCase().equals(requested.toUpperCase())) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon"); } else if (myPokemon != null){ if(myPokemon.getName().equals(requested) && myPokemon.getnHP() > 0) { System.out.println("That Pokemon is already in battle."); return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if((myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) && (myParty[pSlot].getnHP() != 0)) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { boolean validNumber = false; while(!validNumber) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); String userNumberWord = sc.next(); try { int i = Integer.parseInt(userNumberWord); validNumber = true; } catch(NumberFormatException nfe) { validNumber = false; } } int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Trainer { private BufferedImage pic; private int x = 0; private int addX = 0; public Trainer(String picDirectory) throws IOException { URL openingURL = new URL (picDirectory); pic = ImageIO.read(openingURL); } public void setPic(String newDirectory) throws IOException { URL openingURL = new URL (newDirectory); pic = ImageIO.read(openingURL); } public BufferedImage getPic() { return pic; } public void setX(int x) { this.x = x; } public void addX(int x) { this.x += x; this.addX += x; } public int getAddX() { return addX; } public int getX() { return x; } }
Revision: 71341
Updated Code
at February 28, 2017 09:26 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488233373757.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } } } else { e = null; } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488231227545.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException, IOException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException, IOException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException, IOException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon. (Keep in mind it is case sensitive)"); } else if (myPokemon != null){ if(myPokemon.getName().equals(requested) && myPokemon.getnHP() > 0) { System.out.println("That Pokemon is already in battle."); return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if((myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) && (myParty[pSlot].getnHP() != 0)) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { boolean validNumber = false; while(!validNumber) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); String userNumberWord = sc.next(); try { int i = Integer.parseInt(userNumberWord); validNumber = true; } catch(NumberFormatException nfe) { validNumber = false; } } int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Trainer { private BufferedImage pic; private int x = 0; private int addX = 0; public Trainer(String picDirectory) throws IOException { URL openingURL = new URL (picDirectory); pic = ImageIO.read(openingURL); } public void setPic(String newDirectory) throws IOException { URL openingURL = new URL (newDirectory); pic = ImageIO.read(openingURL); } public BufferedImage getPic() { return pic; } public void setX(int x) { this.x = x; } public void addX(int x) { this.x += x; this.addX += x; } public int getAddX() { return addX; } public int getX() { return x; } }
Revision: 71340
Updated Code
at February 28, 2017 09:21 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488233373757.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } } } else { e = null; } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488231227545.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException, IOException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException, IOException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException, IOException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().equals(requested) && myPokemon.getnHP() > 0) { System.out.println("That Pokemon is already in battle."); return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if((myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) && (myParty[pSlot].getnHP() != 0)) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { boolean validNumber = false; while(!validNumber) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); String userNumberWord = sc.next(); try { int i = Integer.parseInt(userNumberWord); validNumber = true; } catch(NumberFormatException nfe) { validNumber = false; } } int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Trainer { private BufferedImage pic; private int x = 0; private int addX = 0; public Trainer(String picDirectory) throws IOException { URL openingURL = new URL (picDirectory); pic = ImageIO.read(openingURL); } public void setPic(String newDirectory) throws IOException { URL openingURL = new URL (newDirectory); pic = ImageIO.read(openingURL); } public BufferedImage getPic() { return pic; } public void setX(int x) { this.x = x; } public void addX(int x) { this.x += x; this.addX += x; } public int getAddX() { return addX; } public int getX() { return x; } }
Revision: 71339
Updated Code
at February 28, 2017 09:18 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; URL mainURL = new URL("http://oi63.tinypic.com/2zpip89.jpg"); BufferedImage main = ImageIO.read(mainURL); URL openingURL = new URL ("http://oi65.tinypic.com/2wnbh4n.jpg"); BufferedImage opening = ImageIO.read(openingURL); URL bridgeURL = new URL ("http://oi68.tinypic.com/2vwv7nm.jpg"); BufferedImage bridge = ImageIO.read(bridgeURL); URL closingURL = new URL ("http://oi67.tinypic.com/25kh05c.jpg"); BufferedImage closing = ImageIO.read(closingURL); URL grassURL = new URL ("http://oi65.tinypic.com/123r060.jpg"); BufferedImage grass = ImageIO.read(grassURL); URL waterURL = new URL ("http://oi68.tinypic.com/2hibfao.jpg"); BufferedImage water = ImageIO.read(waterURL); URL hFenceURL = new URL ("http://oi63.tinypic.com/2eao6me.jpg"); BufferedImage horizontalFence = ImageIO.read(hFenceURL); URL vFenceURL = new URL ("http://oi68.tinypic.com/2j3s1sk.jpg"); BufferedImage verticalFence = ImageIO.read(vFenceURL); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void readTrainerFile() throws FileNotFoundException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488233373757.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException, IOException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException, IOException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {} } } } else { e = null; } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException, MalformedURLException, IOException { URL url = new URL("http://m.uploadedit.com/ba3s/1488231227545.txt"); Scanner fr = new Scanner(url.openStream()).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException, IOException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException, IOException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException, IOException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().equals(requested) && myPokemon.getnHP() > 0) { System.out.println("That Pokemon is already in battle."); return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException, MalformedURLException, IOException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; URL url = new URL("http://m.uploadedit.com/ba3s/1488232904670.txt"); Scanner fr2 = new Scanner(url.openStream()).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if((myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) && (myParty[pSlot].getnHP() != 0)) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { boolean validNumber = false; while(!validNumber) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); String userNumberWord = sc.next(); try { int i = Integer.parseInt(userNumberWord); validNumber = true; } catch(NumberFormatException nfe) { validNumber = false; } } int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; /** * * @author bensauberman */ class Trainer { private BufferedImage pic; private int x = 0; private int addX = 0; public Trainer(String picDirectory) throws IOException { URL openingURL = new URL (picDirectory); pic = ImageIO.read(openingURL); } public void setPic(String newDirectory) throws IOException { URL openingURL = new URL (newDirectory); pic = ImageIO.read(openingURL); } public BufferedImage getPic() { return pic; } public void setX(int x) { this.x = x; } public void addX(int x) { this.x += x; this.addX += x; } public int getAddX() { return addX; } public int getX() { return x; } }
Revision: 71338
Updated Code
at February 28, 2017 00:27 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; BufferedImage main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainback.png")); BufferedImage opening = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/opening.png")); BufferedImage bridge = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/center bridge.png")); BufferedImage closing = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/closing.png")); BufferedImage grass = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/grass.png")); BufferedImage water = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/water.png")); BufferedImage exclamationpoint = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/exclamationpoint.png")); BufferedImage verticalFence = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/verticalFence.png")); BufferedImage horizontalFence = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/horizontalFence.png")); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); String directoryForPokemonFile = "/Users/bensauberman/Desktop/PokemonSprites/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/PokemonSprites/EffectivenessChart.txt"; int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void readTrainerFile() throws FileNotFoundException, IOException { File TrainerFile = new File("/Users/bensauberman/Desktop/PokemonSprites/TrainerFile.txt"); Scanner fr = new Scanner(TrainerFile).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } } } } else { e = null; } } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); application.setResizable(false); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty and your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().equals(requested) && myPokemon.getnHP() > 0) { System.out.println("That Pokemon is already in battle."); return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } } import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * * @author bensauberman */ public class Trainer { private BufferedImage pic; private int x = 0; private int addX = 0; public Trainer(String picDirectory) throws IOException { pic = ImageIO.read(new File(picDirectory)); } public void setPic(String newDirectory) throws IOException { pic = ImageIO.read(new File(newDirectory)); } public BufferedImage getPic() { return pic; } public void setX(int x) { this.x = x; } public void addX(int x) { this.x += x; this.addX += x; } public int getAddX() { return addX; } public int getX() { return x; } }
Revision: 71337
Updated Code
at February 27, 2017 14:41 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author bensauberman */ public class PokemonBattle extends JPanel implements ActionListener, KeyListener { boolean walk = false; BufferedImage main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainback.png")); BufferedImage opening = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/opening.png")); BufferedImage bridge = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/center bridge.png")); BufferedImage closing = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/closing.png")); BufferedImage grass = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/grass.png")); BufferedImage water = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/water.png")); BufferedImage exclamationpoint = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/exclamationpoint.png")); BufferedImage verticalFence = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/verticalFence.png")); BufferedImage horizontalFence = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/horizontalFence.png")); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; int stoppedLine = 0; int wins = 0; int losses = 0; boolean shiftActivated = false; static boolean kp = false; static int round = 1; static String trainerName = ""; int tD = 0; int x1 = (int)(bridge.getWidth()/2 + 7); int y1 = 720; final int up = -232+tD; final int left = 15; final int right = bridge.getWidth() - 15; final int down = 780; ArrayList<Integer> trainerLines = new ArrayList<>(); ArrayList<Boolean> validTrainerLines = new ArrayList<>(); ArrayList<Boolean> isWild = new ArrayList<>(); ArrayList<Trainer> trainers = new ArrayList<>(); public PokemonBattle() throws IOException // set up graphics window { super(); readTrainerFile(); setBackground(Color.WHITE); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); for(int y = 1; y <= 25; y++) { trainerLines.add(708-(34*y) - (bridge.getHeight()/2)); validTrainerLines.add(true); } } public void paintComponent(Graphics g) // draw graphics in the panel { if(y1 <= 200) { tD = 500; if(!shiftActivated) { for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, oldLine+tD); } } shiftActivated = true; } if(y1 > 200) { if(shiftActivated) { tD = 0; for(int x = 0; x < trainerLines.size(); x++) { int oldLine = trainerLines.get(x); trainerLines.set(x, (oldLine-500)); } } shiftActivated = false; } super.paintComponent(g); // call superclass to make panel display correctly g.drawImage(water, 0, 0, this); g.drawImage(water, 0, 400, this); for(int y = 721; y < 1000; y+= grass.getHeight()) { for(int x = 15; x < bridge.getWidth(); x += grass.getWidth()) { g.drawImage(grass, x, y+tD, this); } } g.drawImage(verticalFence, 10, 721+tD, this); g.drawImage(verticalFence, 10, 750+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 721+tD, this); g.drawImage(verticalFence, bridge.getWidth(), 750+tD, this); g.drawImage(horizontalFence, 10, 796+tD, this); g.drawImage(opening, 7, 650+tD, this); g.drawImage(closing, 7, -232+tD, this); boolean trainerIsRight = true; for(int y = 1; y <= 25; y++) { g.drawImage(bridge, 7, 708-(34*y) - bridge.getHeight() +tD, this); if(trainerIsRight) { g.drawImage(trainers.get(y-1).getPic(), bridge.getWidth() - 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(bridge.getWidth() - 15 + trainers.get(y-1).getAddX()); trainerIsRight = false; } else { g.drawImage(trainers.get(y-1).getPic(), 15 + trainers.get(y-1).getAddX(), 708-(34*y) - (bridge.getHeight()/2) +tD, this); trainers.get(y-1).setX(15 + trainers.get(y-1).getAddX()); trainerIsRight = true; } } g.drawImage(main, x1, y1+tD, this); } public void setMainPos(int x, int y) { this.x1 = x; this.y1 = y; } public void keyPressed(KeyEvent e) { if (kp) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainleftwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1-1 > left) { x1 = x1-1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainrightwalk2.png")); walk = true; } } catch (IOException ex) {} if(x1+1 < right) { x1 = x1+1; repaint(); } } if(e.getKeyCode() == KeyEvent.VK_UP) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainbackwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1-1 > up) { y1 = y1-1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("Congratulations on winning the game! Your final record is " + wins + " - " + losses); System.exit(0); } } if(e.getKeyCode() == KeyEvent.VK_DOWN) { try { if(walk) { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk1.png")); walk = false; } else { main = ImageIO.read(new File("/Users/bensauberman/Desktop/PokemonSprites/mainfrontwalk2.png")); walk = true; } } catch (IOException ex) {} if(y1+1 < down) { y1 = y1+1; repaint(); try { checkForBattle(1); } catch (InterruptedException ex) {} catch (FileNotFoundException ex) { Logger.getLogger(PokemonBattle.class.getName()).log(Level.SEVERE, null, ex); } } } } else { e = null; } } public void checkForBattle(int movement) throws InterruptedException, FileNotFoundException { z: for(int x = 0; x < trainerLines.size(); x++) { if(y1+tD+movement == trainerLines.get(x) && validTrainerLines.get(x) == true) { int moveCounter = 0; if(x % 2 == 0) { while(trainers.get(x).getX()-10 > x1) { trainers.get(x).addX(-1); moveCounter++; repaint(); } //trainers.get(x).addX(moveCounter); } if(x % 2 != 0) { while(trainers.get(x).getX()+10 < x1) { trainers.get(x).addX(1); moveCounter++; repaint(); } //trainers.get(x).addX(-moveCounter); } repaint(); initiateBattle(); validTrainerLines.set(x, false); break z; } } } public void initiateBattle() throws FileNotFoundException { setFocusable(false); kp = false; System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); setFocusable(true); kp = true; } @Override public void actionPerformed(ActionEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public void run(){ repaint(); } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here PokemonBattle panel = new PokemonBattle(); // window for drawing JFrame application = new JFrame(); // the program itself application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit // when it is closed application.add(panel); application.setSize(260, 1000); // window is 500 pixels wide, 400 high application.setVisible(true); System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty but your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); System.out.println(); System.out.println("Move using the arrow keys."); kp = true; } public void readTrainerFile() throws FileNotFoundException, IOException { File TrainerFile = new File("/Users/bensauberman/Desktop/CS/AP CS/TrainerFile.txt"); Scanner fr = new Scanner(TrainerFile).useDelimiter(","); while (fr.hasNextLine()) { String vL = fr.nextLine(); if(vL.equals("|||,")) { String picDirectory = fr.next(); Trainer trainer = new Trainer(picDirectory); trainers.add(trainer); } } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty, null); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { Pokemon oldPokemon = myPokemon; myPokemon = pickMyPokemon(myParty, myPokemon); if(oldPokemon.getName() != (myPokemon.getName())) { validMove = true; } } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty, null); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[], Pokemon myPokemon) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); boolean validName = false; for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { validName = true; if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + " has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else if (myPokemon != null){ if(myPokemon.getName().equals(requested) && myPokemon.getnHP() > 0) { System.out.println("That Pokemon is already in battle."); return myParty[x]; } } if(myPokemon != null || myPokemon == null) { if(myParty[x].getnHP() > 0) { isValid = true; return myParty[x]; } } } } } if(!validName) { System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); round++; } if(!result) { System.out.println("You lose the battle."); round++; } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break z; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getrHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } } import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * * @author bensauberman */ public class Trainer { private BufferedImage pic; private int x = 0; private int addX = 0; public Trainer(String picDirectory) throws IOException { pic = ImageIO.read(new File(picDirectory)); } public void setPic(String newDirectory) throws IOException { pic = ImageIO.read(new File(newDirectory)); } public BufferedImage getPic() { return pic; } public void setX(int x) { this.x = x; } public void addX(int x) { this.x += x; this.addX += x; } public int getAddX() { return addX; } public int getX() { return x; } }
Revision: 71336
Updated Code
at February 24, 2017 10:46 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty but your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 25; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } System.out.println("Congratulations on finishing the game! Your final record is " + wins + " - " + losses + ". Play again!"); } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71335
Updated Code
at February 21, 2017 18:35 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("As you progress, the opponents increase in difficulty but your Pokemon gain exp and level up as you defeat enemies. "); System.out.println("After each opponent you defeat you get the option of either taking one of their Pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 25; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } System.out.println("Congratulations on finishing the game! Your final record is " + wins + " - " + losses + ". Play again!"); } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null && oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 5) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send out to battle?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 6; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[0].getName().toUpperCase()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[1].getName().toUpperCase()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[2].getName().toUpperCase()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.toUpperCase().equals(myPokemon.getMoves()[3].getName().toUpperCase()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 10)+5); while(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s Max HP has now increased to " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use or press '0' to Quit."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 0: return false; case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = useRevive(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71334
Updated Code
at February 21, 2017 16:17 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 25 opponents in the game. "); System.out.println("They increase in difficulty as you go but your pokemon gain exp and level up as you beat them. "); System.out.println("After each opponent you defeat you get the option of either taking one of their pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 25; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } System.out.println("Congratulations on finishing the game! Your final record is " + wins + " - " + losses + ". Play again!"); } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 6; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71333
Updated Code
at February 21, 2017 16:16 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 20 opponents in the game. "); System.out.println("They increase in difficulty as you go but your pokemon gain exp and level up as you beat them. "); System.out.println("After each opponent you defeat you get the option of either taking one of their pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 20; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } System.out.println("Congratulations on finishing the game! Your final record is " + wins + " - " + losses + ". Play again!"); } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if((myMove.getSpeed() * myPokemon.getLevel()) >= (oppMove.getSpeed() * oppPokemon.getLevel())) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if(((oppMove.getSpeed() * oppPokemon.getLevel())) > ((myMove.getSpeed()) * myPokemon.getLevel())|| oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 6; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71332
Updated Code
at February 21, 2017 13:23 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattletester; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattleTester { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 20 opponents in the game. "); System.out.println("They increase in difficulty as you go but your pokemon gain exp and level up as you beat them. "); System.out.println("After each opponent you defeat you get the option of either taking one of their pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 20; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } System.out.println("Congratulations on finishing the game! Your final record is " + wins + " - " + losses + ". Play again!"); } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 6; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71331
Updated Code
at February 21, 2017 09:37 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattletester; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattleTester { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 20 opponents in the game. "); System.out.println("They increase in difficulty as you go but your pokemon gain exp and level up as you beat them. "); System.out.println("After each opponent you defeat you get the option of either taking one of their pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 20; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 6; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author bensauberman */ class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71330
Updated Code
at February 21, 2017 09:27 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here String directoryForPokemonFile = "/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"; String directoryForEffectivenessChart = "/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"; System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 20 opponents in the game. "); System.out.println("They increase in difficulty as you go but your pokemon gain exp and level up as you beat them. "); System.out.println("After each opponent you defeat you get the option of either taking one of their pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 20; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine, directoryForPokemonFile); boolean didWin = battle(myParty, oppParty, trainerName, myBag, directoryForEffectivenessChart); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line, String directoryForPokemonFile) throws FileNotFoundException { File PokemonFile = new File(directoryForPokemonFile); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag, String directoryForEffectivenessChart) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon, directoryForEffectivenessChart); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon, directoryForEffectivenessChart); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon, String directoryForEffectivenessChart) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType(), directoryForEffectivenessChart); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 6; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord, String directoryForEffectivenessChart) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File(directoryForEffectivenessChart); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71329
Updated Code
at February 21, 2017 09:18 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); System.out.println(); System.out.println("There are 20 opponents in the game. "); System.out.println("They increase in difficulty as you go but your pokemon gain exp and level up as you beat them. "); System.out.println("After each opponent you defeat you get the option of either taking one of their pokemon to add to your party"); System.out.println("or receiving a random item. After each battle your Pokemon's moves and HP get restored. "); System.out.println("The goal is to win as many games as possible."); Pokemon[] myParty = new Pokemon[6]; Pokemon[] oppParty = new Pokemon[6]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 20; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException { File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 6; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 6; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 6; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 6; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 6; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(); System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 6; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 6; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 6; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 6; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(move+1 + ": " + myMoves[move].getName() + spaces + " " + myMoves[move].getDamage()); if(myMoves[move].getDamage() < 10) { System.out.print(" " + myMoves[move].getAccuracy() + " " + + myMoves[move].getnPP()); } else { System.out.print(" " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP()); } if(myMoves[move].getnPP() < 10) { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } else { System.out.print(" " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); System.out.println(); } } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71328
Updated Code
at February 21, 2017 03:34 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); Pokemon[] myParty = new Pokemon[7]; Pokemon[] oppParty = new Pokemon[100]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 11; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException { File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 7; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 7; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 7; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 7; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 7; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 7; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 7; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71327
Updated Code
at February 21, 2017 03:15 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); Pokemon[] myParty = new Pokemon[7]; Pokemon[] oppParty = new Pokemon[100]; ArrayList<Item> myBag = new ArrayList<Item>(); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 50; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException { File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 7; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.print(party[x].getName() + spaces + party[x].getnHP()); if(party[x].getnHP() >= 100) { System.out.print(" " + party[x].getLevel()); } else if(party[x].getnHP() >= 10 && party[x].getnHP() <= 99) { System.out.print(" " + party[x].getLevel()); } else { System.out.print(" " + party[x].getLevel()); } if(party[x].getLevel() >= 10) { System.out.print(" " + party[x].getType()); } else { System.out.print(" " + party[x].getType()); } System.out.println(); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 7; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 7; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 7; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 7; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 7; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 7; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71326
Updated Code
at February 20, 2017 18:11 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); Pokemon[] myParty = new Pokemon[7]; Pokemon[] oppParty = new Pokemon[100]; ArrayList<Item> myBag = new ArrayList<Item>(); File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 50; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println(); System.out.println("Record: " + wins + " - " + losses); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException { File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 7; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(party[x].getName() + spaces + party[x].getnHP() + " " + party[x].getLevel() + " " + party[x].getType()); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 7; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but the attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 7; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 7; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 7; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 7; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } return; } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { boolean hasTaken = false; while(!hasTaken) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 7; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } else { return false; } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); } return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; nHP = rHP; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(move+1 + ": " + myMoves[move].getName() + spaces + myMoves[move].getDamage() + " " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP() + " " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71325
Updated Code
at February 20, 2017 17:04 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = "Trainer ".concat(sc.next()); System.out.println("Welcome " + trainerName + "!"); Pokemon[] myParty = new Pokemon[7]; Pokemon[] oppParty = new Pokemon[100]; ArrayList<Item> myBag = new ArrayList<Item>(); File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int stoppedLine = 0; int wins = 0; int losses = 0; for(int round = 1; round <= 50; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); boolean didWin = battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); if(didWin) { reward(myParty, oppParty, myBag); wins++; } else { losses++; } removeParty(oppParty); System.out.println("Record: " + wins + " - " + losses); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException { File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 7; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(party[x].getName() + spaces + party[x].getnHP() + " " + party[x].getLevel() + " " + party[x].getType()); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)){ party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 7; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static boolean battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println(); System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { boolean hasBeenUsed = chooseItem(myBag, myParty); if(hasBeenUsed) { validMove = true; } } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } int checkFaint = useMoves(myMove, oppMove, myPokemon, oppPokemon); if(checkFaint == 1) { oppPokemon = pickOppPokemon(oppParty); } if(checkFaint == 2) { myPokemon = pickMyPokemon(myParty); } } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); return false; } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); return true; } return true; } public static int useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; boolean checkFaint = false; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return 0; } if(myMove != null && oppMove == null) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } if(myMove == null && oppMove != null) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { return 0; } else { return 2; } } else { return 1; } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { checkFaint = useMove(oppMove, oppPokemon, myPokemon); if(checkFaint) { checkFaint = useMove(myMove, myPokemon, oppPokemon); if(checkFaint) { return 0; } else { return 1; } } else { return 2; } } } return 0; } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGen = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGen) { int criticalGen = (int)(100.0 * Math.random() +1); boolean isCritical = false; int criticalMultiplier = 1; if(criticalGen <= 10) { isCritical = true; criticalMultiplier = 2; } int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double effectiveMultiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() * (-effectiveMultiplier) * (criticalMultiplier))); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } if(criticalMultiplier == 2){ System.out.println(attMove.getName() + " was a critical hit!"); } calcEXP(attPokemon, defPokemon); return false; } else if (effectiveMultiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + "'s HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(effectiveMultiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(effectiveMultiplier == 2.0) { System.out.println(attMove.getName() + " was super effective!"); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but his attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 7; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 7; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 7; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() * 5)+5); if(attPokemon.getEXP() >= attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); attPokemon.setrHP((int)(attPokemon.getrHP() * .05)); System.out.println(attPokemon.getName() + "'s HP is now " + attPokemon.getrHP()); System.out.println(); for(int x = 0; x < 4; x++) { if(attPokemon.getMoves()[x] != null) { attPokemon.getMoves()[x].setDamage(); System.out.println(attPokemon.getName() + "'s " + attPokemon.getMoves()[x].getName() + " has increased its damage to " + attPokemon.getMoves()[x].getDamage()); } } } System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 7; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option of either taking" + " an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1) { rewardChosen = takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); rewardChosen = true; } } } public static boolean takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return true; } if(myParty[pSlot].getName().equals(desiredName)) { System.out.println("This Pokemon already exists in the party. You cannot have more than one of the " + "same Pokemon in a party. Would you like to switch the two? (Yes or No)?"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { myParty[pSlot] = oppParty[x]; return true; } else { return false; } } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 7; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return true; } } } } } } } System.out.println("You must enter a valid Pokemon to capture. Please repick."); takeOppPokemon(myParty, oppParty); return false; } public static boolean chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); z: switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 4: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 4) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; case 5: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 5) { hasBeenUsed = usePotion(x, myBag, myParty); if(hasBeenUsed) { break; } else { return false; } } } System.out.println("There are none of this type of item. Please repick."); break z; } } return true; } public static boolean useRevive(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() <= 0) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Revive someRevive = (Revive)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); if(myParty[pSlot].getnHP() <= 0) { myParty[pSlot].setnHP((int)(myParty[pSlot].getrHP()/someRevive.getRevival())); System.out.println(myParty[pSlot].getName() + " has bwwn revived and " + myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } else { System.out.println("The Pokemon must be fainted if you wish to revive."); } } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are alive. Cannot use a revive."); return false; } public static boolean usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { boolean hasBeenUsed = true; for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getnHP() < myParty[pSlot].getnHP()) { hasBeenUsed = false; } } } while(!hasBeenUsed) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; myBag.remove(x); return true; } } } System.out.println("Please pick another Pokemon."); } System.out.println("All Pokemon are fully healed. Cannot use a potion."); return false; } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would " + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; nHP = rHP; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(move+1 + ": " + myMoves[move].getName() + spaces + myMoves[move].getDamage() + " " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP() + " " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71324
Updated Code
at February 20, 2017 14:17 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = sc.next(); System.out.println("Welcome " + trainerName + "!"); Pokemon[] myParty = new Pokemon[7]; Pokemon[] oppParty = new Pokemon[100]; ArrayList<Item> myBag = new ArrayList<Item>(); File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int stoppedLine = 0; for(int round = 1; round <= 50; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); reward(myParty, oppParty, myBag); removeParty(oppParty); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException { File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 7; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(party[x].getName() + spaces + party[x].getnHP() + " " + party[x].getLevel() + " " + party[x].getType()); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)) { party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 7; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static void battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; //MY OPTIONS boolean validMove = false; while(!validMove) { System.out.println("What would you like to do?"); int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; validMove = true; } else { myMove = myPokemon.getMoves()[mSlot]; validMove = true; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); validMove = true; } if(userChoice == 3) { if(myBag.size() > 0) { chooseItem(myBag, myParty); validMove = true; } else { System.out.println("Bag is empty. Pick another option."); System.out.println(); } } } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } useMoves(myMove, oppMove, myPokemon, oppPokemon); } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); } } public static void useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; int checkFaint = -1; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return; } if(myMove != null && oppMove == null) { useMove(myMove, myPokemon, oppPokemon); return; } if(myMove == null && oppMove != null) { useMove(oppMove, oppPokemon, myPokemon); return; } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { boolean shouldContinue = useMove(myMove, myPokemon, oppPokemon); if(shouldContinue) { useMove(oppMove, oppPokemon, myPokemon); } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { boolean shouldContinue = useMove(oppMove, oppPokemon, myPokemon); if(shouldContinue) { useMove(myMove, myPokemon, oppPokemon); } } } } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGenerator = (int)(100.0 * Math.random() +1); System.out.println(); if(attMove.getAccuracy() > accuracyGenerator) { int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double multiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() *-multiplier)); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(multiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(multiplier == 2.0) { System.out.println(attMove.getName() + " was super effective."); } calcEXP(attPokemon, defPokemon); return false; } else if (multiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + " HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(multiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(multiplier == 2.0) { System.out.println(attMove.getName() + " was super effective."); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but his attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 7; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println(); System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 7; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 7; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; int requestedMove = 0; while(!isValid) { Scanner s = new Scanner(System.in); String requestedMoveWord = s.nextLine(); if(((myPokemon.getMoves()[0] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[0].getName()))) || (requestedMoveWord.equals("1"))) { requestedMove = 1; } if(((myPokemon.getMoves()[1] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[1].getName()))) || (requestedMoveWord.equals("2"))) { requestedMove = 2; } if(((myPokemon.getMoves()[2] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[2].getName()))) || (requestedMoveWord.equals("3"))) { requestedMove = 3; } if(((myPokemon.getMoves()[3] != null) && (requestedMoveWord.equals(myPokemon.getMoves()[3].getName()))) || (requestedMoveWord.equals("4"))) { requestedMove = 4; } if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() / 5)+5); if(attPokemon.getEXP() > attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); } System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 7; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory!"); System.out.println("As a reward you get the option to either take" + "an opponent's Pokemon of your choosing or receiving a random item."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println(); System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1 || userChoice == 2) { rewardChosen = true; } } if(userChoice == 1) { takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); } } public static void takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return; } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 7; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return; } } } } } } } } public static void chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { usePotion(x, myBag, myParty); } } break; case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { usePotion(x, myBag, myParty); } } break; case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { usePotion(x, myBag, myParty); } } break; case 4: break; case 5: break; } } } public static void usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; return; } } } System.out.println("Please pick another Pokemon."); } } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; break; case 2: superPotionCounter++; break; case 3: hyperPotionCounter++; break; case 4: reviveCounter++; break; case 5: maxReviveCounter++; break; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would" + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; nHP = rHP; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(move+1 + ": " + myMoves[move].getName() + spaces + myMoves[move].getDamage() + " " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP() + " " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71323
Updated Code
at February 20, 2017 13:14 by bsauberman
Updated Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = sc.next(); System.out.println("Welcome " + trainerName + "!"); Pokemon[] myParty = new Pokemon[7]; Pokemon[] oppParty = new Pokemon[100]; ArrayList<Item> myBag = new ArrayList<Item>(); File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int stoppedLine = 0; for(int round = 1; round <= 50; round++) { System.out.println(); System.out.println("ROUND " + round); stoppedLine = readFile(myParty, oppParty, stoppedLine); battle(myParty, oppParty, trainerName, myBag); resetParty(myParty); resetParty(oppParty); reward(myParty, oppParty, myBag); removeParty(oppParty); } } public static int readFile(Pokemon myParty[], Pokemon oppParty[], int line) throws FileNotFoundException { File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int lineCounter = line; int stoppedLine = 0; for(int i = 0; i < line; i++) { fr.nextLine(); } while (fr.hasNextLine()) { String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); lineCounter+=2; String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { lineCounter++; stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); lineCounter++; } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 7; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(party[x].getName() + spaces + party[x].getnHP() + " " + party[x].getLevel() + " " + party[x].getType()); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)) { party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 7; x++) { if(party[x]!= null) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static void battle(Pokemon myParty[], Pokemon oppParty[], String trainerName, ArrayList<Item> myBag) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; System.out.println("What would you like to do?"); //MY OPTIONS int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; } else { myMove = myPokemon.getMoves()[mSlot]; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); } if(userChoice == 3) { chooseItem(myBag, myParty); } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent has sent out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println("The opponent has sent out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } useMoves(myMove, oppMove, myPokemon, oppPokemon); } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); } } public static void useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return; } if(myMove != null && oppMove == null) { useMove(myMove, myPokemon, oppPokemon); return; } if(myMove == null && oppMove != null) { useMove(oppMove, oppPokemon, myPokemon); return; } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { boolean shouldContinue = useMove(myMove, myPokemon, oppPokemon); if(shouldContinue) { useMove(oppMove, oppPokemon, myPokemon); } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { boolean shouldContinue = useMove(oppMove, oppPokemon, myPokemon); if(shouldContinue) { useMove(myMove, myPokemon, oppPokemon); } } } } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGenerator = (int)(100.0 * Math.random() +1); if(attMove.getAccuracy() > accuracyGenerator) { int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double multiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() *-multiplier)); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(multiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(multiplier == 2.0) { System.out.println(attMove.getName() + " was super effective."); } calcEXP(attPokemon, defPokemon); return false; } else if (multiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + " HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(multiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(multiplier == 2.0) { System.out.println(attMove.getName() + " was super effective."); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but his attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 7; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { System.out.println("The opponent sends out " + oppParty[x].getName()); return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 7; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 7; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; while(!isValid) { Scanner s = new Scanner(System.in); int requestedMove = s.nextInt(); if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() / 5)+5); if(attPokemon.getEXP() > attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); } System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } public static void removeParty(Pokemon party[]) { for(int x = 0; x < 7; x++) { party[x] = null; } } public static void resetParty(Pokemon party[]) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] != null) { party[pSlot].resetnHP(); for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] != null) { party[pSlot].getMoves()[mSlot].resetnPP(); } } } } } public static void reward(Pokemon myParty[], Pokemon oppParty[], ArrayList<Item> myBag) { System.out.println("Congratulations on the victory! As a reward you get the option to either take" + "an opponent's Pokemon of your choosing or receiving a random option."); System.out.println(); System.out.println("Defeated Opponent's Party:"); printParty(oppParty); boolean rewardChosen = false; int userChoice = 0; while(!rewardChosen) { System.out.println("1: Add/Replace Pokemon"); System.out.println("2: Receive Random Item"); Scanner sc = new Scanner(System.in); String userChoiceWord = sc.next(); if(userChoiceWord.equals("Add/Replace Pokemon")) { userChoice = 1; } if(userChoiceWord.equals("Receive Random Item")) { userChoice = 2; } if(userChoiceWord.equals("1")) { userChoice = 1; } if(userChoiceWord.equals("2")) { userChoice = 2; } if(userChoice == 1 || userChoice == 2) { rewardChosen = true; } } if(userChoice == 1) { takeOppPokemon(myParty, oppParty); } if(userChoice == 2) { takeItem(myBag); } } public static void takeOppPokemon(Pokemon myParty[], Pokemon oppParty[]) { System.out.println("Enter the name of the Pokemon you would like to take."); Scanner sc = new Scanner(System.in); String desiredName = sc.next(); for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getName().equals(desiredName)) { for(int pSlot = 0; pSlot < 7; pSlot++) { if(myParty[pSlot] == null) { myParty[pSlot] = oppParty[x]; System.out.println(oppParty[x].getName() + " has been successfully added to your party."); return; } } System.out.println("Your party is full. To add " + oppParty[x].getName() + " you must remove one of your own Pokemon."); System.out.println("This is your party."); System.out.println(); printParty(myParty); System.out.println(); System.out.println("Would you like to replace? (Yes or No)"); String answer = sc.next(); if(answer.toUpperCase().equals("YES")) { System.out.println("Which Pokemon would you like to remove to make room for " + oppParty[x].getName() + "?"); String desiredChange = sc.next(); for(int y = 0; y < 7; y++) { if(myParty[y] != null) { if(myParty[y].getName().equals(desiredChange)) { String oldPokemonName = myParty[y].getName(); myParty[y] = oppParty[x]; System.out.println(oldPokemonName + " has been successfully switched out for " + oppParty[x].getName()); return; } } } } } } } } public static void chooseItem(ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println(); printBag(myBag); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { System.out.println("Enter the number of the item you would like to use."); Scanner sc = new Scanner(System.in); int itemID = sc.nextInt(); switch(itemID) { case 1: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 1) { usePotion(x, myBag, myParty); } } case 2: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 2) { usePotion(x, myBag, myParty); } } case 3: for(int x = 0; x < myBag.size(); x++) { if(myBag.get(x).getID() == 3) { usePotion(x, myBag, myParty); } } case 4: case 5: } } } public static void usePotion(int x, ArrayList<Item> myBag, Pokemon myParty[]) { System.out.println("Which Pokemon would you like to use " + myBag.get(x).getName() + " on?"); System.out.println(); boolean hasBeenUsed = false; while(!hasBeenUsed) { printParty(myParty); Scanner sc = new Scanner(System.in); String pokemon = sc.next(); for(int pSlot = 0; pSlot < 7; x++) { if(myParty[pSlot] != null) { if(myParty[pSlot].getName().equals(pokemon)) { Potion somePotion = (Potion)myBag.get(x); int oldHP = myParty[pSlot].getnHP(); myParty[pSlot].setnHP(somePotion.getHealValue()); System.out.println(myParty[pSlot].getName() + "'s HP increased from " + oldHP + " to " + myParty[pSlot].getnHP()); hasBeenUsed = true; return; } } } System.out.println("Please pick another Pokemon."); } } public static void printBag(ArrayList<Item> myBag) { int potionCounter = 0; int superPotionCounter = 0; int hyperPotionCounter = 0; int reviveCounter = 0; int maxReviveCounter = 0; for(int x = 0; x < myBag.size(); x++) { int ID = myBag.get(x).getID(); switch(ID) { case 1: potionCounter++; case 2: superPotionCounter++; case 3: hyperPotionCounter++; case 4: reviveCounter++; case 5: maxReviveCounter++; } } System.out.println("1: Potions: " + potionCounter); System.out.println("2: Super Potions: " + superPotionCounter); System.out.println("3: Hyper Potions: " + hyperPotionCounter); System.out.println("4: Revives: " + reviveCounter); System.out.println("5: Max Revives: " + maxReviveCounter); } public static void takeItem(ArrayList<Item> myBag) { System.out.println("There are 100 boxes each containting one item in them. Enter the box number you would" + "like to open for your free item. (1-100)"); Scanner sc = new Scanner(System.in); int userNumber = sc.nextInt(); int randNumber = (int)(100.0 * Math.random() + 1); if(randNumber < 45) { Item potion = new Potion("Potion", 20); myBag.add(potion); System.out.println("You won a Potion! It has been added to your bag."); } if(randNumber >= 45 && randNumber < 60) { Item superPotion = new Potion("Super Potion", 50); myBag.add(superPotion); System.out.println("You won a Super Potion! It has been added to your bag."); } if(randNumber >= 60 && randNumber < 70) { Item hyperPotion = new Potion("Hyper Potion", 200); myBag.add(hyperPotion); System.out.println("You won a Hyper Potion! It has been added to your bag."); } if(randNumber >= 70 && randNumber < 95) { Item revive = new Revive("Revive", 0.5); myBag.add(revive); System.out.println("You won a Revive! It has been added to your bag."); } if(randNumber >= 95 && randNumber <= 100) { Item maxRevive = new Revive("Max Revive", 1); myBag.add(maxRevive); System.out.println("You won a Max Revive! It has been added to your bag."); } System.out.println(myBag.get(0).getName()); System.out.println(myBag.get(0).getID()); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } if(nHP > rHP) { nHP = rHP; } } public void resetnHP() { nHP = rHP; } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; nHP = rHP; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(move+1 + ": " + myMoves[move].getName() + spaces + myMoves[move].getDamage() + " " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP() + " " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public void resetnPP() { nPP = rPP; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Item { private String name; private int ID; public Item(String name) { this.name = name; switch(name) { case "Potion": ID = 1; case "Super Potion": ID = 2; case "Hyper Potion": ID = 3; case "Revive": ID = 4; case "Max Revive": ID = 5; } } public String getName() { return name; } public int getID() { return ID; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Potion extends Item{ private int healValue; public Potion(String name, int healValue) { super(name); this.healValue = healValue; } public int getHealValue() { return healValue; } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Revive extends Item { private double revival; public Revive(String name, double revival) { super(name); this.revival = revival; } public double getRevival() { return revival; } }
Revision: 71322
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 20, 2017 07:05 by bsauberman
Initial Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author bensauberman */ public class PokemonBattle { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here System.out.println("Welcome to Pokemon Simulator. Please enter your name."); Scanner sc = new Scanner(System.in); String trainerName = sc.next(); System.out.println("Welcome " + trainerName + "!"); Pokemon[] myParty = new Pokemon[7]; Pokemon[] oppParty = new Pokemon[100]; File PokemonFile = new File("/Users/bensauberman/Desktop/CS/AP CS/PokemonFile.txt"); Scanner fr = new Scanner(PokemonFile).useDelimiter(","); int stoppedLine = 0; stoppedLine = readFile(fr, myParty, oppParty, stoppedLine); battle(myParty, oppParty, trainerName); resetParties(myParty, oppParty); stoppedLine = readFile(fr, myParty, oppParty, stoppedLine); battle(myParty, oppParty, trainerName); } public static int readFile(Scanner fr, Pokemon myParty[], Pokemon oppParty[], int line) { int lineCounter = -1; int stoppedLine = 0; while (fr.hasNextLine()) { lineCounter++; if(lineCounter < line) { fr.nextLine(); } String vL = fr.next(); if(vL.equals("||")) { fr.nextLine(); String info = fr.nextLine(); addPokemon(info, myParty); } if(vL.equals("|")) { fr.nextLine(); String info = fr.nextLine(); addMove(info, myParty); } if(vL.equals("||||")) { fr.nextLine(); String info = fr.nextLine(); addPokemon(info, oppParty); } if(vL.equals("|||")) { fr.nextLine(); String info = fr.nextLine(); addMove(info, oppParty); } if(vL.equals("|||||")) { stoppedLine = lineCounter; return stoppedLine; } if(vL.charAt(0) == ('/')) { fr.nextLine(); } } return stoppedLine; } public static void printParty(Pokemon party[]) { System.out.println("Pokemon HP Level Type"); for(int x = 0; x < 7; x++) { if(party[x] != null) { int spaceCount = (14 - party[x].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(party[x].getName() + spaces + party[x].getnHP() + " " + party[x].getLevel() + " " + party[x].getType()); } } } public static void addPokemon(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); int pHP = fr.nextInt(); int pLevel = fr.nextInt(); String pType = fr.next(); Pokemon newPokemon = new Pokemon(pName, pHP, pLevel, pType); for(int pSlot = 0; pSlot < 7; pSlot++) { if(party[pSlot] == null || party[pSlot].getName().equals(pName)) { party[pSlot] = newPokemon; break; } } } public static void addMove(String info, Pokemon party[]) { Scanner fr = new Scanner(info).useDelimiter(","); String pName = fr.next(); String mName = fr.next(); int mPP = fr.nextInt(); int mDamage = fr.nextInt(); int mAccuracy = fr.nextInt(); int mSpeed = fr.nextInt(); String mType = fr.next(); Move newMove = new Move(mName, mPP, mDamage, mAccuracy, mSpeed, mType); int pSlot = 10; y: for(int x = 0; x < 7; x++) { if(party[x].getName().equals(pName)) { pSlot = x; break y; } } if(pSlot == 10) { throw new IllegalArgumentException("The move " + mName + " cannot be added because the " + "Pokemon it belongs to is not currently in the party."); } boolean freeSpot = false; y: for(int mSlot = 0; mSlot < 4; mSlot++) { if(party[pSlot].getMoves()[mSlot] == null) { party[pSlot].getMoves()[mSlot] = newMove; freeSpot = true; System.out.println(mName + " added to " + pName); break y; } } //REPLACES MOVE IF ALL 4 SLOTS ARE FILLED if(!freeSpot) { System.out.println("Which move would you like to replace for " + mName); party[pSlot].printMoves(); System.out.println("4: Do not add."); Scanner sc = new Scanner(System.in); int mSlot = sc.nextInt(); if(mSlot >= 0 && mSlot <= 3) { party[pSlot].addMove(newMove, mSlot); System.out.println(mName + " added to " + pName); } else { System.out.println(mName + " not added."); } } } public static void battle(Pokemon myParty[], Pokemon oppParty[], String trainerName) throws FileNotFoundException { System.out.println(); System.out.println(); System.out.println("Welcome to the battle!"); int oppPokemonCounter = 0; for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { oppPokemonCounter++; } } System.out.println("Your opponent has " + oppPokemonCounter + " pokemon."); System.out.println(); System.out.print("Your party:"); Pokemon myPokemon = pickMyPokemon(myParty); Pokemon oppPokemon = pickOppPokemon(oppParty); System.out.println(); System.out.println("The opponent sends out " + oppPokemon.getName()); while(checkForValidPokemon(myParty) && checkForValidPokemon(oppParty)) { System.out.println(); System.out.println(trainerName + "'s Party: "); printParty(myParty); System.out.println(); System.out.println("Opponent's Party: "); printParty(oppParty); System.out.println(); Move myMove = null; Move oppMove = null; System.out.println("What would you like to do?"); //MY OPTIONS int userChoice = chooseOption(); if(userChoice == 1) { int mSlot = pickMyMove(myPokemon, myParty); if(mSlot == -2) { userChoice = 2; } else { myMove = myPokemon.getMoves()[mSlot]; } } if(userChoice == 2) { myPokemon = pickMyPokemon(myParty); } if(userChoice == 3) { } //OPPONENTS OPTIONS boolean hasMadeTurn = false; while(!hasMadeTurn) { if(checkForValidMove(oppPokemon) == false) { oppPokemon = pickOppPokemon(oppParty); System.out.println("The opponent has sent out " + oppPokemon); hasMadeTurn = true; } boolean hasPickedValidMove = false; int randomMoveCounter = 0; while(!hasPickedValidMove) { int randomMove = (int)(4.0 * Math.random()); randomMoveCounter++; if(oppPokemon.getMoves()[randomMove] != null) { if(oppPokemon.getMoves()[randomMove].getnPP() > 0) { oppMove = oppPokemon.getMoves()[randomMove]; hasPickedValidMove = true; hasMadeTurn = true; } } if(randomMoveCounter >= 200) { oppPokemon = pickOppPokemon(oppParty); System.out.println("The opponent has sent out " + oppPokemon); hasMadeTurn = true; hasPickedValidMove = true; } } } useMoves(myMove, oppMove, myPokemon, oppPokemon); } if(!checkForValidPokemon(myParty)) { System.out.println(); printResult(false); } if(!checkForValidPokemon(oppParty)) { System.out.println(); printResult(true); } } public static void useMoves(Move myMove, Move oppMove, Pokemon myPokemon, Pokemon oppPokemon) throws FileNotFoundException { int myMoveSlot = -1; int oppMoveSlot = -1; for(int x = 0; x < 4; x++) { if(myMove != null) { if(myPokemon.getMoves()[x] != null) { if(myMove.getName().equals(myPokemon.getMoves()[x].getName())) { myMoveSlot = x; } } } if(oppMove != null) { if(oppPokemon.getMoves()[x] != null) { if(oppMove.getName().equals(oppPokemon.getMoves()[x].getName())) { oppMoveSlot = x; } } } } if(myMove == null & oppMove == null) { return; } if(myMove != null && oppMove == null) { useMove(myMove, myPokemon, oppPokemon); return; } if(myMove == null && oppMove != null) { useMove(oppMove, oppPokemon, myPokemon); return; } if(myMove != null && oppMove != null) { if(myMove.getSpeed() >= oppMove.getSpeed()) { boolean shouldContinue = useMove(myMove, myPokemon, oppPokemon); if(shouldContinue) { useMove(oppMove, oppPokemon, myPokemon); } } if((oppMove.getSpeed() > myMove.getSpeed()) || oppMoveSlot == -1) { boolean shouldContinue = useMove(oppMove, oppPokemon, myPokemon); if(shouldContinue) { useMove(myMove, myPokemon, oppPokemon); } } } } public static boolean useMove(Move attMove, Pokemon attPokemon, Pokemon defPokemon) throws FileNotFoundException { int accuracyGenerator = (int)(100.0 * Math.random() +1); if(attMove.getAccuracy() > accuracyGenerator) { int HPBeforeAttack = defPokemon.getnHP(); attMove.decnPP(); double multiplier = decideEffectiveness(attMove.getType(), defPokemon.getType()); defPokemon.setnHP((int)(attMove.getDamage() *-multiplier)); if(defPokemon.getnHP() <= 0) { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " and " + defPokemon.getName() + " fainted."); if(multiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(multiplier == 2.0) { System.out.println(attMove.getName() + " was super effective."); } calcEXP(attPokemon, defPokemon); return false; } else if (multiplier == 0) { System.out.println(attMove.getName() + " does not affect " + defPokemon.getName()); } else { System.out.println(attPokemon.getName() + " used " + attMove.getName() + " decreasing " + defPokemon.getName() + " HP from " + HPBeforeAttack + " to " + defPokemon.getnHP()); if(multiplier == 0.5) { System.out.println(attMove.getName() + " was not very effective."); } if(multiplier == 2.0) { System.out.println(attMove.getName() + " was super effective."); } return true; } } else { System.out.println(attPokemon.getName() + " tried to use " + attMove.getName() + " but his attack missed!"); return true; } return true; } public static Pokemon pickMyPokemon(Pokemon myParty[]) { System.out.println(); System.out.println(); if(checkForValidPokemon(myParty) == false) { System.out.println("No more available Pokemon. Try Again."); return null; } printParty(myParty); boolean isValid = false; while(!isValid) { System.out.println(); System.out.println("Which Pokemon would you like to send?"); Scanner s = new Scanner(System.in); String requested = s.next(); for(int x = 0; x < 7; x++) { if(myParty[x] != null) { if(myParty[x].getName().equals(requested)) { if(myParty[x].getnHP() <= 0) { System.out.println(myParty[x].getName() + "has an HP of 0 and can therefore not battle. " + " Choose another Pokemon."); } else { isValid = true; return myParty[x]; } } } } System.out.println("The Pokemon entered is not in the party and therefore cannot be sent out." + " Choose another Pokemon."); } return null; } public static Pokemon pickOppPokemon(Pokemon oppParty[]) { if(checkForValidPokemon(oppParty) == false) { printResult(true); } for(int x = 0; x < 7; x++) { if(oppParty[x] != null) { if(oppParty[x].getnHP() > 0) { if(checkForValidMove(oppParty[x])) { return oppParty[x]; } } } } return null; } public static Boolean checkForValidPokemon(Pokemon party[]) { for(int x = 0; x < 7; x++) { if(party[x] != null && party[x].getnHP() > 0) { return true; } } return false; } public static Boolean checkForValidMove(Pokemon pokemon) { for(int x = 0; x < 4; x++) { if(pokemon.getMoves()[x] != null) { if(pokemon.getMoves()[x].getnPP() > 0) { return true; } } } return false; } public static int pickMyMove(Pokemon myPokemon, Pokemon myParty[]) { if(!checkForValidMove(myPokemon)) { System.out.println("This Pokemon has no PP left in any of his moves. Please change Pokemon."); /*Pokemon newPokemon = pickMyPokemon(myParty); for(int x = 0; x < 7; x++) { if(newPokemon.getName() == myParty[x].getName()) { return x*-1; } }*/ return -2; } System.out.println(); System.out.println("Pick a move."); myPokemon.printMoves(); boolean isValid = false; while(!isValid) { Scanner s = new Scanner(System.in); int requestedMove = s.nextInt(); if(requestedMove >= 1 && requestedMove <= 4) { if(myPokemon.getMoves()[requestedMove-1] != null) { if(myPokemon.getMoves()[requestedMove-1].getnPP() > 0) { return requestedMove-1; } else { System.out.println("That move has no PP left. Please pick another move."); } } else { System.out.println("There is no move in that slot. Please pick a valid move."); } } else { System.out.println("The entered number must be a valid number between 1 and 4. Please pick a valid move."); } } return -1; } public static void printResult(boolean result) { if(result) { System.out.println("You win the battle!"); } if(!result) { System.out.println("You lose the battle."); } } public static int chooseOption() { System.out.println("1: Pick a move."); System.out.println("2: Change Pokemon."); System.out.println("3: Use item."); int userChoice = 0; boolean isValid = false; while (isValid == false) { Scanner s = new Scanner(System.in); userChoice = s.nextInt(); if(userChoice < 1 || userChoice > 3) { System.out.println("Please enter a valid option."); } else { isValid = true; } } return userChoice; } public static int effectivenessNumber(String type) { switch(type) { case "Normal": return 0; case "Fire": return 1; case "Water": return 2; case "Electric": return 3; case "Grass": return 4; case "Ice": return 5; case "Fighting": return 6; case "Poison": return 7; case "Ground": return 8; case "Flying": return 9; case "Psychic": return 10; case "Bug": return 11; case "Rock": return 12; case "Ghost": return 13; case "Dragon": return 14; case "Dark": return 15; case "Steel": return 16; } return -1; } public static double decideEffectiveness(String moveTypeWord, String defPokemonTypeWord) throws FileNotFoundException { int moveType = effectivenessNumber(moveTypeWord); int defPokemonType = effectivenessNumber(defPokemonTypeWord); double[][] effectivenessChart = new double[17][17]; File EffectivenessChart = new File("/Users/bensauberman/Desktop/CS/AP CS/EffectivenessChart.txt"); Scanner fr2 = new Scanner(EffectivenessChart).useDelimiter(","); for(int x = 0; x < 17; x++) { for(int y = 0; y < 17; y++) { effectivenessChart[x][y] = fr2.nextDouble(); } fr2.nextLine(); } fr2.close(); return effectivenessChart[moveType][defPokemonType]; } public static void calcEXP(Pokemon attPokemon, Pokemon defPokemon) { int previousEXP = attPokemon.getEXP(); int previousMaxEXP = attPokemon.getEXP(); attPokemon.addEXP((defPokemon.getLevel() / 5)+5); if(attPokemon.getEXP() > attPokemon.getMaxEXP()) { attPokemon.incLevel(); attPokemon.setMaxEXP(); attPokemon.setEXP(attPokemon.getEXP() - previousMaxEXP); System.out.println(attPokemon.getName() + " has grown from Level " + (attPokemon.getLevel()-1) + " to Level " + attPokemon.getLevel()); } System.out.println(attPokemon.getName() + " now has " + attPokemon.getEXP() + " out of " + attPokemon.getMaxEXP() + " EXP to get from " + "Level " + attPokemon.getLevel() + " to Level " + (attPokemon.getLevel()+1)); } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Pokemon { private String name; private int nHP; private int rHP; private int level; private int EXP; private int maxEXP; private String type; Move[] myMoves = new Move[4]; public Pokemon(String name, int HP, int level, String type) { this.name = name; this.rHP = HP; this.nHP = HP; this.level = level; this.EXP = 0; maxEXP = (level * 10); this.type = type; } public String getType() { return type; } public String getName() { return name; } public int getnHP() { return nHP; } public int getrHP() { return rHP; } public void setnHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP nHP += HPchanger; if(nHP <= 0) { nHP = 0; } } public void setrHP(int HPchanger) { //MAKE SURE HPCHANGER IS NEGATIVE IF YOU WANT TO DECREASE HP rHP += HPchanger; nHP = rHP; if(rHP <= 0) { rHP = 0; nHP = 0; } } public int getLevel() { return level; } public int getEXP() { return EXP; } public int getMaxEXP() { return maxEXP; } public void incLevel() { level++; } public void addEXP(int additionalEXP) { EXP += additionalEXP; } public void setEXP(int newEXP) { EXP = newEXP; } public void setMaxEXP() { maxEXP = level * 20; } public void addMove(Move m, int slot) { myMoves[slot] = m; } public Move[] getMoves() { return myMoves; } public void printMoves() { System.out.println(" Name Damage Accuracy PP Speed Type"); for(int move = 0; move < 4; move++) { if(myMoves[move] != null) { int spaceCount = (12 - myMoves[move].getName().length()); StringBuilder spaces = new StringBuilder(); for(int y = 0; y < spaceCount; y++) { spaces.append(" "); } System.out.println(move+1 + ": " + myMoves[move].getName() + spaces + myMoves[move].getDamage() + " " + myMoves[move].getAccuracy() + " " + myMoves[move].getnPP() + " " + myMoves[move].getSpeed() + " " + myMoves[move].getType()); } else { System.out.println(move+1 + ": ---"); } } } } /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pokemonbattle; /** * * @author bensauberman */ public class Move { private String name; private int damage; private int accuracy; private int rPP; //r stands for regular. This is the attribute before affected by the battle. //n stands for new. This is the attribute affected midbattle. private int nPP; private int speed; private String type; public Move(String name, int PP, int damage, int accuracy, int speed, String type) { this.name = name; this.rPP = PP; this.nPP = PP; this.damage = damage; this.accuracy = accuracy; this.speed = speed; this.type = type; } public String getName() { return name; } public int getrPP() { return rPP; } public int getnPP() { return nPP; } public void decnPP() { nPP--; } public int getDamage() { return damage; } public void setDamage() { damage += damage * .1; } public int getAccuracy() { return accuracy; } public int getSpeed() { return speed; } public String getType() { return type; } }
Initial URL
Initial Description
Finished :) by Ben Sauberman
Initial Title
Pokemon Battle Simulator
Initial Tags
Initial Language
Java