Given the non OOP code of Tic Tac Toe below, make it a OOP approach. Use python as programming language. from tkinter import * root = Tk() root.title('Ysmael Trias - Tic Tac Toe') # To set the title of the application root.geometry('510x537') # To set the size of the window of the application player = 'X' stop_game = False def callback(r, c): global player if player == 'X' and states[r][c] == 0 and stop_game==False: b[r][c].configure(text='X', fg='yellow', bg='blue') states[r][c] = 'X' player = 'O' if player == 'O' and states[r][c] == 0 and stop_game==False: b[r][c].configure(text='O', fg='green', bg='red') states[r][c] = 'O' player = 'X' check_for_winner() def check_for_winner(): global stop_game for i in range(3): if states[i][0]==states[i][1]==states[i][2]!=0: b[i][0].configure(bg='black') b[i][1].configure(bg='black') b[i][2].configure(bg='black') stop_game = True for i in range(3): if states[0][i]==states[1][i]==states[2][i]!=0: b[0][0].configure(bg='black') b[1][i].configure(bg='black') b[2][i].configure(bg='black') stop_game = True if states[0][0]==states[1][1]==states[2][2]!=0: b[0][0].configure(bg='black') b[1][1].configure(bg='black') b[2][2].configure(bg='black') stop_game = True if states[2][0]==states[1][1]==states[0][2]!=0: b[2][0].configure(bg='black') b[1][1].configure(bg='black') b[0][2].configure(bg='black') stop_game = True b = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] states = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] def reset(): global stop_game for i in range(3): for j in range(3): b[i][j].configure(text='', fg='black', bg='powder blue') states[i][j] = 0 stop_game = False for i in range(3): for j in range(3): b[i][j] = Button(font=('Calibri', 58, 'bold'), width=4, bg='powder blue', command=lambda r=i, c=j: callback(r, c)) b[i][j].grid(row=i, column=j) reset_game = Button(text='Reset', font=('Calibri', 15, 'bold'), width=4, height=1, fg='black', bg='green', command=lambda :reset()) reset_game.grid(row=3, column=0, columnspan=2, sticky='nsew') exit_game = Button(text='Exit', font=('Calibri', 15, 'bold'), width=4, height=1, fg='black', bg='green', command=lambda :root.destroy()) exit_game.grid(row=3, column=2, columnspan=2, sticky='nsew') root.resizable(0, 0) mainloop()
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps