Skip to content

LamesCode/Cpp-TicTacToe-Terminal-Game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

❌⭕ Tic Tac Toe — C++ Terminal Game

A terminal-based Tic Tac Toe game written in C++, played on a coordinate-based board (columns a/b/c, rows 1/2/3) against a bot. This was my second C++ project, where I tackled more complex game logic, worked extensively with arrays, and learned the hard way why planning before coding matters.


📸 Preview

=====================================
  _______ _        _______
 |__   __(_)      |__   __|
    | |   _  ___     | | __ _  ___
    | |  | |/ __|    | |/ _` |/ __|
    | |  | | (__     | | (_| | (__
    |_|  |_|\___|    |_|\__,_|\___|

        TIC  TAC  TOE
=====================================

Choose one of the options bellow to determine who is going first:

[1] Player
[2] Bot
[3] Random
Choice: 1

Current position:

     #   #
3    # O #
  ###########
     #   #
2    # X #
  ###########
     #   #
1  X # O #
   a   b   c

Select a column (a,b,c): b
Select a row (1,2,3): 3

⚙️ Features

  • Coordinate-based input — select moves using column (a/b/c) and row (1/2/3) notation
  • Who goes first? — choose Player, Bot, or let it be Random
  • Win detection — checks all rows, columns, and both diagonals after every move
  • Occupied cell validation — can't place on a cell that's already taken
  • Input validation — handles invalid or non-numeric input without crashing
  • Tie detection — correctly identifies a draw when all 9 cells are filled
  • ASCII art banner — clean title screen on launch

🧠 How the Board Works

The board is stored as three separate char arrays — topRow, middleRow, and bottomRow — each holding 3 characters (' ', 'X', or 'O'). Player input is mapped from letter/number coordinates into array indices:

Column:  a → index 0 | b → index 1 | c → index 2
Row:     1 → bottomRow | 2 → middleRow | 3 → topRow

Win detection checks all 8 possible winning combinations (3 rows, 3 columns, 2 diagonals) for both X and O after every move.


🛠️ Tech Stack

C++ GCC Terminal

  • Language: C++
  • Compiler: g++ / GCC
  • Libraries: <iostream>, <ctime>

🚀 How to Run

Requirements

  • g++ compiler installed (MinGW on Windows, or built-in on Linux/macOS)

Steps

# 1. Clone the repository
git clone https://github.com/YOUR_USERNAME/Cpp-TicTacToe-Terminal-Game.git

# 2. Navigate into the folder
cd Cpp-TicTacToe-Terminal-Game

# 3. Compile the source file
g++ main.cpp -o tictactoe

# 4. Run the game
./tictactoe       # Linux / macOS
tictactoe.exe     # Windows

🧠 What I Learned

This was my second C++ project and a big step up in complexity. Here's what I took away from it:

  • Working with arrays — managing the board state across three char arrays and correctly reading and writing to them was the core challenge; I got very comfortable with array indexing
  • Passing arrays to functions — learned how C++ handles arrays as pointers when passed to functions, and why changes inside functions affect the original data
  • Coordinate mapping — converting user-friendly input (a, b, c and 1, 2, 3) into array indices using ASCII values ((int)column - 96)
  • Boolean references — used bool &validMove as a reference parameter to return validity state from a function without a return value
  • Planning matters — I got lost mid-project because I started coding without a clear structure. By the end, I understood that thinking through the data structures and function responsibilities before writing a single line saves a lot of time and confusion

🧩 Challenges

Board state management was the toughest part. Unlike my previous project, here I had to track 9 cells across 3 arrays, pass them between multiple functions, and make sure every function was reading and writing the correct cell. Getting the coordinate-to-index mapping right (column letter → array index, row number → which array) took real thought.

Win detection also required careful logic — 8 combinations to check for both X and O, running after every single move to catch a win immediately.

The biggest lesson though was project planning. I started coding without a clear structure and ended up getting lost in the middle. Having to untangle the logic mid-project taught me that a few minutes of planning at the start is worth hours of debugging later.


🔮 Future Plans

  • Add a proper AI using the Minimax algorithm (unbeatable bot)
  • Add difficulty levels (random bot vs smart bot)
  • Rewrite using classes and OOP (v2)
  • Add a score tracker across multiple rounds

📄 License

This project is open source under the MIT License.


Second project. Harder logic, better habits. Still learning. 🚀

About

Simple TicTacToe terminal game made in C++

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages