Skip to content

Commit

Permalink
[Refactoring] Strategy pattern of getScore() in Lane.java
Browse files Browse the repository at this point in the history
  • Loading branch information
inrir committed Jun 2, 2023
1 parent a177d92 commit fb1e5a7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 95 deletions.
38 changes: 38 additions & 0 deletions DefaultScoringStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,42 @@ class DefaultScoringStrategy implements ScoringStrategy {
public String getScoreSymbol(int[] bowlerScores, int ballIndex) {
return Integer.toString(bowlerScores[ballIndex]);
}
@Override
public void computeScore(int[] curScore, int[][] cumulScores, int bowlIndex, int current, int i) {
//We're dealing with a normal throw, add it and be on our way.
if( i%2 == 0 && i < 18){
if ( i/2 == 0 ) {
//First frame, first ball. Set his cumul score to the first ball
if(curScore[i] != -2){
cumulScores[bowlIndex][i/2] += curScore[i];
}
} else if (i/2 != 9){
//add his last frame's cumul to this ball, make it this frame's cumul.
if(curScore[i] != -2){
cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 - 1] + curScore[i];
} else {
cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 - 1];
}
}
} else if (i < 18){
if(curScore[i] != -1 && i > 2){
if(curScore[i] != -2){
cumulScores[bowlIndex][i/2] += curScore[i];
}
}
}
if (i/2 == 9){
if (i == 18){
cumulScores[bowlIndex][9] += cumulScores[bowlIndex][8];
}
if(curScore[i] != -2){
cumulScores[bowlIndex][9] += curScore[i];
}
} else if (i/2 == 10) {
if(curScore[i] != -2){
cumulScores[bowlIndex][9] += curScore[i];
}
}
}

}
5 changes: 5 additions & 0 deletions FoulScoringStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ class FoulScoringStrategy implements ScoringStrategy {
public String getScoreSymbol(int[] bowlerScores, int ballIndex) {
return (bowlerScores[ballIndex] == -2) ? "F" : null;
}

@Override
public void computeScore(int[] curScore, int[][] cumulScores, int bowlIndex, int current, int i) {
// foul은 점수 계산에 영향이 없음
}
}
107 changes: 13 additions & 94 deletions Lane.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public class Lane extends Thread implements PinsetterObserver {
private ScoreHistoryFile shf;
private Bowler currentThrower; // = the thrower who just took a throw

private ScoringStrategy strategy; // 점수 계산 전략 패턴
/** Lane()
*
* Constructs a new lane and starts its thread
Expand Down Expand Up @@ -454,109 +455,27 @@ private LaneEvent lanePublish( ) {
*
* @return The bowlers total score
*/
private int getScore( Bowler Cur, int frame) { // 전략 패턴 도입 예정
private int getScore( Bowler Cur, int frame) { // 전략 패턴 도입
int[] curScore;
int strikeballs = 0;
int totalScore = 0;
curScore = scores.get(Cur);
for (int i = 0; i != 10; i++){
cumulScores[bowlIndex][i] = 0;
}
int current = 2*(frame - 1)+ball-1;
//Iterate through each ball until the current one.
for (int i = 0; i != current+2; i++){
//Spare:
if( i%2 == 1 && curScore[i - 1] + curScore[i] == 10 && i < current - 1 && i < 19){
//This ball was a the second of a spare.
//Also, we're not on the current ball.
//Add the next ball to the ith one in cumul.
cumulScores[bowlIndex][(i/2)] += curScore[i+1] + curScore[i];
if (i > 1) {
//cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 -1];
}
} else if( i < current && i%2 == 0 && curScore[i] == 10 && i < 18){
strikeballs = 0;
//This ball is the first ball, and was a strike.
//If we can get 2 balls after it, good add them to cumul.
if (curScore[i+2] != -1) {
strikeballs = 1;
if(curScore[i+3] != -1) {
//Still got em.
strikeballs = 2;
} else if(curScore[i+4] != -1) {
//Ok, got it.
strikeballs = 2;
}
}
if (strikeballs == 2){
//Add up the strike.
//Add the next two balls to the current cumulscore.
cumulScores[bowlIndex][i/2] += 10;
if(curScore[i+1] != -1) {
cumulScores[bowlIndex][i/2] += curScore[i+1] + cumulScores[bowlIndex][(i/2)-1];
if (curScore[i+2] != -1){
if( curScore[i+2] != -2){
cumulScores[bowlIndex][(i/2)] += curScore[i+2];
}
} else {
if( curScore[i+3] != -2){
cumulScores[bowlIndex][(i/2)] += curScore[i+3];
}
}
} else {
if ( i/2 > 0 ){
cumulScores[bowlIndex][i/2] += curScore[i+2] + cumulScores[bowlIndex][(i/2)-1];
} else {
cumulScores[bowlIndex][i/2] += curScore[i+2];
}
if (curScore[i+3] != -1){
if( curScore[i+3] != -2){
cumulScores[bowlIndex][(i/2)] += curScore[i+3];
}
} else {
cumulScores[bowlIndex][(i/2)] += curScore[i+4];
}
}
} else {
break;
}
}else {
//We're dealing with a normal throw, add it and be on our way.
if( i%2 == 0 && i < 18){
if ( i/2 == 0 ) {
//First frame, first ball. Set his cumul score to the first ball
if(curScore[i] != -2){
cumulScores[bowlIndex][i/2] += curScore[i];
}
} else if (i/2 != 9){
//add his last frame's cumul to this ball, make it this frame's cumul.
if(curScore[i] != -2){
cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 - 1] + curScore[i];
} else {
cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 - 1];
}
}
} else if (i < 18){
if(curScore[i] != -1 && i > 2){
if(curScore[i] != -2){
cumulScores[bowlIndex][i/2] += curScore[i];
}
}
}
if (i/2 == 9){
if (i == 18){
cumulScores[bowlIndex][9] += cumulScores[bowlIndex][8];
}
if(curScore[i] != -2){
cumulScores[bowlIndex][9] += curScore[i];
}
} else if (i/2 == 10) {
if(curScore[i] != -2){
cumulScores[bowlIndex][9] += curScore[i];
}
}

for (int i = 0; i != current+2; i++){ // spare, strike, normal 나누기
if (i%2 == 1 && curScore[i - 1] + curScore[i] == 10 && i < current - 1 && i < 19){
strategy = new SpareScoringStrategy();
} else if (i < current && i%2 == 0 && curScore[i] == 10 && i < 18){
strategy = new StrikeScoringStrategy();
} else {
strategy = new DefaultScoringStrategy();
}
}
strategy.computeScore(curScore, cumulScores, bowlIndex, current, i);

}
return totalScore;
}

Expand Down
4 changes: 3 additions & 1 deletion ScoringStrategy.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
interface ScoringStrategy {
interface ScoringStrategy { // 심볼 표시와 점수 계산 인터페이스
String getScoreSymbol(int[] bowlerScores, int ballIndex);

void computeScore(int[] curScore, int[][] cumulScores, int bowlIndex, int current, int i);
}
11 changes: 11 additions & 0 deletions SpareScoringStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@ class SpareScoringStrategy implements ScoringStrategy {
public String getScoreSymbol(int[] bowlerScores, int ballIndex) {
return (ballIndex > 0 && bowlerScores[ballIndex] + bowlerScores[ballIndex - 1] == 10 && ballIndex % 2 == 1) ? "/" : null;
}

@Override
public void computeScore(int[] curScore, int[][] cumulScores, int bowlIndex, int current, int i) {
// This ball was a the second of a spare.
//Also, we're not on the current ball.
//Add the next ball to the ith one in cumul.
cumulScores[bowlIndex][i/2] += curScore[i+1] + curScore[i];
if (i > 1) {
//cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 -1];
}
}
}
50 changes: 50 additions & 0 deletions StrikeScoringStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,54 @@ class StrikeScoringStrategy implements ScoringStrategy {
public String getScoreSymbol(int[] bowlerScores, int ballIndex) {
return (bowlerScores[ballIndex] == 10 && (ballIndex % 2 == 0 || ballIndex == 19)) ? "X" : null;
}


@Override
public void computeScore(int[] curScore, int[][] cumulScores, int bowlIndex, int current, int i) {
//your original Strike related logic here.
int strikeballs = 0;
//This ball is the first ball, and was a strike.
//If we can get 2 balls after it, good add them to cumul.
if (curScore[i+2] != -1) {
strikeballs = 1;
if(curScore[i+3] != -1) {
//Still got em.
strikeballs = 2;
} else if(curScore[i+4] != -1) {
//Ok, got it.
strikeballs = 2;
}
}
if (strikeballs == 2){
//Add up the strike.
//Add the next two balls to the current cumulscore.
cumulScores[bowlIndex][i/2] += 10;
if(curScore[i+1] != -1) {
cumulScores[bowlIndex][i/2] += curScore[i+1] + cumulScores[bowlIndex][(i/2)-1];
if (curScore[i+2] != -1){
if( curScore[i+2] != -2){
cumulScores[bowlIndex][(i/2)] += curScore[i+2];
}
} else {
if( curScore[i+3] != -2){
cumulScores[bowlIndex][(i/2)] += curScore[i+3];
}
}
} else {
if ( i/2 > 0 ){
cumulScores[bowlIndex][i/2] += curScore[i+2] + cumulScores[bowlIndex][(i/2)-1];
} else {
cumulScores[bowlIndex][i/2] += curScore[i+2];
}
if (curScore[i+3] != -1){
if( curScore[i+3] != -2){
cumulScores[bowlIndex][(i/2)] += curScore[i+3];
}
} else {
cumulScores[bowlIndex][(i/2)] += curScore[i+4];
}
}
}
}

}

0 comments on commit fb1e5a7

Please sign in to comment.