New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Tic-Tac-Toe Grid Pattern

The tic-tac-toe grid pattern prints an empty game board made of plus signs, dashes and vertical bars. It is the first step of almost every console tic-tac-toe program.

Quick answer: Print 2n + 1 lines. Even lines are borders: "+---" for each cell and a closing +. Odd lines are cell rows: "| " for each cell and a closing |. The line number’s remainder by 2 decides which kind to print.

This program is part of the Symbol Patterns collection.

Table of contents8 sections
  1. Output
  2. How the tic-tac-toe grid pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Tic-Tac-Toe Grid Pattern output

Output (3 × 3 cells):

+---+---+---+
|   |   |   |
+---+---+---+
|   |   |   |
+---+---+---+
|   |   |   |
+---+---+---+
Tic-Tac-Toe Grid Pattern layout diagram, row by row (3 × 3 cells)
How each row of the tic-tac-toe grid pattern is built (3 × 3 cells). Blue cells are printed characters; grey dots are spaces.

How the tic-tac-toe grid pattern works

A grid of n cells per side has n + 1 border lines and n cell lines, alternating. Each kind of line is one short unit repeated n times plus one closing character for the right edge. That is why the code only needs a remainder test and a repeat, no matter how big the board is.

Key takeaways

  • The board has 2n + 1 lines: borders and cell rows take turns.
  • Each line is one unit repeated n times plus a closing character.
  • Cells are three characters wide, enough to print an X or O in the middle.
  • Change n to 9 for a Sudoku-style grid; the code stays the same.

Tic-Tac-Toe Grid Pattern in C

#include <stdio.h>

int main(void) {
    int n = 3;                                  /* cells per side */

    for (int r = 0; r <= 2 * n; r++) {
        for (int c = 0; c < n; c++)
            printf(r % 2 == 0 ? "+---" : "|   ");
        printf(r % 2 == 0 ? "+\n" : "|\n");
    }
    return 0;
}

Tic-Tac-Toe Grid Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 3;                                  // cells per side

    for (int r = 0; r <= 2 * n; r++) {
        for (int c = 0; c < n; c++)
            cout << (r % 2 == 0 ? "+---" : "|   ");
        cout << (r % 2 == 0 ? "+" : "|") << endl;
    }
    return 0;
}

Tic-Tac-Toe Grid Pattern in Java

public class TicTacToeGrid {
    public static void main(String[] args) {
        int n = 3;                                  // cells per side

        for (int r = 0; r <= 2 * n; r++) {
            for (int c = 0; c < n; c++)
                System.out.print(r % 2 == 0 ? "+---" : "|   ");
            System.out.println(r % 2 == 0 ? "+" : "|");
        }
    }
}

Tic-Tac-Toe Grid Pattern in Python

n = 3                                       # cells per side

for r in range(2 * n + 1):
    if r % 2 == 0:
        print("+---" * n + "+")
    else:
        print("|   " * n + "|")

Time and space complexity

The board has 2n + 1 lines of about 4n characters, so printing it takes O(n²) time with O(1) extra memory.

Frequently asked questions

How do I show X and O in the cells?

Keep the board in a 3 × 3 array and, on cell rows, print "| " + cell + " " instead of "| ".

Why print the board line by line instead of cell by cell?

A terminal prints one line at a time, so each line has to contain the matching part of every cell in that row. Thinking in lines, border line then cell line, is simpler than drawing each cell separately.

Why are the cells three characters wide?

Three characters give one space on each side of a single symbol, which keeps the board easy to read in a terminal.

How do I change the size of the board?

Change n. A 4 × 4 board needs 9 lines, and every line grows by four characters per extra cell. Nothing else in the code changes, because each line is built from the same repeated unit.

Each cell is a tiny border box; neighbouring cells share their edges.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 11 of 17 in Symbol Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.