Last active
March 12, 2023 01:54
-
-
Save jacob-herr/167531db0aa808b6a1a18b111634c71b to your computer and use it in GitHub Desktop.
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
import time | |
import random | |
board = [[' ', ' ', ' ', ' ', ' ', ' '], | |
[' ', ' ', ' ', ' ', ' ', ' '], | |
[' ', ' ', ' ', ' ', ' ', ' '], | |
[' ', ' ', ' ', ' ', ' ', ' '], | |
[' ', ' ', ' ', ' ', ' ', ' ']] | |
def print_board(): | |
print('\n\n\n\n 1', ' 2 ', ' 3', ' 4 ', ' 5', ' 6') | |
for i in range(5): | |
print(' | ' + ' | '.join(board[i]) + ' |') | |
print(' -------------------------') | |
def get_input(player): | |
while True: | |
column = input(player + 'where would you like to go?: ') | |
full = 0 | |
if not column.isdigit(): | |
print('\nplease input a number\n') | |
continue | |
if not (0 < int(column) < 7): | |
print('\n\nthat column doesnt exist :(\n\n') | |
continue | |
for i in range(5): | |
if board[i][int(column)-1] != ' ': | |
full += 1 | |
if full == 5: | |
print('\n\nyou cannot play a full column\n\n') | |
continue | |
return int(column) | |
#draws something at last available square in column | |
def draw_player(column, player): | |
empty = 0 | |
for i in range(5): | |
if board[i][column - 1] != ' ': | |
board[i - 1][column - 1] = player | |
print_board() | |
return | |
else: | |
empty += 1 | |
if empty == 5: | |
board[4][column - 1] = player | |
print_board() | |
#will be used to decide what functions alternate | |
def twoplayer(): | |
twoplayers = False | |
while True: | |
answer = input("\n\n\n\n\n\n\n\n\n\ntype 1 for two players, type 2 to play a computer: ") | |
if not answer.isdigit(): | |
print('please input a number') | |
continue | |
if not (0 < int(answer) < 3): | |
print('\ninvalid number') | |
continue | |
if answer == '1': | |
twoplayers = True | |
return twoplayers | |
# place a random piece | |
def draw_computer(char): | |
x = random.randint(1,6) | |
draw_player(x, char) | |
def win(player, player_char): | |
#joins all the rows into str checks if 4 in a row is in the str | |
for i in range(5): | |
rows = ''.join(board[i]) | |
if player_char+player_char+player_char+player_char in rows: | |
print('\n\n' + player + ' won!\n\n\n\n') | |
exit() | |
#joins all the columns into strings and checks for four in a row there as well | |
for i in range(6): | |
cols = ''.join(board[0][i] + board[1][i] + board[2][i] + board[3][i] + board[4][i]) | |
if player_char+player_char+player_char+player_char in cols: | |
print('\n\n' + player + ' won!\n\n\n') | |
exit() | |
#sort of scuffed solution to diagonal wins | |
for i in range(3): | |
if board[4][0+i] == player_char and board[3][1+i] == player_char and board[2][2+i] == player_char and board[1][3+i] == player_char: | |
print('\n\n' + player + ' won!\n\n\n') | |
exit() | |
elif board[4-1][0+i] == player_char and board[3-1][1+i] == player_char and board[2-1][2+i] == player_char and board[1-1][3+i] == player_char: | |
print('\n\n' + player + ' won!\n\n\n') | |
exit() | |
elif board[4][5-i] == player_char and board[3][4-i] == player_char and board[2][3-i] == player_char and board[1][2-i] == player_char: | |
print('\n\n' + player + ' won!\n\n\n') | |
exit() | |
elif board[4-1][5-i] == player_char and board[3-1][4-i] == player_char and board[2-1][3] == player_char and board[1-1][2-i] == player_char: | |
print('\n\n' + player + ' won!\n\n\n') | |
exit() | |
if twoplayer(): | |
while True: | |
print_board() | |
draw_player(get_input('player 1, '), '#') | |
win('player 1', '#') | |
draw_player(get_input('player 2, '), '0') | |
win('player 2', '0') | |
else: | |
print_board() | |
while True: | |
draw_player(get_input('\n'), '#') | |
win('you', '#') | |
time.sleep(0.5) | |
draw_computer('0') | |
win('computer', '0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment