implement game loop

This commit is contained in:
2026-05-07 22:00:42 +03:30
parent b7c3086fd0
commit add61e75aa
14 changed files with 188 additions and 32 deletions
+139 -21
View File
@@ -15,50 +15,168 @@ 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(12, 5);
ArrayList<Player> players = new ArrayList<>();
players.add(new Assassin("Pia", 100, 100, wp, new KnightArmor(0, 1)));
players.add(new Knight("Khan", 100, 100, wp, new KnightArmor(50, 1)));
players.add(new Wizard("Daalou", 100, 100, wp, new KnightArmor(50, 1)));
ArrayList<Enemy> enemies = new ArrayList<>();
enemies.add(new Goblin(100, 100, wp));
enemies.add(new Skeleton(50, 100, wp));
enemies.add(new Vampire(90, 110, wp));
List<Location> locations = new ArrayList<>();
ArrayList<Location> locations = new ArrayList<>();
locations.add(new Location("Jungle", enemies));
Player player;
Enemy enemy;
locations.add(new Location("China", new ArrayList<>(enemies.subList(0, 2))));
locations.add(new Location("Iran", new ArrayList<>(enemies.subList(1, 3))));
Player player = null;
Enemy enemy = null;
Location location = null;
int isTurn = 0;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("1.Assassin\n2.Knight\n3.Wizard\nChoose your Character: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
player = new Assassin("Pia", 100, 100, wp, new KnightArmor(50, 1));
System.out.println("You chose " + player.getName() + "(Assassin)");
player = choosePlayer(players);
while (true) {
location = chooseLocation(locations);
enemy = chooseEnemy(location);
System.out.println("wanna fight?\n1.Yes\n2.No");
int choice = sc.nextInt();
if (choice == 1) {
combat(player,enemy);
System.out.println("player AHP: " + player.getHP());
break;
case 2:
player = new Knight("Khan", 200, 100, wp, new KnightArmor(80, 1));
System.out.println("You chose " + player.getName() + "(Knight)");
break;
default:
player = new Wizard("Daalou", 90, 100, wp, new KnightArmor(100, 1));
System.out.println("You chose " + player.getName() + "(Wizard)");
System.out.println();
}
else {
continue;
}
}
break;
}
}
public void displayLocations(List<Location> locations) {
/////////////////////////////////////////////////////////////////////////////////////////
public static void displayLocations(List<Location> locations) {
for (Location location : locations) {
System.out.println(location);
}
}
}
public static Player choosePlayer(ArrayList<Player> players) {
System.out.print("1.Assassin\n2.Knight\n3.Wizard\nChoose your Character: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
Player player = null;
switch (choice) {
case 1:
player = players.get(0);
break;
case 2:
player = players.get(1);
break;
default:
player = players.get(2);
break;
}
System.out.println("You chose " + player.getName() + "(" + player.getClass().getSimpleName() + ")");
return player;
}
public static Location chooseLocation(ArrayList<Location> locations) {
displayLocations(locations);
System.out.print("1.Jungle\n2.Iran\n3.China\nChoose Location: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
Location location = null;
switch (choice) {
case 1:
location = locations.get(0);
break;
case 2:
location = locations.get(1);
break;
case 3:
location = locations.get(2);
break;
}
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 Enemey is: " + enemy.getClass().getSimpleName());
return enemy;
}
public static void combat(Player player, Enemy enemy) {
Scanner sc = new Scanner(System.in);
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");
choice = sc.nextInt();
switch (choice) {
case 1: player.lightAttack(enemy);
break;
case 2: player.heavyAttack(enemy);
break;
case 3: player.defend();
break;
case 4: player.heal(10);
break;
case 5: player.specialAbility(enemy);
break;
default:
System.out.println("invalid choice");
}
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));
System.out.println(enemy.getClass().getSimpleName() + "'s turn: ");
turn = 0;
choice = new Random().nextInt(3);
switch (choice) {
case 0: enemy.attack(player);
break;
case 1: enemy.defend();
break;
case 2: enemy.heal(10);
}
}
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!");
if (enemy.dropKey()) {
System.out.println(enemy.getClass().getSimpleName() + "'s key dropped!");
player.achiveKey(enemy);
}
}
}
}