diff --git a/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java b/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java new file mode 100644 index 0000000..276c8e1 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java @@ -0,0 +1,43 @@ +package com.codefortomorrow.advanced.chapter13.examples; + +public class BinarySearch { + + // Java implementation of recursive Binary Search + // Returns index of x if it is present in arr[l.. + // r], else return -1 + public static int binarySearch(int arr[], int l, int r, int x) { + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the + // middle itself + if (arr[mid] == x) return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not present + // in array + return -1; + } + + // Driver method to test above + public static void main(String args[]) { + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = binarySearch(arr, 0, n - 1, x); + if (result == -1) System.out.println( + "Element not present" + ); else System.out.println("Element found at index " + result); + } + /* This code is contributed by Rajat Mishra + https://www.geeksforgeeks.org/binary-search/ */ + +} diff --git a/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java b/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java new file mode 100644 index 0000000..700e810 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java @@ -0,0 +1,29 @@ +package com.codefortomorrow.advanced.chapter13.examples; + +public class Fibonacci { + + public static int fibonacciRecursion(int nthNumber) { + //use recursion + if (nthNumber == 0) { + return 0; + } else if (nthNumber == 1) { + return 1; + } + return ( + fibonacciRecursion(nthNumber - 1) + + fibonacciRecursion(nthNumber - 2) + ); + } + + public static int fibonacciLoop(int nthNumber) { + //use loop + int previouspreviousNumber, previousNumber = 0, currentNumber = 1; + for (int i = 1; i < nthNumber; i++) { + previouspreviousNumber = previousNumber; + previousNumber = currentNumber; + currentNumber = previouspreviousNumber + previousNumber; + } + return currentNumber; + } + // Credit: https://dev.to/khalilsaboor/fibonacci-recursion-vs-iteration--474l +} diff --git a/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java b/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java new file mode 100644 index 0000000..9708f35 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java @@ -0,0 +1,6 @@ +package com.codefortomorrow.advanced.chapter13.practice; + +public class RecurMergeSort { + + public static void recurMergeSort(Comparable[] array, int start, int end) {} +} diff --git a/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java b/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java new file mode 100644 index 0000000..da7c9c9 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java @@ -0,0 +1,42 @@ +package com.codefortomorrow.advanced.chapter13.solutions; + +public class RecurMergeSort { + + public static void recurMergeSort(Comparable[] array, int start, int end) { + if (start >= end) { + return; + } + int middle = (start + end) / 2; + recurMergeSort(array, start, middle); + recurMergeSort(array, middle + 1, end); + int i = start; + int j = middle + 1; + Comparable[] sortedArray = new Comparable[end - start + 1]; + int k = 0; + while (i <= middle && j <= end) { + if (array[i].compareTo(array[j]) > 0) { + sortedArray[k] = array[i]; + i++; + } else { + sortedArray[k] = array[j]; + j++; + } + k++; + } + while (i <= middle) { + sortedArray[k] = array[i]; + i++; + k++; + } + while (j <= end) { + sortedArray[k] = array[j]; + j++; + k++; + } + k = 0; + for (int l = start; l <= end; l++) { + array[l] = sortedArray[k]; + k++; + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java b/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java new file mode 100644 index 0000000..c43fd98 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java @@ -0,0 +1,41 @@ +package com.codefortomorrow.advanced.chapter14.examples; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +/** + * Source: https://beginnersbook.com/2014/01/how-to-read-file-in-java-using-bufferedreader/ + */ +public class BufferedReaderExample { + + public static void main(String[] args) { + BufferedReader br = null; + BufferedReader br2 = null; + + try { + br = new BufferedReader(new FileReader("B:\\myfile.txt")); + + //One way of reading the file + System.out.println("Reading the file using readLine() method:"); + String contentLine = br.readLine(); + while (contentLine != null) { + System.out.println(contentLine); + contentLine = br.readLine(); + } + + br2 = new BufferedReader(new FileReader("B:\\myfile2.txt")); + + //Second way of reading the file + System.out.println("Reading the file using read() method:"); + int num = 0; + char ch; + while ((num = br2.read()) != -1) { + ch = (char) num; + System.out.print(ch); + } + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java new file mode 100644 index 0000000..0842368 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class InvalidAgeException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java new file mode 100644 index 0000000..f6f6029 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class InvalidPasswordException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java new file mode 100644 index 0000000..677b349 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class InvalidUsernameException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java new file mode 100644 index 0000000..5dcaf7a --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java @@ -0,0 +1,36 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Welcome to Account Creation!"); + while (true) { + System.out.print("Enter username (4 to 10 characters): "); + String username = sc.next(); + System.out.print("Enter age: "); + int age = sc.nextInt(); + System.out.print("Enter password (4 to 10 characters): "); + String password = sc.next(); + System.out.print("Confirm password (4 to 10 characters): "); + String confirmPassword = sc.next(); + + // TODO: try and catch to handle exceptions + createAccount(username, password, age, confirmPassword); + System.out.println("Account created successfully!"); + } + } + + public static void createAccount( + String username, + String password, + int age, + String confirmPassword + ) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + // TODO: complete + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java new file mode 100644 index 0000000..e2f7c98 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class PasswordMismatchException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java new file mode 100644 index 0000000..17084ef --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class InvalidAgeException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java new file mode 100644 index 0000000..8f65f31 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class InvalidPasswordException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java new file mode 100644 index 0000000..68ccbc2 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class InvalidUsernameException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java new file mode 100644 index 0000000..2000e41 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java @@ -0,0 +1,57 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Welcome to Account Creation!"); + while (true) { + System.out.print("Enter username (4 to 10 characters): "); + String username = sc.next(); + System.out.print("Enter age: "); + int age = sc.nextInt(); + System.out.print("Enter password (4 to 10 characters): "); + String password = sc.next(); + System.out.print("Confirm password (4 to 10 characters): "); + String confirmPassword = sc.next(); + + try { + createAccount(username, password, age, confirmPassword); + System.out.println("Account created successfully!"); + break; + } catch (InvalidUsernameException e) { + System.out.println("Invalid username."); + } catch (InvalidPasswordException e) { + System.out.println("Invalid password."); + } catch (InvalidAgeException e) { + System.out.println("Must be 18 or older to create an account."); + } catch (PasswordMismatchException e) { + System.out.println("Passwords don't match!"); + } + } + } + + public static void createAccount( + String username, + String password, + int age, + String confirmPassword + ) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + if (username.length() < 4 || username.length() > 10) { + throw new InvalidUsernameException(); + } + if (password.length() < 4 || password.length() > 10) { + throw new InvalidPasswordException(); + } + if (age < 18) { + throw new InvalidAgeException(); + } + if (!password.equals(confirmPassword)) { + throw new PasswordMismatchException(); + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java new file mode 100644 index 0000000..248d178 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java @@ -0,0 +1,3 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class PasswordMismatchException extends Exception {} diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java new file mode 100644 index 0000000..961e20b --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java @@ -0,0 +1,52 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +import java.util.Scanner; + +public class Battle { + private Scanner sc = new Scanner(System.in); + private Pokemon pokemonOne; + private Pokemon pokemonTwo; + private int turn; + + public Battle(Pokemon pokemonOne, Pokemon pokemonTwo) { + this.pokemonOne = pokemonOne; + this.pokemonTwo = pokemonTwo; + turn = 1; + } + + public void runBattle() { + while (true) { + if (turn == 1) { + executeTurn(pokemonOne, pokemonTwo); + } else { + executeTurn(pokemonTwo, pokemonOne); + } + System.out.println(pokemonOne); + System.out.println(pokemonTwo); + if ( + pokemonOne.getCurrentHP() <= 0 || pokemonTwo.getCurrentHP() <= 0 + ) { + break; + } + } + } + + public void executeTurn(Pokemon pokemon, Pokemon other) { + System.out.println("It's " + pokemon.getName() + "'s move!"); + + Move[] pokemonMoves = pokemon.getMoveList(); + for (int i = 1; i <= pokemonMoves.length; i++) { + System.out.println(i + ": " + pokemonMoves[i - 1].getName()); + } + System.out.print("Move #: "); + int move = sc.nextInt(); + pokemon.attack(other, move - 1); + System.out.println(); + + if (turn == 1) { + turn = 0; + } else { + turn = 1; + } + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java new file mode 100644 index 0000000..185a151 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java @@ -0,0 +1,62 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +public class Main { + + public static void main(String[] args) { + Move a1 = new Move("Water Gun", false, false, false, false, false, 15); + Move a2 = new Move("Tackle", false, false, false, false, false, 10); + Move a3 = new Move("Bubble", false, false, false, false, false, 16); + Move a4 = new Move( + "Water Splash", + false, + false, + false, + false, + false, + 6 + ); + Move[] moveListA = { a1, a2, a3, a4 }; + Pokemon a = new Pokemon( + moveListA, + 40, + 0, + "Squirtle", + "Water", + "Squirtle", + 1 + ); + + Move b1 = new Move( + "Thunder Shock", + false, + false, + false, + false, + false, + 25 + ); + Move b2 = new Move("Slam", false, false, false, false, false, 16); + Move b3 = new Move( + "Quick Attack", + false, + false, + false, + false, + false, + 10 + ); + Move b4 = new Move("Thunder", false, false, false, false, false, 6); + Move[] moveListB = { b1, b2, b3, b4 }; + Pokemon b = new Pokemon( + moveListB, + 30, + 0, + "Pikachu", + "Electric", + "Pikachu", + 1 + ); + Battle battle = new Battle(a, b); + battle.runBattle(); + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java new file mode 100644 index 0000000..7e2559f --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java @@ -0,0 +1,55 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +/** + * represents a Pokemon's move + */ +public class Move { + private String name; + private boolean poisoning; + private boolean sleeping; + private boolean paralyzing; + private boolean burning; + private boolean freezing; + private int baseDamage; + + /** + * Constructs a Move object. This has been done for you. + * @param name move's name + * @param poisoning whether the move poisons + * @param sleeping whether the move sleeps + * @param paralyzing whether the move paralyzes + * @param burning whether the move burns + * @param freezing whether the move freezes + * @param baseDamage base damage of the move + */ + public Move( + String name, + boolean poisoning, + boolean sleeping, + boolean paralyzing, + boolean burning, + boolean freezing, + int baseDamage + ) { + this.name = name; + this.poisoning = poisoning; + this.sleeping = sleeping; + this.paralyzing = paralyzing; + this.burning = burning; + this.freezing = freezing; + this.baseDamage = baseDamage; + } + + /** + * calculates the move's damage by multiplying the base damage of the move by the given level + * @param level the Pokemon's level + * @return the move's total damage + */ + public int calculateDamage(int level) { + return 0; //TODO: Fix + } + + public String getName() { + return name; + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java new file mode 100644 index 0000000..277ffd6 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java @@ -0,0 +1,109 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +/** + * represents a Pokemon object + */ +public class Pokemon { + private Move[] moveList; + private int maxHP; + private int currentHP; + private int XP; + private String species; + private String type; + private String name; + private int level; + private boolean poisoned; + private boolean asleep; + private boolean paralyzed; + private boolean burned; + private boolean frozen; + private boolean fainted; + + /** + * Complete this constructor. The move list should be initialized to an empty array of size 4. + * Both Max HP and Current HP should be set to 40. Name should be set to species. Level should be + * set to 1. XP should be set to 0. All effects should be set to false, including fainted. + * @param species the species of the Pokemon + * @param type type of the Pokemon + */ + public Pokemon(String species, String type) {} + + /** + * Complete this constructor. See the above constructor for default values. + * @param species the species of the Pokemon + * @param type of the Pokemon + * @param name the name/nickname of the Pokemon + */ + public Pokemon(String species, String type, String name) {} + + /** + * Complete this constructor. See the first constructor for default values. + */ + public Pokemon( + Move[] moveList, + int maxHP, + int XP, + String species, + String type, + String name, + int level + ) {} + + /** + * Places given move in the moveList at the given index. If a move was previously present, it + * is replaced, and returned + * @param index the index to place the move + * @param move the move to learn + * @return old move or null if not applicable + */ + public Move learnMove(int index, Move move) { + return null; // TODO: Fix + } + + /** + * attacks enemy Pokemon with the move at the given moveIndex + * @param enemy enemy Pokemon + * @param moveIndex index of the move to make + */ + public void attack(Pokemon enemy, int moveIndex) { + // Type your code here + System.out.println( + name + + " used " + + moveList[moveIndex].getName() + + " on " + + enemy.getName() + ); + System.out.println(enemy.getName() + " took " + " damage."); // TODO: Fix + } + + /** + * Subtracts damage from currentHP. Returns true if Pokemon fainted + * @param damage damage to take + */ + public boolean takeDamage(int damage) { + return false; //TODO: Fix + } + + /** + * Adds given HP to current HP + * @param hp given hp to heal + */ + public void heal(int hp) {} + + public String getName() { + return ""; //TODO: Fix + } + + public int getCurrentHP() { + return 0; // TODO: Fix + } + + public Move[] getMoveList() { + return null; // TODO: Fix + } + + public String toString() { + return name + "\n" + "HP: " + currentHP + " / " + maxHP; + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java new file mode 100644 index 0000000..9689388 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java @@ -0,0 +1,52 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; + +import java.util.Scanner; + +public class Battle { + private Scanner sc = new Scanner(System.in); + private Pokemon pokemonOne; + private Pokemon pokemonTwo; + private int turn; + + public Battle(Pokemon pokemonOne, Pokemon pokemonTwo) { + this.pokemonOne = pokemonOne; + this.pokemonTwo = pokemonTwo; + turn = 1; + } + + public void runBattle() { + while (true) { + if (turn == 1) { + executeTurn(pokemonOne, pokemonTwo); + } else { + executeTurn(pokemonTwo, pokemonOne); + } + System.out.println(pokemonOne); + System.out.println(pokemonTwo); + if ( + pokemonOne.getCurrentHP() <= 0 || pokemonTwo.getCurrentHP() <= 0 + ) { + break; + } + } + } + + public void executeTurn(Pokemon pokemon, Pokemon other) { + System.out.println("It's " + pokemon.getName() + "'s move!"); + + Move[] pokemonMoves = pokemon.getMoveList(); + for (int i = 1; i <= pokemonMoves.length; i++) { + System.out.println(i + ": " + pokemonMoves[i - 1].getName()); + } + System.out.print("Move #: "); + int move = sc.nextInt(); + pokemon.attack(other, move - 1); + System.out.println(); + + if (turn == 1) { + turn = 0; + } else { + turn = 1; + } + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java new file mode 100644 index 0000000..edc0a34 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java @@ -0,0 +1,62 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; + +public class Main { + + public static void main(String[] args) { + Move a1 = new Move("Water Gun", false, false, false, false, false, 15); + Move a2 = new Move("Tackle", false, false, false, false, false, 10); + Move a3 = new Move("Bubble", false, false, false, false, false, 16); + Move a4 = new Move( + "Water Splash", + false, + false, + false, + false, + false, + 6 + ); + Move[] moveListA = { a1, a2, a3, a4 }; + Pokemon a = new Pokemon( + moveListA, + 40, + 0, + "Squirtle", + "Water", + "Squirtle", + 1 + ); + + Move b1 = new Move( + "Thunder Shock", + false, + false, + false, + false, + false, + 25 + ); + Move b2 = new Move("Slam", false, false, false, false, false, 16); + Move b3 = new Move( + "Quick Attack", + false, + false, + false, + false, + false, + 10 + ); + Move b4 = new Move("Thunder", false, false, false, false, false, 6); + Move[] moveListB = { b1, b2, b3, b4 }; + Pokemon b = new Pokemon( + moveListB, + 30, + 0, + "Pikachu", + "Electric", + "Pikachu", + 1 + ); + Battle battle = new Battle(a, b); + battle.runBattle(); + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java new file mode 100644 index 0000000..257c231 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java @@ -0,0 +1,55 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; + +/** + * represents a Pokemon's move + */ +public class Move { + private String name; + private boolean poisoning; + private boolean sleeping; + private boolean paralyzing; + private boolean burning; + private boolean freezing; + private int baseDamage; + + /** + * Constructs a Move object. This has been done for you. + * @param name move's name + * @param poisoning whether the move poisons + * @param sleeping whether the move sleeps + * @param paralyzing whether the move paralyzes + * @param burning whether the move burns + * @param freezing whether the move freezes + * @param baseDamage base damage of the move + */ + public Move( + String name, + boolean poisoning, + boolean sleeping, + boolean paralyzing, + boolean burning, + boolean freezing, + int baseDamage + ) { + this.name = name; + this.poisoning = poisoning; + this.sleeping = sleeping; + this.paralyzing = paralyzing; + this.burning = burning; + this.freezing = freezing; + this.baseDamage = baseDamage; + } + + /** + * calculates the move's damage by multiplying the base damage of the move by the given level + * @param level the Pokemon's level + * @return the move's total damage + */ + public int calculateDamage(int level) { + return level * baseDamage; + } + + public String getName() { + return name; + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java new file mode 100644 index 0000000..2050900 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java @@ -0,0 +1,139 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; + +/** + * represents a Pokemon object + */ +public class Pokemon { + private Move[] moveList; + private int maxHP; + private int currentHP; + private int XP; + private String species; + private String type; + private String name; + private int level; + private boolean poisoned; + private boolean asleep; + private boolean paralyzed; + private boolean burned; + private boolean frozen; + private boolean fainted; + + /** + * Complete this constructor. The move list should be initialized to an empty array of size 4. + * Both Max HP and Current HP should be set to 40. Name should be set to species. Level should be + * set to 1. XP should be set to 0. All effects should be set to false, including fainted. + * @param species the species of the Pokemon + * @param type type of the Pokemon + */ + public Pokemon(String species, String type) { + this.species = species; + this.type = type; + } + + /** + * Complete this constructor. See the above constructor for default values. + * @param species the species of the Pokemon + * @param type of the Pokemon + * @param name the name/nickname of the Pokemon + */ + public Pokemon(String species, String type, String name) { + this.species = species; + this.type = type; + this.name = name; + } + + /** + * Complete this constructor. See the first constructor for default values. + */ + public Pokemon( + Move[] moveList, + int maxHP, + int XP, + String species, + String type, + String name, + int level + ) { + this.moveList = moveList; + this.maxHP = maxHP; + this.XP = XP; + this.species = species; + this.type = type; + this.name = name; + this.level = level; + currentHP = maxHP; + } + + /** + * Places given move in the moveList at the given index. If a move was previously present, it + * is replaced, and returned + * @param index the index to place the move + * @param move the move to learn + * @return old move or null if not applicable + */ + public Move learnMove(int index, Move move) { + Move oldMove = moveList[index]; + moveList[index] = move; + return oldMove; + } + + /** + * attacks enemy Pokemon with the move at the given moveIndex + * @param enemy enemy Pokemon + * @param moveIndex index of the move to make + */ + public void attack(Pokemon enemy, int moveIndex) { + int damage = moveList[moveIndex].calculateDamage(level); + enemy.takeDamage(damage); + System.out.println( + name + + " used " + + moveList[moveIndex].getName() + + " on " + + enemy.getName() + ); + System.out.println(enemy.getName() + " took " + damage + " damage."); + } + + /** + * Subtracts damage from currentHP. Returns true if Pokemon fainted + * @param damage damage to take + */ + public boolean takeDamage(int damage) { + currentHP -= damage; + if (currentHP < 0) { + currentHP = 0; + return true; + } + return false; + } + + /** + * Adds given HP to current HP + * @param hp given hp to heal + */ + public void heal(int hp) { + if (currentHP + hp > maxHP) { + currentHP = maxHP; + return; + } + currentHP += hp; + } + + public String getName() { + return name; + } + + public int getCurrentHP() { + return currentHP; + } + + public Move[] getMoveList() { + return moveList; + } + + public String toString() { + return name + "\n" + "HP: " + currentHP + " / " + maxHP; + } +}