Skip to content

Commit 03e94d9

Browse files
authored
Update readme.md
1 parent 38d3f42 commit 03e94d9

File tree

1 file changed

+53
-1
lines changed
  • Python Projects/Advanced/Tic Tac Toe

1 file changed

+53
-1
lines changed

Python Projects/Advanced/Tic Tac Toe/readme.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def empty_space():
124124
```
125125
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.
126126

127-
7. `players list` and `player variable`:
127+
7. **`players list` and `player variable`**:
128128

129129
```python
130130
players=["X", "O"]
@@ -133,3 +133,55 @@ def empty_space():
133133

134134
`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.
135135

136+
137+
8. **Base Code**:
138+
```python
139+
label = Label(screen, text=player + " Turns ", font=('consolas',40))
140+
label.pack()
141+
142+
resetButton = Button(screen, text="restart", font=('consolas', 20), command=newGame)
143+
resetButton.pack(side="top")
144+
145+
frame = Frame(screen)
146+
frame.pack()
147+
buttons=[
148+
[0,0,0],
149+
[0,0,0],
150+
[0,0,0]
151+
]
152+
153+
for row in range(3):
154+
for column in range(3):
155+
buttons[row][column] = Button(frame, text="", font=("consolas",40), width=4, height=1, command= lambda row=row, column=column: next_turn(row,column))
156+
buttons[row][column].grid(row=row,column=column)
157+
158+
159+
160+
#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

Comments
 (0)