我正在Haskell开发一个nim游戏,当我试图实现一种方法来选择人类玩家何时可以移动时,我遇到了一个问题。
代码的工作原理是应该的,但只适用于人类玩家。问题是,当计算机人工智能应该采取一个行动。因此,我必须创建一个if来检查谁在转弯。
我还没有实现ai,但是目标是让ai在player == 2时移动,或者通过为if player == 1 then使用一个or。
这是运行我游戏的代码:
-- To run the game
play :: Board -> Int -> IO ()
play board player =
do newline
putBoard board 1
if finished board then
do newline
putStr "Player "
putStr (show (next player))
putStrLn " wins!"
else
if player == 1 then
do newLine
putStr "Player "
putStrLn (show player)
row <- getDigit "Enter a row number: "
num <- getDigit "Stars to remove: "
if valid board row num then
play (move board row num) (next player)
else
do newline
putStrLn "That move is not valid, try again!"
play board player
nim :: Int -> IO ()
nim x = play (initial x) 1游戏开始时给玩家一个整数(player)。我想要实现的是,当这个变量等于1(它在1到2之间变化)时,这个函数给人类玩家移动的空间。这是我得到的错误代码:
parse error (possibly incorrect indentation or mismatched brackets)
|
124 | nim :: Int -> IO ()
| ^添加if player == 1 then行之前,没有弹出此错误。
任何帮助都是非常感谢的!
发布于 2019-10-31 13:53:46
每个if都需要else..。包括你的if player == 1。
话虽如此,我推荐两件事:
Int。case代替Int如下所示:
data Player = Human | AI
play :: Board -> Player -> IO ()
play board player = do
newline
putBoard board player
case (finished board, player) of
(True, _) -> do
newline
putStrLn $ "Player " ++ show (next player) ++ " wins!"
(_, Human) -> do
newline
row <- getDigit "Enter a row number: "
{- ... etc. -}
_ -> {- compute a move -}https://stackoverflow.com/questions/58644245
复制相似问题