Created
September 8, 2022 14:58
-
-
Save zmrdltl/70f3e8595ce26a22c1bea5a77af19dd5 to your computer and use it in GitHub Desktop.
tdd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//볼링 한 game은 10번 frame으로 구성 | |
//1 frame 당 2번 공 던지기 가능 | |
//2번 공 번져서 모두 쓰러뜨림 -> spare -> 다음 라운드의 점수를 2배로 | |
//1번에 -> strike -> 다음 + 다다음 라운드의 점수를 2배 | |
//마지막 frame -> 3번 던질 수 있음 단, 2번을 최소 스페어해야됨 | |
pub struct Game { | |
score: u32, | |
rolls: Vec<u32>, | |
} | |
impl Game { | |
pub fn new() -> Game { | |
Game { | |
score: 0, | |
rolls: vec![], | |
} | |
} | |
pub fn roll(&mut self, pin: u32) { | |
self.rolls.push(pin); | |
} | |
pub fn score(&mut self) -> u32 { | |
let mut round = 0; | |
(0..10).for_each(|_| { | |
if self.rolls[round] == 10 { | |
//strike | |
self.score += 10 + self.rolls[round + 1] + self.rolls[round + 2]; | |
round += 1; | |
} else if self.rolls[round] + self.rolls[round + 1] == 10 { | |
//spare | |
self.score += 10 + self.rolls[round + 2]; | |
round += 2; | |
} else { | |
self.score += self.rolls[round] + self.rolls[round + 1]; | |
round += 2; | |
} | |
}); | |
self.score | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn game_starts() { | |
//set up 혹은 step up test -> 기본 골조 test임 | |
let _ = Game::new(); | |
} | |
#[test] | |
fn gutter_game() { | |
let mut game = Game::new(); | |
(0..20).for_each(|_| game.roll(0)); | |
assert_eq!(game.score(), 0); | |
} | |
#[test] | |
fn all_ones() { | |
let mut game = Game::new(); | |
(0..20).for_each(|_| game.roll(1)); | |
assert_eq!(game.score(), 20); | |
} | |
#[test] | |
fn one_spare() { | |
let mut game = Game::new(); | |
game.roll(5); | |
game.roll(5); //spare 발생 | |
game.roll(4); //점수 2배이므로 4*2 = 8로 계산됨 | |
(0..17).for_each(|_| game.roll(0)); | |
assert_eq!(game.score(), 18); | |
} | |
#[test] | |
fn one_strike() { | |
let mut game = Game::new(); | |
game.roll(10); | |
game.roll(5); //5 * 2 = 10 | |
game.roll(5); //5 * 2 = 10 | |
(0..16).for_each(|_| game.roll(0)); | |
assert_eq!(game.score(), 30); | |
} | |
#[test] | |
fn two_strike() { | |
let mut game = Game::new(); | |
game.roll(10); | |
game.roll(5); //5 * 2 = 10 | |
game.roll(5); //5 * 2 = 10 | |
game.roll(10); //5 * 2 = 10 | |
game.roll(2); //5 * 2 = 10 | |
game.roll(3); //5 * 2 = 10 | |
(0..14).for_each(|_| game.roll(0)); | |
assert_eq!(game.score(), 60); | |
} | |
#[test] | |
fn perfect_game() { | |
let mut game = Game::new(); | |
(0..12).for_each(|_| game.roll(10)); | |
assert_eq!(game.score(), 300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment