-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.py
137 lines (110 loc) · 4.43 KB
/
Board.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
from config import *
class Board:
def __init__(self):
self._board = [[' ' for _ in range(7)] for _ in range(6)]
@property
def board(self):
return self._board
@board.setter
def board(self, board):
self._board = board
# print the board
def printBoard(self):
for row in self._board:
print ('|' + '|'.join(row) + '|')
# print horizontal line
print('---------------')
# print column numbers
print(' 1 2 3 4 5 6 7')
def dropPiece(self, column, player):
for row in range(5, -1, -1):
if self._board[row][column] == EMPTY:
self._board[row][column] = player
break
def checkWin(self):
board = self._board
# check horizontal
for row in range(6):
for col in range(4):
if board[row][col] == board[row][col + 1] == board[row][col + 2] == board[row][col + 3] != EMPTY:
return board[row][col]
# check vertical
for row in range(3):
for col in range(7):
if board[row][col] == board[row + 1][col] == board[row + 2][col] == board[row + 3][col] != EMPTY:
return board[row][col]
# check diagonal
for row in range(3):
for col in range(4):
if board[row][col] == board[row + 1][col + 1] == board[row + 2][col + 2] == board[row + 3][col + 3] != EMPTY:
return board[row][col]
for row in range(3):
for col in range(3, 7):
if board[row][col] == board[row + 1][col - 1] == board[row + 2][col - 2] == board[row + 3][col - 3] != EMPTY:
return board[row][col]
return None
# a function to evaluate the window
# a window is a list of 4 cells in the board (row, column, diagonal)
@staticmethod
def evaluate_window(window, player):
score = 0
opponent = PLAYER1 if player == PLAYER2 else PLAYER2
if window.count(player) == 4:
score += 100
elif window.count(player) == 3 and window.count(EMPTY) == 1:
score += 5
elif window.count(player) == 2 and window.count(EMPTY) == 2:
score += 2
if window.count(opponent) == 3 and window.count(EMPTY) == 1:
score -= 4
return score
# a function to evaluate user score based on the board state
# it is a simple heuristic function to evaluate the score of the board
# used by the AI to make a move (minimax algorithm)
def evalScore(self, player):
score = 0
# the center column is the best column to play in because it gives the player more options
# so we give it a little higher score
center_column = []
for row in range(6):
center_column.append(self._board[row][3])
center_count = center_column.count(player)
score += center_count * 3
# horizontal
for row in range(6):
row_array = self._board[row]
for col in range(4):
window = row_array[col:col + 4]
score += self.evaluate_window(window, player)
# vertical
for col in range(7):
col_array = []
for row in range(6):
col_array.append(self._board[row][col])
for row in range(3):
window = col_array[row:row + 4]
score += self.evaluate_window(window, player)
# diagonal
for row in range(3):
for col in range(4):
window = [self._board[row + i][col + i] for i in range(4)]
score += self.evaluate_window(window, player)
for row in range(3):
for col in range(4):
window = [self._board[row + 3 - i][col + i] for i in range(4)]
score += self.evaluate_window(window, player)
return score
# a function to get all the possible moves
def getValidMoves(self):
valid_moves = []
for col in range(7):
if self._board[0][col] == EMPTY:
valid_moves.append(col)
return valid_moves
# a function to check if the board is full
def isBoardFull(self):
for row in range(6):
for col in range(7):
if self._board[row][col] == EMPTY:
return False
return True