Answer to Question #256743 in Python for Guru

Question #256743
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 in the Tic-Tac-Toe game. Input The input will be three lines contain O's and X's separated by space. Output The output should be a single line containing either "Abhinav Wins" or "Anjali Wins" or "Tie". Explanation For example, if the input is O X O O X X O O X as three of O's are in vertical row print "Abhinav Wins". 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 i want exact sample outputs sir
1
Expert's answer
2021-10-26T05:09:41-0400
theBoard = {'7': ' ' , '8': ' ' , '9': ' ' ,
	            '4': ' ' , '5': ' ' , '6': ' ' ,
	            '1': ' ' , '2': ' ' , '3': ' ' }
	

	board_keys = []
	

	for key in theBoard:
	    board_keys.append(key)
	

	def printBoard(board):
	    print(board['7'] + '|' + board['8'] + '|' + board['9'])
	    print('-+-+-')
	    print(board['4'] + '|' + board['5'] + '|' + board['6'])
	    print('-+-+-')
	    print(board['1'] + '|' + board['2'] + '|' + board['3'])
	

	def game()
	    turn = 'X'
	    count = 0
	
	    for i in range(10):
	        printBoard(theBoard)
	        print("Your turn," + turn)
	

	        move = input()        
	

	        if theBoard[move] == ' ':
	            theBoard[move] = turn
	            count += 1
	        else:
	            print("That place is not free")
	            continue
	

	        
	        if count >= 5:
	            if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ':
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")                
	                break
	            elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': 
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")
	                break
	            elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': 
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")
	                break
	            elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': 
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")
	                break
	            elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': 
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")
	                break
	            elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': 
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")
	                break 
	            elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': 
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")
	                break
	            elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': 
	                printBoard(theBoard)
	                print("\nGame Over.\n")                
	                print(" **** " +turn + " won. ****")
	                break 
	
.
	        if count == 9:
	            print("\nGame Over.\n")                
	            print("Tie!!")
	

	        
	        if turn =='X':
	            turn = 'O'
	        else:
	            turn = 'X'        
	    
	   
	    
	

	if __name__ == "__main__":
	    game()

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