Showing posts with label python logic games fun python projects for school. Show all posts
Showing posts with label python logic games fun python projects for school. Show all posts

Wednesday, June 18, 2025

🎮 Build Your Own Tic Tac Toe Game Using Python – For Beginers

 Do you love playing Tic Tac Toe? Today, we’ll show you how to build your very own Tic Tac Toe game in Python — no advanced skills needed!

By the end of this blog, you’ll:

  • Create a board using Python lists

  • Take input from two players

  • Check for a winner

  • Learn the basics of logic and conditionals

Let’s go! 🐍💻


🧠 What You’ll Learn

✅ Lists in Python
✅ Loops and conditionals (if, while)
✅ Functions and logic building
✅ Making a fun, text-based game


🏗️ Step-by-Step Code: Tic Tac Toe

✅ Step 1: Create the Game Board

board = [' ' for _ in range(9)] # 3x3 board
def print_board(): print() print(board[0] + ' | ' + board[1] + ' | ' + board[2]) print('--+---+--') print(board[3] + ' | ' + board[4] + ' | ' + board[5]) print('--+---+--') print(board[6] + ' | ' + board[7] + ' | ' + board[8]) print()

✅ Step 2: Check for Winner

def check_winner(player):
win_conditions = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], # rows [0, 3, 6], [1, 4, 7], [2, 5, 8], # columns [0, 4, 8], [2, 4, 6] # diagonals ] for condition in win_conditions: if board[condition[0]] == board[condition[1]] == board[condition[2]] == player: return True return False

✅ Step 3: Play the Game

def play_game():
print("Welcome to Tic Tac Toe!") print_board() current_player = 'X' for turn in range(9): move = int(input(f"Player {current_player}, choose a position (1-9): ")) - 1 if board[move] == ' ': board[move] = current_player print_board() if check_winner(current_player): print(f"Player {current_player} wins! 🎉") return current_player = 'O' if current_player == 'X' else 'X' else: print("That spot is taken. Try again.") continue print("It’s a tie!") play_game()

🎯 Game Output Example

| |
--+---+-- | | --+---+-- | | Player X, choose a position (1-9): 5 | | --+---+-- | X | --+---+-- | |

💡 Extra Challenges for You

Try adding:

  • A score counter 🏆

  • Single player mode vs computer (using random) 🤖

  • GUI version using Tkinter 🖱️


🧠 What You Learned

  • How to use lists to make a board

  • How to use functions to organize code

  • How to use if conditions and loops to control game logic

  • You just built a real game in Python!


📚 Final Words

Programming can be fun — especially when you build games! Tic Tac Toe is a great first project that uses logic, input, and Python’s power.

Want to try more games? Next up: Snake, Hangman, or Quiz App!


🚀 Exploring Gemini AI and Python: A Beginner’s Guide for School Students

 Have you ever wondered how your favorite apps recognize your face, suggest videos, or even talk back like a real human? Welcome to the worl...