#!/usr/bin/python3 import time import sys import os class Game: def __init__(self): self.player = 1 self.board = [[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] self.labels = [1, 2, 3, 4, 5, 6, 7] def draw_board(self): for x in self.board: for y in range(len(x)): print(x[y], end=" ") print("") def draw_labels(self): for x in self.labels: print(x, end=" ") print("") def turn(self, slot): landed_piece = False if slot > 6: raise Exception("You fucked up") if self.board[0][slot] != 0: # The space is filled and no longer valid return for x in range(len(self.board)): if self.board[x][slot] != 0: self.board[x-1][slot] = self.player landed_piece = True break if landed_piece is False: self.board[len(self.board) - 1][slot] = self.player self.player = 2 if self.player == 1 else 1 def is_board_won(self): def _verticle_win(board): for x in range(7): instances = [0,0] for y in range(6): if board[y][x] == 1: instances[0] += 1 elif board[y][x] == 2: instances[1] += 1 if instances[0] == 4: return 1 elif instances[1] == 4: return 2 return 0 def _horizontal_win(board): for x in range(6): for y in range(7): f = board[x][y] if board[x][y] != 0: try: if board[x][y + 1] == board[x][y] and board[x][y + 2] == board[x][y] and board[x][y + 3] == board[x][y]: return board[x][y] except IndexError: # We hit the bounds of the board so we don't care pass return 0 def _diagonal_win(board): return 0 hor = _horizontal_win(self.board) ver = _verticle_win(self.board) diag = _diagonal_win(self.board) if hor != 0: return hor if ver != 0: return ver if diag != 0: return diag return 0 if __name__ == '__main__': playing = True game = Game() while playing is True: os.system('clear') win = game.is_board_won() if win != 0: print(f"Player {win} has won") game.draw_board() sys.exit() print(f"It is player {game.player}'s turn") game.draw_board() print('-------------') game.draw_labels() in_data = input("(q/quit) Where do you want to put your piece? ") if in_data == 'q' or in_data == "quit": playing = False break try: in_data = int(in_data) - 1 game.turn(in_data) except Exception as e: print(f"Got an error: {e}")