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.

61
main.py
View File

@ -1,6 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import time import time
import sys
import os import os
class Game: class Game:
@ -38,17 +39,53 @@ class Game:
self.player = 2 if self.player == 1 else 1 self.player = 2 if self.player == 1 else 1
def check_victory(self): def is_board_won(self):
# Write logic to determine winner def _verticle_win(board):
for x in range(len(self.board)): for x in range(7):
if any(self.board[x]): instances = [0,0]
for y in range(len(self.board[x])): for y in range(6):
if self.board[x][y] != 0: if board[y][x] == 1:
current_value = self.board[x][y] instances[0] += 1
print(f"checking current value: {current_value}") 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 pass
return False 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__': if __name__ == '__main__':
@ -57,7 +94,11 @@ if __name__ == '__main__':
while playing is True: while playing is True:
os.system('clear') 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") print(f"It is player {game.player}'s turn")
game.draw_board() game.draw_board()
print('-------------') 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)