Sync
This commit is contained in:
parent
0af1832ee5
commit
8331d16fe4
11
Pipfile
Normal file
11
Pipfile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[[source]]
|
||||||
|
url = "https://pypi.org/simple"
|
||||||
|
verify_ssl = true
|
||||||
|
name = "pypi"
|
||||||
|
|
||||||
|
[packages]
|
||||||
|
|
||||||
|
[dev-packages]
|
||||||
|
|
||||||
|
[requires]
|
||||||
|
python_version = "3.10"
|
||||||
BIN
__pycache__/main.cpython-310.pyc
Normal file
BIN
__pycache__/main.cpython-310.pyc
Normal file
Binary file not shown.
65
main.py
65
main.py
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
@ -37,18 +38,54 @@ class Game:
|
|||||||
self.board[len(self.board) - 1][slot] = self.player
|
self.board[len(self.board) - 1][slot] = self.player
|
||||||
|
|
||||||
self.player = 2 if self.player == 1 else 1
|
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):
|
def _horizontal_win(board):
|
||||||
# Write logic to determine winner
|
for x in range(6):
|
||||||
for x in range(len(self.board)):
|
for y in range(7):
|
||||||
if any(self.board[x]):
|
f = board[x][y]
|
||||||
for y in range(len(self.board[x])):
|
if board[x][y] != 0:
|
||||||
if self.board[x][y] != 0:
|
try:
|
||||||
current_value = self.board[x][y]
|
if board[x][y + 1] == board[x][y] and board[x][y + 2] == board[x][y] and board[x][y + 3] == board[x][y]:
|
||||||
print(f"checking current value: {current_value}")
|
return board[x][y]
|
||||||
|
except IndexError:
|
||||||
pass
|
# We hit the bounds of the board so we don't care
|
||||||
return False
|
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__':
|
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
56
test.py
Normal 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)
|
||||||
Loading…
x
Reference in New Issue
Block a user