Last active
May 5, 2017 11:03
-
-
Save ictrobot/1e4e20efe996b734e79e1b814c72696c to your computer and use it in GitHub Desktop.
Four in a row
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
# The MIT License (MIT) | |
# | |
# Copyright (c) 2017 Ethan Jones | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# | |
# Author: Ethan Jones <[email protected]> | |
# Version: 1.0.1 | |
num_rows = 6 | |
num_columns = 7 | |
player1_color = 'red' | |
player2_color = 'green' | |
grid = [["" for _dummy1 in range(7)] for _dummy2 in range(6)] | |
winning_positions = [] | |
names = {} | |
colors = {} | |
move_number = 0 | |
def check_vertical_win(player_name): | |
winning_positions.clear() | |
for column in range(num_columns): | |
counter = 0 | |
for row in range(num_rows): | |
if grid[row][column] == player_name: | |
counter = counter + 1 | |
winning_positions.append((row, column)) | |
else: | |
counter = 0 | |
winning_positions.clear() | |
if counter == 4: | |
return True | |
def check_horizontal_win(player_name): | |
winning_positions.clear() | |
for row in range(num_rows): | |
counter = 0 | |
for column in range(num_columns): | |
if grid[row][column] == player_name: | |
counter += 1 | |
winning_positions.append((row, column)) | |
else: | |
counter = 0 | |
winning_positions.clear() | |
if counter == 4: | |
return True | |
def check_diagonal(player_name): | |
for row in range(num_rows): | |
for column in range(num_columns): | |
winning_positions.clear() | |
counter = 0 | |
r = row | |
c = column | |
while r < num_rows and c < num_columns and counter < 4: | |
if grid[r][c] == player_name: | |
counter += 1 | |
winning_positions.append((r, c)) | |
else: | |
break | |
r += 1 | |
c += 1 | |
if counter == 4: | |
return True | |
winning_positions.clear() | |
counter = 0 | |
r = row | |
c = column | |
while r >= 0 and c < num_columns and counter < 4: | |
if grid[r][c] == player_name: | |
counter += 1 | |
winning_positions.append((r, c)) | |
else: | |
break | |
r -= 1 | |
c += 1 | |
if counter == 4: | |
return True | |
winning_positions.clear() | |
def check_draw(): | |
global num_rows, num_columns, grid | |
for column in range(num_columns): | |
if not grid[0][column]: | |
return False | |
return True | |
def play_disc(player_name, selected_column): | |
global num_rows, num_columns, grid | |
if selected_column < 0 or selected_column >= num_columns: | |
output(player_name, colored("Invalid column", "yellow")) | |
return 999 | |
for row in range(num_rows - 1, -1, -1): | |
if not grid[row][selected_column]: | |
grid[row][selected_column] = player_name | |
return row | |
output(player_name, colored("Full column", "yellow")) | |
return 999 | |
def print_grid(): | |
global num_rows, num_columns, grid, colors | |
for row in range(num_rows): | |
for column in range(num_columns): | |
if not grid[row][column]: | |
print("-", end=" ") | |
else: | |
if (row, column) in winning_positions: | |
print(colored(grid[row][column], colors[grid[row][column]], attributes=["underline", "bold"]), end=" ") | |
else: | |
print(colored(grid[row][column], colors[grid[row][column]]), end=" ") | |
print("") | |
def output(p, *args): | |
print("") | |
print("=" * 30) | |
print("Move " + str(move_number) + " - " + colored(names[p], colors[p])) | |
print("") | |
print_grid() | |
print("") | |
for msg in args: | |
print(msg) | |
def input_column(player): | |
global names, colors | |
try: | |
return int(input(colored("Choose a column? ", colors[player]))) | |
except ValueError: | |
return -1 | |
def get_initials(): | |
while True: | |
player1_name, player2_name = input(colored("Player 1 name? ", player1_color)), input(colored("Player 2 name? ", player2_color)) | |
player1 = player1_name[:1].upper() | |
player2 = player2_name[:1].upper() | |
if player1 == player2: | |
print(colored("Both players cannot have the same initial\n", "yellow")) | |
continue | |
names[player1] = player1_name | |
names[player2] = player2_name | |
colors[player1] = player1_color | |
colors[player2] = player2_color | |
print("") | |
print(colored(player1 + " - " + player1_name, player1_color)) | |
print(colored(player2 + " - " + player2_name, player2_color)) | |
return player1, player2 | |
def play(): | |
global move_number | |
player1, player2 = get_initials() | |
while True: | |
for p in [player1, player2]: | |
move_number += 1 | |
output(p) | |
while play_disc(p, input_column(p) - 1) == 999: | |
pass | |
if check_horizontal_win(p) or check_vertical_win(p) or check_diagonal(p): | |
output(p, colored(names[p], colors[p], attributes=['bold', 'underline']) + " wins!") | |
return | |
if check_draw(): | |
output(p, colored("Draw!", "yellow", attributes=['bold', 'underline'])) | |
return | |
# Colored text | |
COLORED_TEXT_COLORS = {'grey': 30, 'red': 31, 'green': 32, 'yellow': 33, 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37} | |
COLORED_TEXT_ATTRIBUTES = {'bold': 1, 'underline': 4, 'reverse': 7} | |
COLORED_TEXT = True | |
def colored(text, color=None, attributes=None): | |
if COLORED_TEXT: | |
fmt_str = '\033[%dm%s' | |
if color is not None: | |
text = fmt_str % (COLORED_TEXT_COLORS[color], text) | |
if attributes is not None: | |
for attr in attributes: | |
text = fmt_str % (COLORED_TEXT_ATTRIBUTES[attr], text) | |
text += '\033[0m' | |
return text | |
if __name__ == '__main__': | |
print(colored("Four in a row\n", "blue", attributes=['bold', 'underline'])) | |
play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment