In: Computer Science
we will focus on a simplistic simulation of a hockey game. We have made a method for you in HockeyGame.java called rollDice(), which will return an int between 1 and 100. We will use this to simulate two teams playing against each other. In Hockey, players can pass the puck between each other, shoot, or lose the puck to the other team. Here, you will print out the actions of the game as they are simulated You will need to write code that implements the following scenarios when HockeyGame.java is run. An example of all events to be pushed onto the team’s stack are included with the lab code:
Stage 1: Loose Puck o Call rollDice(), if result is 1-50, team 1 (stack 1) gets the puck. o Else (51-100) team 2 (stack 2) gets the puck. o For the appropriate team’s stack, push a node onto the stack.
Stage 2: Puck acquired or Pass Received o Call rollDice() and then do the following: 1-10, return to stage 1 (Team loses puck, Empty stack) 11-20, other team intercepts puck, return to Stage 2 with other team. Empty stack. 21-80, Team passes successfully, return to Stage 2 for same team. Record a pass. 81-90, Team shoots but doesn’t score. Proceed to Stage 1 & empty stack. Record a shot. 91-100, Team shoots and scores! Add a goal for Team. Proceed to Stage 1 & empty stack. Record a shot.
Your program should print out: o All actions taken and by which team, o the number of shots taken by each team, o the number of passes made by each team, and o the final score.
Your final line should look something like (with different numbers most likely):
FINAL SCORE
TORONTO with 3 goals and 5 shots and 9 passes
VS
MONTREAL with 2 goals and 2 shots and 1 pass
Here are the class files:
/**Hockey game Class**/
public class HockeyGame {
private HockeyTeam toronto;
private HockeyTeam montreal;
private String[] torontoPlayers = new String[] { "Tavares",
"Matthews", "Marner", "Rielly", "Ceci" };
private String[] montrealPlayers = new String[] { "Tatar",
"Danault", "Lehkonen", "Chiarot", "Weber" };
private int goalsToWin = 3;
private ArrayStack eventLog;
public HockeyGame () {
eventLog = new ArrayStack();
}
public void emptyEventLog() {
while (eventLog.isEmpty() == false) {
System.out.println(eventLog.pop().toString());
}
}
public void simulate () {
toronto = new HockeyTeam(torontoPlayers,"Toronto");
montreal = new HockeyTeam(montrealPlayers,"Montreal");
boolean torontoPossession = true;
boolean intercept = false, pass = false;
//Simulation code goes inside
while (toronto.getGoals() < this.goalsToWin &&
montreal.getGoals() < this.goalsToWin) {
//If we have an intercept, we will want to set possession
manually.
//Otherwise, simulate randomly which team gets the puck.
if (!intercept & !pass) {
if(HockeyGame.rollDice() >= 50) {
torontoPossession = true;
eventLog.push(new
HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()],"Toronto",
"retrieved the puck"));
}
else {
torontoPossession = false;
eventLog.push(new
HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()],"Montreal",
"retrieved the puck"));
}
}
else if (intercept) {
intercept = false;
torontoPossession = !torontoPossession;
}
//Any previous pass flags are reset as we move into the simulation
behaviour.
pass = false;
// This is the code block for the Toronto team's events.
if(torontoPossession) {
int randomEvent = HockeyGame.rollDice();
//System.out.println(randomEvent);
if (randomEvent <= 10) {
eventLog.push(new
HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()],"Toronto",
"lost the puck! It's loose now!"));
this.emptyEventLog();
System.out.println("");
}
else if (randomEvent <= 20){
}
else if (randomEvent <= 80) {
}
else if (randomEvent <= 90) {
}
else {
}
} // End Toronto Possession.
//This is the code block for Montreal team events
else {
int randomEvent = HockeyGame.rollDice();
//System.out.println(randomEvent);
if (randomEvent <= 10) {
}
else if (randomEvent <= 20){
}
else if (randomEvent <= 80) {
}
else if (randomEvent <= 90) {
}
else {
}
} // End Montreal simulation code.
} // End while loop
//Put final score output here.
}
public static int rollDice() {
return ((int)(Math.random()*100)+1);
}
public static int randomPlayerNumber() {
return (int)(Math.random()*5);
}
public static void main(String[] args) {
HockeyGame simulateGame = new HockeyGame();
simulateGame.simulate();
}
}
/**HockeyTeam Class **/
public class HockeyTeam {
private String[] players;
private String team;
private int shots;
private int goals;
private int passes;
public HockeyTeam (String[] players, String team) {
this.players = players;
this.team = team;
shots = 0;
goals = 0;
}
public String getPlayer (int index) {
return players[index];
}
public String getTeam () {
return team;
}
public void addShot() {
this.shots++;
}
public void addGoal() {
this.goals++;
}
public void addPass() {
this.passes++;
}
public int getPasses() {
return this.passes;
}
public int getShots() {
return this.shots;
}
public int getGoals() {
return this.goals;
}
}
/**HockeyEvent**/
public class HockeyEvent {
private String playerName;
private String eventType;
private String teamName;
public HockeyEvent(String player_name, String teamName, String
event_type) {
this.playerName = player_name;
this.teamName = teamName;
this.eventType = event_type;
}
public String getPlayerName() {
return this.playerName;
}
public String getEventType() {
return this.eventType;
}
public String getTeam() {
return this.teamName;
}
public String toString() {
return playerName + " from " + teamName + " " + eventType;
}
}
public class HockeyGame {
private HockeyTeam toronto;
private HockeyTeam montreal;
private String[] torontoPlayers = new String[] { "Tavares", "Matthews", "Marner", "Rielly", "Ceci" };
private String[] montrealPlayers = new String[] { "Tatar", "Danault", "Lehkonen", "Chiarot", "Weber" };
private int goalsToWin = 3;
private ArrayStack eventLog;
public HockeyGame () {
eventLog = new ArrayStack();
}
public void emptyEventLog() {
while (!eventLog.isEmpty()) {
System.out.println(eventLog.pop().toString());
}
System.out.println();
}
public void simulate () {
toronto = new HockeyTeam(torontoPlayers,"Toronto");
montreal = new HockeyTeam(montrealPlayers,"Montreal");
boolean torontoPossession = true;
boolean intercept = false, pass = false;
//Simulation code goes inside
while (toronto.getGoals() < this.goalsToWin && montreal.getGoals() < this.goalsToWin) {
//If we have an intercept, we will want to set possession manually.
//Otherwise, simulate randomly which team gets the puck.
if (!intercept & !pass) {
if(HockeyGame.rollDice() > 50) {
torontoPossession = true;
eventLog.push(new HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()],"Toronto", "retrieved the puck"));
}
else {
torontoPossession = false;
eventLog.push(new HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()],"Montreal", "retrieved the puck"));
}
}
else if (intercept) {
intercept = false;
torontoPossession = !torontoPossession;
}
//Any previous pass flags are reset as we move into the simulation behaviour.
pass = false;
// This is the code block for the Toronto team's events.
if(torontoPossession) {
int randomEvent = HockeyGame.rollDice();
//System.out.println(randomEvent);
if (randomEvent <= 10) {
eventLog.push(new HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()],"Toronto", "lost the puck! It's loose now!"));
this.emptyEventLog();
}
else if (randomEvent <= 20){
intercept = true;
eventLog.push(new HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()], "Toronto", "intercepted by Montreal"));
this.emptyEventLog();
}
else if (randomEvent <= 80) {
toronto.addPass();
eventLog.push(new HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()], "Toronto", "pass successful"));
}
else if (randomEvent <= 90) {
toronto.addShot();
eventLog.push(new HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()], "Toronto", "shoot! no score"));
this.emptyEventLog();
}
else {
toronto.addShot();
toronto.addGoal();
eventLog.push(new HockeyEvent(torontoPlayers[HockeyGame.randomPlayerNumber()], "Toronto", "shoot! aaaand goal!!!"));
this.emptyEventLog();
}
} // End Toronto Possession.
//This is the code block for Montreal team events
else {
int randomEvent = HockeyGame.rollDice();
//System.out.println(randomEvent);
if (randomEvent <= 10) {
eventLog.push(new HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()], "Montreal", "lost the puck! It's loose now"));
this.emptyEventLog();
}
else if (randomEvent <= 20){
intercept = true;
eventLog.push(new HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()], "Montreal", "intercepted by Toronto"));
this.emptyEventLog();
}
else if (randomEvent <= 80) {
montreal.addPass();
eventLog.push(new HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()], "Montreal", "pass successful"));
}
else if (randomEvent <= 90) {
montreal.addShot();
eventLog.push(new HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()], "Montreal", "shoot! no score"));
this.emptyEventLog();
}
else {
montreal.addShot();
montreal.addGoal();
eventLog.push(new HockeyEvent(montrealPlayers[HockeyGame.randomPlayerNumber()], "Montreal", "shoot! aaaand goal!!!"));
this.emptyEventLog();
}
} // End Montreal simulation code.
} // End while loop
System.out.println("FINAL SCORE");
int montrealGoals = montreal.getGoals();
int torontoGoals = toronto.getGoals();
if (torontoGoals > montrealGoals) {
System.out.println("TORONTO with " + torontoGoals + " goals and " + toronto.getShots() + " and " + toronto.getPasses() + "passes");
System.out.println("VS");
System.out.println("MONTREAL with " + montrealGoals + " goals and " + montreal.getShots() + " and " + montreal.getPasses() + "passes");
}
else {
System.out.println("MONTREAL with " + montrealGoals + " goals and " + montreal.getShots() + " and " + montreal.getPasses() + "passes");
System.out.println("VS");
System.out.println("TORONTO with " + torontoGoals + " goals and " + toronto.getShots() + " and " + toronto.getPasses() + "passes");
}
}
public static int rollDice() {
return ((int)(Math.random()*100)+1);
}
public static int randomPlayerNumber() {
return (int)(Math.random()*5);
}
public static void main(String[] args) {
HockeyGame simulateGame = new HockeyGame();
simulateGame.simulate();
}
}
HockeyGame.java contains the simulate() method where the game runs until some team scores the maximum goals i.e 3. eventLog is a stack where we store all the events. As provided in the description we use the rollDice() method in both stage 1 and stage 2 to decide the type of event, and finally we print the FINAL SCORE.
/**HockeyTeam Class **/
public class HockeyTeam {
private String[] players;
private String team;
private int shots;
private int goals;
private int passes;
public HockeyTeam (String[] players, String team) {
this.players = players;
this.team = team;
shots = 0;
goals = 0;
}
public String getPlayer (int index) {
return players[index];
}
public String getTeam () {
return team;
}
public void addShot() {
this.shots++;
}
public void addGoal() {
this.goals++;
}
public void addPass() {
this.passes++;
}
public int getPasses() {
return this.passes;
}
public int getShots() {
return this.shots;
}
public int getGoals() {
return this.goals;
}
}
HockeyTeam.java is the class (Data Object) for the team, which has attributes as team name, shots, passes and goals by that team.
/**HockeyEvent**/
public class HockeyEvent {
private String playerName;
private String eventType;
private String teamName;
public HockeyEvent(String player_name, String teamName, String event_type) {
this.playerName = player_name;
this.teamName = teamName;
this.eventType = event_type;
}
public String getPlayerName() {
return this.playerName;
}
public String getEventType() {
return this.eventType;
}
public String getTeam() {
return this.teamName;
}
public String toString() {
return playerName + " from " + teamName + " " + eventType;
}
}
HockeyEvent.java is used to specify all the events.