//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //NHL Shootout //Description: Simulates an ice hockey shootout based on goaltender save // percentages. //CS 284 //Programmed by Jonathan Voris //for 1/24/06 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //for keyboard input import java.util.Scanner; class shootout { //create a new scanner for reading keyboard input static Scanner sc = new Scanner(System.in); //create static variables for goalie save percentages. stats found at: //http://www.nhl.com/nhlstats/stats?service=direct&context=Home/reportBuilder.shootoutGoaliesLink static double BrodeurSavePercentage = .87; static double LundqvistSavePercentage = .821; //The score function takes a goalie's save percentage and the //shooting team name. It then outputs a score or miss to the //console and returns true for a score and false for a miss. public static boolean score(double savePercentage, String team) { //generate a random number and compare it to the defending //goalie's save percentage if (Math.random() >= savePercentage) { System.out.println(team + " score!"); return true; } else { System.out.println("No luck for the " + team); return false; } } public static void main(String[] args) { //get number of rounds from the user System.out.print("How many rounds did the shoot out last?"); int round = sc.nextInt(); //determine who won the shootout whoWon(round, 0, 0); } //whoWon accepts three integers as input - the number of rounds remaining //in the shootout, the score for the first team, and the score for the second //team. It then determines the winner of the shootout by simulating a round //and recursively calling itself. public static void whoWon(int round, int DevilsScore, int RangersScore) { //Base Case - Shootouts start with each team alterating three shots //regardless of their outcome. Therefore, if whoWon is called with //a round input of 3 or less, it runs three rounds and //returns the winner. if (round <= 3) { //loop once for each manditory round for (int s = 1; s < 4; s++) { //if the Devils score, give them a point if (score(LundqvistSavePercentage, "Devils")) { DevilsScore++; } //if the Rangers score, give them a point if (score(BrodeurSavePercentage, "Rangers")) { RangersScore++; } } //output the outcome of the shootout based on the team's respective scores if (DevilsScore > RangersScore) { System.out.println("The Devils Win!"); } else if (DevilsScore < RangersScore) { System.out.println("The Rangers Win!"); } else { System.out.println("It was still a tie after those rounds."); } } //Recursion - Shootouts continue until one team has scored more goals than the //other. Therefore, if whoWon is called with a round input of 4 or more, //it performs a round, decrements it, and recursively calls itself. else { //if the Devils score, give them a point if (score(LundqvistSavePercentage, "Devils")) { DevilsScore++; } //if the Rangers score, give them a point if (score(BrodeurSavePercentage, "Rangers")) { RangersScore++; } //recursive call whoWon(round-1, DevilsScore, RangersScore); } } }