001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package eg.bowling.fixtures;
004:
005: import eg.bowling.*;
006: import fitnesse.fixtures.TableFixture;
007:
008: public class ScoreGame extends TableFixture {
009: private Bowling game;
010:
011: public static final int STRIKE = -1;
012: public static final int SPARE = -2;
013: public static final int BLANK = -3;
014: public static final int ERROR = -4;
015:
016: protected void doStaticTable(int rows) {
017: game = new BowlingGame();
018: doRolls();
019: doScores();
020: }
021:
022: private void doRolls() {
023: for (int roll = 0; roll < 21; roll++) {
024: int pins = parseRoll(roll);
025: if (pins == SPARE)
026: spare(roll);
027: else if (pins == STRIKE)
028: strike(roll);
029: else if (pins == BLANK)
030: blank(roll);
031: else if (pins == ERROR)
032: wrongRoll(roll);
033: else {
034: roll(pins);
035: }
036: }
037: }
038:
039: private int parseRoll(int roll) {
040: String rollText = getText(1, roll);
041: if (rollText.equals("/"))
042: return SPARE;
043: else if (rollText.equals("X"))
044: return STRIKE;
045: else if (rollText.equals(""))
046: return BLANK;
047: else {
048: try {
049: int pins = Integer.parseInt(rollText);
050: return pins;
051: } catch (NumberFormatException e) {
052: return ERROR;
053: }
054: }
055: }
056:
057: private void spare(int rollNumber) {
058: if (odd(rollNumber))
059: wrongRoll(rollNumber);
060: else {
061: int previousRoll = parseRoll(rollNumber - 1);
062: if (previousRoll < 0)
063: wrongRoll(rollNumber);
064: else
065: roll(10 - previousRoll);
066: }
067: }
068:
069: private void wrongRoll(int rollNumber) {
070: wrong(1, rollNumber);
071: }
072:
073: private void strike(int rollNumber) {
074: if (rollNumber == 18)
075: roll(10);
076: else if (rollNumber == 19 && parseRoll(18) == STRIKE)
077: roll(10);
078: else if (rollNumber == 20
079: && (parseRoll(19) == STRIKE || parseRoll(19) == SPARE))
080: roll(10);
081: else if (odd(rollNumber))
082: wrongRoll(rollNumber);
083: else {
084: int previousRoll = parseRoll(rollNumber - 1);
085: if (previousRoll != BLANK)
086: wrongRoll(rollNumber);
087: else
088: roll(10);
089: }
090: }
091:
092: private boolean odd(int rollNumber) {
093: return (rollNumber % 2) != 1;
094: }
095:
096: private void blank(int roll) {
097: if (roll == 20 && parseRoll(19) != SPARE
098: && parseRoll(18) != STRIKE)
099: return;
100: if (parseRoll(roll + 1) == STRIKE)
101: return;
102: wrongRoll(roll);
103: }
104:
105: private void roll(int pins) {
106: game.roll(pins);
107: }
108:
109: private void doScores() {
110: for (int frame = 0; frame < 10; frame++) {
111: int expectedScore = getScore(frame);
112: int actualScore = game.score(frame + 1);
113: if (expectedScore == actualScore)
114: rightScore(frame);
115: else
116: wrongScore(frame, "" + actualScore);
117: }
118:
119: }
120:
121: private void rightScore(int frame) {
122: right(2, scoreIndex(frame));
123: }
124:
125: private void wrongScore(int frame, String actual) {
126: wrong(2, scoreIndex(frame), actual);
127: }
128:
129: private int getScore(int frame) {
130: return Integer.parseInt(getText(2, scoreIndex(frame)));
131: }
132:
133: private int scoreIndex(int frame) {
134: return frame * 2 + 1;
135: }
136: }
|