Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/homework-java-ironbattle.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions HomeWorkMain/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>HomeWorkMain</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
182 changes: 182 additions & 0 deletions HomeWorkMain/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package org.example;

import org.example.entity.BattleSimulator;
import org.example.entity.Character;
import org.example.entity.Warrior;
import org.example.entity.Wizard;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main {
private static final Scanner SC = new Scanner(System.in);
private static final Random RANDOM = new Random();

public static void main(String[] args) {
Character player1 = null;
Character player2 = null;
boolean isRunning = true;

while (isRunning) {
System.out.println("\n=== IRON BATTLE MENU ===");
System.out.println("1) Create Player 1 " + (player1 != null ? "[" + player1.getName() + " Ready]" : "[Not Created]"));
System.out.println("2) Create Player 2 " + (player2 != null ? "[" + player2.getName() + " Ready]" : "[Not Created]"));
System.out.println("3) Load Players from CSV (Bonus)");
System.out.println("4) Start Battle ");
System.out.println("5) Simulate Random Battle (Bonus) ");
System.out.println("6) Exit Game ");
System.out.print("Choose an option: ");

String choice = SC.nextLine();
switch (choice) {
case "1":
player1 = createCharacter(1);
break;
case "2":
player2 = createCharacter(2);
break;
case "3":
// CSV-dən oxumaq
List<Character> importedList = importCharactersFromCSV("characters.csv");
if (importedList.size() >= 2) {
System.out.println("\n--- Characters available in CSV ---");
for (int i = 0; i < importedList.size(); i++) {
Character c = importedList.get(i);
System.out.println((i + 1) + ") " + c.getName() + " [" + c.getClass().getSimpleName() + ", HP: " + c.getHp() + "]");
}

try {
System.out.print("\nSelect Player 1 (Enter number): ");
int p1Index = Integer.parseInt(SC.nextLine()) - 1;
player1 = importedList.get(p1Index);

System.out.print("Select Player 2 (Enter number): ");
int p2Index = Integer.parseInt(SC.nextLine()) - 1;
player2 = importedList.get(p2Index);

System.out.println("Players loaded successfully from CSV!");
} catch (Exception e) {
System.out.println("Invalid selection! Please try again.");
}
} else {
System.out.println("Not enough characters in the CSV file or file not found.");
}
break;
case "4":
if (player1 != null && player2 != null) {
BattleSimulator battleSimulator = new BattleSimulator();
battleSimulator.startBattle(player1, player2);

// Döyüş bitdikdən sonra oyunçuları sıfırlayırıq
player1 = null;
player2 = null;
} else {
System.out.println("\nYou must create or load both players before starting the battle!");
}
break;
case "5":
System.out.println("\n--- Simulating Random Battle ---");
Character randomP1 = createFullyRandomCharacter();
Character randomP2 = createFullyRandomCharacter();

BattleSimulator randomSimulator = new BattleSimulator();
randomSimulator.startBattle(randomP1, randomP2);
break;
case "6":
System.out.println("Exiting game. Goodbye!");
isRunning = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
SC.close();
}

private static Character createCharacter(int playerNum) {
System.out.println("\nSelect Class for Player " + playerNum + ":");
System.out.println("1. Warrior");
System.out.println("2. Wizard");
System.out.print("Choice: ");

int type = -1;
try {
type = Integer.parseInt(SC.nextLine());
if (type != 1 && type != 2) throw new NumberFormatException();
} catch (NumberFormatException e) {
System.out.println("Invalid class selection. Creation cancelled.");
return null;
}

System.out.print("Enter Character Name: ");
String name = SC.nextLine();

if (type == 1) {
int hp = RANDOM.nextInt(101) + 100;
int stamina = RANDOM.nextInt(41) + 10;
int strength = RANDOM.nextInt(10) + 1;
System.out.println("Player " + playerNum + " (Warrior) created successfully!");
return new Warrior(name, hp, stamina, strength);
} else {
int hp = RANDOM.nextInt(51) + 50;
int mana = RANDOM.nextInt(41) + 10;
int intelligence = RANDOM.nextInt(50) + 1;
System.out.println("Player " + playerNum + " (Wizard) created successfully!");
return new Wizard(name, hp, mana, intelligence);
}
}

private static Character createFullyRandomCharacter() {
boolean isWarrior = RANDOM.nextBoolean();

if (isWarrior) {
String[] warriorNames = {"Conan", "Arthur", "Leonidas", "Achilles", "Thor"};
String name = warriorNames[RANDOM.nextInt(warriorNames.length)];
int hp = RANDOM.nextInt(101) + 100;
int stamina = RANDOM.nextInt(41) + 10;
int strength = RANDOM.nextInt(10) + 1;
return new Warrior(name, hp, stamina, strength);
} else {
String[] wizardNames = {"Gandalf", "Merlin", "Dumbledore", "Sauron", "Voldemort"};
String name = wizardNames[RANDOM.nextInt(wizardNames.length)];
int hp = RANDOM.nextInt(51) + 50;
int mana = RANDOM.nextInt(41) + 10;
int intelligence = RANDOM.nextInt(50) + 1;
return new Wizard(name, hp, mana, intelligence);
}
}

private static List<Character> importCharactersFromCSV(String filePath) {
List<Character> importedCharacters = new ArrayList<>();
try (Scanner fileScanner = new Scanner(new File(filePath))) {
// İlk sətri (başlıqları) ötürürük
if (fileScanner.hasNextLine()) {
fileScanner.nextLine();
}
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] data = line.split(",");

if (data.length == 5) {
String type = data[0].trim();
String name = data[1].trim();
int hp = Integer.parseInt(data[2].trim());
int resource = Integer.parseInt(data[3].trim());
int power = Integer.parseInt(data[4].trim());

if (type.equalsIgnoreCase("Warrior")) {
importedCharacters.add(new Warrior(name, hp, resource, power));
} else if (type.equalsIgnoreCase("Wizard")) {
importedCharacters.add(new Wizard(name, hp, resource, power));
}
}
}
} catch (Exception e) {
System.out.println("Error reading CSV: " + e.getMessage() + "\nMake sure '" + filePath + "' exists in the project root directory.");
}
return importedCharacters;
}
}
54 changes: 54 additions & 0 deletions HomeWorkMain/src/main/java/org/example/entity/BattleSimulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.example.entity;

