import random
class Game:
def __init__(S):
S.field = []
def create_field(S):
for i in range(3):
R = []
for j in range(3):
R.append('-')
S.field .append(R)
def get_first(S):
return random.randint(0, 1)
def fix_spot(S, R, C, player):
S.field [R][C] = player
def is_player_win(S, player):
win = None
n = len(S.field )
for i in range(n):
win = True
for j in range(n):
if S.field [i][j] != player:
win = False
break
if win:
return win
for i in range(n):
win = True
for j in range(n):
if S.field [j][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if S.field [i][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if S.field [i][n - 1 - i] != player:
win = False
break
if win:
return win
return False
for R in S.field:
for item in R:
if item == '-':
return False
return True
def is_field_filled(S):
for R in S.field:
for item in R:
if item == '-':
return False
return True
def swap_player(S, player):
return 'X' if player == 'O' else 'O'
def show_field(S):
for R in S.field:
for item in R:
print(item, end=" ")
print()
def start(S):
S.create_field()
player = 'X' if S.get_first() == 1 else 'O'
while True:
print(f"Player {player} turn")
S.show_field()
R, col = list(
map(int, input("Enter row and column: ").split()))
print()
S.fix_spot(R - 1, col - 1, player)
if S.is_player_win(player):
print(f"Player {player} wins the game!")
print(" Abhinav Wins")
break
if S.is_field_filled():
print("Match Draw!")
break
player = S.swap_player(player)
print()
S.show_field()
gam = Game()
gam.start()
Comments
Leave a comment