-- -- TIKUMÄNG -- -- Kasutamine: interact tikud -- -- Käigud peavad olema kujul , -- kus on kas A, B või C. -- Näiteks: A3 eemaldab kuhjast A kolm tikku. -- module Tikud(tikud) where import Minimax import Interactive import Char(toUpper,isDigit,digitToInt) data Board = Board Player Int Int Int deriving Show initialBoard = Board player 3 5 7 movesP (Board pl x y z) = [Board pl' x' y z | x' <- [0..x-1]] ++ [Board pl' x y' z | y' <- [0..y-1]] ++ [Board pl' x y z' | z' <- [0..z-1]] where pl' = opponent pl winP pl' (Board pl 0 0 0) = pl' == pl winP pl' (Board pl x y z) = False staticP (Board pl x y z) = case ws of [] -> 1 [x] -> if x /= 1 then 1 else -1 [x,y] -> if min x y == 1 || x /= y then 1 else -1 _ -> 0 where ws = [i| i<-[x,y,z], i /= 0] dynamicP (Board pl x y z) = x > 0 && y > 0 && z > 0 instance Position Board where moves = movesP static = staticP dynamic = dynamicP win = winP showBoard :: Board -> Interact -> Interact showBoard (Board pl x y z) = writeStr (unlines ["A " ++ showKuhi x, "B " ++ showKuhi y, "C " ++ showKuhi z ++ "\n"]) where showKuhi x = replicate x 'I' computersMove brd | win machine brd = writeStr ("Minu võit!!!\n") \$ end | otherwise = writeStr ("\nMinu käik: " ++ k ++ show(n) ++ "\n") \$ showBoard brd' \$ rest where (k,n) = getMove brd brd' brd' = makeMove 0 brd getMove (Board _ x1 y1 z1) (Board _ x2 y2 z2) | x1 /= x2 = ("A", x1-x2) | y1 /= y2 = ("B", y1-y2) | otherwise = ("C", z1-z2) rest | win player brd' = writeStr ("Sinu võit!!!\n") \$ end | otherwise = playersMove brd' playersMove brd = writeStr "Sisestage käik: " \$ readChar \$ \k -> let kuhi = toUpper k in readChar \$ \n -> if not (checkMove kuhi n brd) then writeStr ("\nError: Vale käik\n") \$ playersMove brd else let brd' = doMove kuhi (digitToInt n) brd in writeStr ([kuhi, n] ++ "\n") \$ showBoard brd' \$ computersMove brd' checkMove k n (Board _ x y z) = isDigit n && n' > 0 && elem k "ABC" && not (checkRange k) where checkRange 'A' = n' > x checkRange 'B' = n' > y checkRange 'C' = n' > z n' = digitToInt(n) doMove 'A' n (Board pl x y z) = Board (opponent pl) (x-n) y z doMove 'B' n (Board pl x y z) = Board (opponent pl) x (y-n) z doMove 'C' n (Board pl x y z) = Board (opponent pl) x y (z-n) tikud = clearScreen \$ writeStr home \$ showBoard initialBoard \$ playersMove initialBoard main = interact tikud