I'm a newbie in C++. How can I create a game of tic-tac-toe game using simple codes in C++? Any answers would be much appreciated.. Thanks!:)

Dani AI

Generated

Good pointers so far: is right that a grid works well, and is spot-on about learning by building. ’s prime-factor idea is clever for win detection, but if you are new to C++ a straightforward approach is easier to reason about and debug.

A simple plan:

  • Represent the board as a 1D array of 9 chars, initialized to spaces. Use inputs 1..9 to map to indices 0..8.
  • After each move: validate input (range + empty cell), place the mark, check for a win or draw, then swap players.
  • Keep win detection explicit by testing 8 fixed triplets of indices. This is easy to test and avoids hidden complexity.
  • Be defensive with input: clear std::cin on bad input and re-prompt; this saves lots of frustration.

Example helpers you can drop in:

// Return true if player p has a winning line on board b.
bool has_win(const char b[9], char p) {
    static const int W[8][3] = {
        {0,1,2},{3,4,5},{6,7,8}, // rows
        {0,3,6},{1,4,7},{2,5,8}, // cols
        {0,4,8},{2,4,6}          // diags
    };
    for (int i = 0; i < 8; ++i)
        if (b[W[i][0]] == p && b[W[i][1]] == p && b[W[i][2]] == p) return true;
    return false;
}
// Read a legal move; map 1..9 to 0..8; reject occupied cells.
int get_move(const char b[9]) {
    int pos;
    for (;;) {
        std::cout << "Enter position (1-9): ";
        if (!(std::cin >> pos)) { std::cin.clear(); std::cin.ignore(10000, '\n'); continue; }
        if (pos >= 1 && pos <= 9 && b[pos-1] == ' ') return pos - 1;
        std::cout << "Invalid or occupied. Try again.\n";
    }
}

Wire these into a simple loop that alternates players for at most 9 turns, calls get_move, updates b, prints the board, and checks has_win. Once that works, consider adding a computer opponent (start with random legal moves, then upgrade to minimax).

Recommended Answers

All 4 Replies

hello wintercold,
try using a two dimentional array.

char board[3][3];

then display the board first, then asked where to put the sign.

This is a really good tutorial

If this is for a school project, be sure to not just copy the source code and make a few changes - you'll learn nothing that way.

Goodluck

commented: great link man +1

This is a really good tutorial

If this is for a school project, be sure to not just copy the source code and make a few changes - you'll learn nothing that way.

Goodluck

Wow, thanks for that website, i have never seen it before!

I'm a newbie in C++. How can I create a game of tic-tac-toe game using simple codes in C++? Any answers would be much appreciated.. Thanks!:)

Use prime numbers instead to make your professor go WTF.

#include <iostream>
#include <string>
        
using namespace std;
                
int main(){
         
int primes[9] = {2,3,5,7,11,13,17,19,23};
        
int x_primes = 1;
int o_primes = 1;
             
string b[9] = {"|   |", "|   |", "|   |", "|   |", "|   |", "|   |", "|   |", "|   |", "|   |"};

char player = 'X';
            
int move;
                 
while (x_primes * o_primes < 223092870){
        
        cout << player << "'s turn.  (press 0-8 where 0 is top left and 8 is bottom right)" << endl;
                
        cin >> move;
                
        if (player == 'X'){
                x_primes *= primes[move];
                b[move] = "| X |";
                player = 'O';
        }

        else {
                o_primes *= primes[move];
                b[move] = "| O |";
                player = 'X';
        }

        cout << b[0] << b[1] << b[2] << endl
             << "---------------\n"
             << b[3] << b[4] << b[5] << endl
             << "---------------\n"
             << b[6] << b[7] << b[8] << endl;

        if (x_primes % 30 == 0 || x_primes % 1001 == 0 || x_primes % 7429 == 0 || x_primes % 238 == 0 || x_primes % 627 == 0 ||
            x_primes % 1495 == 0 || x_primes % 506 == 0 || x_primes % 935 == 0){
                cout << "X wins!" << endl; exit(0);
                }
                 
        if (o_primes % 30 == 0 || o_primes % 1001 == 0 || o_primes % 7429 == 0 || o_primes % 238 == 0 || o_primes % 627 == 0 ||
            o_primes % 1495 == 0 || o_primes % 506 == 0 || o_primes % 935 == 0){
                cout << "Y wins!" << endl; exit(0);
                }
}
cout << "Draw!" << endl;

return 0;
}

-Greywolf bored with SC2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.