Skip to content

Jaykumar-Inc/tic-tac-toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic-Tac-Toe Game

A fully functional two-player Tic-Tac-Toe game built in C++ demonstrating the practical use of 2D arrays for game development.

🎮 Features

  • Two-Player Mode: Play against a friend locally
  • Interactive CLI: Clean, user-friendly console interface
  • 2D Array Board: Uses a 3×3 character array to represent game state
  • Input Validation: Prevents invalid moves and handles errors gracefully
  • Win Detection: Automatically checks rows, columns, and diagonals
  • Draw Detection: Identifies when the board is full with no winner
  • Game Statistics: Tracks wins and draws across multiple games
  • Multiple Rounds: Play as many games as you want

🚀 Quick Start

Prerequisites

  • C++ compiler (g++ or clang++) with C++17 support
  • Make (optional, for easy building)

Build and Run

Using Make:

make
./tictactoe

Or compile manually:

g++ -std=c++17 -I include src/*.cpp -o tictactoe
./tictactoe

📖 How to Play

Starting the Game

  1. Launch the game
  2. Enter names for Player 1 (X) and Player 2 (O)
  3. Optionally load sample data or start fresh

Making Moves

  • Enter row number (0-2) when prompted
  • Enter column number (0-2) when prompted
  • The board shows position indices on the top and left edges

Board Layout

     0   1   2
   ┌───┬───┬───┐
 0 │   │   │   │  ← Row 0
   ├───┼───┼───┤
 1 │   │   │   │  ← Row 1
   ├───┼───┼───┤
 2 │   │   │   │  ← Row 2
   └───┴───┴───┘
   ↑   ↑   ↑
 Col 0 1   2

Winning

Get three of your symbols (X or O) in a row:

  • Horizontal: Complete any row
  • Vertical: Complete any column
  • Diagonal: Top-left to bottom-right OR top-right to bottom-left

Example Game

     0   1   2
   ┌───┬───┬───┐
 0 │ X │ O │ X │
   ├───┼───┼───┤
 1 │ O │ X │   │
   ├───┼───┼───┤
 2 │   │   │   │
   └───┴───┴───┘

X wins! (Diagonal)

🏗️ Project Structure

.
├── README.md           # This file
├── LICENSE            # MIT License
├── Makefile           # Build configuration
├── .gitignore         # Git ignore rules
├── include/
│   └── TicTacToe.h    # Game class declaration
└── src/
    ├── TicTacToe.cpp  # Game logic implementation
    └── main.cpp       # Main game loop and UI

🎓 Technical Details

Data Structure

char board[3][3];  // 2D array representing the game board

Time Complexity

Operation Complexity Description
Place Move O(1) Direct array access
Check Win O(1) Fixed 8 checks (3 rows + 3 cols + 2 diagonals)
Check Draw O(1) Simple counter check
Display Board O(1) Fixed 9 cell traversal
Validate Move O(1) Bounds + empty cell check

Win Detection Algorithm

The game checks for winning patterns in constant time:

  1. Three rows (horizontal wins)
  2. Three columns (vertical wins)
  3. Two diagonals (diagonal wins)

Total: 8 checks regardless of board state.

🎯 Learning Objectives

This project demonstrates:

  1. 2D Arrays: Understanding multi-dimensional arrays and memory layout
  2. Game Logic: Implementing turn-based game mechanics
  3. Pattern Matching: Detecting winning combinations efficiently
  4. Input Validation: Handling user input safely
  5. State Management: Tracking game state using arrays
  6. Object-Oriented Design: Clean class structure and encapsulation

🔧 Customization Ideas

Easy Modifications

  • Change board size (4×4, 5×5)
  • Use different symbols instead of X and O
  • Add colors using ANSI escape codes
  • Modify win condition (require 4 in a row)

Advanced Extensions

  • Implement AI opponent (minimax algorithm)
  • Add undo/redo functionality
  • Create save/load game feature
  • Network multiplayer support
  • GUI version using SDL or SFML

🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Built as an educational project to demonstrate 2D arrays in C++
  • Inspired by the classic Tic-Tac-Toe game
  • Great for learning data structures and game development

📧 Contact

Questions or suggestions? Feel free to open an issue!


Enjoy the game! 🎮

make && ./tictactoe

About

Simple Tic-Tac-Toe game using an array to store the board, check moves, and detect wins.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors