Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is supposed to read scores in a basketabll game. I can't seem to figure out why it won't read past the 4th quarter, it needs to read up until the 9th so basically from 5th-9th quarter should be considered OT. Also, how can I add the quarter label above the output score. Thanks in advance!
Java
public static int REGULATION_NUM_QUARTERS = 4;

public static int MAX_BOX_SCORE_LENGTH = 9;

public static final int SENTINEL = -999;

private static final int AWAY = 1;
private static final int HOME = 2; 


public static void initialize (int[] awayScores, int [] homeScores ) {
	
	for (int i = 0; i < MAX_BOX_SCORE_LENGTH; i++ ){
		awayScores [i] = SENTINEL; 

	}

	for (int i = 0; i < MAX_BOX_SCORE_LENGTH; i++ ){
		homeScores [i] = SENTINEL; 
	}

}

public static void readScores(Scanner scores, int[] awayScores, int[] homeScores) {

	int team = AWAY; 
	int quarter = 1;

	while (!gameIsOver(quarter, team, awayScores, homeScores)) {

		if (team == AWAY) { 
			awayScores [quarter -1]=  scores.nextInt();
			team = HOME; 
		}

		else {
			homeScores [quarter-1]=  scores.nextInt(); 
			team = AWAY; 
			quarter++;
		}
	}
}

public static int gameScore(int[] teamBoxScore) {
	int output = 0;
	for (int i : teamBoxScore) {
		if (i != SENTINEL) {
			output += i;
		}
	}
	return output;
}


public static int quarters(int[] teamBoxScore) {
	int output = 0;
	for (int i : teamBoxScore) {
		if (i != SENTINEL) {
			output++;
		}
	}
	return output;
}

public static boolean gameIsOver(int quarter, int team, int[] awayScores, int[] homeScores) {

	int awayScore = gameScore(awayScores);
	int homeScore = gameScore(homeScores);

	return (quarter > REGULATION_NUM_QUARTERS && team == AWAY && awayScore != homeScore)
			|| (quarter == REGULATION_NUM_QUARTERS && team == HOME && awayScore < homeScore);
}

public static void printBoxScore(int[] awayScores, int[] homeScores) {

	System.out.println();
	System.out.print(" Away: ");
	for (int v : awayScores) {
		if (v != SENTINEL) {
			System.out.print(v);
		}
	}
	System.out.println();

	System.out.print("    Home: ");
	for (int v : homeScores) {
		if (v != SENTINEL) {
			System.out.print(v);		
		}
	}			
	System.out.println();
}

public static int printBoxScore(int[] teamBoxScore) {
	int i;
	for (i = 0; i < teamBoxScore.length; i++) {
		if (teamBoxScore[i] != SENTINEL) {
			System.out.print(teamBoxScore[i] + " ");
		} else
			break;
	}
	System.out.println();
	return i;
}

public static int printBoxScore(String name, int[] teamBoxScore) {
	System.out.print(name);
	return printBoxScore(teamBoxScore);
}


public static String result(int quarter, int awayScore, int homeScore) {

	if (awayScore > homeScore) {
		return "The away team won " 
				+ awayScore + " to " + homeScore
				+ " in REGULATION";
	} 
	if (homeScore > awayScore ) {
		return "The home team won " 
				+ homeScore + " to " + awayScore
				+ " in REGULATION";
	}
	return "";
}

public static String result(int[] awayScores, int[] homeScores) {

	int awayScore = gameScore(awayScores);
	int homeScore = gameScore(homeScores);

	int quarter = printBoxScore(" Away : ", awayScores);

	printBoxScore("    Home: ", homeScores);

	return result(quarter, awayScore, homeScore);
}

public static void main(String[] args) {

	int[] awayScores = new int [MAX_BOX_SCORE_LENGTH]; 
	int[] homeScores = new int [MAX_BOX_SCORE_LENGTH];

	initialize (awayScores, homeScores);

	Scanner input = new Scanner(System.in);
	System.out.print("Do you want to see BoxScore? (y or n): ");
	String answer = input.next();

	System.out.println("Please enter the scores below");
	readScores(input, awayScores, homeScores);

	
	
	if (answer.charAt(0) == 'y' || answer.charAt(0) == 'Y') {
		System.out.println(result(awayScores, homeScores));
	} 
	else {
		System.out.println(result(quarters(awayScores), gameScore(awayScores), gameScore(homeScores)));

		
	}		
	
}
}

[Edit: pre tags]
Posted
Updated 10-Mar-14 23:21pm
v5
Comments
phil.o 11-Mar-14 5:28am    
5th to 9th -> these are not quarters anymore...
I don't get it: what do these quarters define? If they define the game-periods in a basketball game, so why are there more than 4 of them? Moreover, your REGULATION_NUM_QUARTERS constant seems to define it right. Did you write this code by yourself?
Richard MacCutchan 11-Mar-14 5:34am    
The return value from gameIsOver must be incorrect. I have tried to run this but cannot figure out exactly what it is supposed to be doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900