From a799ed6ef0f3a768d91c6dc0782868983d46abd1 Mon Sep 17 00:00:00 2001 From: HJavid010 Date: Sun, 14 Apr 2024 23:21:23 +0330 Subject: [PATCH 1/7] initial commit for creating project files --- .../40230112037/Quidditch/.idea/.gitignore | 3 + .../40230112037/Quidditch/.idea/encodings.xml | 7 + Answers/40230112037/Quidditch/.idea/misc.xml | 14 ++ .../Quidditch/.idea/uiDesigner.xml | 124 ++++++++++++++++++ Answers/40230112037/Quidditch/.idea/vcs.xml | 6 + 5 files changed, 154 insertions(+) create mode 100644 Answers/40230112037/Quidditch/.idea/.gitignore create mode 100644 Answers/40230112037/Quidditch/.idea/encodings.xml create mode 100644 Answers/40230112037/Quidditch/.idea/misc.xml create mode 100644 Answers/40230112037/Quidditch/.idea/uiDesigner.xml create mode 100644 Answers/40230112037/Quidditch/.idea/vcs.xml diff --git a/Answers/40230112037/Quidditch/.idea/.gitignore b/Answers/40230112037/Quidditch/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Answers/40230112037/Quidditch/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Answers/40230112037/Quidditch/.idea/encodings.xml b/Answers/40230112037/Quidditch/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/Answers/40230112037/Quidditch/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Answers/40230112037/Quidditch/.idea/misc.xml b/Answers/40230112037/Quidditch/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/Answers/40230112037/Quidditch/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112037/Quidditch/.idea/uiDesigner.xml b/Answers/40230112037/Quidditch/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/Answers/40230112037/Quidditch/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112037/Quidditch/.idea/vcs.xml b/Answers/40230112037/Quidditch/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/Answers/40230112037/Quidditch/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From 8c61bc51ded328a1624802f0dc16a57c0433fffd Mon Sep 17 00:00:00 2001 From: HJavid010 Date: Sun, 14 Apr 2024 23:21:39 +0330 Subject: [PATCH 2/7] Created Success interface --- .../Quidditch/src/main/java/org/quidditch/Success.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Answers/40230112037/Quidditch/src/main/java/org/quidditch/Success.java diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Success.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Success.java new file mode 100644 index 0000000..8d402b6 --- /dev/null +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Success.java @@ -0,0 +1,5 @@ +package org.quidditch; + +public interface Success { + boolean isSuccessful(); +} From 09d5f3bfd41a7a34d0eb8f19ee440b0e86a2265a Mon Sep 17 00:00:00 2001 From: HJavid010 Date: Sun, 14 Apr 2024 23:22:21 +0330 Subject: [PATCH 3/7] Created Player class and other players. --- .../src/main/java/org/quidditch/Player.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java new file mode 100644 index 0000000..03c6436 --- /dev/null +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java @@ -0,0 +1,45 @@ +package org.quidditch; + +import java.util.Random; + +public abstract class Player implements Success { + protected int successChancePercent; + Random random = new Random(); + + Player() { + } + + @Override + public boolean isSuccessful() { + if (this.random.nextInt(100) + 1 < this.successChancePercent) { + return true; + } + return false; + } +} + +class Keeper extends Player { + + Keeper() { + this.successChancePercent = 70; + } +} + +class Seeker extends Player { + Seeker() { + this.successChancePercent = 5; + } +} + +class Chaser extends Player { + + Chaser() { + this.successChancePercent = 30; + } +} + +class Beater extends Player { + Beater() { + this.successChancePercent = 40; + } +} From 9b71b21f2b41e7707d922389f3881156feabf40c Mon Sep 17 00:00:00 2001 From: HJavid010 Date: Sun, 14 Apr 2024 23:25:38 +0330 Subject: [PATCH 4/7] Created Team class and set goals conditions. --- .../src/main/java/org/quidditch/Team.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java new file mode 100644 index 0000000..ab00030 --- /dev/null +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java @@ -0,0 +1,45 @@ +package org.quidditch; + +public class Team { + Keeper keeper = new Keeper(); + Seeker seeker = new Seeker(); + Chaser[] chaser = new Chaser[3]; + Beater[] beater = new Beater[2]; + private int goals; + + public Team() { + for (int i = 0; i < this.chaser.length; i++) { + this.chaser[i] = new Chaser(); + } + for (int i = 0; i < this.beater.length; i++) { + this.beater[i] = new Beater(); + } + this.goals = 0; + } + + private void setGoal() { + this.goals++; + } + + public int getGoals() { + return this.goals; + } + + protected void play() { + if (this.keeper.isSuccessful() && (this.beater[0].isSuccessful()) || this.beater[1].isSuccessful()) { + int chasersSucceeded = 0; + for (Chaser chaser : this.chaser) { + if (chaser.isSuccessful()) chasersSucceeded++; + } + if (chasersSucceeded >= 2) setGoal(); + } + } + + protected boolean seekerPlayWasSuccessful() { + if (this.seeker.isSuccessful()) { + this.goals += 150; + return true; + } + return false; + } +} From 17b623db0876354073bb862c1846fe75eb366921 Mon Sep 17 00:00:00 2001 From: HJavid010 Date: Sun, 14 Apr 2024 23:28:35 +0330 Subject: [PATCH 5/7] Created Match class and printTheResults function. --- .../src/main/java/org/quidditch/Match.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java new file mode 100644 index 0000000..14d5114 --- /dev/null +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java @@ -0,0 +1,51 @@ +package org.quidditch; + +public class Match { + Team[] team = new Team[2]; + int currentRound; + int numberOfRounds; + + Match() { + this.numberOfRounds = 150; + for (int i = 0; i < this.team.length; i++) { + this.team[i] = new Team(); + } + } + + Match(int rounds) { + this.numberOfRounds = rounds; + for (int i = 0; i < this.team.length; i++) { + this.team[i] = new Team(); + } + } + + void start() { + for (this.currentRound = 0; this.currentRound < this.numberOfRounds; this.currentRound++) { + for (Team playingTeam : this.team) { + playingTeam.play(); + } + } + printTheResults(); + } + + void printTheResults() { + boolean snitchCaught=false; + for(Team team:this.team){ + if(team.seekerPlayWasSuccessful()){ + System.out.println("Team 1 has caught the snitch and wins!"); + snitchCaught=true; + break; + } + } + if(!snitchCaught) { + + if (this.team[0].getGoals() != this.team[1].getGoals()) { + System.out.printf("Team %d wins!\n", (this.team[0].getGoals() > this.team[1].getGoals()) ? 1 : 2); + } else { + System.out.println("Draw!"); + } + } + System.out.printf("Results:\tTeam 1: %d\t Team 2: %d", this.team[0].getGoals(), this.team[1].getGoals()); + } + +} From 653a22385fa91ee45ad2a7b9a9faf8e42cf354d5 Mon Sep 17 00:00:00 2001 From: HJavid010 Date: Sun, 14 Apr 2024 23:29:11 +0330 Subject: [PATCH 6/7] Created main function inside MyApp class and started a match inside it. --- .../Quidditch/src/main/java/org/quidditch/MyApp.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java new file mode 100644 index 0000000..d315ee6 --- /dev/null +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java @@ -0,0 +1,8 @@ +package org.quidditch; + +public class MyApp { + public static void main(String[] args) { + Match match=new Match(); + match.start(); + } +} From 84e8878db0a3b364f40a9da2046af1d32ca57b92 Mon Sep 17 00:00:00 2001 From: HJavid010 Date: Sun, 14 Apr 2024 23:56:41 +0330 Subject: [PATCH 7/7] Added name support to teams and players in last minute! --- .../src/main/java/org/quidditch/Match.java | 30 +++++++++---------- .../src/main/java/org/quidditch/MyApp.java | 6 +++- .../src/main/java/org/quidditch/Player.java | 20 ++++++++----- .../src/main/java/org/quidditch/Team.java | 21 +++++++------ 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java index 14d5114..dfef73d 100644 --- a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Match.java @@ -5,18 +5,16 @@ public class Match { int currentRound; int numberOfRounds; - Match() { + Match(String team1Name, String[] team1PlayerNames, String team2Name, String[] team2PlayerNames) { this.numberOfRounds = 150; - for (int i = 0; i < this.team.length; i++) { - this.team[i] = new Team(); - } + team[0] = new Team(team1Name, team1PlayerNames); + team[1] = new Team(team2Name, team2PlayerNames); } - Match(int rounds) { + Match(int rounds, String team1Name, String[] team1PlayerNames, String team2Name, String[] team2PlayerNames) { this.numberOfRounds = rounds; - for (int i = 0; i < this.team.length; i++) { - this.team[i] = new Team(); - } + team[0] = new Team(team1Name, team1PlayerNames); + team[1] = new Team(team2Name, team2PlayerNames); } void start() { @@ -29,23 +27,23 @@ void start() { } void printTheResults() { - boolean snitchCaught=false; - for(Team team:this.team){ - if(team.seekerPlayWasSuccessful()){ - System.out.println("Team 1 has caught the snitch and wins!"); - snitchCaught=true; + boolean snitchCaught = false; + for (Team team : this.team) { + if (team.seekerPlayWasSuccessful()) { + System.out.printf("%s from %s has caught the snitch and wins!", team.seeker.name, team.name); + snitchCaught = true; break; } } - if(!snitchCaught) { + if (!snitchCaught) { if (this.team[0].getGoals() != this.team[1].getGoals()) { - System.out.printf("Team %d wins!\n", (this.team[0].getGoals() > this.team[1].getGoals()) ? 1 : 2); + System.out.printf("Team %s wins!\n", (this.team[0].getGoals() > this.team[1].getGoals()) ? this.team[0].name : this.team[1].name); } else { System.out.println("Draw!"); } } - System.out.printf("Results:\tTeam 1: %d\t Team 2: %d", this.team[0].getGoals(), this.team[1].getGoals()); + System.out.printf("Results:\t%s: %d\t %s: %d", this.team[0].name, this.team[0].getGoals(), this.team[1].name, this.team[1].getGoals()); } } diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java index d315ee6..8f05b92 100644 --- a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/MyApp.java @@ -2,7 +2,11 @@ public class MyApp { public static void main(String[] args) { - Match match=new Match(); + String team1Name = "Gryffindor"; + String[] team1PlayerNames = {"Steve", "ali", "asghar", "hamid", "zahra", "navid", "yasamin"}; + String team2Name = "Slytherins"; + String[] team2PlayerNames = {"Garry", "Bob", "Sara", "Jack", "Kenshi", "Oliver", "Emma"}; + Match match = new Match(team1Name, team1PlayerNames, team2Name, team2PlayerNames); match.start(); } } diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java index 03c6436..618c109 100644 --- a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Player.java @@ -5,8 +5,12 @@ public abstract class Player implements Success { protected int successChancePercent; Random random = new Random(); + final String name; + final int number; - Player() { + Player(String name, int number) { + this.name = name; + this.number = number; } @Override @@ -19,27 +23,29 @@ public boolean isSuccessful() { } class Keeper extends Player { - - Keeper() { + Keeper(String name, int number) { + super(name, number); this.successChancePercent = 70; } } class Seeker extends Player { - Seeker() { + Seeker(String name, int number) { + super(name, number); this.successChancePercent = 5; } } class Chaser extends Player { - - Chaser() { + Chaser(String name, int number) { + super(name, number); this.successChancePercent = 30; } } class Beater extends Player { - Beater() { + Beater(String name, int number) { + super(name, number); this.successChancePercent = 40; } } diff --git a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java index ab00030..e0afb2c 100644 --- a/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java +++ b/Answers/40230112037/Quidditch/src/main/java/org/quidditch/Team.java @@ -1,19 +1,22 @@ package org.quidditch; public class Team { - Keeper keeper = new Keeper(); - Seeker seeker = new Seeker(); + final String name; + Keeper keeper; + Seeker seeker; Chaser[] chaser = new Chaser[3]; Beater[] beater = new Beater[2]; private int goals; - public Team() { - for (int i = 0; i < this.chaser.length; i++) { - this.chaser[i] = new Chaser(); - } - for (int i = 0; i < this.beater.length; i++) { - this.beater[i] = new Beater(); - } + public Team(String name, String[] playerNames) { + this.name = name; + this.keeper = new Keeper(playerNames[0], 1); + this.seeker = new Seeker(playerNames[1], 2); + this.chaser[0] = new Chaser(playerNames[2], 3); + this.chaser[1] = new Chaser(playerNames[3], 4); + this.chaser[2] = new Chaser(playerNames[4], 5); + this.beater[0] = new Beater(playerNames[5], 6); + this.beater[1] = new Beater(playerNames[6], 7); this.goals = 0; }