Tic-tac-toes game; computer vs human

I am trying to make it so that if the human enters an number of a space that is already occupied, it will give an error message and not continue the program until a proper number is entered. currently, it gives an error message but the computer still takes its next turn. i think its giving an error message no matter what entered actually. This is the most pressing matter, but i am also trying to make the game be agressive against the human (take the middle spot if its not already take, if two spots are taken in a row/column/diagonally, fill the space so that it wins the game for the computer, or if thats not possible, block the human from getting a winning move). there is also a problem with the buffer that make there be an error message when starting the game over again.

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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sstream>
#include <fstream>
#include <cctype>
#include <iomanip>

using namespace std;

int Initialize(char[], const int);
int OuputGameBoard(char[], const int);
int InputMove(int, char[], int);
int validateMove(int, char[], int);
int InsertGamePiece(int, char[], int, const int);
int IsspaceOccupied(char[], int);
int winCondition(char[], char);
int drawCondition(char[], const int);
int playAgain(void);
int main(void)
{
    char GameBoard[] = {'0','0','0','0','0','0','0','0','0'};
    
    int    iIndex = 0;
    int    iSwitch = 0;
    int    iGameOver = 0;
    int i_IndexPosition=0;
    Initialize(GameBoard, 9);

    while(iGameOver==0)
    {
        
        iIndex = InputMove(iSwitch, GameBoard, i_IndexPosition);
        iGameOver = InsertGamePiece(iSwitch, GameBoard, iIndex, 9);
        OuputGameBoard(GameBoard, 9);
        iSwitch = iSwitch + 1;
        if (iSwitch==2)
            iSwitch = 0;
        
        if (iGameOver==1)
            iGameOver = playAgain();
        
        if (iGameOver==2)
        {
            iIndex = 0;
            iSwitch = 0;
            iGameOver=0;
            Initialize(GameBoard, 9);
        }   
    }
    return 0;
    
}

int Initialize(char cGameBoard[], const int iMaxNumberOfSpaces)
{
    int iReturnValue = 0;
    for (int iIndex=0;iIndex<iMaxNumberOfSpaces;iIndex++)
    {
        cGameBoard[iIndex] = char(49+iIndex);
    }
    return iReturnValue;
    
}

int OuputGameBoard(char cGameBoard[], const int iMaxNumberOfSpaces)
{
    int iReturnValue = 0;
    cout << "     |     |     " << endl;
    cout << "  " << cGameBoard[0] << "  |  " << cGameBoard[1] << "  |  " << cGameBoard[2] << endl;
    
    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;
    
    cout << "  " << cGameBoard[3] << "  |  " << cGameBoard[4] << "  |  " << cGameBoard[5] << endl;
    
    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;
    
    cout << "  " << cGameBoard[6] << "  |  " << cGameBoard[7] << "  |  " << cGameBoard[8] << endl;
    
    cout << "     |     |     " << endl << endl;
    
    return iReturnValue;
    
}
int InputMove(int iSwitch, char c_GameBoard[], int i_IndexPosition)
{
    string sBuffer; 
    int iReturnValue = 0;
    
    while (iReturnValue==0)
    {
        if (iSwitch==0)
        {
            cout << "Please Enter your move" << endl;
            getline(cin, sBuffer);
            istringstream buffer(sBuffer);
            buffer >> iReturnValue;
        }
        else
        {
            iReturnValue = 0;
        }
        
        if (iSwitch==1)
        {
            iReturnValue = rand() % 9;
        }
        iReturnValue = validateMove(iReturnValue, c_GameBoard, i_IndexPosition);
        
    }
    
    iReturnValue = iReturnValue - 1;
    
    return iReturnValue;
    
}
int validateMove(int entryNumber, char c_GameBoard[], int i_IndexPosition)
{
    
    int iReturnVaue = 0;
    
    if ((entryNumber>=1) && (entryNumber<=9))
    {
        iReturnVaue = entryNumber;
    }
    if(IsspaceOccupied(c_GameBoard, i_IndexPosition)==0)
    {
        cout << "Invalid entry." << endl;
        //iReturnVaue=0;
    }
    else
    {
        cout << "Invalid entry." << endl;
    }
    
    
    return iReturnVaue;
    
}

