Last active
January 6, 2020 10:41
-
-
Save akanehara/7547788 to your computer and use it in GitHub Desktop.
すごいHaskell読書会 in 大阪で紹介されたボウリングのスコア計算のお題(http://yashigani.hatenablog.com/entry/2013/10/23/150059)が面白そうなのでぼくもやってみました。
This file contains hidden or 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
{- | |
- 課題 | |
- ボウリングのスコアを計算するプログラムを作成する。 | |
- ストライクはX,スペアは/,ガーターは-と表現し、 | |
- ゲームの結果は以下のように入力される。 | |
- XXXXXXXXXXXX #パーフェクト | |
- ------------------/5 # 9フレームまでガーターで10フレームでスペア | |
-} | |
module Main (main) where | |
import System.Environment (getArgs) | |
import Data.Char (digitToInt) | |
pin :: Char -> Int | |
pin 'X' = 10 | |
pin '-' = 0 | |
pin n = digitToInt n | |
bowlingScore :: String -> Int | |
bowlingScore sheet = frame 1 0 sheet | |
where | |
frame 10 s ('X' : 'X' : 'X' : []) = s + 30 | |
frame 10 s ('X' : _ : '/' : []) = s + 20 | |
frame 10 s ('X' : x : y : []) = s + 10 + pin x + pin y | |
frame 10 s ( _ : '/' : x : []) = s + 10 + pin x | |
frame 10 s ( x : y : []) = s + pin x + pin y | |
frame n s ('X' : rest@(_ : '/' : _)) = frame (n + 1) (s + 10 + 10) rest | |
frame n s ('X' : rest@(x : y : _)) = frame (n + 1) (s + 10 + pin x + pin y) rest | |
frame n s ( _ : '/' : rest@(x : _)) = frame (n + 1) (s + 10 + pin x) rest | |
frame n s ( x : y : rest) = frame (n + 1) (s + pin x + pin y) rest | |
printTest (testCase, result) = do | |
putStrLn $ "test case: " ++ testCase | |
putStrLn $ if (bowlingScore testCase) == result then "CLEAR" else "FAIL" | |
testCases = [ | |
("9-9-9-9-9-9-9-9-9-9-", 90) | |
, ("X54----------------", 28) | |
, ("1/5-----------------", 20) | |
, ("1/5-2/-/8-----------", 56) | |
, ("------XX----------", 30) | |
, ("------XXX--------", 60) | |
, ("XXXXXXXXXXXX", 300) | |
, ("--------------------", 0) | |
, ("-------------------/5", 15) | |
, ("------------------X54", 19) | |
, ("5/5/5/5/5/5/5/5/5/5/5", 150) | |
, ("X1/----------------", 30) | |
, ("------------------X1/", 20) | |
, ("------------------XXX", 30) | |
] | |
test = mapM_ printTest testCases | |
main = test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
パターン漏れてたので直しました