Last active
September 3, 2023 03:46
-
-
Save cryptomail/1db2cdc3a7c9ad7f1d8863eb7998404b to your computer and use it in GitHub Desktop.
play connect 4 with python (I think this works)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Connect4: | |
COLOR_BLUE = 1 | |
COLOR_RED = 2 | |
EMPTY = -1 | |
NORTH = 1 | |
SOUTH = 2 | |
EAST = 3 | |
WEST = 4 | |
NORTH_EAST = 5 | |
NORTH_WEST = 6 | |
SOUTH_EAST = 7 | |
SOUTH_WEST = 8 | |
directions = [NORTH, SOUTH, EAST, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST] | |
last_color = -1 | |
game_over = False | |
board = [ | |
[-1, -1, -1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1, -1, -1] | |
] | |
def addPiece(self, column, color): | |
if self.game_over: | |
return -1 | |
if column >= len(self.board[0]) or color not in [self.COLOR_RED, self.COLOR_BLUE]: | |
return -1 | |
if self.last_color != color: | |
self.last_color = color | |
else: | |
return -1 | |
x = -1 | |
while (x < len(self.board) - 1): | |
x = x + 1 | |
if self.board[x][column] != self.EMPTY: | |
x = x - 1 | |
break | |
if x >= len(self.board): | |
return -1 | |
if self.board[x][column] != self.EMPTY: | |
return -1 | |
self.board[x][column] = color | |
if self.winner(x, column, color) == color: | |
self.game_over = True | |
return color | |
return 0 | |
def cnt(self, direction, row, column, color): | |
if direction == self.NORTH: | |
x = 0 | |
y = -1 | |
elif direction == self.SOUTH: | |
x = 0 | |
y = 1 | |
elif direction == self.EAST: | |
x = 1 | |
y = 0 | |
elif direction == self.WEST: | |
x = -1 | |
y = 0 | |
elif direction == self.NORTH_WEST: | |
x = -1 | |
y = -1 | |
elif direction == self.SOUTH_WEST: | |
x = -1 | |
y = 1 | |
elif direction == self.NORTH_EAST: | |
x = 1 | |
y = -1 | |
elif direction == self.SOUTH_EAST: | |
x = 1 | |
y = 1 | |
else: | |
return 0 | |
next_col = column + x | |
next_row = row + y | |
cumulator = 0 | |
while len(self.board[0]) > next_col >= 0 and 0 <= next_row < len(self.board): | |
if self.board[next_row][next_col] == color: | |
cumulator = cumulator + 1 | |
next_col = next_col + x | |
next_row = next_row + y | |
return cumulator | |
def winner(self, row, column, color): | |
n_cnt = self.cnt(self.NORTH, row, column, color) | |
s_cnt = self.cnt(self.SOUTH, row, column, color) | |
if n_cnt + s_cnt + 1 >= 4: | |
return color | |
nw_cnt = self.cnt(self.NORTH_WEST, row, column, color) | |
se_cnt = self.cnt(self.SOUTH_EAST, row, column, color) | |
if nw_cnt + se_cnt + 1 >= 4: | |
return color | |
e_cnt = self.cnt(self.EAST, row, column, color) | |
w_cnt = self.cnt(self.WEST, row, column, color) | |
if e_cnt + w_cnt + 1 >= 4: | |
return color | |
sw_cnt = self.cnt(self.SOUTH_WEST, row, column, color) | |
ne_cnt = self.cnt(self.NORTH_EAST, row, column, color) | |
if sw_cnt + ne_cnt + 1 >= 4: | |
return color | |
return -1 | |
game = Connect4() | |
game.addPiece(0, game.COLOR_RED) | |
game.addPiece(1, game.COLOR_BLUE) | |
game.addPiece(0, game.COLOR_RED) | |
game.addPiece(1, game.COLOR_BLUE) | |
game.addPiece(0, game.COLOR_RED) | |
game.addPiece(5, game.COLOR_BLUE) | |
assert(not game.game_over) | |
assert (game.addPiece(0, game.COLOR_RED) == game.COLOR_RED) | |
assert(game.game_over) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment