-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
executable file
·65 lines (58 loc) · 2.09 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- Creates a very simple Reversi game where you can play against the AI.
-- Just for demonstration purposes. No input validation is implemented!
module Main where
import Reversi_AI as AI
import CustomTypes
import Data.Array
import Data.Maybe
import Data.List
main :: IO ()
main = do
putStr $ boardToAscii (AI.startState White)
gameLoop (AI.startState White)
where
gameLoop gameState = do
putStr "You are playing with (X). Enter coordinates:\n"
x <- getLine
y <- getLine
newGameState <- return $ snd $
if ((read(x)::Int) == 10 && (read(y)::Int) == 10) then
AI.nextState gameState Pass
else
AI.nextState gameState (Play (read(x)::Int,read(y)::Int))
putStr $ boardToAscii newGameState
putStr "Your score: "
putStr $ show $ countScore newGameState Opponent
putStr "\tScore AI: "
putStrLn $ show $ countScore newGameState Mine
gameLoop newGameState
countScore :: State -> Tile -> Int
countScore state player = length [(x,y) | x<- [0..7], y<-[0..7], (state ! (x,y))==player]
countAllChips :: State -> Int
countAllChips state =
foldl counter 0 [(x,y)| x <- [0..7], y <- [0..7]]
where
counter n coord
| state ! coord /= Neutral = n + 1
| otherwise = n
-- String representation of the game state
-- Source: https://github.com/geon/Othello-in-Haskell
boardToAscii :: State -> String
boardToAscii board =
"\n 0 1 2 3 4 5 6 7 \n +-+-+-+-+-+-+-+-+\n" ++
(intercalate
"\n +-+-+-+-+-+-+-+-+\n"
(map (boardToRow board) [0..7])
) ++
"\n +-+-+-+-+-+-+-+-+\n 0 1 2 3 4 5 6 7 \n"
boardToRow :: State -> Int -> String
boardToRow board row =
show row ++ "|" ++
(intercalate "|"
(map (\position -> colorToAscii (board ! position)) ([(x, row) | x <- [0..7]]))
) ++ "|" ++ show row
colorToAscii :: Tile -> String
colorToAscii color = case color of
Neutral -> " "
Mine -> "O"
Opponent -> "X"