📘 Lesson · Lesson 95
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.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.