Created
February 7, 2021 19:36
-
-
Save brakkum/8add9a5c4c7e0d7291fecd4547db9e7a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import re | |
def get_new_board(): | |
# board[row][column] | |
return [["" for _ in range(7)] for _ in range(6)] | |
t = { | |
"Red": "R", | |
"Yellow": "Y", | |
"": "-", | |
} | |
def print_board(board): | |
print("") | |
for row in board: | |
print(" ".join([t[v] for v in row])) | |
def print_board_with_checks_highlighted(board, check_locations): | |
print("") | |
for y, row in enumerate(board): | |
print(" ".join([f"\033[94m{t[v]}\033[0m" if (y, x) in check_locations else t[v] for x, v in enumerate(row)])) | |
def find_lowest_available_row_for_col(board, board_col): | |
for i, row in enumerate(board): | |
if row[board_col]: | |
return i - 1 | |
return len(board) - 1 | |
def four_values_are_equal(values): | |
return values[0] and all(v == values[0] for v in values) | |
def check_rows(board): | |
for row in board: | |
for x in range(0, len(row) - 3): | |
values = [row[y] for y in range(x, x + 4)] | |
if four_values_are_equal(values): | |
return values[0] | |
def check_cols(board): | |
rotated_board = list(zip(*board[::-1])) | |
return check_rows(rotated_board) | |
def check_diagonals(board): | |
# down to right | |
for y in range(0, len(board) - 3): | |
for x in range(0, len(board[y]) - 3): | |
# check_locations = [(y + z, x + z) for z in range(4)] | |
# print_board_with_checks_highlighted(board, check_locations) | |
values = [board[y + z][x + z] for z in range(4)] | |
if four_values_are_equal(values): | |
return values[0] | |
# down to left | |
for y in range(len(board) - 4, -1, -1): | |
for x in range(len(board[y]) - 1, len(board[y]) - 5, -1): | |
# check_locations = [(y + z, x - z) for z in range(4)] | |
# print_board_with_checks_highlighted(board, check_locations) | |
values = [board[y + z][x - z] for z in range(4)] | |
if four_values_are_equal(values): | |
return values[0] | |
def who_is_winner(pieces_position_list): | |
board = get_new_board() | |
checks = [check_rows, check_cols, check_diagonals] | |
for move_count, move in enumerate(pieces_position_list): | |
[(col_letter, player)] = re.findall("(.)_(.*)", move) | |
col = ord(col_letter.lower()) - 97 | |
row = find_lowest_available_row_for_col(board, col) | |
board[row][col] = player | |
if move_count < 7: | |
continue | |
for check in checks: | |
winner = check(board) | |
if winner: | |
return winner | |
return "Draw" | |
game_winner = who_is_winner([ | |
"C_Yellow", "E_Red", "G_Yellow", "B_Red", "D_Yellow", "B_Red", "B_Yellow", "G_Red", "C_Yellow", "C_Red", | |
"D_Yellow", "F_Red", "E_Yellow", "A_Red", "A_Yellow", "G_Red", "A_Yellow", "F_Red", "F_Yellow", "D_Red", | |
"B_Yellow", "E_Red", "D_Yellow", "A_Red", "G_Yellow", "D_Red", "D_Yellow", "C_Red" | |
]) | |
print(game_winner) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment