Skip to content

Instantly share code, notes, and snippets.

@addisaden
Created December 16, 2011 16:24
Show Gist options
  • Select an option

  • Save addisaden/1486705 to your computer and use it in GitHub Desktop.

Select an option

Save addisaden/1486705 to your computer and use it in GitHub Desktop.
My first haskell app - another TicTacToe ;)
import System.IO
main = tictactoe
tictactoe = do
hSetBuffering stdin NoBuffering
play " " "qweasdyxc" 0
drawTTT ttt = [
if mod (fst x) 3 == 2
then (if y then snd x else '\n')
else (if y then snd x else '|')
|
x <- (zip [0..] ttt),
y <- [True, False]
]
-- #### win algorithmus START ####
-- return 'X' oder 'O' wenn jemand gewonnen hat
-- return '-' Wenn unentschieden ist
-- return ' ' Wenn Spiel noch läuft
winpattern = [ "ZZZ ",
" ZZZ ",
" ZZZ",
"Z Z Z ",
" Z Z Z ",
" Z Z Z",
"Z Z Z",
" Z Z Z " ]
-- Kombiniert Spiel mit Winpattern
win' g = [
(
[
if (fst x) == 'Z'
then
if (snd x) == z then (snd x) else (fst x)
else
(snd x)
|
x <- (zip a g)
],
z) | a <- winpattern, z <- "XO"]
win'' g = [ x | x <- win' g, (fst x) == g ]
win g = do
if ( ((length . win'') g) > 0 )
then
(\ (x:_) -> snd x) (win'' g)
else
if (elem ' ' g) then ' ' else '-'
-- #### win algorithmus ENDE ####
-- Ändert das Spielfeld, wenn eingabe gültig
-- (g)ame (c)ontrol (e)ingabe (p)layer -> 'X' | 'O'
change g c e p = [
if ((fst y) == ' ' && (snd y) /= ' ') then p
else (fst y)
|
y <- zip g [ if x == e then x else ' ' | x <- c ]
]
-- (g)ame (c)ontrol (r)ound
play g c r = do
putStrLn ("\nAuswahl:\n" ++ drawTTT c ++ "\nSpielfeld:\n" ++ drawTTT g ++ "\n")
let p = if mod r 2 == 0 then 'X' else 'O'
if win g == ' '
then do
putStrLn ("Spieler " ++ [p] ++ " ist am Zug.\n")
eingabe <- getChar
-- Erzeigt 50 Zeilenumbrüche
putStrLn ( ( concat . take 50 . repeat ) ['\n'] )
let fIndex = change g c eingabe p
if fIndex == g then do
putStrLn "Eingabe nicht korrekt."
play g c r
else do
play fIndex c (r+1)
else if (elem (win g) "XO") then
putStrLn ("Spieler " ++ [win g] ++ " hat gewonnen")
else
putStrLn "Es ist unentschieden."
@addisaden

Copy link
Copy Markdown
Author

if you have an qwerty keybord just change

line 7 from
play " " "qweasdyxc" 0
to
play " " "qweasdzxc" 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment