diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..c398b0d --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.10" diff --git a/__pycache__/main.cpython-310.pyc b/__pycache__/main.cpython-310.pyc new file mode 100644 index 0000000..ba8b19a Binary files /dev/null and b/__pycache__/main.cpython-310.pyc differ diff --git a/main.py b/main.py index fd020b1..5fc5689 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import time +import sys import os class Game: @@ -37,18 +38,54 @@ class Game: 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 check_victory(self): - # Write logic to determine winner - for x in range(len(self.board)): - if any(self.board[x]): - for y in range(len(self.board[x])): - if self.board[x][y] != 0: - current_value = self.board[x][y] - print(f"checking current value: {current_value}") - - pass - return False + 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__': @@ -57,7 +94,11 @@ if __name__ == '__main__': while playing is True: os.system('clear') - game.check_victory() + 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('-------------') diff --git a/test.py b/test.py new file mode 100644 index 0000000..e0655e7 --- /dev/null +++ b/test.py @@ -0,0 +1,56 @@ +# board = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 2, 0, 0, 0], [0, 2, 1, 2, 0, 0, 0]] +board = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 2], [1, 0, 0, 0, 1, 2, 1], [1, 2, 2, 1, 2, 2, 2]] + +import main + +g = main.Game() +g.board = board +g.draw_board() + + +def is_board_won(board): + 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(board) + ver = _verticle_win(board) + diag = _diagonal_win(board) + + if hor != 0: + return hor + if ver != 0: + return ver + if diag != 0: + return diag + +is_board_won(board)