simple tic tac toe game

Hi, recently i made a program during my spare time.It's tic tac toe. I was wondering if anyone could help me improve it in terms of coding, or organization, because I think I've over-complicated the codes. >.> I am in high school and started C++ 3 weeks ago, my knowledge isn't very deep, so please post codes that I can understand. >.<
What I've included in the programs are what I can comprehend so...=3 Any help would be appreciated. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//Tic Tac Toe Game
#include <iostream.h>
#define X 'X'
#define O 'O'
#define yes 1
#define no 0

int check (char[][3], int, int);
void counteratk (char x[][3], char turn);
void progress (char x[][3]);
void main()
{
        char x[3][3];
        int row,col,correct=yes,turn,game=yes,counter=0,r,c,d;
        for (row=0; row<3; row++)
        {
                for (col=0; col<3; col++)
                {
                        x[row][col]=' ';
                }
        }
        cout << "You, are about to witness the program of the century.\n\n" <<
                "COMMENCE TIC TAC TOE!!!!\n\n";
        cout << "Ladies first, please pick the row then the column.\n\n";
        progress(x);
        do
        {
                do
                {
                        turn=X;
                        cout << "Row:\n";
                        cin >> row;
                        cout << "Column:\n";
                        cin >> col;
                        row--;
                        col--;
                        x[row][col];
                        correct=check (x,row,col);
                        if (!correct)
                }while (!correct);
                x[row][col]=X;
                counter++;
                game=winlose(x, game);
                turn=O;
                if (!game)
                {
                        progress(x);
                        cout << "\nYou win!!!\n\n" << endl;
                        cin >> game;
                        game=go_on(game);
                        if (!game)
                                break;
                }
                if (game)
                {
                        counteratk (x, turn);
                        game=winlose(x,game);
                }
                if (game==2)
                {
                        cout << "\n You lost...how lame >.>\n\n" << endl;
                        cout << "Play again\n" << "1.yes\n" << "0.no\n" << endl;
                        cin >> game;
                        game=go_on(game);
                        if(!game)
                                break;
                }
                if (counter==5 && game)
                {
                        cout << "\nIt's a tie!!\n\n" << endl;
                        cin >> game;
                        game=go_on(game);
                        if(!game)
                                break;
                }
        }while (game);
}

