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 .idea/.gitignore

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

9 changes: 9 additions & 0 deletions .idea/Quidditch.iml

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

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

11 changes: 11 additions & 0 deletions Answers/Program/Program.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
11 changes: 11 additions & 0 deletions Answers/Program/src/Beater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Beater extends Player {
public Beater(String name, int number) {
super(name, number);
}

@Override
public boolean isSuccessful() {
// Beater has a 40% chance to stop chasers
return Math.random() < 0.4;
}
} // end class Beater
11 changes: 11 additions & 0 deletions Answers/Program/src/Chaser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Chaser extends Player {
public Chaser(String name, int number) {
super(name, number);
}

@Override
public boolean isSuccessful() {
// Chaser has a 30% chance to score a goal
return Math.random() < 0.3;
}
} // end class Chaser
11 changes: 11 additions & 0 deletions Answers/Program/src/Keeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Keeper extends Player {
public Keeper(String name, int number) {
super(name, number);
}

@Override
public boolean isSuccessful() {
// Keeper has a 70% chance to save a goal
return Math.random() < 0.7;
}
} // end class Keeper
30 changes: 30 additions & 0 deletions Answers/Program/src/Match.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Match {
private final Team team1;
private final Team team2;

public Match(Team team1, Team team2) {
this.team1 = team1;
this.team2 = team2;
}

public void start() {
for (int i = 0; i < 100; i++) {
team1.play();
team2.play();
}

int team1Goals = team1.getGoals();
int team2Goals = team2.getGoals();

System.out.println("Team 1 goals: " + team1Goals);
System.out.println("Team 2 goals: " + team2Goals);

if (team1Goals == team2Goals) {
System.out.println("It's a draw!");
} else if (team1Goals > team2Goals) {
System.out.println("Team 1 wins!");
} else {
System.out.println("Team 2 wins!");
}
}
}
25 changes: 25 additions & 0 deletions Answers/Program/src/MyApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class MyApp {
public static void main(String[] args) {
// Creating players for team 1
Keeper keeper1 = new Keeper("Keeper 1", 1);
Seeker seeker1 = new Seeker("Seeker 1", 2);
Chaser[] chasers1 = {new Chaser("Chaser 1", 3), new Chaser("Chaser 2", 4),
new Chaser("Chaser 3", 5)};
Beater[] beaters1 = {new Beater("Beater 1", 6),
new Beater("Beater 2", 7)};
Team team1 = new Team(keeper1, seeker1, chasers1, beaters1);

// Creating players for team 2
Keeper keeper2 = new Keeper("Keeper 2", 1);
Seeker seeker2 = new Seeker("Seeker 2", 2);
Chaser[] chasers2 = {new Chaser("Chaser 4", 3), new Chaser("Chaser 5", 4),
new Chaser("Chaser 6", 5)};
Beater[] beaters2 = {new Beater("Beater 3", 6),
new Beater("Beater 4", 7)};
Team team2 = new Team(keeper2, seeker2, chasers2, beaters2);

// Starting the match
Match match = new Match(team1, team2);
match.start();
}
}
17 changes: 17 additions & 0 deletions Answers/Program/src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
interface Success {
boolean isSuccessful();
}

public class Player implements Success {
private final String name;
private final int number;

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

public boolean isSuccessful() {
return false;
}
}
11 changes: 11 additions & 0 deletions Answers/Program/src/Seeker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Seeker extends Player {
public Seeker(String name, int number) {
super(name, number);
}

@Override
public boolean isSuccessful() {
// Seeker has a 5% chance to find the golden snitch
return Math.random() < 0.05;
}
} // end class Seeker
50 changes: 50 additions & 0 deletions Answers/Program/src/Team.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Team {
private final Keeper keeper;
private final Seeker seeker;
private final Chaser[] chasers;
private final Beater[] beaters;
private int goals;

public Team(Keeper keeper, Seeker seeker, Chaser[] chasers, Beater[] beaters) {
this.keeper = keeper;
this.seeker = seeker;
this.chasers = chasers;
this.beaters = beaters;
this.goals = 0;
}

private void setGoal() {
goals++;
}

public void play() {
boolean keeperSuccess = keeper.isSuccessful();
int successfulBeaters = 0;
int successfulChasers = 0;

for (Beater beater : beaters) {
if (beater.isSuccessful()) {
successfulBeaters++;
}
}

for (Chaser chaser : chasers) {
if (chaser.isSuccessful()) {
successfulChasers++;
}
}

if (keeperSuccess && successfulBeaters >= 1 && successfulChasers >= 2) {
setGoal();
}

if (seeker.isSuccessful()) {
System.out.println("Seeker found the golden snitch!");
goals += 150;
}
}

public int getGoals() {
return goals;
}
}
Binary file added out/production/Program/Beater.class
Binary file not shown.
Binary file added out/production/Program/Chaser.class
Binary file not shown.
Binary file added out/production/Program/Keeper.class
Binary file not shown.
Binary file added out/production/Program/Match.class
Binary file not shown.
Binary file added out/production/Program/MyApp.class
Binary file not shown.
Binary file added out/production/Program/Player.class
Binary file not shown.
Binary file added out/production/Program/Seeker.class
Binary file not shown.
Binary file added out/production/Program/Success.class
Binary file not shown.
Binary file added out/production/Program/Team.class
Binary file not shown.