[ create a new paste ] login | about

Link: http://codepad.org/X1G25Bu8    [ raw code | output | fork ]

mohit_at_codepad - Haskell, pasted on Jul 9:
-- Original problem statement @ http://nabetani.sakura.ne.jp/hena/1/
-- Input is game steps for tic tac toe
-- o takes the first move. Moves keep alternating between o and x then onwards
-- If o or x makes a line of 3 in any direction, it is declared as won
-- It o or x makes a move over already marked block, it is declared as foul
-- and opponent is declared as winner
-- Input is never broken. So you don't need to handle the exception case.
--  1 | 2 | 3
--  --+---+--
--  4 | 5 | 6    <---------     Board layout
--  --+---+--
--  7 | 8 | 9
import Char         -- I want to use ord

main = ticTacToe ["79538246", "35497162193", "61978543", "254961323121", "6134278187",
                  "4319581", "9625663381", "7975662", "2368799597", "18652368566",
                  "965715", "38745796", "371929", "758698769", "42683953",
                  "618843927", "36535224", "882973", "653675681", "9729934662",
                  "972651483927", "5439126787", "142583697", "42198637563", "657391482"] where
       ticTacToe = putStr . unlines . map findWinner

firstMover = 'o'

flipMover 'o' = 'x'
flipMover 'x' = 'o'

findWinner = playOnBoard firstMover emptyBoard 0 where
             emptyBoard = take 9 seaOfDots
             seaOfDots = repeat '.'

playOnBoard _ _ 9 _ = "Draw Game."
playOnBoard curMover curBoard nMoves [] = "Broken Input."
playOnBoard curMover curBoard nMoves (x:xs) = if curBoard !! nIndex == '.' then ifCase else elseCase where
                                                               ifCase = evalBoard curMover curBoard nMoves nIndex xs
                                                               elseCase = "Foul : " ++ ((flipMover curMover):[]) ++ " won."
                                                               nIndex = charToNumIndex x

evalBoard curMover curBoard nMoves idx = evalHelper (nMoves + 1) curMover $ move curMover curBoard idx
-- Eight winning positions for each
-- Output of tictactoe script starts
evalHelper _ _ ('o':_:_:'o':_:_:'o':_   ) _ = "o won."
evalHelper _ _ (_:'o':_:_:'o':_:_:'o':_ ) _ = "o won."
evalHelper _ _ (_:_:'o':_:_:'o':_:_:"o" ) _ = "o won."
evalHelper _ _ ('o':'o':'o':_           ) _ = "o won."
evalHelper _ _ (_:_:_:'o':'o':'o':_     ) _ = "o won."
evalHelper _ _ (_:_:_:_:_:_:"ooo"       ) _ = "o won."
evalHelper _ _ ('o':_:_:_:'o':_:_:_:"o" ) _ = "o won."
evalHelper _ _ (_:_:'o':_:'o':_:'o':_   ) _ = "o won."

evalHelper _ _ ('x':_:_:'x':_:_:'x':_   ) _ = "x won."
evalHelper _ _ (_:'x':_:_:'x':_:_:'x':_ ) _ = "x won."
evalHelper _ _ (_:_:'x':_:_:'x':_:_:"x" ) _ = "x won."
evalHelper _ _ ('x':'x':'x':_           ) _ = "x won."
evalHelper _ _ (_:_:_:'x':'x':'x':_     ) _ = "x won."
evalHelper _ _ (_:_:_:_:_:_:"xxx"       ) _ = "x won."
evalHelper _ _ ('x':_:_:_:'x':_:_:_:"x" ) _ = "x won."
evalHelper _ _ (_:_:'x':_:'x':_:'x':_   ) _ = "x won."
-- Output of tictactoe script ends

evalHelper nMoves curMover newBoard remainingMoves = playOnBoard (flipMover curMover) newBoard nMoves remainingMoves

move c (b:bs) 0 = c:bs
move c (b:bs) loc = b : move c bs (loc - 1)

-- '1' -> 0, '2' -> 1 ....
charToNumIndex x = ord x - ord '1'

-- EOF


Output:
x won.
x won.
x won.
x won.
x won.
Foul : x won.
Foul : x won.
Foul : x won.
Foul : x won.
Foul : x won.
o won.
o won.
o won.
o won.
o won.
Foul : o won.
Foul : o won.
Foul : o won.
Foul : o won.
Foul : o won.
Draw Game.
Draw Game.
Draw Game.
Draw Game.
Draw Game.


Create a new paste based on this one


Comments: