TDD practice (3)

Practical questions: Scoring in
        bowling games A bowling game is generally divided into ten innings, and a maximum of two balls can be thrown in each inning. If the first ball knocks down all the bottles, there is no need to play the second ball. However, the score of each game may depend on the score of subsequent throws, that is, if the first ball tossed in this game scores 10 points, we call it a complete hit, and the score of this game = 10 points + the score of the next two balls thrown, If a total of 10 points are thrown for two balls in this game, we call it a hit, and the score in this game = 10 points + the score of the next ball thrown.

        The following is a picture drawn by the coach during agile training:

        There are many answers. The following is my personal approach, and the specific process will not be shown. Attach the code directly:

BowlingGameTest.java

package com.bijian.study.bowling.test;

import org.junit.Assert;
import org.junit.Test;

import com.bijian.study.bowling.Game;

public class BowlingGameTest {

	@Test
	public void normal_roll_should_return_sum_of_tow_rolls() {
		Game game = new Game(20);
		game.roll(4);
		game.roll(5);
		Assert.assertEquals(4+5, game.getFrameScore(1));
		Assert.assertEquals(4+5, game.getTotalScore());
	}
	
	@Test
	public void spare_roll_should_return_sum_contain_next_roll() {
		Game game = new Game(20);
		game.roll(5);
		game.roll(5);
		game.roll(4);
		game.roll(5);
		Assert.assertEquals(5+5+4, game.getFrameScore(1));
		Assert.assertEquals(4+5, game.getFrameScore(2));
		Assert.assertEquals((5+5+4)+(4+5), game.getTotalScore());
	}
	
	@Test
	public void strike_roll_should_return_sum_contain_next_two_rolls() {
		Game game = new Game(20);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		Assert.assertEquals(10+5+4, game.getFrameScore(1));
		Assert.assertEquals(5+4, game.getFrameScore(2));
		Assert.assertEquals((10+5+4)+(5+4), game.getTotalScore());
	}
	
	@Test
	public void last_normal_roll_should_return_last_frame() {
		Game game = new Game(20);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		game.roll(3);
		game.roll(4);
		game.roll(10);
		game.roll(3);
		game.roll(3);
		game.roll(0);
		game.roll(3);
		game.roll(2);
		game.roll(8);
		game.roll(10);
		game.roll(10);
		game.roll(2);
		game.roll(3);
		Assert.assertEquals(10+5+4, game.getFrameScore(1));
		Assert.assertEquals(5+4, game.getFrameScore(2));
		Assert.assertEquals(3+4, game.getFrameScore(3));
		Assert.assertEquals(10+3+3, game.getFrameScore(4));
		Assert.assertEquals(3+3, game.getFrameScore(5));
		Assert.assertEquals(0+3, game.getFrameScore(6));
		Assert.assertEquals(2+8+10, game.getFrameScore(7));
		Assert.assertEquals(10+10+2, game.getFrameScore(8));
		Assert.assertEquals(10+2+3, game.getFrameScore(9));
		Assert.assertEquals(2+3, game.getFrameScore(10));
		Assert.assertEquals((10+5+4)+(5+4)+(3+4)+(10+3+3)+(3+3)+(0+3)+(2+8+10)+(10+10+2)+(10+2+3)+(2+3), game.getTotalScore());
	}
	
	@Test
	public void last_spare_roll_should_return_last_frame_with_add_one_roll() {
		Game game = new Game(20);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		game.roll(0);
		game.roll(0);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		game.roll(10);
		game.roll(10);
		game.roll(2);
		game.roll(8);
		game.roll(9);
		Assert.assertEquals(2+8+9, game.getFrameScore(10));
		Assert.assertEquals((10+5+4)+(5+4)+(10+5+4)+(5+4)+(0+0)+(10+5+4)+(5+4)+(10+10+2)+(10+2+8)+(2+8+9), game.getTotalScore());
	}
	
	@Test
	public void last_strike_roll_should_return_last_frame_with_add_two_roll() {
		Game game = new Game(20);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(8);
		game.roll(2);
		
		//Multiple throws to count invalid balls
		game.roll(3);
		
		Assert.assertEquals(10+5+4, game.getFrameScore(1));
		Assert.assertEquals(5+4, game.getFrameScore(2));
		Assert.assertEquals(10+10+10, game.getFrameScore(3));
		Assert.assertEquals(10+10+10, game.getFrameScore(4));
		Assert.assertEquals(10+10+5, game.getFrameScore(5));
		Assert.assertEquals(10+5+4, game.getFrameScore(6));
		Assert.assertEquals(5+4, game.getFrameScore(7));
		Assert.assertEquals(10+10+10, game.getFrameScore(8));
		Assert.assertEquals(10+10+8, game.getFrameScore(9));
		Assert.assertEquals(10+8+2, game.getFrameScore(10));
		
		//only 10 rounds
		Assert.assertEquals(0, game.getFrameScore(11));
		
		Assert.assertEquals((10+5+4)+(5+4)+(10+10+10)+(10+10+10)+(10+10+5)+(10+5+4)+(5+4)+(10+10+10)+(10+10+8)+(10+8+2), game.getTotalScore());
	}
	
