tic tac toe

so in the midst of creating this tic tac toe game,i have this problem.it goes like this.
TURN 1
when the player picks the number 4,the computer will choose the number 2.
and when the player picks anything other than 4,the computer will choose 4.
TURN 2
the computer will try to intercept you to prevent you from winning.

so the problem is that when i pick any number other than 4,the computer will not choose 4 but instead jump to the options in turn 2.i cant seem to figure out why.

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
  #include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

// ArrayDemo - demonstrate the use of arrays
//             by reading a sequence of integers
//             and then displaying them and their sum



void tictactoe(void);
int gamemode(void);//to select single or multiplayer
int singleplayer(void);
int multiplayer(void);

int main(int nNumberofArgs, char* pszArgs[])
{
             
           
           
           cout<<"TIC TAC TOE"<<endl
               <<endl
               <<"1:singleplayer"<<endl
               <<endl
               <<"2:multiplayer"<<endl;
               
               tictactoe();
            
            
            int option = gamemode();//select single or multiplayer
            if (option == 1)
            {

            	singleplayer();
            	
            	
			}
			else
			{
				
				multiplayer();
				
			}
        
            

}

int singleplayer(void)
{
	
	int positions[9] = {0,1,2,3,4,5,6,7,8};

	int PlayerMoveStorage[5];
	
	int AIMovesStorage[5];
	
	
	for(;;)
	{
		
		int turn = 1;
		cout<<"turn "<<turn<<endl;
	cout<<"your turn."<<endl;
	int PlayerMove;
	cin>>PlayerMove;
	
	
	if((PlayerMove>8)||(PlayerMove<0))
	{
		cout<<"invalid position."<<endl;
		continue;
	}
	
	/*
	int checker1 = 0;
	int checker2 = 0;
	int UsedNumbers[9];
	for(;checker1<8;checker1++)
	{
        for(;checker2<5;checker2++)
        {
        	if(UsedNumbers[checker1]==PlayerMoveStorage[checker2])
        	{
        		cout<<"this number have been used before"<<endl;
        		continue;
			}
		}
	}
	*/
	//player turn
	int PlayerCounter = 0;
	PlayerMoveStorage[PlayerCounter] = positions[PlayerMove];
	PlayerCounter++;
	
	//A.I turn
	int AIMove;
	int AICounter = 0;
	
	//IF TURN IS 1
    if(turn == 1)
    {
    	
    	if((PlayerMove)!=(4))
    	{
    		
    		AIMove = 4;
    		
		}
		else if(PlayerMove == 4)
		{
			
			AIMove = 2;
			
		}
		turn++;
	}
	
	//IF TURN IS 2
	if(turn == 2)
	{
		if(PlayerMove == 8)
		{
			AIMove = 0;
		}
		if(PlayerMove == 7)
		{
			AIMove = 1;
		}
		if(PlayerMove == 6)
		{
			AIMove = 0;
		}
		if(PlayerMove == 5)
		{
			AIMove = 3;
		}
		if(PlayerMove == 3)
		{
			AIMove = 5;
		}
		if(PlayerMove == 1)
		{
			AIMove = 7;
		}
		if(PlayerMove == 0)
		{
			AIMove = 8;
		}
		turn++;
	}
	
	AIMovesStorage[AICounter] = positions[AIMove];
	cout<<"AI move-"<<AIMovesStorage[AICounter]<<endl;
    AICounter++;
    
    /*
    int checkingCounter = 0;
    UsedNumbers[checkingCounter]=PlayerMove;
    checkingCounter++;
    UsedNumbers[checkingCounter]=AIMove;
    checkingCounter++;
    */
    
    }
	
}

int multiplayer(void)
{
	
}


int gamemode(void)//select single or multiplayer
{
	int option;

	
	for(;;)
	{
	cout<<"choose game mode-";
	cin>>option;
       
       if((option == 1) || (option == 2))
       {
       	return option;
       	break;
	   }
	   
	cout<<"invalid option"<<endl;
		
	}
	
}





void tictactoe()
{
	    cout<<"    |     |    "<<endl
            <<"  0 |  1  |  2 "<<endl
            <<"____|_____|____"<<endl
            <<"    |     |    "<<endl
            <<"  3 |  4  |  5 "<<endl
            <<"____|_____|____"<<endl
            <<"    |     |    "<<endl
            <<"  6 |  7  |  8 "<<endl
            <<"    |     |    "<<endl;
}
One problem are the braces of the if on line 102. The if body contains the second check of turn on line 121 which will never be true.
Topic archived. No new replies allowed.