-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.hs
74 lines (63 loc) · 2.52 KB
/
game.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
66
67
68
69
70
71
72
73
74
import Board
import Data.Char
board = makeBoard
main = do
putStrLn "Bem vindo ao jogo de xadrez!\n"
putStrLn (printBoard 8 board)
game board
game :: Board -> IO ()
game b = if (isKingInCheckMate b True)
then blackWin
else if (isKingInCheckMate b False)
then whiteWin
else do
whiteBoard <- nextWhiteMove b
blackBoard <- nextBlackMove whiteBoard
game blackBoard
blackWin :: IO ()
blackWin = putStrLn "Xeque Mate! O jogador preto ganhou!"
whiteWin :: IO ()
whiteWin = putStrLn "Xeque Mate! O jogador branco ganhou!"
nextWhiteMove :: Board -> IO Board
nextWhiteMove b = do
putStrLn "Jogador branco, é sua vez"
newBoard <- getMove b True
return newBoard
nextBlackMove :: Board -> IO Board
nextBlackMove b = do
putStrLn "Jogador preto, é a sua vez"
newBoard <- getMove b False
return newBoard
getMove :: Board -> Bool -> IO Board
getMove b c = do
putStrLn "\tInsira sua jogada e tecle enter"
move <- getLine
newBoard <- makeMove b c (convertMove board move)
return newBoard
convertMove :: Board -> String -> ((Char, Int),(Char,Int))
convertMove board (a:b:c:d:e:f) = ((a,(digitToInt b)),(d,(digitToInt e)))
makeMove :: Board -> Bool -> ((Char, Int),(Char,Int)) -> IO Board
makeMove b c ((oc,ol),(dc,dl)) = do
let piece = (findPiece b (oc,ol))
if ((piece == (King c)) || (piece == (Queen c)) || (piece == (Bishop c)) || (piece == (Knight c)) || (piece == (Rook c)) || (piece == (Pawn c)))
&& (isValidMove b piece (translateLine(oc),ol) (translateLine(dc),dl))
&& ((findPiece b (dc,dl) /= (King c)) && (findPiece b (dc,dl) /= (Queen c)) && (findPiece b (dc,dl) /= (Bishop c)) && (findPiece b (dc,dl) /= (Knight c)) && (findPiece b (dc,dl) /= (Rook c)) && (findPiece b (dc,dl) /= (Pawn c)))
then do
let newBoard = (movePiece b ((oc,ol),(dc,dl)))
putStrLn (printBoard 8 newBoard)
if (isKingInCheck b c 1 1)
then do
putStrLn "Xeque!"
return newBoard
else do
putStrLn ""
return newBoard
else do
putStrLn "\nMovimento Inválido, tente novamente\n"
if c
then do
newBoard <- nextWhiteMove b
return newBoard
else do
newBoard <- nextBlackMove b
return newBoard