	@Test
	public void last_strike_roll_should_return_last_frame_with_add_two_max_score_roll() {
		Game game = new Game(20);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(5);
		game.roll(5);
		game.roll(5);
		game.roll(4);
		game.roll(10);
		game.roll(5);
		game.roll(4);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		
		//Multiple throws void
		game.roll(10);
		
		Assert.assertEquals(0+0, game.getFrameScore(1));
		Assert.assertEquals(0+0, game.getFrameScore(2));
		Assert.assertEquals(0+0, game.getFrameScore(3));
		Assert.assertEquals(5+5+5, game.getFrameScore(4));
		Assert.assertEquals(5+4, game.getFrameScore(5));
		Assert.assertEquals(10+5+4, game.getFrameScore(6));
		Assert.assertEquals(5+4, game.getFrameScore(7));
		Assert.assertEquals(10+10+10, game.getFrameScore(8));
		Assert.assertEquals(10+10+10, game.getFrameScore(9));
		Assert.assertEquals(10+10, game.getFrameScore(10));
		Assert.assertEquals((0+0)+(0+0)+(0+0)+(5+5+5)+(5+4)+(10+5+4)+(5+4)+(10+10+10)+(10+10+10)+(10+10), game.getTotalScore());
	}
	
	@Test
	public void all_strike_roll_should_return_all_roll_with_last_two_max_score() {
		Game game = new Game(20);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		game.roll(10);
		
		//Multiple throws to count invalid balls
		game.roll(5);
		
		Assert.assertEquals(10+10+10, game.getFrameScore(1));
		Assert.assertEquals(10+10+10, game.getFrameScore(2));
		Assert.assertEquals(10+10+10, game.getFrameScore(3));
		Assert.assertEquals(10+10+10, game.getFrameScore(4));
		Assert.assertEquals(10+10+10, game.getFrameScore(6));
		Assert.assertEquals(10+10+10, game.getFrameScore(7));
		Assert.assertEquals(10+10+10, game.getFrameScore(8));
		Assert.assertEquals(10+10+10, game.getFrameScore(9));
		Assert.assertEquals(10+10, game.getFrameScore(10));
		Assert.assertEquals((10+10+10)+(10+10+10)+(10+10+10)+(10+10+10)+(10+10+10)+(10+10+10)+(10+10+10)+(10+10+10)+(10+10+10)+(10+10), game.getTotalScore());
	}
	
	@Test
	public void all_zero_roll_should_return_zero_score() {
		Game game = new Game(20);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		game.roll(0);
		Assert.assertEquals(0+0+0, game.getFrameScore(1));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(2));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(3));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(4));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(5));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(6));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(7));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(8));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(9));//0
		Assert.assertEquals(0+0+0, game.getFrameScore(10));//0
		Assert.assertEquals((0+0+0) + (0+0+0) + (0+0+0) + (0+0+0) + (0+0+0) + (0+0+0) + (0+0+0) + (0+0+0) + (0+0+0) + (0+0+0), game.getTotalScore());
	}
}

Game.java

package com.bijian.study.bowling;

/**
 * Bowling game, calculate the score of each round and the total score
 * Implementation:
 * Use the rollScore array to record the score of each pitch in the game. If the first ball is a full score in an inning, the second ball does not need to be pitched, but it is set to 0 in this array
 * Use the rollScoreState array to record whether the subscript corresponding to the rollScore is the real score or the value set for free voting
 *
 * @author BIJIAN
 */
public class Game {
	
	//full score
	public static int MAX_SCORE = 10;
	//Record the score of each pitch in the game. If the first ball is a full score in an inning, the second ball does not need to be pitched, but it is set to 0 in this array
	private int[] rollScore;
	//rollScoreState records whether the subscript corresponding to rollScore is the real score or the value set for free
	private boolean[] rollScoreState;
	//num is 2* the number of actual games
	private int num;
	// pitch counter
	private int currentRoll;
	
	/*
	 * Construction method
	 */
	public Game(int num) {
		this.num = num;
		rollScore = new int[num+4];
		rollScoreState = new boolean[num+2];
		this.currentRoll = 0;
	}
	
	/*
	 * Pitching
	 */
	public void roll(int pin) {
		if(currentRoll >= num + 2) {
			return;
		}
		if(currentRoll > num && rollScore[currentRoll-1] == MAX_SCORE) {
			return;
		}
		rollScore[currentRoll++] = pin;
		if(pin == MAX_SCORE && currentRoll < num) {
			rollScore[currentRoll] = 0;
			rollScoreState[currentRoll++] = true;
		}
	}
	
	/*
	 * Get the score of the specified round
	 */
	public int getFrameScore(int frame) {
		if(frame > num /2) {
			return 0;
		}
		int res = rollScore[(frame-1)*2] + rollScore[(frame-1)*2 + 1];
		if(rollScore[(frame-1)*2] == MAX_SCORE) {
			res += rollScore[frame*2] + rollScore[frame*2 + 1];
			if(rollScore[frame*2] == MAX_SCORE && rollScoreState[(frame-1)*2 + 1]) {
				res += rollScore[(frame+1)*2];
			}
		}else if(res == MAX_SCORE) {
			res += rollScore[frame*2];
		}
		return res;
	}
	
	/*
	 * Get the total score of the game
	 */
	public int getTotalScore () {
		int totalScore = 0;
		for(int i=1;i<=currentRoll/2;i++) {
			if(i <= this.num/2) {
				totalScore += getFrameScore(i);
			}
		}
		return totalScore;
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326458952&siteId=291194637
TDD