我是 Haskell 的新手,我必须实现一个简单的井字游戏。它不需要人工智能。
我需要更新我的矩阵,但我不知道该怎么做。
这是我蹩脚的代码:
import Array
table :: Array (Int, Int) Char
table = array ((1,1),(3,3))
[
((1,1), '0'), ((1,2), '0'), ((1,3), '0'),
((2,1), '0'), ((2,2), '0'), ((2,3), '0'),
((3,1), '0'), ((3,2), '0'), ((3,3), '0')
]
getGrid :: IO()
getGrid = do
print (elems table)
putStr "Escolha o numero da casa em que deseja jogar: "
n <- getChar
if isValid n == (0, 0) then
getGrid
else if (table ! isValid n) == '0' then do
putStr "\nProssiga: "
getPlayer n
else do
putStr "\nCasa ocupada, tente novamente: "
getGrid
getPlayer :: Char -> IO()
getPlayer n = do
putStr "\nJogador X: "
j <- getChar
if (j /= 'b' && j /= 'B' && j /= 'p' && j /= 'P') then do
getPlayer n
else do
(table ! isValid n) = j
getGrid
isValid :: Char -> (Int,Int)
isValid n
| n == '0' = (1, 1)
| n == '1' = (1, 2)
| n == '2' = (1, 3)
| n == '3' = (2, 1)
| n == '4' = (2, 2)
| n == '5' = (2, 3)
| n == '6' = (3, 1)
| n == '7' = (3, 2)
| n == '8' = (3, 3)
| otherwise = (0, 0)
我不知道该怎么做才能用其他值替换 0...:/