Question

In: Computer Science

we will focus on a simplistic simulation of a hockey game. We have made a method...

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

}

Solutions

Expert Solution

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.


Related Solutions

Setup The game that we will use for this simulation is "darts." We will randomly throw...
Setup The game that we will use for this simulation is "darts." We will randomly throw a number of darts at a specially configured dartboard. The set up for our board is shown below. In the figure, you can see that we have a round ‘dart board’ mounted on a square piece of ‘wood’. The dartboard has a radius of one unit. The piece of wood is exactly two units square so that the round board fits perfectly inside the...
javascript BlackJack i made a blackjack game, the code is below //this method will return a...
javascript BlackJack i made a blackjack game, the code is below //this method will return a shuffled deck function shuffleDeck() { //this will store array fo 52 objects const decks = [] const suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades'] const values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'One'] //run a loop till 52 times for (let i = 0; i < 52; i++) { //push a random item decks.push({ suit: suits[Math.floor(Math.random() *...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living neighbors, it...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation...
Assignment Implement Conway’s Game of Life IN C The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes...
Assignment Implement Conway’s Game of Life. The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows: If a cell is alive: If it has less than two living neighbors, it...
we focus on different methods used to estimate ending inventory – the gross profit method and...
we focus on different methods used to estimate ending inventory – the gross profit method and the retail inventory method. Discuss how each method is used to calculated estimated ending inventory. Under what circumstances is it appropriate to use these methods? Support your response to question #2 with a reference from the ASC (Accounting Standard Codification).
Use 5% level of significance. At the fifth hockey game of the season at a certain...
Use 5% level of significance. At the fifth hockey game of the season at a certain arena, 200 people were selected at random and asked as to how many of the previous four games they had attended. The results are given in the table below. Number of games previously attended Number of People 0 33 1 67 2 66 3 15 4 19 Test the hypothesis that these  200  observed values can be regarded as a random sample from a  binomial distribution,where  ‘θ’  is the...
Children are playing a game of ice hockey on a frozen lake. One of the children...
Children are playing a game of ice hockey on a frozen lake. One of the children throws a 1200g water bottle with velocity 0.4 m/s to another kid, weighing 41 kg, sitting motionless on the ice who catches the bottle 0.2 m from their COM at angle of impact of 12 degrees. The child on the ice has a center mass moment of inertia of 38 kg m^2. Assuming the ice is perfectly friction-less, answer the following. 1.) How does...
1. Ryan is readying for a face-off at the begninning of a hockey game. What is...
1. Ryan is readying for a face-off at the begninning of a hockey game. What is the total Force that ryan exerts on the ice? What is the total force of the ice on Ryan? Assume that Ryans total mass is 100 kg. Also take up ot be the +y direction, and towards the opponents goal to be +x direction. 2. Ryan is skating down the ice at 5 m/s. What is Ryan's momentum? Kinetic Energy? 3. Preparing to take...
Discuss about Monte Carlo simulation, with a focus on its steps. (100% rating)
Discuss about Monte Carlo simulation, with a focus on its steps. (100% rating)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT