/**
* Hayley Pappas
*
* Champions.java
*
* World Series Champions Game
*
* Menu-driven Interface program that allows the user to:
* 1) Display rules of the game.
* 2) Guess the championships.
* 3) Quit.
*/
// Import classes
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
import java.io.*;
public class Champions {
// Scanner object for standard input
static Scanner stdIn = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
// Constants & Variables
final int RULES = 1, GUESS = 2, QUIT = 3;
int choice;
// ArrayList to hold champion teams
ArrayList<String> champions;
// Random number generator
Random teamRandom = new Random();
int teamIdx, teamWins = 0, teamGuess = 0;
String teamName;
// Intro
System.out.println("\nWorld Series Champions Game ...");
// Read "champions.txt" into ArrayList champions[]
champions = loadChampions();
// Menu
do
{
// Display menu & prompt user for choice
System.out.println("\nChoose one of the following options");
System.out.println("\t1) Display Rules of the Game.");
System.out.println("\t2) Guess the Championships.");
System.out.println("\t3) Quit.");
System.out.print("Option: ");
choice = stdIn.nextInt();
stdIn.nextLine(); // Flush buffer
// Drive menu options
switch (choice)
{
case RULES:
displayRules(); // Make method to display rules
break;
case GUESS:
// Select a team from champions[]
teamIdx = teamRandom.nextInt(champions.size());
teamName = champions.get(teamIdx);
System.out.println("\nYour team champion is " + teamName);
// Calculate total number of championships won
teamWins = calcWins(champions, teamName);
// Prompt user for number of times team has won
teamGuess = getGuess(teamName);
// Determine winner
if (teamWins == teamGuess)
System.out.println("\nCorrect ... You know your World Series!");
else
System.out.printf("\nSorry ... The %s has won %d times.\n", teamName, teamWins);
break;
case QUIT:
System.out.println("\nGood Bye ...");
break;
default:
System.out.println("Error ... Invalid option. Try again.");
}
} while (choice != QUIT);
// Close scanner object
stdIn.close();
}
/**
* Loads the content of "champions.txt" into ArrayList<String> & returns it back to the caller
*
* @return - List of World Series champions
* @throws IOException
*/
private static ArrayList<String> loadChampions() throws IOException
{
// Open file
File fileObj = new File("champions.txt");
if (!fileObj.exists())
{
System.out.println("\nFatal error ... Could not load champions");
System.exit(0);
}
Scanner inFile = new Scanner(fileObj);
// Load file into ArrayList
ArrayList<String> arr = new ArrayList<String>();
while(inFile.hasNext())
{
arr.add(inFile.nextLine());
}
// Close file
inFile.close();
return arr;
}
/**
* Displays the rules of the game when user chooses DISPLAY
*/
private static void displayRules()
{
System.out.println("\nThe goal of the World Series Champions game");
System.out.println("is to guess how many times a team has won");
System.out.println("the tournament. How much do you know about baseball?");
System.out.println("Let's see ...");
}
/**
* Calculates the number of times a team has won the World Series
*
* @param champions
* @param teamName
* @return wins
*/
private static int calcWins(ArrayList<String> champions, String teamName)
{
int wins = 0;
for (String team : champions)
{
if (team.equals(teamName))
{
wins++;
}
}
return wins;
}
/**
* Prompt user for number of times a team has won the World Series
*
* @param teamName - Team name to guess
* @return guess
*/
private static int getGuess(String teamName)
{
int guess = 0;
do
{
System.out.printf("\nHow many World Series has the %s won? ", teamName);
guess = stdIn.nextInt();
stdIn.nextLine();
if (guess < 1)
{
System.out.println("Error ... Your guess must be at least 1. Try again.");
}
} while (guess < 1);
return guess;
}
}