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!:)

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

http://xoax.net/comp/cpp/console/Lesson9.php

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.