Skip to content

Instantly share code, notes, and snippets.

@jacob-herr
Last active March 26, 2023 16:57
Show Gist options
  • Save jacob-herr/ee2253a71d29497a528a84c9b78c6bd9 to your computer and use it in GitHub Desktop.
Save jacob-herr/ee2253a71d29497a528a84c9b78c6bd9 to your computer and use it in GitHub Desktop.
from termcolor import colored, cprint
#this class will create a board array as well as draw input to the board
class Board:
#creates a 2d array relative to rows and columns
def __init__(self, rows, columns):
self.board = [[' ' for col in range(columns)] for row in range(rows)]
self.rows = rows
self.columns = columns
self.in_a_row = 0
#draws the board all fancy
#(looks like slop)
def draw_board(self):
print('\n' * 50)
print(' ', end = '')
for i in range(self.columns):
print(f' {i + 1} ',end = '')
print('\n')
for i in range(self.rows):
print(f'{i + 1}', end = '')
print(' | '+' | '.join(self.board[i])+ ' |',)
print(' +----+'+'----+' * (len(range(self.columns)) - 1),)
print('\n' * 10)
#updates board and returns the position of move
def draw_move(self, col, char):
row = range(self.rows)[-1]
for i in range(self.rows):
if self.board[i][col] != ' ':
row = i - 1
break
self.board[row][col] = char
self.draw_board()
self.row = row
self.col = col
#this class will have the lengthy logic used to check for wins
class Manager():
def __init__(self, board):
self.board = board
#this function is used to check for vertical and digaonal connections
def check_direction(self, y_dir, x_dir, char):
#input either 1, -1 or 0, will check in those directions for four in a row.
for i in range (1,4):
if self.board.board[self.board.row + (i * y_dir)][self.board.col + (i * x_dir)] == char:
self.board.in_a_row += 1
else:
return
if self.board.in_a_row >= 3:
return True
#largest function, checks all of the winstates.
def check_dubs(self, char, player):
win = False
tie = 0
self.board.in_a_row = 0
#turns row into a string, checks the string
for i in range(self.board.rows):
if char*4 in ''.join(self.board.board[i]):
win = True
#uses the check direction funciton downwards, uses try except to avoid errors when
#checking non existent indexes
try:
if self.check_direction(1, 0, char):
win = True
except:
pass
self.board.in_a_row = 0
#resets counter, checks the up-left/down-right diagonal
try:
if self.check_direction(1, 1, char):
win = True
except:
pass
try:
if self.check_direction(-1, -1, char):
win = True
except:
pass
self.board.in_a_row = 0
#resets counter, checks the up-left/down-right diagonal
try:
if self.check_direction(-1, 1, char):
win = True
except:
pass
try:
if self.check_direction(1, -1, char):
win = True
except:
pass
#check for tie just in case lol
for i in range(self.board.rows):
for j in range(self.board.columns):
if not ' ' == self.board.board[i][j]:
tie += 1
if tie == self.board.rows * self.board.columns:
print('its a tie!' + '\n' * 4)
exit()
if win:
winner(player, char)
# removes non winner characters when they win for fun.
def winner(player, char):
for i in range(game_board.rows):
for j in range(game_board.columns):
if ' ' not in game_board.board[i][j] and game_board.board[i][j] != char:
game_board.board[i][j] = ' '
game_board.draw_board()
cprint(f'congrats, {player} won!' + '\n' * 5, 'green')
exit()
# stupid proofs the game
def get_input(player, char):
while True:
column = input(f'{player} ({char}), where would you like to go?? :')
full = 0
if not column.isdigit():
game_board.draw_board()
print('please input a number')
continue
if not (0 < int(column) <= game_board.columns):
game_board.draw_board()
print('that column doesnt exist')
continue
for row in range(game_board.rows):
if game_board.board[row][int(column) - 1] != ' ':
full += 1
if full == game_board.rows:
game_board.draw_board()
print('that column is full!')
continue
return int(column) - 1
game_board = Board(6,12)
game_board.draw_board()
game_managewins = Manager(game_board)
while True:
game_board.draw_move(get_input('player 1', colored('0', 'blue')), colored('0', 'blue'))
game_managewins.check_dubs(colored('0', 'blue'), 'player 1')
game_board.draw_move(get_input('player 2' ,colored('x', 'red')), colored('x', 'red'))
game_managewins.check_dubs(colored('x', 'red'), 'player 2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment