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
Tic-Tac-Toe Grid Pattern output
Output (3 × 3 cells):
+---+---+---+ | | | | +---+---+---+ | | | | +---+---+---+ | | | | +---+---+---+

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 + 1lines: borders and cell rows take turns. - Each line is one unit repeated
ntimes plus a closing character. - Cells are three characters wide, enough to print an X or O in the middle.
- Change
nto 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.
How is it related to the border box?
Each cell is a tiny border box; neighbouring cells share their edges.






