haskell game - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

haskell game

Hi,
here I'm writing a NIM game have and I need to change one small thing to make it work however not sure how...
I need to change the second player to act like a computer so it moves automatically...
can I have some help please?
[code]import System.IO

main = play [1,2,3,4,5] ["Player 1","Player 2"]

play :: [Int] -> [String] -> IO ()
play board players = do
(newBoard,winner) <- takeTurns board players
if null winner
then play newBoard players
else do putStrLn (winner ++ " won!")
return ()

takeTurns :: [Int] -> [String] -> IO ([Int],String)
takeTurns board [] = return (board,[])
takeTurns board (player:players) = do
newBoard <- takeTurn board player
if sum newBoard == 0
then return (newBoard,player)
else takeTurns newBoard players

takeTurn :: [Int] -> String -> IO [Int]
takeTurn board player = do
putStrLn ("
" ++ player ++ "'s Turn")
displayBoard board
row <- getRow board
count <- getInt "How many stars? " 1 (board!!row)
return $ take row board ++ [board!!row - count] ++ drop (row+1) board

displayBoard :: [Int] -> IO ()
displayBoard [] = return ()
displayBoard board = do
putStrLn $ show (length board) ++ " : " ++ replicate (last board) '*'
displayBoard (init board)

getRow :: [Int] -> IO Int
getRow board = do
row <- getInt "Which row? " 1 (length board)
if board!!(row-1) == 0
then do putStrLn "That row is empty!"
getRow board
else return (row-1)

getInt msg min max = do
putStr msg
input <- getLine
let parsed = reads input :: [(Int,String)]
if null parsed
then badNumber "That's not a number!"
else testNumber (fst (head parsed))
where
badNumber error = do putStrLn error
getInt msg min max
testNumber number
| number < min = badNumber "That number is too small."
| number > max = badNumber "That number is too big."
| otherwise = do return number
[/code]
Sign In or Register to comment.