Commit 570741b4 authored by Qi LI's avatar Qi LI
Browse files

add isComplete()

parent ce1ce6c2
......@@ -17,6 +17,26 @@ public class ScoreTable {
return frames.get(frames.size() - 1).getScore();
}
public Frame getCurrentFrame() {
if (frames.size() == 0) return null;
else return frames.get(frames.size() - 1);
}
public boolean isComplete() {
int framesNumber = frames.size();
if (framesNumber < 10) return false;
else if (framesNumber == 10) {
Frame frame = frames.get(framesNumber - 1);
return frame.isFinished() && !frame.isSpare() && !frame.isStrike();
} else if (framesNumber == 11) {
Frame currentFrame = frames.get(framesNumber - 1);
Frame lastFrame = frames.get(framesNumber - 2);
boolean hasTwoBalls = currentFrame.isSpare() || (currentFrame.isFinished() && !currentFrame.isStrike());
return lastFrame.isSpare() || (lastFrame.isStrike() && hasTwoBalls);
} else return true;
}
private Frame getFrameToEnter() {
Frame frame = null;
......@@ -71,9 +91,4 @@ public class ScoreTable {
currentFrame.addBonus(bonus);
}
}
}
}
\ No newline at end of file
package test;
import javax.swing.plaf.metal.MetalBorders.TableHeaderBorder;
import org.junit.Assert;
import org.junit.Test;
......@@ -81,4 +83,55 @@ public class TestScoreTable {
Assert.assertArrayEquals(expected, new int[] {table.getCurrentScore()});
}
@Test
public void testCase6() {
ScoreTable table = new ScoreTable();
int[] points = {10,10,10,10,10,10,10,10,10,8,1};
boolean[] expected = new boolean[points.length];
boolean[] actual = new boolean[points.length];
for (int i = 0; i < points.length; ++i) {
table.enterPoint(points[i]);
expected[i] = false;
actual[i] = table.isComplete();
}
expected[expected.length - 1] = true;
Assert.assertArrayEquals(expected, actual);
}
@Test
public void testCase7() {
ScoreTable table = new ScoreTable();
int[] points = {10,10,10,10,10,10,10,10,10,8,2,10};
boolean[] expected = new boolean[points.length];
boolean[] actual = new boolean[points.length];
for (int i = 0; i < points.length; ++i) {
table.enterPoint(points[i]);
expected[i] = false;
actual[i] = table.isComplete();
}
expected[expected.length - 1] = true;
Assert.assertArrayEquals(expected, actual);
}
@Test
public void testCase8() {
ScoreTable table = new ScoreTable();
int[] points = {10,10,10,10,10,10,10,10,10,10,7,2};
boolean[] expected = new boolean[points.length];
boolean[] actual = new boolean[points.length];
for (int i = 0; i < points.length; ++i) {
table.enterPoint(points[i]);
expected[i] = false;
actual[i] = table.isComplete();
}
expected[expected.length - 1] = true;
Assert.assertArrayEquals(expected, actual);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment