Cross-Platform Game Development for C++ Developers, Part IV: ClanLib SDK, Page 3
168 void Boxes::inputHandler(const CL_InputEvent &i)
169 {
170 if (redturn) {
171 switch(i.id) {
172 case CL_KEY_LEFT:
173 case CL_KEY_G:
174 if (curs.x > 1) curs.x--;
175 break;
176 case CL_KEY_RIGHT:
177 case CL_KEY_J:
178 if (curs.x < boardsize) curs.x++;
179 break;
180 case CL_KEY_UP:
181 case CL_KEY_Y:
182 if (!curs.vert && curs.y > 1) {
183 curs.y--;
184 curs.vert = !curs.vert;
185 }
186 else if (curs.vert) curs.vert = false;
187 break;
188 case CL_KEY_DOWN:
189 case CL_KEY_H:
190 if (curs.vert && curs.y < boardsize) {
191 curs.y++;
192 curs.vert = !curs.vert;
193 }
194 else if (!curs.vert) curs.vert = true;
195 break;
196 }
197 if (curs.x == boardsize && !curs.vert) curs.x--;
198 if (curs.y == boardsize && curs.vert)
curs.vert = false;
199
200 if (i.id == CL_KEY_SPACE || i.id == CL_KEY_ENTER) {
201 if (curs.vert) {
202 if (!ver[curs.x-1][curs.y-1]) {
203 ver[curs.x-1][curs.y-1] = true;
204 if (!findsquares()) redturn = !redturn;
205 }
206 }
207 else {
208 if (!hor[curs.x-1][curs.y-1]) {
209 hor[curs.x-1][curs.y-1] = true;
210 if (!findsquares()) redturn = !redturn;
211 }
212 }
213 }
214 }
215 }
Lastly, the endOfGame() method computes the final score. Remember that the game is not over until the board is full (see line 48) or someone resigns by hitting the ESC key (see line 46). Last of all, you fade out the volume to 0 percent over the course of one second.
217 void Boxes::endOfGame()
218 {
219 // Count scores
220 int redscore, bluescore;
221 redscore = bluescore = 0;
222 for (int x = 0; x < boardsize - 1; x++)
223 for (int y = 0; y < boardsize - 1; y++) {
224 if (squares[x][y] == red) redscore++;
225 else if (squares[x][y] == blue) bluescore++;
226 }
227
228 cout << "Red: " << redscore
<< "\nBlue: " << bluescore
<< endl;
229 if (bluescore != redscore)
230 cout << (bluescore > redscore ? "Blue" : "Red")
<< " player wins\n";
231 else cout << "It was a tie\n";
232
233 if (fullup) {
234 fade->fade_to_volume(0.0f, 1000);
235 CL_System::sleep(1000);
236 }
237 }
Stay Tuned!
Be sure to come back next week for the fifth and final installment of Cross-Platform Game Development for C++ Developers.
About the Author
Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries; just drop an e-mail to sysop@HAL9K.com.
