implement Game class
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
package org.project;
|
||||
|
||||
import org.project.entity.enemies.*;
|
||||
import org.project.entity.players.Assassin;
|
||||
import org.project.entity.players.Knight;
|
||||
import org.project.entity.players.Player;
|
||||
import org.project.entity.players.Wizard;
|
||||
import org.project.location.Location;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Game {
|
||||
private final ArrayList<Player> players;
|
||||
private final ArrayList<Enemy> regularEnemies;
|
||||
private final ArrayList<Location> locations;
|
||||
private final Scanner sc;
|
||||
private Player player;
|
||||
private final ArrayList<Class<? extends Enemy>> collectedKeyTypes = new ArrayList<>();
|
||||
|
||||
public Game() {
|
||||
sc = new Scanner(System.in);
|
||||
|
||||
players = new ArrayList<>();
|
||||
players.add(new Assassin());
|
||||
players.add(new Knight());
|
||||
players.add(new Wizard());
|
||||
|
||||
regularEnemies = new ArrayList<>();
|
||||
regularEnemies.add(new Goblin());
|
||||
regularEnemies.add(new Skeleton());
|
||||
regularEnemies.add(new Vampire());
|
||||
|
||||
locations = new ArrayList<>();
|
||||
locations.add(new Location("Jungle", regularEnemies));
|
||||
locations.add(new Location("China", new ArrayList<>(regularEnemies.subList(0, 2))));
|
||||
locations.add(new Location("Iran", new ArrayList<>(regularEnemies.subList(1, 3))));
|
||||
}
|
||||
|
||||
public void start() {
|
||||
player = choosePlayer();
|
||||
|
||||
while (true) {
|
||||
displayKeys();
|
||||
Location location = chooseLocation();
|
||||
Enemy enemy = chooseEnemy(location);
|
||||
|
||||
System.out.println("Wanna fight?\n1. Yes\n2. No");
|
||||
int choice = sc.nextInt();
|
||||
if (choice != 1) continue;
|
||||
|
||||
combat(player, enemy);
|
||||
System.out.println("Player HP: " + player.getHP());
|
||||
|
||||
if (!player.isAlive()) {
|
||||
System.out.println("Game over.");
|
||||
break;
|
||||
}
|
||||
|
||||
if (canChallengeDragon()) {
|
||||
System.out.println("\nYou have collected all keys! The Dragon awaits...");
|
||||
System.out.println("Challenge the Dragon?\n1. Yes\n2. No");
|
||||
if (sc.nextInt() == 1) {
|
||||
challengeDragon();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean canChallengeDragon() {
|
||||
for (Enemy enemy : regularEnemies) {
|
||||
if (!collectedKeyTypes.contains(enemy.getClass())) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void challengeDragon() {
|
||||
Dragon dragon = new Dragon();
|
||||
combat(player, dragon);
|
||||
if (player.isAlive()) {
|
||||
System.out.println("Congratulations! You defeated the Dragon and completed the game!");
|
||||
} else {
|
||||
System.out.println("The Dragon was too powerful. Better luck next time.");
|
||||
}
|
||||
}
|
||||
|
||||
private Player choosePlayer() {
|
||||
System.out.print("1. Assassin\n2. Knight\n3. Wizard\nChoose your character: ");
|
||||
int choice = sc.nextInt();
|
||||
Player chosen = switch (choice) {
|
||||
case 1 -> players.get(0);
|
||||
case 2 -> players.get(1);
|
||||
default -> players.get(2);
|
||||
};
|
||||
System.out.println("You chose " + chosen.getName()
|
||||
+ " (" + chosen.getClass().getSimpleName() + ")");
|
||||
return chosen;
|
||||
}
|
||||
|
||||
private void displayKeys() {
|
||||
System.out.println("=".repeat(30));
|
||||
System.out.print("Keys collected: ");
|
||||
List<String> collected = new ArrayList<>();
|
||||
List<String> missing = new ArrayList<>();
|
||||
for (Enemy enemy : regularEnemies) {
|
||||
if (collectedKeyTypes.contains(enemy.getClass())) collected.add(enemy.getClass().getSimpleName());
|
||||
else missing.add(enemy.getClass().getSimpleName());
|
||||
}
|
||||
if (collected.isEmpty()) {
|
||||
System.out.println("none");
|
||||
} else {
|
||||
System.out.println(String.join(", ", collected));
|
||||
}
|
||||
if (!missing.isEmpty()) {
|
||||
System.out.println("Still needed: " + String.join(", ", missing));
|
||||
}
|
||||
System.out.println("=".repeat(30));
|
||||
}
|
||||
|
||||
private void displayLocations() {
|
||||
for (Location location : locations) {
|
||||
System.out.println(location);
|
||||
}
|
||||
}
|
||||
|
||||
private Location chooseLocation() {
|
||||
displayLocations();
|
||||
System.out.print("1. Jungle\n2. China\n3. Iran\nChoose location: ");
|
||||
int choice = sc.nextInt();
|
||||
Location location = switch (choice) {
|
||||
case 1 -> locations.get(0);
|
||||
case 2 -> locations.get(1);
|
||||
default -> locations.get(2);
|
||||
};
|
||||
System.out.println("You chose " + location.getName());
|
||||
return location;
|
||||
}
|
||||
|
||||
private Enemy chooseEnemy(Location location) {
|
||||
int index = new Random().nextInt(location.getEnemies().size());
|
||||
Enemy enemy = location.getEnemies().get(index);
|
||||
enemy.setHP(enemy.getMaxHP());
|
||||
enemy.setMP(enemy.getMaxMP());
|
||||
System.out.println("The enemy is: " + enemy.getClass().getSimpleName());
|
||||
return enemy;
|
||||
}
|
||||
|
||||
private void combat(Player player, Enemy enemy) {
|
||||
int turn = 0;
|
||||
|
||||
while (player.isAlive() && enemy.isAlive()) {
|
||||
|
||||
if (turn == 0) {
|
||||
System.out.println("Choose your action:");
|
||||
System.out.println("1. Light attack 2. Heavy attack 3. Defend" +
|
||||
" 4. Heal 5. Special ability 6. Flask");
|
||||
int choice = sc.nextInt();
|
||||
boolean actionTaken = handlePlayerAction(choice, enemy);
|
||||
if (!actionTaken) continue;
|
||||
turn = 1;
|
||||
printCombatStatus(enemy);
|
||||
if (!enemy.isAlive()) break;
|
||||
}
|
||||
|
||||
else {
|
||||
System.out.println("-".repeat(30));
|
||||
|
||||
if (player instanceof Assassin && player.getUsingSpecialAbility()) {
|
||||
player.setUsingSpecialAbility(false);
|
||||
System.out.println(enemy.getClass().getSimpleName() + " skipped its turn!");
|
||||
turn = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println(enemy.getClass().getSimpleName() + "'s turn:");
|
||||
boolean enemyActed = handleEnemyAction(enemy);
|
||||
if (!enemyActed) continue;
|
||||
turn = 0;
|
||||
printCombatStatus(enemy);
|
||||
if (!player.isAlive()) break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!player.isAlive()) {
|
||||
System.out.println("You lost!");
|
||||
} else {
|
||||
System.out.println("You won!");
|
||||
player.gainXP(Player.xpRewardFor(enemy));
|
||||
player.setHP(player.getMaxHP());
|
||||
player.setMP(player.getMaxMP());
|
||||
|
||||
if (!(enemy instanceof Dragon)) {
|
||||
Class<? extends Enemy> type = enemy.getClass();
|
||||
if (!collectedKeyTypes.contains(type)) {
|
||||
collectedKeyTypes.add(type);
|
||||
System.out.println(enemy.getClass().getSimpleName() + "'s key dropped!");
|
||||
player.achieveKey(enemy);
|
||||
} else {
|
||||
System.out.println("You already have " + enemy.getClass().getSimpleName() + "'s key.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean handlePlayerAction(int choice, Enemy enemy) {
|
||||
switch (choice) {
|
||||
case 1 -> { player.lightAttack(enemy); return true; }
|
||||
case 2 -> { player.heavyAttack(enemy); if (player.isSuccessfulAction()) return true; }
|
||||
case 3 -> { player.defend(); if (player.isSuccessfulAction()) return true; }
|
||||
case 4 -> { player.heal(10); if (player.isSuccessfulAction()) return true; }
|
||||
case 5 -> { player.specialAbility(enemy); if (player.isSuccessfulAction()) return true; }
|
||||
case 6 -> { player.getFlask().use(player); return true; }
|
||||
default -> { System.out.println("Invalid choice."); return false; }
|
||||
}
|
||||
System.out.println("Not enough mana! Choose another action (1. Light attack is always free).");
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean handleEnemyAction(Enemy enemy) {
|
||||
int choice = new Random().nextInt(3);
|
||||
switch (choice) {
|
||||
case 1 -> { enemy.defend(); if (enemy.isSuccessfulAction()) return true; }
|
||||
case 2 -> { enemy.heal(10); if (enemy.isSuccessfulAction()) return true; }
|
||||
}
|
||||
|
||||
enemy.attack(player);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private void printCombatStatus(Enemy enemy) {
|
||||
System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP() +
|
||||
" HP | " + player.getMP() + "/" + player.getMaxMP() + "]");
|
||||
System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP() +
|
||||
" HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + "]");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,198 +1,7 @@
|
||||
package org.project;
|
||||
|
||||
import org.project.entity.enemies.*;
|
||||
import org.project.entity.players.Assassin;
|
||||
import org.project.entity.players.Knight;
|
||||
import org.project.entity.players.Player;
|
||||
import org.project.entity.players.Wizard;
|
||||
import org.project.item.weapons.Sword;
|
||||
import org.project.item.weapons.Weapon;
|
||||
import org.project.location.Location;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Weapon wp = new Sword();
|
||||
ArrayList<Player> players = new ArrayList<>();
|
||||
players.add(new Assassin());
|
||||
players.add(new Knight());
|
||||
players.add(new Wizard());
|
||||
ArrayList<Enemy> enemies = new ArrayList<>();
|
||||
enemies.add(new Goblin());
|
||||
enemies.add(new Skeleton());
|
||||
enemies.add(new Vampire());
|
||||
ArrayList<Location> locations = new ArrayList<>();
|
||||
locations.add(new Location("Jungle", enemies));
|
||||
locations.add(new Location("China", new ArrayList<>(enemies.subList(0, 2))));
|
||||
locations.add(new Location("Iran", new ArrayList<>(enemies.subList(1, 3))));
|
||||
Player player;
|
||||
Enemy enemy;
|
||||
Location location;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
player = choosePlayer(players,sc);
|
||||
while (true) {
|
||||
location = chooseLocation(locations,sc);
|
||||
enemy = chooseEnemy(location);
|
||||
System.out.println("wanna fight?\n1.Yes\n2.No");
|
||||
int choice = sc.nextInt();
|
||||
if (choice == 1) {
|
||||
combat(player,enemy,sc);
|
||||
System.out.println("player AHP: " + player.getHP());
|
||||
break;
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
new Game().start();
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
public static void displayLocations(List<Location> locations) {
|
||||
for (Location location : locations) {
|
||||
System.out.println(location);
|
||||
}
|
||||
}
|
||||
|
||||
public static Player choosePlayer(ArrayList<Player> players, Scanner sc) {
|
||||
System.out.print("1.Assassin\n2.Knight\n3.Wizard\nChoose your Character: ");
|
||||
int choice = sc.nextInt();
|
||||
Player player;
|
||||
switch (choice) {
|
||||
case 1 -> player = players.get(0);
|
||||
case 2 -> player = players.get(1);
|
||||
default -> player = players.get(2);
|
||||
}
|
||||
System.out.println("You chose " + player.getName() + "(" + player.getClass().getSimpleName() + ")");
|
||||
return player;
|
||||
}
|
||||
|
||||
public static Location chooseLocation(ArrayList<Location> locations, Scanner sc) {
|
||||
|
||||
displayLocations(locations);
|
||||
System.out.print("1.Jungle\n2.China\n3.Iran\nChoose Location: ");
|
||||
int choice = sc.nextInt();
|
||||
Location location = null;
|
||||
|
||||
switch (choice) {
|
||||
case 1 -> location = locations.get(0);
|
||||
case 2 -> location = locations.get(1);
|
||||
case 3 -> location = locations.get(2);
|
||||
}
|
||||
System.out.println("You chose " + location.getName() );
|
||||
return location;
|
||||
}
|
||||
|
||||
public static Enemy chooseEnemy(Location location) {
|
||||
int random = new Random().nextInt(location.getEnemies().size());
|
||||
Enemy enemy = location.getEnemies().get(random);
|
||||
System.out.println("The Enemy is: " + enemy.getClass().getSimpleName());
|
||||
return enemy;
|
||||
}
|
||||
|
||||
public static void combat(Player player, Enemy enemy, Scanner sc) {
|
||||
int turn = 0;
|
||||
int choice;
|
||||
while (player.isAlive() && enemy.isAlive()) {
|
||||
if (turn == 0) {
|
||||
turn = 1;
|
||||
System.out.println("choose your action:\n1.light attack 2.heavy attack " +
|
||||
"3.defend 4.heal 5.special ability 6.flask");
|
||||
choice = sc.nextInt();
|
||||
switch (choice) {
|
||||
case 1:
|
||||
player.lightAttack(enemy);
|
||||
break;
|
||||
case 2:
|
||||
player.heavyAttack(enemy);
|
||||
if (player.isSuccessfulAction()) break;
|
||||
else {turn = 0; continue;}
|
||||
case 3:
|
||||
player.defend();
|
||||
if (player.isSuccessfulAction()) break;
|
||||
else {turn = 0; continue;}
|
||||
case 4:
|
||||
player.heal(10);
|
||||
if (player.isSuccessfulAction()) break;
|
||||
else {turn = 0; continue;}
|
||||
|
||||
case 5:
|
||||
player.specialAbility(enemy);
|
||||
if (player.isSuccessfulAction()) break;
|
||||
else {turn = 0; continue;}
|
||||
|
||||
case 6:
|
||||
player.getFlask().use(player);
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("invalid choice");
|
||||
turn = 0;
|
||||
continue;
|
||||
}
|
||||
System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP()
|
||||
+ " HP | " + player.getMP() + "/" + player.getMaxMP() + " Mana" + "]");
|
||||
System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP()
|
||||
+ " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]");
|
||||
|
||||
}
|
||||
if (turn == 1) {
|
||||
System.out.println("-".repeat(30));
|
||||
if (player instanceof Assassin) {
|
||||
if (player.getUsingSpecialAbility()){
|
||||
player.setUsingSpecialAbility(false);
|
||||
turn = 0;
|
||||
System.out.println(enemy.getClass().getSimpleName() + " skipped it's turn!");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
System.out.println(enemy.getClass().getSimpleName() + "'s turn: ");
|
||||
turn = 0;
|
||||
choice = new Random().nextInt(3);
|
||||
switch (choice) {
|
||||
case 0:
|
||||
enemy.attack(player);
|
||||
if (enemy.isSuccessfulAction()) break;
|
||||
else {turn = 1; continue;}
|
||||
case 1:
|
||||
enemy.defend();
|
||||
if (enemy.isSuccessfulAction()) break;
|
||||
else {turn = 1; continue;}
|
||||
case 2:
|
||||
enemy.heal(10);
|
||||
if (enemy.isSuccessfulAction()) break;
|
||||
else {turn = 1; continue;}
|
||||
}
|
||||
}
|
||||
System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP()
|
||||
+ " HP | " + player.getMP() + "/" + player.getMaxMP() +" Mana" + "]");
|
||||
System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP()
|
||||
+ " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]");
|
||||
|
||||
}
|
||||
if (!player.isAlive()) {
|
||||
System.out.println("You lost!");
|
||||
}
|
||||
else {
|
||||
System.out.println("You Won!");
|
||||
player.gainXP(Player.xpRewardFor(enemy));
|
||||
player.setHP(player.getMaxHP());
|
||||
player.setMP(player.getMaxMP());
|
||||
if (!(enemy instanceof Dragon) && enemy.dropKey()) {
|
||||
System.out.println(enemy.getClass().getSimpleName() + "'s key dropped!");
|
||||
player.achieveKey(enemy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
package org.project.entity.players;
|
||||
|
||||
import org.project.entity.Entity;
|
||||
import org.project.entity.enemies.Dragon;
|
||||
import org.project.entity.enemies.Goblin;
|
||||
import org.project.entity.enemies.Skeleton;
|
||||
import org.project.entity.enemies.*;
|
||||
import org.project.item.armors.Armor;
|
||||
import org.project.item.consumables.Consumable;
|
||||
import org.project.item.consumables.Flask;
|
||||
@@ -257,4 +255,10 @@ public abstract class Player implements Entity, CombatOptions {
|
||||
|
||||
public int getXP() { return xp; }
|
||||
|
||||
public boolean hasKey(Enemy enemy) {
|
||||
if (enemy instanceof Goblin) return hasGoblinKey;
|
||||
if (enemy instanceof Skeleton) return hasSkeletonKey;
|
||||
if (enemy instanceof Vampire) return hasVampireKey;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user