My Tic-Tac-Toe - Advice/Tips?

Hi guys,

I'm just new to C++ for the last 4 days. Could you offer any tips and advice about things I've done in the below program?

I know the following:
vector (a little bit - more like a resizeable array)
array
standard input/output
if, while and for loops
how to sorta create a function (it seems to work at least)

so I thought I'd learn better from practise and people suggested to try tic-tac-toe with what I know.

Check it:

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <algorithm>
#include <cctype>

using namespace std;

//set up moves list
vector<string> moves;
vector<string>::iterator iter;
     
//set up board
const int ROWS = 3;
const int COLS = 3;
char board[ROWS][COLS] = {
{'-','-','-'},
{'-','-','-'},
{'-','-','-'}};

//test if board is full
bool full_board(char full)
{
     for (int i = 0; i < ROWS; ++i)
     {
         for (int j = 0; j < COLS; ++j)
             if (board[i][j] == full)
             return false;
     }
     return true;
}

//test if someone wins     
bool winner(char user)
{
 //EVERY POSSIBEL WINNING COMBINATION!
   while ((board[0][0] == user) &&
          (board[0][1] == user) && 
          (board[0][2] == user)){
          return true;};
   
   while ((board[1][0] == user) &&
          (board[1][1] == user) && 
          (board[1][2] == user)){
          return true;};
   
   while ((board[2][0] == user) &&
          (board[2][1] == user) && 
          (board[2][2] == user)){
          return true;};
   
   while ((board[0][0] == user) &&
          (board[1][0] == user) && 
          (board[2][0] == user)){
          return true;};
   
   while ((board[0][1] == user) &&
          (board[1][1] == user) && 
          (board[2][1] == user)){
          return true;};
   
   while ((board[0][2] == user) &&
          (board[1][2] == user) && 
          (board[2][2] == user)){
          return true;};
   
   while ((board[0][0] == user) &&
          (board[1][1] == user) && 
          (board[2][2] == user)){
          return true;};
   
   while ((board[2][0] == user) &&
          (board[1][1] == user) && 
          (board[0][2] == user)){
          return true;};
   return false;
}

//test if user has made an OK move
bool user_move_ok(string move)
{
     for (iter = moves.begin(); iter < moves.end(); ++iter)
     {
         if (*iter == move)
         {
            moves.erase(iter);
            return true;
         }
     }
     cout << "That's not a valid move.";
     return false;
}

//make the computers' move
string comp_move()
{
     srand(time(0));
     random_shuffle(moves.begin(), moves.end()); 
     string compMove = moves[0];
     moves.erase(moves.begin());
     return compMove;  
}

//play the game!
void play()
{
     //list available moves (initializes for new game)
     moves.push_back("1,1");
     moves.push_back("1,2");
     moves.push_back("1,3");
     moves.push_back("2,1");
     moves.push_back("2,2");
     moves.push_back("2,3");
     moves.push_back("3,1");
     moves.push_back("3,2");
     moves.push_back("3,3");
     //-----------------------------------------------------------     
     //GAME LOOP BEGINS
     do
     {
     //display board
     for (int i = 0; i < ROWS; ++i)
     {
         cout << "\t\t\t";
         for (int j = 0; j < COLS; ++j)
         cout << " " << board[i][j];
     cout << endl;
     }
     
     string userMove;
     do
     {
     //prompt for input
     cout << "\nYour move (ROW,COL): ";
     cin >> userMove;
     
     //ensure input is valid
     }
     while (!user_move_ok(userMove));
     
     //break down moves to array co-ordinates
     //users' move
     string userStrRow = userMove.substr(0,1);
     string userStrCol = userMove.substr(2,1);
     int userIntRow = atoi(userStrRow.c_str());
     int userIntCol = atoi(userStrCol.c_str());
     
     //put the moves in the array
     //users'
     board[userIntRow-1][userIntCol-1] = 'X';
     
     //check if user wins or board is full
     if (winner('X'))
        break;
     if (full_board('-'))
        break;
     //get computer move
     string computer = comp_move();
     
     //break down moves to array co-ordinates
     //computers' move
     string compStrRow = computer.substr(0,1);
     string compStrCol = computer.substr(2,1);
     int compIntRow = atoi(compStrRow.c_str());
     int compIntCol = atoi(compStrCol.c_str());
     
     //put the moves in the array
     //computers'
     board[compIntRow-1][compIntCol-1] = 'O';
     
     //check if computer wins or board is full
     if (winner('O'))
        break;
     if (full_board('-'))
        break;
     //GAME LOOP ENDS
     //----------------------------------------------------------- 
     } while ((!winner('X')) || (!winner('O')) || (!full_board('-')));
}

//check if user wants to play again
bool check_again(char yn)
{
     char checker = toupper(yn);
     if (checker == 'Y')
        return true;
     if (checker == 'N')
        return false;
}
int main()
{
    //welcome
    cout << "\t\tWELCOME TO TIC TAC TOE!\n\n";
    char again = 'Y';
    do
    {
    play();
    //display end board
    for (int i = 0; i < ROWS; ++i)
    {
         cout << "\t\t\t";
         for (int j = 0; j < COLS; ++j)
         cout << " " << board[i][j];
    cout << endl;
    }
    //announce winner or full board
    if (winner('X'))
    cout << "\nYOU'RE THE WINNER!\n\n";
    if (winner('O'))
    cout << "\nTHE COMPUTER WINS!\n\n";
    if (full_board('-'))
    cout << "\nNO ONE WINS!\n\n";
    
    //play again?
    cout << "Play again? (y/n): ";
    cin >> again;
    if ((check_again(again)) == false)
       {
       cout << "\nOK, goodbye\n\n";
       break;
       }
    if ((check_again(again)) == true)
    {
       moves.clear();
       for (int i = 0; i < ROWS; ++i)
       {
         for (int j = 0; j < COLS; ++j)
             board[i][j] = '-';
       }
    }
    } while (again == 'Y' || again == 'y');
    system("PAUSE");
    return 0;
}
Last edited on
Please recopy the code again from your text editor and insert into code tags (<> on the right).
It IS a pain to look at code without <code> tag, people !!!
Thanks, fixed it up :)
To be honest, it is just my own preferences that I use only 1 exit point for any value return function. multiple exit can be difficult to debug in more complex context.

and dont use
system("PAUSE");
I guess.
A lot of functions that return values require more than one exit point. Even in more complex functions, you might have more exit points than you have statements in your main.
Last edited on
of course, I just try to avoid using multiple exit point as much as possible.

for example, this one is simple and I will avoid using it that way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool full_board(char full)
{
     bool bResult = true;
     for (int i = 0; i < ROWS; ++i)
     {
         for (int j = 0; j < COLS; ++j)
         {
             if (board[i][j] == full)
             {
                   bResult = false;
             }
         }
     }
     return bResult;
}


you get the idea.

Last edited on
Why force the program to use more memory than it needs to when you could simply just return false or true and eliminate the variable?
Topic archived. No new replies allowed.