You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Python Projects/Advanced/Tic Tac Toe/readme.md
+53-1Lines changed: 53 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ def empty_space():
124
124
```
125
125
This function checks if there are any empty spaces left on the board. If all the spaces are filled, the function returns False. Otherwise, it returns True.
126
126
127
-
7.`players list` and `player variable`:
127
+
7.**`players list` and `player variable`**:
128
128
129
129
```python
130
130
players=["X", "O"]
@@ -133,3 +133,55 @@ def empty_space():
133
133
134
134
`players` is a list containing the two possible values that can be assigned to the player variable: `"X"` and `"O"`. The player variable is initially assigned a random value from the players list.
#stopping the screen to wait until close button is clicked
161
+
screen.mainloop()
162
+
```
163
+
Explanation line by line:
164
+
165
+
- label = Label(screen, text=player + " Turns ", font=('consolas',40)): creates a label widget named label with the specified text and font to display the current player's turn.
166
+
167
+
- label.pack(): organizes the label widget on the screen using the pack geometry manager.
168
+
169
+
- resetButton = Button(screen, text="restart", font=('consolas', 20), command=newGame): creates a button widget named resetButton with the specified text, font, and command to execute when the button is clicked.
170
+
171
+
- resetButton.pack(side="top"): organizes the resetButton widget on the screen using the pack geometry manager and places it at the top.
172
+
173
+
- frame = Frame(screen): creates a frame widget named frame to hold the buttons.
174
+
175
+
- frame.pack(): organizes the frame widget on the screen using the pack geometry manager.
176
+
177
+
- buttons=[[0,0,0], [0,0,0], [0,0,0]]: creates a 2D list of zeros to represent the Tic Tac Toe board.
178
+
179
+
- for row in range(3):: loops through each row of the 2D list.
180
+
181
+
- for column in range(3):: loops through each column of the current row in the 2D list.
182
+
183
+
- buttons[row][column] = Button(frame, text="", font=("consolas",40), width=4, height=1, command= lambda row=row, column=column: next_turn(row,column)): creates a button widget at the current row and column with the specified text, font, size, and command to execute when the button is clicked. The lambda function is used to pass the current row and column as arguments to the next_turn() function.
184
+
185
+
- buttons[row][column].grid(row=row,column=column): organizes the current button widget on the screen using the grid geometry manager.
186
+
187
+
- screen.mainloop(): starts the main event loop to wait for user input until the close button is clicked, which ends the program.
0 commit comments