-
Notifications
You must be signed in to change notification settings - Fork 52
Homework done #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Homework done #64
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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>com.ironhack</groupId> | ||
| <artifactId>Battle</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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package org.example; | ||
|
|
||
| public interface Attacker { | ||
| void attack(Characters target); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,38 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.example; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.UUID; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public abstract class Characters { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final String id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private String name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private int hp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private boolean isAlive=true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public Characters(String name,int hp){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.id = UUID.randomUUID().toString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.name=name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.hp=hp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.isAlive=true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String getName(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void setName(String name){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.name=name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public int getHp(){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return hp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void setHp(int hp){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.hp=hp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(this.hp<0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.hp=0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isAlive=false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public boolean getAlive(){return isAlive;} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void setAlive(boolean isAlive){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.isAlive=isAlive; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+31
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private boolean isAlive=true; | |
| public Characters(String name,int hp){ | |
| this.id = UUID.randomUUID().toString(); | |
| this.name=name; | |
| this.hp=hp; | |
| this.isAlive=true; | |
| } | |
| public String getName(){ | |
| return name; | |
| } | |
| public void setName(String name){ | |
| this.name=name; | |
| } | |
| public int getHp(){ | |
| return hp; | |
| } | |
| public void setHp(int hp){ | |
| this.hp=hp; | |
| if(this.hp<0) { | |
| this.hp=0; | |
| isAlive=false; | |
| }} | |
| public boolean getAlive(){return isAlive;} | |
| public void setAlive(boolean isAlive){ | |
| this.isAlive=isAlive; | |
| private boolean isAlive = true; | |
| public Characters(String name, int hp){ | |
| this.id = UUID.randomUUID().toString(); | |
| this.name = name; | |
| this.hp = hp; | |
| this.isAlive = true; | |
| } | |
| public String getName(){ | |
| return name; | |
| } | |
| public void setName(String name){ | |
| this.name = name; | |
| } | |
| public int getHp(){ | |
| return hp; | |
| } | |
| public void setHp(int hp){ | |
| this.hp = hp; | |
| if (this.hp < 0) { | |
| this.hp = 0; | |
| isAlive = false; | |
| }} | |
| public boolean getAlive(){return isAlive;} | |
| public void setAlive(boolean isAlive){ | |
| this.isAlive = isAlive; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package org.example; | ||
|
|
||
| import java.util.Scanner; | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| boolean exit = false; | ||
| System.out.println("================================="); | ||
| System.out.println(" WELCOME TO IRON BATTLE!"); | ||
| System.out.println("=================================\n"); | ||
| while (!exit) { | ||
| System.out.println("\n--- MAIN MENU ---"); | ||
| System.out.println("1. Create Battle"); | ||
| System.out.println("2. Exit"); | ||
|
Comment on lines
+13
to
+14
|
||
| System.out.print("Choose an option: "); | ||
| int choice = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
| switch (choice) { | ||
| case 1: | ||
| customBattle(scanner); | ||
| break; | ||
| case 2: | ||
| System.out.println("Thanks for playing! Goodbye!"); | ||
| exit = true; | ||
| break; | ||
| default: | ||
| System.out.println("Invalid option. Please try again."); | ||
| } | ||
| } | ||
| scanner.close(); | ||
| } | ||
| private static void customBattle(Scanner scanner) { | ||
| System.out.println("\n=== CREATE CHARACTER 1 ==="); | ||
| Characters character1 = createCharacter(scanner, "Character 1"); | ||
| System.out.println("\n=== CREATE CHARACTER 2 ==="); | ||
| Characters character2 = createCharacter(scanner, "Character 2"); | ||
| startBattle(character1, character2); | ||
| } | ||
| private static Characters createCharacter(Scanner scanner, String prompt) { | ||
| System.out.println("Choose character type:"); | ||
| System.out.println("1. Warrior"); | ||
| System.out.println("2. Wizard"); | ||
| System.out.print("Enter choice: "); | ||
| int type = scanner.nextInt(); | ||
| scanner.nextLine(); | ||
| System.out.print("Enter character name: "); | ||
| String name = scanner.nextLine(); | ||
| Characters character; | ||
| if (type == 1) { | ||
| character = new Warriors(name); | ||
| System.out.println("Warrior created! HP: " + character.getHp()); | ||
| } else { | ||
| character = new Wizards(name); | ||
| System.out.println("Wizard created! HP: " + character.getHp()); | ||
| } | ||
| return character; | ||
| } | ||
| private static void startBattle(Characters char1, Characters char2) { | ||
| int battleNumber = 1; | ||
| while (true) { | ||
| System.out.println("\n" + "=".repeat(50)); | ||
| System.out.println("BATTLE #" + battleNumber + " BEGINS!"); | ||
| System.out.println("=".repeat(50)); | ||
| System.out.println(char1.getName() + " (HP: " + char1.getHp() + ") VS " + | ||
| char2.getName() + " (HP: " + char2.getHp() + ")"); | ||
| System.out.println("=".repeat(50) + "\n"); | ||
| if (battleNumber > 1) { | ||
| String name1 = char1.getName(); | ||
| String name2 = char2.getName(); | ||
| if (char1 instanceof Warriors) { | ||
| char1 = new Warriors(name1); | ||
| } else { | ||
| char1 = new Wizards(name1); | ||
| } | ||
| if (char2 instanceof Warriors) { | ||
| char2 = new Warriors(name2); | ||
| } else { | ||
| char2 = new Wizards(name2); | ||
| } | ||
| System.out.println("Characters reset for new battle!"); | ||
| System.out.println(char1.getName() + " HP: " + char1.getHp()); | ||
| System.out.println(char2.getName() + " HP: " + char2.getHp()); | ||
| } | ||
|
Comment on lines
+67
to
+83
|
||
| int round = 1; | ||
| while (char1.getAlive() && char2.getAlive()) { | ||
| System.out.println("\n--- Round " + round + " ---"); | ||
| if (char1 instanceof Attacker) { | ||
| ((Attacker) char1).attack(char2); | ||
| } | ||
| if (char2 instanceof Attacker) { | ||
| ((Attacker) char2).attack(char1); | ||
| } | ||
| System.out.println(char1.getName() + " HP: " + char1.getHp() + " | " + | ||
| char2.getName() + " HP: " + char2.getHp()); | ||
| round++; | ||
| } | ||
| System.out.println("\n" + "=".repeat(50)); | ||
| System.out.println(" BATTLE OVER!"); | ||
| System.out.println("=".repeat(50)); | ||
| if (!char1.getAlive() && !char2.getAlive()) { | ||
| System.out.println("IT'S A TIE! Both warriors fell!"); | ||
| System.out.println("Restarting battle to determine a clear winner...\n"); | ||
| battleNumber++; | ||
| continue; // Restart battle | ||
| } else if (char1.getAlive()) { | ||
| System.out.println(" WINNER: " + char1.getName()); | ||
| System.out.println("Remaining HP: " + char1.getHp()); | ||
| } else { | ||
| System.out.println("WINNER: " + char2.getName() ); | ||
| System.out.println("Remaining HP: " + char2.getHp()); | ||
| } | ||
| System.out.println("=".repeat(50)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||||
| package org.example; | ||||||||||
| import java.util.concurrent.ThreadLocalRandom; | ||||||||||
| public class Warriors extends Characters implements Attacker { | ||||||||||
|
||||||||||
| public class Warriors extends Characters implements Attacker { | |
| public class Warriors extends Characters implements Attacker { |
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing around operators. The assignment operators should have consistent spacing. Lines 8-9 have inconsistent spacing around the '=' operator (no spaces).
| this.stamina=ThreadLocalRandom.current().nextInt(10,51); | |
| this.strength=ThreadLocalRandom.current().nextInt(1,11); | |
| this.stamina = ThreadLocalRandom.current().nextInt(10, 51); | |
| this.strength = ThreadLocalRandom.current().nextInt(1, 11); |
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error in attack method. When the warrior uses a WEAK ATTACK (line 37-41), the stamina is being incremented (+= 1) instead of decremented. This allows warriors to gain stamina while attacking, which contradicts the resource management logic. The weak attack should cost stamina, not increase it.
| this.stamina += 1; | |
| this.stamina -= 1; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,50 @@ | ||||||||||||||||||||
| package org.example; | ||||||||||||||||||||
| import java.util.concurrent.ThreadLocalRandom; | ||||||||||||||||||||
| public class Wizards extends Characters implements Attacker { | ||||||||||||||||||||
| private int mana; | ||||||||||||||||||||
| private int intelligence; | ||||||||||||||||||||
| public Wizards(String name){ | ||||||||||||||||||||
|
||||||||||||||||||||
| public Wizards(String name){ | |
| public Wizards(String name){ |
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing around operators. The assignment operators should have consistent spacing. Lines 8-9 have inconsistent spacing around the '=' operator (no spaces).
| this.mana=ThreadLocalRandom.current().nextInt(10,51); | |
| this.intelligence=ThreadLocalRandom.current().nextInt(1,51); | |
| this.mana = ThreadLocalRandom.current().nextInt(10,51); | |
| this.intelligence = ThreadLocalRandom.current().nextInt(1,51); |
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error in attack method. When the wizard uses a STAFF attack (line 37-41), the mana is being incremented (+= 1) instead of decremented. This allows wizards to gain mana while attacking, which contradicts the resource management logic. The staff attack should either cost mana or not modify it, not increase it.
| this.mana += 1; | |
| this.mana -= 1; |
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation. The closing braces are misaligned with the rest of the code structure. Lines 42-44 should maintain consistent indentation throughout the else block.
| } else { this.mana += 2; | |
| System.out.println(getName() + " out of mana! Recovers 2 mana."); | |
| } | |
| } | |
| } else { | |
| this.mana += 2; | |
| System.out.println(getName() + " out of mana! Recovers 2 mana."); | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
|  | ||||||
| \ | ||||||
|
||||||
| \ | |
|  |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing. Line 29 should have consistent spacing around braces and the return statement, following Java formatting conventions.