/* * GameResult.java * * Created on June 18, 2003, 8:06 PM */ package gamekeeper2a; /** Information about a single, completed game. * * @author dickey */ public class GameResult implements IGameResult { /** Name of the winner. */ private String winnerName = ""; /** Name of the loser. */ private String loserName = ""; /** Score of the winner. */ private int winnerScore = -1; /** Score of the loser. */ private int loserScore = -1; /** Creates a new instance of GameResult. * High score is assumed to be the winner. * @param rawInforString is exactly as for the 2-argument version * of this constructor. * rawInfoString is not null. */ public GameResult(String rawInfoString) { this(rawInfoString, true); } /** Creates a new instance of GameResult. * @param highScoreWins true if high score wins the game, false * if low score wins. * @param rawInfoString a string giving the game result. * Valid strings have this format: * , * The two names must be different. * The scores must be integers. * rawInfoString is not null. * If a string is invalid, the name fields are set to null and the * score fields are set to 0; * a better strategy would be to throw an exception. * The names are trimmed of any leading or trailing spaces. */ public GameResult(String rawInfoString, boolean highScoreWins) { if (rawInfoString == null ) { // invalid format return; } // Invalid format if there is more than one comma // When splitting on a single comma, the resulting array should have length 2 if (rawInfoString.split(",").length != 2) { return; } rawInfoString = rawInfoString.trim(); int commaPos = rawInfoString.indexOf(','); if (commaPos < 0) { // invalid format return; } //Get the space just before the number int score1Start = rawInfoString.lastIndexOf(' ', commaPos); if (score1Start < 0) { // invalid format return; } String tempName1 = rawInfoString.substring(0, score1Start).trim(); if (tempName1.length() < 1) { // invalid format -- no first team name return; } String tempScore1 = rawInfoString.substring(score1Start, commaPos); int tempNumScore1; try { tempNumScore1 = Integer.parseInt(tempScore1.trim()); } catch (NumberFormatException e) { //invalid format return; } // We now know all about the first team: name and score // Now get information about the second team. int spacePos2 = rawInfoString.lastIndexOf(' '); String tempName2 = rawInfoString.substring(commaPos+1, spacePos2).trim(); if (tempName2.length() < 1) { // invalid string -- no second name return; } if (tempName1.equals(tempName2)) { //invalid return; } String tempScore2 = rawInfoString.substring(spacePos2+1, rawInfoString.length()); int tempNumScore2; try { tempNumScore2 = Integer.parseInt(tempScore2.trim()); } catch (NumberFormatException e) { throw e; } //Now we have all the facts, and are sure there were no errors. if ((highScoreWins && tempNumScore1 >= tempNumScore2) || (!highScoreWins && tempNumScore1 < tempNumScore2)) { this.winnerName = tempName1; this.winnerScore = tempNumScore1; this.loserName = tempName2; this.loserScore = tempNumScore2; } else { this.winnerName = tempName2; this.winnerScore = tempNumScore2; this.loserName = tempName1; this.loserScore = tempNumScore1; } //end constructor } /** Figure out the game result, under the default assumption * that high score wins. */ public GameResult(String name1, int score1, String name2, int score2) { this(name1 + " " + score1 + "," + name2 + " " + score2); } /** Figure out the game result. * @param highScoreWins true if high score wins false if low score wins. */ public GameResult(String name1, int score1, String name2, int score2, boolean highScoreWins) { this(name1 + " " + score1 + "," + name2 + " " + score2, highScoreWins); } public boolean equals(Object otherResult) { GameResult otherg = (GameResult) otherResult; return otherg != null && this.winnerName.equals(otherg.winnerName) && this.winnerScore == otherg.winnerScore && this.loserName.equals(otherg.loserName) && this.loserScore == otherg.loserScore; } public int hashCode() { return this.winnerName.hashCode() + 11 * this.winnerScore + 13 * this.loserName.hashCode() + 17 * this.loserScore; } /** Tells the name of the loser of the match. */ public String getLoserName() { return this.loserName; } /** Tells the score of the loser. */ public int getLoserScore() { return this.loserScore; } /** Tells if this match is a draw (true) or not (false). */ public boolean isDraw() { return this.getWinnerScore() == this.getLoserScore(); } /** Tells the name of the winner of the match. * In case of a tie, this should return the 1st name of the two. */ public String getWinnerName() { return this.winnerName; } /** Tells the score of the winner. */ public int getWinnerScore() { return this.winnerScore; } /** Make a nice, one-line printable string. */ public String toString() { String info = "Winner: (" + this.getWinnerName() + " " + this.getWinnerScore() + ") " + "Loser: (" + this.getLoserName() + " " + this.getLoserScore() + ")"; return info; } private static boolean testDrawGame(String gameString) { IGameResult gr = new GameResult(gameString); System.out.println(gr.toString()); assert gr.isDraw(); assert !gr.getWinnerName().equals(gr.getLoserName()); //create a game whethere the score IS different assert gr.getWinnerScore() == gr.getLoserScore(); IGameResult gr2 = new GameResult(gr.getWinnerName(), 1 + gr.getWinnerScore(), gr.getLoserName(), gr.getLoserScore()); assert gr2.getWinnerName().equals(gr.getWinnerName()); assert gr2.getLoserName().equals(gr.getLoserName()); assert gr2.getWinnerScore() != gr.getWinnerScore(); assert gr2.getLoserScore() == gr.getLoserScore(); //same info, different order IGameResult gr3 = new GameResult(gr2.getLoserName(), gr2.getLoserScore(), gr2.getWinnerName(), gr2.getWinnerScore()); assert gr3.getWinnerName().equals(gr2.getWinnerName()); assert gr3.getLoserName().equals(gr2.getLoserName()); assert gr3.equals(gr2); //test "low score wins" IGameResult grL = new GameResult(gameString, false); assert grL.isDraw(); IGameResult grLx = new GameResult(gameString, true); assert grL.isDraw(); IGameResult grL2 = new GameResult(gr.getWinnerName(), 1 + gr.getWinnerScore(), gr.getLoserName(), gr.getLoserScore(), false); assert grL2.getWinnerName().equals(gr.getLoserName()); assert grL2.getLoserName().equals(gr.getWinnerName()); assert grL2.getWinnerScore() == gr.getLoserScore(); assert grL2.getLoserScore() != gr.getWinnerScore(); assert !grL2.equals(gr); return true; } /** Purely for testing. */ public static void main(String [] args) { System.out.println("Start main"); boolean r1 = testDrawGame("Mariners 0, Ducks 00"); assert r1; assert testDrawGame("Blue Devils 14, Red Devils 14"); System.out.println("End main"); } // end class GameResult }