📘 Lesson · Lesson 95
Tic-Tac-Toe Game
Tic-Tac-Toe Game
About this Project
💡 At a Glance
A simple two-player Tic-Tac-Toe on a 3x3 board, showing board printing and win-checking logic.
Core Logic
Python
board = [" "] * 9
def show():
for i in range(0, 9, 3):
print(board[i], "|", board[i+1], "|", board[i+2])
def check_win(p):
wins = [(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)]
return any(board[a]==board[b]==board[c]==p for a,b,c in wins)
board[0]=board[1]=board[2]="X"
show()
print("X wins:", check_win("X"))X | X | X
| |
| |
X wins: True
Summary
- The board is a list of 9 cells; show() prints it as a 3x3 grid.
- check_win() tests all 8 winning lines for a player.
इस Project के बारे में
💡 एक नज़र में
3x3 board पर simple दो-player Tic-Tac-Toe, board printing और win-checking logic दिखाता है।
Core Logic
Python
board = [" "] * 9
def show():
for i in range(0, 9, 3):
print(board[i], "|", board[i+1], "|", board[i+2])
def check_win(p):
wins = [(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)]
return any(board[a]==board[b]==board[c]==p for a,b,c in wins)
board[0]=board[1]=board[2]="X"
show()
print("X wins:", check_win("X"))X | X | X
| |
| |
X wins: True
सारांश
- Board 9 cells की list है; show() इसे 3x3 grid के रूप में print करता है।
- check_win() player के लिए सभी 8 winning lines जाँचता है।