int go_on (int game)
{
        if (game)
        {
                main();
        {
                return no;

int check (char x[][3], int row, int col)
                return no;
}

int winlose (char x[][3], int game)
{
        int row, col, r, c, d, ro, co, dO;
        for (row=0; row<3; row++)
        {
                r=0;
                c=0;
                d=0;
                ro=0;
                co=0;
                dO=0;
                for (col=0; col<3; col++)
                {
                        if(x[row][col]==X)
                                r++;
                        if(x[row][col]==O)
                                ro++;
                        if(x[col][row]==X)
                                c++;
                        if(x[col][row]==O)
                                co++;
                        if(x[col][col]==X)
                                d++;
                        if(x[col][col]==O)
                                dO++;
                        if (ro==3 || co==3 || dO==3)
                                game=2;

                }
        }
        if (x[0][2]==X && x[1][1]==X && x[2][0]==X)
                d=3;
        if (x[0][2]==O && x[1][1]==O && x[2][0]==O)
                game=2;
        if (r==3 || c==3 || d==3)
                game=no;
        return game;
}


void counteratk (char x[][3], char turn)
{
        int a,b,game;
        for (a=0; a<3; a++)
                for (b=0; b<3; b++)
                        if (x[a][b]==' ' && turn==O)
                        {
                                x[a][b]=O;
                                turn=X;
                        }
        progress(x);
}
void progress (char x[][3])
{
        cout << " _________________\n";
        cout << "|     |     |     | \n";
        cout << "|  " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<
"  |\n";
        cout << "|_____|_____|_____|\n";
        cout << "|     |     |     |\n";
        cout << "|  " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<
"  |\n";
        cout << "|_____|_____|_____|\n";
        cout << "|     |     |     |\n";
        cout << "|  " << x[2][0] << "  |  " << x[2][1] << "  |  " << x[2][2] <<
"  |\n";
        cout << "|_____|_____|_____|\n";

}

Yeah, you got lost. It is because you are trying to comprehend it all at once. No one can do that. You have to take it a piece at a time.

You started out quite nicely. You wrote functions to:
- display the current state of the game (progress)
- determine if someone has won the game (and, supposedly, determine if the game is over)
- check for a valid move
- determine whether or not to play the game again

However, you began mixing things up. For example, go_on() is supposed to tell you whether or not the user wants to play again. But the way it is currently written, it doesn't tell you anything you didn't already know. You could have just skipped the function and written:
1
2
cin >> game;
if (game) main();

(Don't do that with main(), BTW. It is illegal in C++ and very bad karma otherwise --like running around with a sharp knife in your hand. Sooner or later you or someone else will get cut.)

Another thing is that you are using int for boolean values, when C++ provides you with a bool type. Always use types to improve clarity.

So, with that preamble, I'll help you refine your design goals and you can start fixing things up. Let's start with the over-all structure.

main()
This is the "controller" of the program. It doesn't actually run the game --we'll palm that off to another function. What it does is greet the user, initialize things, call the function that runs the game, tell the user who won and who lost (and keep track of how many times each), and ask the user if he/she wants to play again.

We'll also have to define the way the gameboard is represented to the user. I'll say that we do it naturally for people, and number rows and columns 1..3. We'll just subtract one from whatever they give us to access the actual item.
We'll store the gameboard the same way you did (nice job, BTW), as a 3 x 3 array of char, with values ' ', 'X', or 'O'.

Let's write that out in a simple version of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

// This defines who we have playing:
enum players { draw, player1, player2 };

int main()
  {
  // This will only display when the program is first run
  cout << "You, are about to witness the program of the century.\n\n";

  // This tracks the number of times each player has won.
  int number_of_wins[ sizeof( players ) ] = {0};

  // And this tells us whether or not to play again.
  bool done = false;

  while (!done)
    {
    // This will display before each game
    cout << "COMMENCE TIC TAC TOE!!!!\n\n"
            "Ladies first, please pick the row then the column (1 through 3).\n\n";

    // Play a game and remember who won (if anyone)
    int winner = playgame();

    // Update it
    number_of_wins[ winner ]++;

    // Display it
    display_winner_stats( number_of_wins, winner );

    done = ask_to_play_again();
    }

  // Finish
  cout << "\n\nThank you for playing!\n";
  return 0;
  }


Well, I've given you that... Now that we've got top-level stuff at a good idea state, let's flesh-out some of those functions we made-up.

playgame()
This function actually plays a game. It runs until either someone wins or until there are no more moves (and a draw occurs).
It displays the current gameboard, asks each player for his move in turn (and keeps asking until he enters a valid one), updates the gameboard, looks for a winner, and if none, repeats (suggesting another loop). Before returning, it displays the gameboard one more time (so the players can see the winning move).

It returns who won as an integer value of draw, player1, or player2.

(Notice how more functions were suggested: your progress() and winlose() and check, etc. Don't worry about how they work inside, figure out what they should do for this function--what they should return and what arguments they need to do it.

display_winner_stats()
Does what it says. Congradulates the winner and also tells how many times each player has won and how many draws there were.

ask_to_play_again()
Does what it says. Asks the user if he/she wants to play again and returns a boolean value: true if yes and false if no.

Notice how we can keep things simple by considering it abstractly from the top. (This is called the "top-down" approach.)

Whew. I've got to get some sleep. I'll help more later.
lol thanks a lot. I'll try and fix it up. xD
That was a wonderful explanation !!, how to write a neat,readable and maintanable code
It's amazing that it compiled. It was a microsoft compiler, wasn't it? Not quite conform to the standard. From which I quote (sect. 3.6.1):
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int [...] The function main shall not be used (3.2) within a program.

That is, (a) your "main()" has no return type (not good, but likely to compile since MS uses it) and (b) you call your main() (it really amazes me that there exists an compiler which does not complain about that)
Lol Well.... I'm trying to fix it :D what compiler you use cause if i fix on wrong one it won't work for you lol!

I assume microsoft cause i got lots errors not microsoft but which one?
Topic archived. No new replies allowed.