int InsertGamePiece(int iPlayerNumber, char c_GameBoard[], int i_IndexPosition, const int i_MaxNumberOfSpaces)
{    
    int iReturnValue = 0;
    int iWinCondtionTest = 0;
    int idrawConditionTest = 0;
    char cGamePiece = NULL;
    string sPlayerType = "NOTHING";
    
    if(iPlayerNumber==0)
        cGamePiece ='X';
    
    if(iPlayerNumber==1)
        cGamePiece ='O';
    
    if (IsspaceOccupied(c_GameBoard, i_IndexPosition)==0)
    {
        if (cGamePiece!=NULL)
        {
            c_GameBoard[i_IndexPosition] = cGamePiece;
        }
        
    }
    else
    {
        if (iPlayerNumber==1)
        { 
            for (int iIndex=0;iIndex<i_MaxNumberOfSpaces;iIndex++)
            {
                if(IsspaceOccupied(c_GameBoard, iIndex)==0)
                {
                    c_GameBoard[iIndex]=cGamePiece;
                    iIndex=i_MaxNumberOfSpaces;
                }   
            }    
        }    
    }
    
    iWinCondtionTest = winCondition(c_GameBoard, cGamePiece);
    idrawConditionTest = drawCondition(c_GameBoard, i_MaxNumberOfSpaces);
    
    if (iWinCondtionTest==1)
    {
        
        if (iPlayerNumber==0)
            sPlayerType = "Human";
        
        if (iPlayerNumber==1)
            sPlayerType = "Computer";
        
        cout <<  sPlayerType << " wins!!!" << endl;
        
    }
    
    if (idrawConditionTest==1)
    {
        cout << "Tie" << endl;    
    }
    
    if ((iWinCondtionTest==1) || (idrawConditionTest==1))
    {
        cout << "Game Over" << endl;
        iReturnValue = 1;
    }
    
    return iReturnValue;    
}

int IsspaceOccupied(char c_GameBoard[], int i_IndexPosition)
{    
    int iReturnValue = 0;
    int iAsciiValue  = 0;
    iAsciiValue = (int) c_GameBoard[i_IndexPosition];
    
    if ((iAsciiValue>=49) && (iAsciiValue<=57))
    {
        iReturnValue = 0;
    }
    else
    {
        iReturnValue = 1;
    }
    
    return iReturnValue;    
}

int winCondition(char c_GameBoard[], char c_GamePiece)
{
    
    int iReturnValue = 0;
    
    /*Across*/
    if ((c_GameBoard[0]==c_GamePiece) && (c_GameBoard[1]==c_GamePiece) && (c_GameBoard[2]==c_GamePiece))
        iReturnValue=1;
    
    if ((c_GameBoard[3]==c_GamePiece) && (c_GameBoard[4]==c_GamePiece) && (c_GameBoard[5]==c_GamePiece))
        iReturnValue=1;
    
    if ((c_GameBoard[6]==c_GamePiece) && (c_GameBoard[7]==c_GamePiece) && (c_GameBoard[8]==c_GamePiece))
        iReturnValue=1;
    
    /*Down*/
    if ((c_GameBoard[0]==c_GamePiece) && (c_GameBoard[3]==c_GamePiece) && (c_GameBoard[6]==c_GamePiece))
        iReturnValue=1;
    
    if ((c_GameBoard[1]==c_GamePiece) && (c_GameBoard[4]==c_GamePiece) && (c_GameBoard[7]==c_GamePiece))
        iReturnValue=1;
    
    if ((c_GameBoard[2]==c_GamePiece) && (c_GameBoard[5]==c_GamePiece) && (c_GameBoard[8]==c_GamePiece))
        iReturnValue=1;
    
    /*Diag*/
    if ((c_GameBoard[0]==c_GamePiece) && (c_GameBoard[4]==c_GamePiece) && (c_GameBoard[8]==c_GamePiece))
        iReturnValue=1;
    
    if ((c_GameBoard[2]==c_GamePiece) && (c_GameBoard[4]==c_GamePiece) && (c_GameBoard[6]==c_GamePiece))
        iReturnValue=1;
    
    return iReturnValue; // No one has won/game continues if game end condition isn't met either
    
}

int drawCondition(char c_GameBoard[], const int iMaxNumberOfSpaces)
{
    
    int iCounter    = 0;
    int iAsciiValue = 0;
    int iReturnValue = 0;
    
    for (int iIndex=0;iIndex<iMaxNumberOfSpaces;iIndex++)
    {
        iAsciiValue = (int) c_GameBoard[iIndex];
        
        if ((iAsciiValue<49) || (iAsciiValue>57))
            iCounter = iCounter + 1;
        
    }
    
    if (iCounter==iMaxNumberOfSpaces)
    {
        cout << "All Spaces are Occupied" << endl;
        iReturnValue = 1;
    }
    
    return iReturnValue;
    
}



int playAgain(void)
{
    
    
    int iReturnValue = -1;
    char cCharacter = NULL;
    
    while(iReturnValue==-1)
    {
        
        cout << "play again? Y/N";
        
        cCharacter = getchar();
        
        if (cCharacter=='y') cCharacter = 'Y';
        if (cCharacter=='n') cCharacter = 'N';
        
        if (cCharacter=='N') iReturnValue = 1;
        if (cCharacter=='Y') iReturnValue = 2;
        
        if ((cCharacter!='N') && (cCharacter!='Y'))
        {
            cout << "Error";
        }
        
    }
    
    return iReturnValue;
    
}
Topic archived. No new replies allowed.