odd and even mouse clicks

We've been tasked to make a tic-tac-toe game but it seems like were all getting stuck on alternating between X and O and instead each click outputs and X in a box.

My idea was to make an if statement saying each even number of clicks will output a X and each odd number of clicks will output an O.

I've tried different things but i'm stuck on this.

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
#include <GameDev2D.h>

//Function prototypes
void Init();
void Shutdown();
void Update(double delta);
void Draw();
int MouseClicked(float x, float y);

//variables
int state[9] = {};
int Click[9] = {};


//Entry point to the application
int WINAPI WinMain(HINSTANCE aCurrentInstance, HINSTANCE aPreviousInstance, LPSTR aCommandLine, int aCommandShow)
{
    //Run GameDev2D, pass in the Init, Shutdown, Update and Draw methods
    GameDev2D::Run(Init, Shutdown, Update, Draw);
    return 0;
}

void Init()
{
    GameDev2D::RegisterLeftMouseButtonPressedCallback(MouseClicked);
    GameDev2D::LoadTexture("x");
    GameDev2D::LoadTexture("o");

}

void Shutdown()
{
    GameDev2D::UnloadTexture("x");
    GameDev2D::UnloadTexture("o");

}

void Update(double aDelta)
{
   
}

void Draw()
{
    //Guide lines
    GameDev2D::DrawRectangle(256.0f, 0.0f, 10.0f, 768.0f, 0.0f, GameDev2D::Color::BlackColor(), true);
    GameDev2D::DrawRectangle(512.0f, 0.0f, 10.0f, 768.0f, 0.0f, GameDev2D::Color::BlackColor(), true);
    GameDev2D::DrawRectangle(0.0f, 256.0f, 768.0f, 10.0f, 0.0f, GameDev2D::Color::BlackColor(), true);
    GameDev2D::DrawRectangle(0.0f, 512.0f, 768.0f, 10.0f, 0.0f, GameDev2D::Color::BlackColor(), true);
	


	//Drawing in diffrent boxes
	if (state[0] == 1)
	{
		GameDev2D::DrawTexture("x", 0.0f, 0.0f, 0.0f); // row 1 / 1 
	}
	 if (state[0] == 2)
	{
		GameDev2D::DrawTexture("o", 0.0f, 0.0f, 0.0f);
	}

	if (state[1] == 1)
	{
		GameDev2D::DrawTexture("x", 256.0f, 0.0f, 0.0f); // row 1 / 2
	}
	if (state[1] == 2)
	{
		GameDev2D::DrawTexture("o", 256.0f, 0.0f, 0.0f);
	}

	if (state[2] == 1)
	{
		GameDev2D::DrawTexture("x", 512.0f, 0.0f, 0.0f); // row 1 / 3
	}
	if (state[2] == 2)
	{
		GameDev2D::DrawTexture("o", 512.0f, 0.0f, 0.0f);
	}

	if (state[3] == 1)
	{
		GameDev2D::DrawTexture("x", 0.0f, 256.0f, 0.0f); // row 2 / 1
	}
	if (state[3] == 2)
	{
		GameDev2D::DrawTexture("o", 0.0f, 256.0f, 0.0f);
	}

	if (state[4] == 1)
	{
		GameDev2D::DrawTexture("x", 256.0f, 256.0f, 0.0f); // row 2 / 2
	}
	if (state[4] == 2)
	{
		GameDev2D::DrawTexture("o", 256.0f, 256.0f, 0.0f);
	}

	if (state[5] == 1)
	{
		GameDev2D::DrawTexture("x", 512.0f, 256.0f, 0.0f); // row 2 / 3
	}
	if (state[5] == 2)
	{
		GameDev2D::DrawTexture("o", 512.0f, 256.0f, 0.0f);
	}

	if (state[6] == 1)
	{
		GameDev2D::DrawTexture("x", 0.0f, 512.0f, 0.0f); // row 3 / 1
	}
	if (state[6] == 2)
	{
		GameDev2D::DrawTexture("o", 0.0f, 512.0f, 0.0f);
	}

	if (state[7] == 1)
	{
		GameDev2D::DrawTexture("x", 256.0f, 512.0f, 0.0f); // row 3 / 2
	}
	if (state[7] == 2)
	{
		GameDev2D::DrawTexture("o", 256.0f, 512.0f, 0.0f);
	}

	if (state[8] == 1)
	{
		GameDev2D::DrawTexture("x", 512.0f, 512.0f, 0.0f); // row 3 / 3
	}
	if (state[8] == 2)
	{
		GameDev2D::DrawTexture("o", 512.0f, 512.0f, 0.0f);
	}

}

int MouseClicked(float x, float y) 
{
    if (x > 0 && x < 256 && y > 0 && y < 256)
    {
		state[0] = 1 || 2;
    }

    if (x > 256 && x < 512 && y > 0 && y < 256)
    {
        if (state[1] == 0)
        {
			state[1] = 1 || 2;
        }
    }

     if (x > 512 && x < 768 && y > 0 && y < 256)
    {
         if (state[2] == 0)
         {
			 state[2] = 1 || 2;
         }
    }

     if (x > 0 && x < 256 && y > 256 && y < 512)
     {
         if (state[3] == 0)
         {
			 state[3] = 1 || 2;
         }
     }

     if (x > 256 && x < 512 && y > 256 && y < 512)
     {
         if (state[4] == 0)
         {
			 state[4] = 1 || 2;
         }
     }

     if (x > 512 && x < 768 && y > 256 && y < 512)
     {
         if (state[5] == 0)
         {
			 state[5] = 1 || 2;
         }
     }

     if (x > 0 && x < 256 && y > 512 && y < 768)
     {
         if (state[6] == 0)
         {
			 state[6] = 1 || 2;
         }
     }

     if (x > 256 && x < 512 && y > 512 && y < 768)
     {
         if (state[7] == 0)
         {
			 state[7] = 1 || 2;

         }
     }

     if (x > 512 && x < 768 && y > 512 && y < 768)
     {
         if (state[8] == 0)
         {
			 state[8] = 1 || 2;
         }
     }
}


Last edited on
Those nested ifs are eyesore as hell. Here are my 2 cents, don't count odd and even clicks, just have a boolean value you can use as a switch. Also use a function to divide the board, rather than just spamming ifs.

1
2
3
4
5
6
7
8
bool isXTurn = true; //start with X

//inside your game loop
   
uint8_t chosenSquare = GetSelectedSquare();     //some function that gets player's input (i.e in which square he clicked)
UpdateBoard(chosenSquare, isXTurn ? 'X' : 'O'); //check if is X's turn, if not use O
isXTurn = !isXTurn;                             //alternate turns
Last edited on
The way we were taught to do it today was with all the if's, i agree its not very efficient and i wish we had gotten an alternative but this is all i know on how to do this.

No one was able to get the mouse input to alternate between X and O.

As far as your response goes i'm not sure where im implementing this. I'm pretty sure the Bool function im adding to the variable list but the rest is a blur since this is 1st year programming we haven't seen extensive coding yet like uint8_t.

Topic archived. No new replies allowed.