Answer to Question #258828 in Python for Raju

Question #258828

Tic-Tac-Toe game


Abhinav and Anjali are playing the Tic-Tac-Toe game.Tic-Tac-Toe is a game played on a grid that's three squares by three squares.Abhinav is O, and Anjali is X.Players take turns putting their marks in empty squares.The first player to get 3 of her marks in a diagonal or horizontal, or vertical row is the winner.When all nine squares are complete,the game is over.If no player has three marks in a row,the game ends in a tie.Write a program to decide the winner inthe Tic-Tac-Toe game.


Input

The input will be three lines contain O's and X's separated by space.


Sample Input 1

O X O

O X X

O O X


Sample Output 1

Abhinav Wins


Sample input 2

O O X

X X O

X O O


Sample output 2

Anjali Wins


1
Expert's answer
2021-10-30T00:38:53-0400
def check_win(board):
 """
 Check if somebody win.
 Return winning symbol or None in case of tie
 """
 for r in range(3):
  if board[r][0] == board[r][1] == board[r][2]:
    return board[r][0]
 
 for c in range(3):
  if board[0][c] == board[1][c] == board[2][c]:
    return board[0][c]


  if board[0][0] == board [1][1] == board [2][2]:
    return board[0][0]
 
  if board[0][2] == board [1][1] == board [2][0]:
    return board[0][2]


board = []
for r in range(3):
 line = input()
 board.append(line.split())


res = check_win(board)
if res is None:
 print(Tie)
elif res.upper() == 'X':
 print('Anjali Wins')
elif res.upper() == 'O':
 print('Abhinav Wins')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog