This commit is contained in:
Benjamyn Love 2024-02-26 15:08:34 +11:00
parent 0af1832ee5
commit 8331d16fe4
4 changed files with 120 additions and 12 deletions

11
Pipfile Normal file
View File

@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
[dev-packages]
[requires]
python_version = "3.10"

Binary file not shown.

65
main.py
View File

@ -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('-------------')

56
test.py Normal file
View File

@ -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)