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
10 changes: 10 additions & 0 deletions .idea/.gitignore

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

13 changes: 13 additions & 0 deletions .idea/compiler.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/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.

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

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

26 changes: 26 additions & 0 deletions HomeWork1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Text-Based Battle Simulator

## Description
This is a console-based Java battle simulator where Warriors and Wizards fight in a 1v1 duel.
The battle is round-based and continues until there is a single winner. If both characters die in the same round, the battle restarts.

## Features
- Object-Oriented Design (Inheritance, Polymorphism, Interface)
- Warrior and Wizard character types
- Random stat generation
- Round-based combat system
- Detailed battle log in console
- Tie detection and automatic restart
- Text-based menu navigation

## How to Run
1. Clone the repository
2. Open the project in your IDE
3. Run `Main.java`
4. Use the console menu to create characters and start the battle

## Technologies Used
- Java
- OOP Principles
- Scanner (Standard Input)
- Random class
17 changes: 17 additions & 0 deletions HomeWork1/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>HomeWork1</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>
7 changes: 7 additions & 0 deletions HomeWork1/src/main/java/org/example/Attacker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example;

public interface Attacker {

void attack(Character target);

}
39 changes: 39 additions & 0 deletions HomeWork1/src/main/java/org/example/BattleSimulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.example;

public class BattleSimulator {

public static void fight(Character c1, Character c2){

while (true) {
// Wizard wizard1=(Wizard) wizard;
// Warrior warrior1=(Warrior) warrior;

int round = 1;

while (c1.isAlive() && c2.isAlive()) {

System.out.println("===== ROUND " + round + " =====");

c1.attack(c2);
c2.attack(c1);

round++;
}

if (!c1.isAlive() && !c2.isAlive()) {
System.out.println("It's a TIE! Restarting battle...");


}
else if (c1.isAlive()) {
System.out.println(c1.getName() + " WINS!");
break;
}
else {
System.out.println(c2.getName() + " WINS!");
break;
}
}
}

}
46 changes: 46 additions & 0 deletions HomeWork1/src/main/java/org/example/Character.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.example;

import java.util.UUID;

public abstract class Character implements Attacker {

private String id;
private String name;
private int hp;
private boolean isAlive=true;
public Character(String name, int hp){
this.id= UUID.randomUUID().toString().substring(0,8);
this.name=name;
this.hp=hp;
}

public String getId(){
return id;
}
public String getName(){
return name;
}

public int getHp(){
return hp;
}
public boolean isAlive() {
return isAlive;
}

public void setHp(int hp){
this.hp=hp;
if(this.hp<=0){
this.hp=0;
this.isAlive=false;
}

}

public void setName(String name){
this.name=name;
}



}
97 changes: 97 additions & 0 deletions HomeWork1/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.example;

import java.util.Random;
import java.util.Scanner;


public class Main {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
Random random = new Random();

Character player1 = null;
Character player2 = null;

while (true) {
System.out.println("1. Create Player 1");
System.out.println("2. Create Player 2");
System.out.println("3. Start Battle");
System.out.println("4. Exit");
System.out.print("Choose option: ");

int choice = scanner.nextInt();
scanner.nextLine();

switch (choice) {

case 1:
player1 = createCharacter(scanner, random);
break;

case 2:
player2 = createCharacter(scanner, random);
break;
case 3:
if (player1 == null || player2 == null) {
System.out.println("Both players must be created first!");
} else {
BattleSimulator.fight(player1, player2);
}
break;

case 4:
System.out.println("Exiting game...");
return;

default:
System.out.println("Invalid option!");
}
}
}

private static Character createCharacter(Scanner scanner, Random random) {

System.out.println("Choose character type:");
System.out.println("1. Warrior");
System.out.println("2. Wizard");
System.out.print("Your choice: ");

int type = scanner.nextInt();
scanner.nextLine();

System.out.print("Enter name: ");
String name = scanner.nextLine();

if (type == 1) {

int hp = random.nextInt(101) + 100;
int stamina = random.nextInt(41) + 10;
int strength = random.nextInt(10) + 1;

Warrior warrior = new Warrior(name, hp, stamina, strength);

System.out.println("Warrior created!");
System.out.println("HP: " + hp + " | Stamina: " + stamina + " | Strength: " + strength);

return warrior;

} else if (type == 2) {

int hp = random.nextInt(51) + 50; // 50-100
int mana = random.nextInt(41) + 10; // 10-50
int intelligence = random.nextInt(50) + 1; // 1-50

Wizard wizard = new Wizard(name, hp, mana, intelligence);

System.out.println("Wizard created!");
System.out.println("HP: " + hp + " | Mana: " + mana + " | Intelligence: " + intelligence);

return wizard;

} else {
System.out.println("Invalid type!");
return null;
}
}
}
Loading