forked from ChieBKAI/minimax-chess-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChienKoNgu.py
156 lines (137 loc) · 5.22 KB
/
ChienKoNgu.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import random
import time
pieceScore = {"K": 0, "Q": 90, "R": 50, "B": 35, "N": 30, "p": 10}
knightScore = [[1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 2, 2, 1],
[1, 2, 3, 3, 3, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 3, 3, 3, 2, 1],
[1, 2, 2, 2, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1, 1]]
bishopScore = [[4, 3, 2, 1, 1, 2, 3, 4],
[3, 4, 3, 2, 2, 3, 4, 3],
[2, 3, 4, 3, 3, 4, 3, 2],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[2, 3, 4, 3, 3, 4, 3, 2],
[3, 4, 3, 2, 2, 3, 4, 3],
[4, 3, 2, 1, 1, 2, 3, 4]]
queenScore = [[1, 1, 1, 3, 1, 1, 1, 1],
[1, 2, 3, 3, 3, 1, 1, 1],
[1, 4, 3, 3, 3, 4, 2, 1],
[1, 2, 3, 3, 3, 2, 2, 1],
[1, 2, 3, 3, 3, 2, 2, 1],
[1, 4, 3, 3, 3, 4, 2, 1],
[1, 2, 3, 3, 3, 1, 1, 1],
[1, 1, 1, 3, 1, 1, 1, 1]]
rookScore = [[4, 3, 4, 4, 4, 4, 3, 4],
[4, 4, 4, 4, 4, 4, 4, 4],
[1, 1, 2, 3, 3, 2, 1, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 1, 2, 3, 3, 2, 1, 1],
[4, 4, 4, 4, 4, 4, 4, 4],
[4, 3, 4, 4, 4, 4, 3, 4]]
whitePawnScore = [[8, 8, 8, 8, 8, 8, 8, 8],
[8, 8, 8, 8, 8, 8, 8, 8],
[5, 6, 6, 7, 7, 6, 6, 5],
[2, 3, 3, 5, 5, 3, 3, 2],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 3, 3, 3, 2, 1],
[1, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0]]
blackPawnScore = [[0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1],
[1, 2, 3, 3, 3, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[2, 3, 3, 5, 5, 3, 3, 2],
[5, 6, 6, 7, 7, 6, 6, 5],
[8, 8, 8, 8, 8, 8, 8, 8],
[8, 8, 8, 8, 8, 8, 8, 8]]
piecePosScores = {'N': knightScore, 'B': bishopScore, 'Q': queenScore, 'R': rookScore, "wp": whitePawnScore, "bp": blackPawnScore}
CHECKMATE = 100000
STALEMATE = 0
MAX_DEPTH = 4
''' Find random move for AI '''
def findRandomMove(validMoves):
if len(validMoves) > 0:
return validMoves[random.randint(0, len(validMoves)-1)]
def findBestMoveMinimax(gs, validMoves):
global nextMove
global nodes
nextMove = None
alpha = -CHECKMATE
beta = CHECKMATE
nodes = 0
start_time = time.time()
findMoveMinimax(gs, validMoves, MAX_DEPTH, alpha, beta, gs.whiteToMove)
end_time = time.time()
elapsed_time = end_time - start_time
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
print(nodes)
return nextMove
def findMoveMinimax(gs, validMoves, depth, alpha, beta, whiteToMove):
global nextMove
global nodes
nodes += 1
if depth == 0 or gs.checkMate or gs.staleMate:
return scoreBoard(gs)
random.shuffle(validMoves)
if whiteToMove:
maxScore = -CHECKMATE
random.shuffle(validMoves)
for move in validMoves:
gs.makeMove(move)
nextMoves = gs.getValidMoves()
score = findMoveMinimax(gs, nextMoves, depth - 1, alpha, beta, False)
gs.undoMove()
if score > maxScore:
maxScore = score
if depth == MAX_DEPTH:
nextMove = move
alpha = max(alpha, score)
if beta <= alpha:
break
return maxScore
else:
minScore = CHECKMATE
random.shuffle(validMoves)
for move in validMoves:
gs.makeMove(move)
nextMoves = gs.getValidMoves()
score = findMoveMinimax(gs, nextMoves, depth - 1, alpha, beta, True)
gs.undoMove()
if score < minScore:
minScore = score
if depth == MAX_DEPTH:
nextMove = move
beta = min(beta, score)
if beta <= alpha:
break
return minScore
def scoreBoard(gs):
if gs.checkMate:
if gs.whiteToMove:
return -CHECKMATE #black win
else:
return CHECKMATE #white win
elif gs.staleMate:
return STALEMATE
# if not checkmate or stalemate:
score = 0
for row in range(8):
for col in range(8):
square = gs.board[row][col]
if square != "--":
piecePosScore = 0
if square[1] != "K":
if square[1] == "p":
piecePosScore = piecePosScores[square][row][col]
else:
piecePosScore = piecePosScores[square[1]][row][col]
if square[0] == 'w':
score += pieceScore[square[1]] + piecePosScore
elif square[0] == 'b':
score -= pieceScore[square[1]] + piecePosScore
return score