Answer to Question #215864 in Python for mouni

Question #215864

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 in the Tic-Tac-Toe game.


1
Expert's answer
2021-07-11T14:39:14-0400
def tictac():
    board = []
    for r in range(3):
        line = input()
        board.append(line.split())
    for r in range(3):
        if board[r][0] == board[r][1] == board[r][2]:
            result = board[r][0]
            
    for c in range(3):
        if board[0][c] == board[1][c] == board[2][c]:
            result = board[0][c]
    if board[0][0] == board [1][1] == board [2][2]:
        result = board[0][0]
        
    if board[0][2] == board [1][1] == board [2][0]:
        result = board[0][2]
    else:
        result = "None"
    return result


while True:
    print("Its you chance to play.")
    result = tictac()
    if result.upper() == 'X':
        print('Anjali Wins')
    elif result.upper() == 'O':
        print('Abhinav Wins')


    else:
        print("No one wins. \n Good bye")
        exit()


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
APPROVED BY CLIENTS