forked from nwestbury/pyConnect4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
227 lines (174 loc) · 7.14 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/python3
import pygame
import time
from main import WINDOW_DIMENSIONS
from piece import Piece
def draw_header(players, player1_moves, player2_moves, surface):
"""
Draw the header of the game
- Title
- Player move count
"""
# erase the surface to be written to
pygame.draw.rect(surface, (100, 100, 100), (0, 0,
WINDOW_DIMENSIONS[0],
WINDOW_DIMENSIONS[1] // 7))
# Set default system font, size 72
title_font = pygame.font.Font(None, 72)
title = title_font.render("Connect 4", True, (255, 255, 255))
surface.blit(title,
(WINDOW_DIMENSIONS[0] // 16,
WINDOW_DIMENSIONS[1] // 18))
moves_font = pygame.font.Font(None, 24)
player1_text = str(players[0]) + " moves: "
player1_title = moves_font.render(player1_text, True, (255, 255, 255))
player1_move = moves_font.render(str(player2_moves), True, (255, 255, 255))
moves_x = WINDOW_DIMENSIONS[0] - WINDOW_DIMENSIONS[0] // 11
surface.blit(player1_move, (moves_x, WINDOW_DIMENSIONS[1] // 15.5))
surface.blit(player1_title, (moves_x - WINDOW_DIMENSIONS[0] // 5.25,
WINDOW_DIMENSIONS[1] // 15.5))
player2_text = str(players[1]) + " moves: "
player2_title = moves_font.render(player2_text, True, (255, 255, 255))
player2_move = moves_font.render(str(player1_moves), True, (255, 255, 255))
# 29 is added to y coordinate for \n since font size is 24
surface.blit(player2_move, (moves_x, WINDOW_DIMENSIONS[1] // 18 + 29))
surface.blit(player2_title, (moves_x - WINDOW_DIMENSIONS[0] // 5.23,
WINDOW_DIMENSIONS[1] // 18 + 29))
def draw_footer(turn, players, timer, surface):
"""
Draw the footer of the game
- Current player playing
- Running time of game
"""
# erase surface to be written to
pygame.draw.rect(surface, (100, 100, 100),
(0, WINDOW_DIMENSIONS[1] - (WINDOW_DIMENSIONS[1] // 12),
WINDOW_DIMENSIONS[0], WINDOW_DIMENSIONS[1] // 12))
footer_font = pygame.font.Font(None, 24)
time_title = footer_font.render("Time Elapsed:", True, (255, 255, 255))
# convert seconds to time
seconds = time.strftime('%H:%M:%S', time.gmtime(timer // 1000))
surface.blit(time_title,
(WINDOW_DIMENSIONS[0] // 16,
WINDOW_DIMENSIONS[1] - WINDOW_DIMENSIONS[1] // 18))
time_count = footer_font.render(seconds, True, (255, 255, 255))
surface.blit(time_count,
(WINDOW_DIMENSIONS[0] // 4.1,
WINDOW_DIMENSIONS[1] - WINDOW_DIMENSIONS[1] // 18))
turn_title = footer_font.render("Turn:", True, (255, 255, 255))
turn_x = WINDOW_DIMENSIONS[0] - (WINDOW_DIMENSIONS[0]) // 4.5
surface.blit(turn_title,
(turn_x,
WINDOW_DIMENSIONS[1] - WINDOW_DIMENSIONS[1] // 18))
# decide to display who's turn
if not turn:
turn_text = str(players[0])
else:
turn_text = str(players[1])
turn = footer_font.render(turn_text, True, (255, 255, 255))
surface.blit(turn,
(turn_x + WINDOW_DIMENSIONS[0] // 14,
WINDOW_DIMENSIONS[1] - WINDOW_DIMENSIONS[1] // 18))
class Board:
def __init__(self, p1, p2, tokensize=80):
width, height = 7, 6 # for now, the width and height will remain fixed
self.TOKENSIZE = tokensize
self.BOARDWIDTH = width*tokensize
self.BOARDHEIGHT = height*tokensize
self.WIDTH = width
self.HEIGHT = height
self.COLOR = (0, 0, 255)
self.XMARG = (WINDOW_DIMENSIONS[0] - self.BOARDWIDTH) // 2
self.YMARG = WINDOW_DIMENSIONS[1] // 6
self.RECT = pygame.Rect(self.XMARG, self.YMARG,
self.BOARDWIDTH, self.BOARDHEIGHT)
self.TURN = 0
self.COUNT1 = 0 # player 1 moves
self.COUNT2 = 0 # player 2 moves
self.PLAYERS = (p1, p2)
self.BITBOARDS = [0, 0]
self.PIECES = []
for piecex in range(width):
colTokens = []
for piecey in reversed(range(height)):
t = Piece(piecex, piecey, self)
colTokens.append(t)
self.PIECES.append(colTokens)
def draw(self, screen):
pygame.draw.rect(screen, self.COLOR, self.RECT)
for col in self.PIECES:
for piece in col:
piece.draw(screen)
def play(self, mouseloc=(0, 0)):
chosenCol = -1
player = self.PLAYERS[self.TURN]
if player.isAI:
chosenCol = player.play(self)
else:
chosenCol = player.play(self, mouseloc)
if chosenCol >= 0:
return self.placeToken(chosenCol)
return False
def placeToken(self, col):
"""
Argument:
col : the column number by default an int between [0,6] where a token
is requested to be placed.
Return: 1 if the placed token wins the game 2 if draw, 0 otherwise
"""
if col < 0: # invalid column
return False
pieceCol = self.PIECES[col]
y = 0
for piece in pieceCol:
if not piece.STATUS: # if the column has an empty space
piece.setColorToPlayer(self.TURN + 1)
self.PLAYERS[self.TURN].flipBit(self, self.TURN, col, y)
return self.endTurn()
y += 1
return False # if the column is full, return False
def hasWon(self, bitboard):
# taken from http://stackoverflow.com/q/7033165/1524592
y = bitboard & (bitboard >> 6)
if (y & (y >> 2 * 6)): # check \ diagonal
return True
y = bitboard & (bitboard >> 7)
if (y & (y >> 2 * 7)): # check horizontal
return True
y = bitboard & (bitboard >> 8)
if (y & (y >> 2 * 8)): # check / diagonal
return True
y = bitboard & (bitboard >> 1)
if (y & (y >> 2)): # check vertical
return True
return False
def hasDrawn(self, overall_bitboard):
"""
If the board has all of its valid slots filled, then it is a draw.
We mask the board to a bitboard with all positions filled
(0xFDFBF7EFDFBF) and if all the bits are active, it is a draw.
"""
return (overall_bitboard & 0xFDFBF7EFDFBF) == 0xFDFBF7EFDFBF
def endTurn(self):
"""
This function is called at the end of every turn. It returns 1 on win
2 on draw
False otherwise
"""
if self.hasWon(self.BITBOARDS[self.TURN]):
return 1
if self.hasDrawn(self.BITBOARDS[0] | self.BITBOARDS[1]):
return 2
self.TURN = not self.TURN
# add to player move count
if self.TURN:
self.COUNT2 += 1
else:
self.COUNT1 += 1
return False
def isAITurn(self):
"""
Return True if the AI's turn, this is needed to ignore user clicks made
during the AI "thinking" phase.
"""
return self.PLAYERS[self.TURN].isAI