Jump to content

[C++ min-game] tic tac toe, how to improve it?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Jacki

Jacki

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Hi people, I was seeing my old c++ programms and I found a tic tac toe game, it work quite fine I think but I believe it can be better, specially the AI. Please, can you tell me how you improve it? Thanks...
#include <iostream>


using namespace std;


class TicTacToe

{

    char* table[9];

    public:

    TicTacToe() {

        table[0] = "1";

        table[1] = "2";

        table[2] = "3";

        table[3] = "4";

        table[4] = "5";

        table[5] = "6";

        table[6] = "7";

        table[7] = "8";

        table[8] = "9";

    }


    void player_choise(int pos) {

        if(table[pos-1] != "X" && table[pos-1] != "O")

            table[pos-1] = "X";

    }


    void comput_choise()

    {

        int r = rand() % 8 + 0;

        if(table[r] != "X" && table[r] != "O")

            table[r] = "O";

        else

            comput_choise();

    }


    void print_table()

    {

        cout << "++-----++-----++-----++" << endl;

        cout << "||  " << table[0] << "  ||  " << table[1] << "  ||  " << table[2] << "  ||" << endl;

        cout << "++-----++-----++-----++" << endl;

        cout << "||  " << table[3] << "  ||  " << table[4] << "  ||  " << table[5] << "  ||" << endl;

        cout << "++-----++-----++-----++" << endl;

        cout << "||  " << table[6] << "  ||  " << table[7] << "  ||  " << table[8] << "  ||" << endl;

        cout << "++-----++-----++-----++" << endl;

    }


    bool is_winner()

    {

        if( (table[0] == "X" && table[1] == "X" && table[2] == "X") ||

            (table[3] == "X" && table[4] == "X" && table[5] == "X") ||

            (table[6] == "X" && table[7] == "X" && table[8] == "X") ||

            (table[0] == "X" && table[3] == "X" && table[6] == "X") ||

            (table[1] == "X" && table[4] == "X" && table[7] == "X") ||

            (table[2] == "X" && table[5] == "X" && table[8] == "X") ||

            (table[0] == "X" && table[4] == "X" && table[8] == "X") ||

            (table[2] == "X" && table[4] == "X" && table[6] == "X")) { return true; } else { return false; }

    }


    bool is_looser()

    {

        if( (table[0] == "O" && table[1] == "O" && table[2] == "O") ||

            (table[3] == "O" && table[4] == "O" && table[5] == "O") ||

            (table[6] == "O" && table[7] == "O" && table[8] == "O") ||

            (table[0] == "O" && table[3] == "O" && table[6] == "O") ||

            (table[1] == "O" && table[4] == "O" && table[7] == "O") ||

            (table[2] == "O" && table[5] == "O" && table[8] == "O") ||

            (table[0] == "O" && table[4] == "O" && table[8] == "O") ||

            (table[2] == "O" && table[4] == "O" && table[6] == "O")) { return true; } else { return false; }

    }

};


int main()

{

    srand(static_cast<int>(time(NULL)));


    TicTacToe game;

    int pos;


    while(true)

    {

        game.print_table();


        if (game.is_winner())

        {

            cout << "You win!" << endl;

            break;

        }

        else if (game.is_looser())

        {

            cout << "You lose!" << endl;

            break;

        }

        else

        {

            cout << "Make your choise: ";

            cin >> pos;

            game.player_choise(pos);

            game.comput_choise();

            continue;

        }

    }


    return 0;

}


Posted Image

Posted Image

#2
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Since I use C and not C++, I am not sure what you can do in classes and stuff, but this could probably be done with a for loop:
    TicTacToe() {
        table[0] = "1";
        table[1] = "2";
        table[2] = "3";
        table[3] = "4";
        table[4] = "5";
        table[5] = "6";
        table[6] = "7";
        table[7] = "8";
        table[8] = "9";
    }

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
And also when you try if someone won or not you don't have to test victory first and the failure you can test them together and if the game is over you can just get which player's turn it is since that must have won when it did the last move. And the actual test could be done with for loops.