Commit ce1ce6c2 authored by Qi LI's avatar Qi LI
Browse files

add class Frame and ScoreTable

parent 57017304
package game;
public class Frame {
private int firstLance = -1;
private int secondLance = -1;
private int score = 0;
public Frame(int previousScore) {
this.score = previousScore;
}
public void enterPoint(int point) {
if (firstLance == -1) this.firstLance = point;
else this.secondLance = point;
this.score += point;
}
public void addBonus(int point) {
this.score += point;
}
public boolean isStrike() {
return firstLance == 10;
}
public boolean isSpare() {
return (firstLance + secondLance) == 10;
}
public boolean isFinished() {
return isStrike() || (firstLance != -1 && secondLance != -1);
}
public int getFirstLance() { return this.firstLance; }
public int getSecondLance() { return this.secondLance; }
public int getScore() { return this.score; }
}
package game;
import java.util.List;
import java.util.ArrayList;
public class ScoreTable {
private List<Frame> frames = new ArrayList<Frame>();
public ScoreTable() {}
public void enterPoint(int point) {
getFrameToEnter().enterPoint(point);
updateScoreTable();
}
public int getCurrentScore() {
return frames.get(frames.size() - 1).getScore();
}
private Frame getFrameToEnter() {
Frame frame = null;
if (frames.size() == 0) {
frame = new Frame(0);
this.frames.add(frame);
} else {
Frame currentFrame = frames.get(frames.size() - 1);
if (currentFrame.isFinished()) {
frame = new Frame(currentFrame.getScore());
this.frames.add(frame);
} else frame = currentFrame;
}
return frame;
}
private void updateScoreTable() {
int numberFrames = frames.size();
Frame currentFrame = frames.get(numberFrames - 1);
Frame lastFrame = numberFrames > 1 ? frames.get(numberFrames - 2) : null;
Frame beforeLastFrame = numberFrames > 2 ? frames.get(numberFrames - 3) : null;
if (lastFrame == null) {
return;
} else if (currentFrame.isStrike() || !currentFrame.isFinished()) {
int bonus = 0;
int lance = currentFrame.getFirstLance();
if (lastFrame.isStrike() || lastFrame.isSpare()) {
lastFrame.addBonus(lance);
bonus += lance;
}
if (beforeLastFrame != null && beforeLastFrame.isStrike()
&& lastFrame.isStrike()) {
beforeLastFrame.addBonus(lance);
lastFrame.addBonus(lance);
bonus += lance;
}
currentFrame.addBonus(bonus);
} else {
int bonus = 0;
int lance = currentFrame.getSecondLance();
if (lastFrame.isStrike()) {
lastFrame.addBonus(lance);
bonus += lance;
}
currentFrame.addBonus(bonus);
}
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
import game.Frame;
public class TestFrame {
public TestFrame() {}
@Test
public void testCase0() {
Frame frame = new Frame(0);
frame.enterPoint(7);
Assert.assertArrayEquals(
new boolean[] {false, false, false},
new boolean[] {frame.isFinished(), frame.isSpare(), frame.isStrike()});
}
@Test
public void testCase1() {
Frame frame = new Frame(0);
frame.enterPoint(10);
Assert.assertArrayEquals(
new boolean[] {true, false, true},
new boolean[] {frame.isFinished(), frame.isSpare(), frame.isStrike()});
}
@Test
public void testCase2() {
Frame frame = new Frame(0);
frame.enterPoint(7);
frame.enterPoint(3);
Assert.assertArrayEquals(
new boolean[] {true, true, false},
new boolean[] {frame.isFinished(), frame.isSpare(), frame.isStrike()});
}
@Test
public void testCase3() {
Frame frame = new Frame(0);
frame.enterPoint(7);
frame.enterPoint(0);
Assert.assertArrayEquals(
new boolean[] {true, false, false},
new boolean[] {frame.isFinished(), frame.isSpare(), frame.isStrike()});
}
}
package test;
import org.junit.Assert;
import org.junit.Test;
import game.ScoreTable;
public class TestScoreTable {
public TestScoreTable() {}
@Test
public void testCase0() {
ScoreTable table = new ScoreTable();
int[] points = {7,2,10,10,7,3,8};
int[] expected = {7, 9, 19, 39, 60, 66, 82};
int[] scores = new int[points.length];
for (int i = 0; i < points.length; ++i) {
table.enterPoint(points[i]);
scores[i] = table.getCurrentScore();
}
Assert.assertArrayEquals(expected, scores);
}
@Test
public void testCase1() {
ScoreTable table = new ScoreTable();
int[] points = {10, 3, 6};
int[] expected = {28};
for (int i = 0; i < points.length; ++i)
table.enterPoint(points[i]);
Assert.assertArrayEquals(expected, new int[] {table.getCurrentScore()});
}
@Test
public void testCase2() {
ScoreTable table = new ScoreTable();
int[] points = {10, 10, 9, 0};
int[] expected = {57};
for (int i = 0; i < points.length; ++i)
table.enterPoint(points[i]);
Assert.assertArrayEquals(expected, new int[] {table.getCurrentScore()});
}
@Test
public void testCase3() {
ScoreTable table = new ScoreTable();
int[] points = {10, 10, 10, 0, 9};
int[] expected = {78};
for (int i = 0; i < points.length; ++i)
table.enterPoint(points[i]);
Assert.assertArrayEquals(expected, new int[] {table.getCurrentScore()});
}
@Test
public void testCase4() {
ScoreTable table = new ScoreTable();
int[] points = {10, 10, 4, 2};
int[] expected = {46};
for (int i = 0; i < points.length; ++i)
table.enterPoint(points[i]);
Assert.assertArrayEquals(expected, new int[] {table.getCurrentScore()});
}
@Test
public void testCase5() {
ScoreTable table = new ScoreTable();
int[] points = {7, 3, 4, 2};
int[] expected = {20};
for (int i = 0; i < points.length; ++i)
table.enterPoint(points[i]);
Assert.assertArrayEquals(expected, new int[] {table.getCurrentScore()});
}
}
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