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
7 changes: 7 additions & 0 deletions Answers/40230212010/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
18 changes: 18 additions & 0 deletions Answers/40230212010/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Getting Started

Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.

## Folder Structure

The workspace contains two folders by default, where:

- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies

Meanwhile, the compiled output files will be generated in the `bin` folder by default.

> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management

The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
Binary file added Answers/40230212010/bin/App.class
Binary file not shown.
12 changes: 12 additions & 0 deletions Answers/40230212010/src/Beater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Beater extends Player{

Beater(String name, int number) {
super(name, number);
//TODO Auto-generated constructor stub
}

@Override
public boolean isSuccessful() {
return Math.random() < 0.4;
}
}
13 changes: 13 additions & 0 deletions Answers/40230212010/src/Chaser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Chaser extends Player{

Chaser(String name, int number) {
super(name, number);
//TODO Auto-generated constructor stub
}

@Override
public boolean isSuccessful() {
return Math.random() < 0.3;
}

}
13 changes: 13 additions & 0 deletions Answers/40230212010/src/Keeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Keeper extends Player{

Keeper(String name, int number) {
super(name, number);
//TODO Auto-generated constructor stub
}

@Override
public boolean isSuccessful() {
return Math.random() < 0.7;
}

}
38 changes: 38 additions & 0 deletions Answers/40230212010/src/Match.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Match {
private Team team1;
private Team team2;
private int rounds;

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

public void start() {
for (int i = 0; i < rounds; i++) {
System.out.println("Round " + (i + 1) + " begins:");

if (team1.play()) {
System.out.println("Team 1 wins with " + team1.getGoals() + " goals!");
return;
}
team2.play();
if (team2.play()) {
System.out.println("Team 2 wins with " + team2.getGoals() + " goals!");
return;
}
System.out.println("End of Round " + (i + 1) + "\n");
}

System.out.println("Final scores:");
System.out.println("Team 1 goals: " + team1.getGoals());
System.out.println("Team 2 goals: " + team2.getGoals());
if (team1.getGoals() == team2.getGoals()) {
System.out.println("It's a draw!");
} else {
Team winner = (team1.getGoals() > team2.getGoals()) ? team1 : team2;
System.out.println("Winner: Team " + ((winner == team1) ? "1" : "2"));
}
}
}
18 changes: 18 additions & 0 deletions Answers/40230212010/src/MyApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class MyApp {
public static void main(String[] args) {
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);

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

Match match = new Match(team1, team2, 100);
match.start();
}
}
16 changes: 16 additions & 0 deletions Answers/40230212010/src/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Player implements Success {

String name;
int number;

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

@Override
public boolean isSuccessful() {
return false;
}

}
13 changes: 13 additions & 0 deletions Answers/40230212010/src/Seeker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Seeker extends Player{

Seeker(String name, int number) {
super(name, number);
//TODO Auto-generated constructor stub
}

@Override
public boolean isSuccessful() {
return Math.random() < 0.05;
}

}
3 changes: 3 additions & 0 deletions Answers/40230212010/src/Success.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface Success {
boolean isSuccessful();
}
62 changes: 62 additions & 0 deletions Answers/40230212010/src/Team.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class Team {
private Keeper keeper;
private Seeker seeker;
private Chaser[] chasers;
private 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 boolean hasSeekerFoundSnitch() {
return seeker.isSuccessful();
}

public boolean play() {
if (hasSeekerFoundSnitch()) {
System.out.println("Golden Snitch found by " + seeker.name + " Team scores 150 points!");
return true;
}

if (keeper.isSuccessful() && atLeastOneBeaterSuccessful() && atLeastTwoChasersSuccessful()) {
setGoal();

}
return false;
}

private boolean atLeastOneBeaterSuccessful() {
for (Beater beater : beaters) {
if (beater.isSuccessful()) {
return true;
}
}
return false;
}

private boolean atLeastTwoChasersSuccessful() {
int successfulChasers = 0;
for (Chaser chaser : chasers) {
if (chaser.isSuccessful()) {
successfulChasers++;
if (successfulChasers >= 2) {
return true;
}
}
}
return false;
}

public int getGoals() {
return goals;
}
}