从用户那里获取输入,直到井字游戏结束
Take input from user until tic-tac-toe game ends
所以我 CIS194 大约八周了,我正在 Haskell 制作一个超级简单的井字游戏。我已经掌握了大部分游戏逻辑,但我对用户输入部分感到困惑。
截至目前,为了检查所有内容,我有一个对用户和计算机的动作进行硬编码的糟糕系统。
putStrLn "Welcome to tic tac toe. Where do you want to move first?"
let board1 = emptyBoard
putStrLn (show board1)
loc1 <- getLine
let moveLoc1 = read loc1
let board2 = findAndReplace board1 (Left moveLoc1) (Right O)
...
let board7 = makeXMove board6
putStrLn (show board7)
loc4 <- getLine
let moveLoc4 = read loc4
let board8 = findAndReplace board7 (Left moveLoc4) (Right O)
putStrLn (show board8)
不过,我的目标是接受用户输入,检查游戏是否结束,让计算机移动,检查游戏是否结束,如果没有结束则重复。在命令式语言中,我会在 while 循环中包含类似这样的内容,但我不确定如何解决 Haskell 中的问题。
根据我在网上看到的内容,有很多东西听起来可能会有帮助,但我不知道从哪里开始。例如,我也在看 this example,我听说过很多关于 Monad
和 State
的内容,但是有没有任何介绍性的文献我可以看一下来总结我的绕过整个事情?
我目前所有的代码都可以找到on github。
基本上我的问题是如何在游戏结束前询问用户输入并在结束时停止询问他们?
您将棋盘传递到递归调用中
player :: Board -> IO ()
player board1 = do
putStrLn (show board1)
loc1 <- getLine
let moveLoc1 = read loc1
let board2 = findAndReplace board1 (Left moveLoc1) (Right O)
if over board2 -- check to see if the game is over and we should stop recursing
then putStrLn (outcome board2) -- what to do when the game is over
else computer board2 -- recurse for the other player if the game isn't over
-- | ^-- the state of the board being passed to the recursive call
-- ^----------- the recursive call to the other player
computer :: Board -> IO ()
computer board1 = do
let board2 = makeXMove board1
if over board2 -- check to see if the game is over and we should stop recursing
then putStrLn (outcome board2) -- what to do when the game is over
else player board2 -- recurse for the other player if the game isn't over
-- | ^-- the state of the board being passed to the recursive call
-- ^----------- the recursive call to the other player
您需要填写检测游戏何时结束的函数 over :: Board -> Bool
和描述游戏结果的 outcome :: Board -> String
。开始游戏时玩家先用
main = do
putStrLn "Welcome to tic tac toe. Where do you want to move first?"
player emptyBoard
player
和 computer
中重复了很多代码。对你自己来说,一个很好的挑战是弄清楚如何摆脱重复的代码。你能不能改变 player
让它不知道 computer
并且反之亦然,然后让两个 player
互相对抗(可能稍微修改一下以告诉哪个 X
和哪个 O
)?
所以我 CIS194 大约八周了,我正在 Haskell 制作一个超级简单的井字游戏。我已经掌握了大部分游戏逻辑,但我对用户输入部分感到困惑。
截至目前,为了检查所有内容,我有一个对用户和计算机的动作进行硬编码的糟糕系统。
putStrLn "Welcome to tic tac toe. Where do you want to move first?"
let board1 = emptyBoard
putStrLn (show board1)
loc1 <- getLine
let moveLoc1 = read loc1
let board2 = findAndReplace board1 (Left moveLoc1) (Right O)
...
let board7 = makeXMove board6
putStrLn (show board7)
loc4 <- getLine
let moveLoc4 = read loc4
let board8 = findAndReplace board7 (Left moveLoc4) (Right O)
putStrLn (show board8)
不过,我的目标是接受用户输入,检查游戏是否结束,让计算机移动,检查游戏是否结束,如果没有结束则重复。在命令式语言中,我会在 while 循环中包含类似这样的内容,但我不确定如何解决 Haskell 中的问题。
根据我在网上看到的内容,有很多东西听起来可能会有帮助,但我不知道从哪里开始。例如,我也在看 this example,我听说过很多关于 Monad
和 State
的内容,但是有没有任何介绍性的文献我可以看一下来总结我的绕过整个事情?
我目前所有的代码都可以找到on github。
基本上我的问题是如何在游戏结束前询问用户输入并在结束时停止询问他们?
您将棋盘传递到递归调用中
player :: Board -> IO ()
player board1 = do
putStrLn (show board1)
loc1 <- getLine
let moveLoc1 = read loc1
let board2 = findAndReplace board1 (Left moveLoc1) (Right O)
if over board2 -- check to see if the game is over and we should stop recursing
then putStrLn (outcome board2) -- what to do when the game is over
else computer board2 -- recurse for the other player if the game isn't over
-- | ^-- the state of the board being passed to the recursive call
-- ^----------- the recursive call to the other player
computer :: Board -> IO ()
computer board1 = do
let board2 = makeXMove board1
if over board2 -- check to see if the game is over and we should stop recursing
then putStrLn (outcome board2) -- what to do when the game is over
else player board2 -- recurse for the other player if the game isn't over
-- | ^-- the state of the board being passed to the recursive call
-- ^----------- the recursive call to the other player
您需要填写检测游戏何时结束的函数 over :: Board -> Bool
和描述游戏结果的 outcome :: Board -> String
。开始游戏时玩家先用
main = do
putStrLn "Welcome to tic tac toe. Where do you want to move first?"
player emptyBoard
player
和 computer
中重复了很多代码。对你自己来说,一个很好的挑战是弄清楚如何摆脱重复的代码。你能不能改变 player
让它不知道 computer
并且反之亦然,然后让两个 player
互相对抗(可能稍微修改一下以告诉哪个 X
和哪个 O
)?