Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from multiprocessing.resource_sharer import stop
import itertools

game = [
[0, 0, 0],
Expand All @@ -7,54 +7,78 @@
]


def game_board(player=0, row=0, column=0, just_display=False):
def game_board(game_map, player=0, row=0, column=0, just_display=False):
try:
if not just_display:
game[row][column] = player
else:
print(' a b c')
for count, row in enumerate(game):
print(count, row)
game_map[row][column] = player

print(' a b c')
for count, row in enumerate(game_map):
print(count, row)
return game_map
except IndexError as e:
print('some error occured:', e)
print('some error occurred:', e)
except Exception as e:
print('some other error occurred:', e)


def checkWinner(game):
def check_winner(game):
# check for winner horizontally
for row in game:
if row.count(row[0]) == len(row) and row[0] != 0:
print('Player 1 won horizontally! ')
return

return True

# Check for winner vertically
for col in range(len(game)):
check = [];
check = []
for row in game:
check.append(row[col])
if check.count(check[0]) == len(check) and check[0] != 0:
print(f'Player {check[0]} won vertically!')
return
return True

# Check for winner diagonally
diags = [];
diags = []
for index, col in enumerate(range(len(game))):
diags.append(game[index][col])

if diags.count(diags[0]) == len(diags) and diags[0] != 0:
print(f'Player {diags[0]} won diagonally! (\\)')
return
return True

diags = [];
diags = []
for index, col in enumerate(reversed(range(len(game)))):
diags.append(game[index][col])
if diags.count(diags[0]) == len(diags) and diags[0] != 0:
print(f'Player {diags[0]} won anti-diagonally! (/)')
return
return True


play = True
while play:
game = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
game_won = False

game = game_board(game, just_display=True)
player_choice = itertools.cycle([1, 2])
while not game_won:
current_player = next(player_choice)
print(f'Player {current_player} turn')
column_choice = int(input('Choose a column: in range (0, 1, 2) '))
row_choice = int(input('Choose a row: in range (0, 1, 2) '))
game = game_board(game, current_player, row_choice, column_choice)

game_board(player=2, row=0, column=0)
game_board(player=2, row=1, column=1)
game_board(player=2, row=2, column=2)
game_board(just_display=True)
checkWinner(game)
if check_winner(game):
game_won = True
again = input('The game is over, would you like to play again? (y/n) ')
if again.lower() == 'y':
print('restarting')
elif again.lower() == 'n':
print('bye')
play = False
else:
print('not a valid answer, bye')
play = False