public class BattleSimulator {
public void startBattle(Character player1, Character player2){
System.out.println("BATTLE START!");
System.out.println(player1.getName() + " VS " + player2.getName());
Character c1 = initCharacter(player1);
Character c2 = initCharacter(player2);




boolean battleOver = false;

int round = 1;
while (!battleOver) {

if(player1.isAlive() && player2.isAlive()){
System.out.println("\n--- Round " + round + " ---");

player1.attack(player2);
player2.attack(player1);

System.out.println("=> " + player1.getName() + " HP: " + player1.getHp());
System.out.println("=> " + player2.getName() + " HP: " + player2.getHp());

round++;
} else if (!player1.isAlive() && !player2.isAlive()) {
System.out.println("\nIT'S A TIE!");
System.out.println("Restarting the battle...\n");
player1=initCharacter(c1);
player2=initCharacter(c2);
} else if (!player2.isAlive()) {
System.out.println(player1.getName() + " won the battle!");
battleOver = true;
}
else {
System.out.println(player2.getName() + " won the battle!");
battleOver = true;

}
}
}


public Character initCharacter(Character c){
if(c instanceof Wizard w){
return new Wizard(w.getName(),w.getHp(),w.getMana(),w.getIntelligence());
}else if(c instanceof Warrior w){
return new Warrior(w.getName(),w.getHp(),w.getStamina(),w.getStrength());
}
return null;
}
}
Loading