DEV Community

Cover image for Building an STL-Based Tic-Tac-Toe Game for CP Solvers in C++
Mohammed Thaha
Mohammed Thaha

Posted on

Building an STL-Based Tic-Tac-Toe Game for CP Solvers in C++

Competitive Programming (CP) is all about solving problems efficiently, and sometimes small projects like Tic-Tac-Toe can be a great way to sharpen your problem-solving mindset.
In this post, we’ll build a Tic-Tac-Toe game in C++ using STL (vector) and basic control flow — perfect for beginners in CP who want to brush up their coding fundamentals.

Why This is Useful for CP

  1. STL Practice: Using vector for dynamic data handling
  2. Logic Building: Win condition checks are similar to pattern-finding problems in CP
  3. Input Validation: Good practice for handling constraints and edge cases
  4. Fast Iteration: The game loop teaches efficient looping patterns

Step 1 — Representing the Board with STL

step1

  • board stores the game state
  • currentPlayer keeps track of whose turn it is
  • isTie checks if the match ends in a draw

Step 2 — Printing the Board

We’ll create a clean, grid-like display for our board.

step2

Step 3 — Player Moves with Validation

In CP, validating input is crucial. We’ll reject invalid moves and recursively retry.

step3

Step 4 — Win & Tie Check Logic

We’ll check rows, columns, and diagonals.
This pattern-checking logic is directly applicable to CP problems involving matrices.

step4

Step 5 — Main Function
We bring everything together in the game loop.

step5

Output

Welcome to Tic-Tac-Toe!

 1 | 2 | 3
---|---|---
 4 | 5 | 6
---|---|---
 7 | 8 | 9

Player 'X', enter your move (1-9): 1

 X | 2 | 3
---|---|---
 4 | 5 | 6
---|---|---
 7 | 8 | 9

Enter fullscreen mode Exit fullscreen mode

…and so on until the game ends.

🔗 Source Code on GitHub: View Here

Conclusion: This STL-powered Tic-Tac-Toe in C++ blends fun with fundamentals — vectors, loops, and logic checks — making it a quick win for CP practice. Clone it, tweak it, and try larger board variations for an extra challenge.

Top comments (0)