Skip to content

Commit

Permalink
added comments in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed Nov 19, 2015
1 parent 4c38ff8 commit 3199d67
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 10 additions & 2 deletions TTTCLI.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import TicTacToe
import System.Exit

main = tictactoe $ Right initialBoard

Expand All @@ -7,22 +8,29 @@ tictactoe bmsg = case bmsg of
(Left msg) -> putStrLn msg
(Right b) -> nextRound b


nextRound :: Board -> IO ()
nextRound b = do

-- check if one of the players won the game
case checkWinnerPure b of
Nothing -> putStrLn $ "Player's turn: " ++ (show $ currentPlayer b)
Just p -> do
putStrLn $ "Player " ++ show p ++ " won the game"
tictactoe $ Right initialBoard
exitSuccess

-- determine current player and output info
let p = currentPlayer b
putStrLn $ show b

-- get choice from input
putStr "Column: "
col <- getLine
let colNum = read col
putStr "Row: "
row <- getLine
let rowNum = read row

-- make choice on board
case mkChoice (colNum,rowNum,p) b of
Left msg -> do
putStrLn msg
Expand Down
22 changes: 16 additions & 6 deletions TicTacToe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ mkChoice c@(col, row, m) b
| otherwise = Left $ "Choice " ++ show c ++ " is not allowed."
where validChoice = isEmpty (col, row) b && isValidIndex (col, row)
r = newRow (col, m) (rowAtIndex row b)




-- checks the range of the indices
Expand Down Expand Up @@ -99,10 +97,22 @@ newRow (col, m) (RowCons f0 f1 f2)
| col == 1 = RowCons f0 (FieldCons m) f2
| col == 2 = RowCons f0 f1 (FieldCons m)


checkWinner :: BoardOrMsg -> Maybe Marker
checkWinner (Right b) = checkWinnerPure b
checkWinner (Left msg) = Nothing
checkWinner :: Board -> BoardOrMsg
checkWinner b =
let w0 = checkRows b in
case w0 of
Just m -> Left $ "Player won: " ++ show m
Nothing -> let w1 = checkCols b in
case w1 of
Just m -> Left $ "Player won: " ++ show m
Nothing -> let w2 = checkDiagonals b in
case w2 of
Just m -> Left $ "Player won: " ++ show m
Nothing -> return b

checkWinner' :: BoardOrMsg -> Maybe Marker
checkWinner' (Right b) = checkWinnerPure b
checkWinner' (Left msg) = Nothing

checkWinnerPure :: Board -> Maybe Marker
checkWinnerPure b = let res0 = checkRows b in
Expand Down

0 comments on commit 3199d67

Please sign in to comment.