Skip to content

Instantly share code, notes, and snippets.

@connlark
Created April 11, 2016 15:39
Show Gist options
  • Save connlark/d93c77978b203c7956dfa8626f638cd9 to your computer and use it in GitHub Desktop.
Save connlark/d93c77978b203c7956dfa8626f638cd9 to your computer and use it in GitHub Desktop.
lot of dirty code I dont care enough to fix.
/**
* Created by Connor on 3/30/16.
*/
public class BowlingScore implements Comparable<BowlingScore>
{
private String bowler;
private String rolls[]; //each pair of rolls is a frame
/**
* Constructor for objects of class BowlingScore
*/
public BowlingScore(String bowler, String[] rolls) {
this.bowler = bowler;
this.rolls = rolls;
}
public BowlingScore(String bowler) {
this.bowler = bowler;
rolls = new String[10];
}
/**
* getScore - calculates score of game
*
* @precondition - the data in rolls represents a valid game
* @return - the scored result of the game, -1 if game is unfinished
*/
public int getScore() {
if (!isValidGame() || !isComplete()) return -1;
int total = 0;
for (int i = 0; i < rolls.length && i != 9; i++) {
String next = "--",nextNext="--";
try {
next = (rolls.length > i)? rolls[i+1]:"--";
nextNext = (rolls.length > i+1)? rolls[i+2]:"--";
}
catch (ArrayIndexOutOfBoundsException e){} // too lazy to fix
if (rolls[i].contains("X"))
total += 10 + getNextTwoRolls(next,nextNext);
else if (rolls[i].contains("/"))
total += 10 + getNextRoll(next);
else total += getSumOfNumbers(rolls[i]);
}
if (rolls.length > 8){
String frame = rolls[9];
if (frame.length() != 3)
total += getNextTwoRolls(frame,"");
else {
if (frame.charAt(1) == 'X')
total += 20 + getNextRoll(""+frame.charAt(2));
else if (frame.charAt(1) == '/')
total += 10+getNextRoll(""+frame.charAt(2));
else
total += 10 + getNextTwoRolls(frame.substring(1),"");
}
}
return total;
}
public static int getNextTwoRolls(String first,String second){
int total = 0;
if (first.length() == 3)
return (first.charAt(1) == '/')? 10:getNextRoll(first.charAt(0)+"") + getNextRoll(first.charAt(1)+"");
if (first.charAt(0)!= 'X'){
total = getSumOfNumbers(first);
total = (first.contains("/")) ? 10:total;
return total;
}
total += 10;
if (second.charAt(0)== 'X') return 10 + total;
return (second.charAt(0) + "").matches("[0-9].*") ? Integer.parseInt(second.substring(0,1)) + total:total;
}
public static int getNextRoll(String str){
int total = 0;
char roll = str.charAt(0);
total = (roll + "").matches("[0-9].*") ? Integer.parseInt(str.substring(0,1)) + total:total;
total = (roll == 'X') ? 10:total;
return total;
}
/**
* compareTo - For a complete game, the higher score is considered larger.
* - Any complete game is considered larger than an incomplete
* one.
* - All incomplete games are equivalent.
*/
@Override
public int compareTo(BowlingScore other) {
return getScore() - other.getScore();
}
/**
* addFrame - add the result of two rolls to the first empty frame
* @precondition - the two rolls represents a valid frame
* - the game is not yet finished
* @return - true if add was successful, false otherwise (preconditions
* not met)
*/
public boolean addFrame(String firstRoll, String secondRoll) {
if (!isComplete() && isValidFrame(firstRoll + secondRoll)){
rolls[rolls.length-1] = firstRoll + secondRoll;
return true;
}
return false;
}
/**
* addFrame - add the result of two or three rolls to the 10th frame
* @precondition - the two rolls represents a valid frame
* - the game is in the 10th frame
* - if there is not "mark" in the frame, thirdRoll == ""
* @return - true if add was successful, false otherwise (preconditions
* not met)
*/
public boolean addFrame(String firstRoll, String secondRoll, String thirdRoll) {
boolean b = true;
if (thirdRoll.equals(""))return isValidFrame(firstRoll+secondRoll) && !secondRoll.equals("/");
if (rolls[9].length() == 3)
b = (rolls[9].contains("/") || rolls[9].contains("X"));
return b && isValidThirdFrame(firstRoll+secondRoll+thirdRoll);
}
/**
* isValidGame - determines whether or not the rolls represent a valid
* game
*/
public boolean isValidGame() {
for (int i = 0; i < rolls.length && i != 9 && rolls[i] != null; i++) {
String frame = rolls[i];
if (!isValidFrame(frame))
return false;
}
return !(rolls.length == 10 && rolls[9] != null) || isValidThirdFrame(rolls[9]);
}
static boolean isValidFrame(String s){
if (s.contains("X"))
return s.equals("X");
return s.length() == 2 && getSumOfNumbers(s) <= 9 &&
s.indexOf("/") != 0;
}
static boolean isValidThirdFrame(String s){
boolean b = true;
if (s.length() == 3) {
if (s.contains("/"))
b = s.charAt(0) != 'X';
else b = s.contains("X");
}
return b && getSumOfNumbers(s.substring(0,2)) <= 9 &&
s.indexOf("/") != 0;
}
//checks if frames == 10
public boolean isComplete(){
int i = 0;
for (String s:rolls)
if (s != null) i++;
return i == 10;
}
public static int getSumOfNumbers(String s){
int out = 0;
char[] n = s.toCharArray();
for (char ch : n) {
String foo = Character.toString(ch);
if ((ch + "").matches("[0-9].*"))
out += Integer.parseInt(foo);
}
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment