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
3 changes: 3 additions & 0 deletions Answers/40230112037/Quidditch/.idea/.gitignore

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

7 changes: 7 additions & 0 deletions Answers/40230112037/Quidditch/.idea/encodings.xml

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

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

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

124 changes: 124 additions & 0 deletions Answers/40230112037/Quidditch/.idea/uiDesigner.xml

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

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

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,49 @@
package org.quidditch;

public class Match {
Team[] team = new Team[2];
int currentRound;
int numberOfRounds;

Match(String team1Name, String[] team1PlayerNames, String team2Name, String[] team2PlayerNames) {
this.numberOfRounds = 150;
team[0] = new Team(team1Name, team1PlayerNames);
team[1] = new Team(team2Name, team2PlayerNames);
}

Match(int rounds, String team1Name, String[] team1PlayerNames, String team2Name, String[] team2PlayerNames) {
this.numberOfRounds = rounds;
team[0] = new Team(team1Name, team1PlayerNames);
team[1] = new Team(team2Name, team2PlayerNames);
}

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.printf("%s from %s has caught the snitch and wins!", team.seeker.name, team.name);
snitchCaught = true;
break;
}
}
if (!snitchCaught) {

if (this.team[0].getGoals() != this.team[1].getGoals()) {
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:\t%s: %d\t %s: %d", this.team[0].name, this.team[0].getGoals(), this.team[1].name, this.team[1].getGoals());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.quidditch;

public class MyApp {
public static void main(String[] args) {
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.quidditch;

import java.util.Random;

public abstract class Player implements Success {
protected int successChancePercent;
Random random = new Random();
final String name;
final int number;

Player(String name, int number) {
this.name = name;
this.number = number;
}

@Override
public boolean isSuccessful() {
if (this.random.nextInt(100) + 1 < this.successChancePercent) {
return true;
}
return false;
}
}

class Keeper extends Player {
Keeper(String name, int number) {
super(name, number);
this.successChancePercent = 70;
}
}

class Seeker extends Player {
Seeker(String name, int number) {
super(name, number);
this.successChancePercent = 5;
}
}

class Chaser extends Player {
Chaser(String name, int number) {
super(name, number);
this.successChancePercent = 30;
}
}

class Beater extends Player {
Beater(String name, int number) {
super(name, number);
this.successChancePercent = 40;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.quidditch;

public interface Success {
boolean isSuccessful();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.quidditch;

public class Team {
final String name;
Keeper keeper;
Seeker seeker;
Chaser[] chaser = new Chaser[3];
Beater[] beater = new Beater[2];
private int goals;

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;
}

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;
}
}