块缩进会导致错误吗?
Does block indentation cause errors?
我正在 Haskell 和 运行 开发一个 nim 游戏,当我试图实现一种选择人类玩家何时可以移动的方法时遇到了问题
该代码可以正常运行,但仅适用于人类玩家。问题是电脑ai应该什么时候走。因此,我必须创建一个 if 来检查轮到谁了。
我还没有实现 ai,但目标是让 ai 在玩家 == 2 时采取行动,或者为 if player == 1 then
使用 else
这是 运行 我的游戏的代码:
-- 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
行之前没有弹出这个错误。
非常感谢任何帮助!
每个 if
都需要一个 else
... 包括您的 if player == 1
。
也就是说,我推荐两件事:
- 使用具有正确值的自定义类型,而不是具有太多值的
Int
。
- 使用
case
代替 if
。
像这样:
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 -}
我正在 Haskell 和 运行 开发一个 nim 游戏,当我试图实现一种选择人类玩家何时可以移动的方法时遇到了问题
该代码可以正常运行,但仅适用于人类玩家。问题是电脑ai应该什么时候走。因此,我必须创建一个 if 来检查轮到谁了。
我还没有实现 ai,但目标是让 ai 在玩家 == 2 时采取行动,或者为 if player == 1 then
这是 运行 我的游戏的代码:
-- 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
行之前没有弹出这个错误。
非常感谢任何帮助!
每个 if
都需要一个 else
... 包括您的 if player == 1
。
也就是说,我推荐两件事:
- 使用具有正确值的自定义类型,而不是具有太多值的
Int
。 - 使用
case
代替if
。
像这样:
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 -}