2 * queens - solves n queens problem, displays
3 * i make no claims that this is an optimal solution to the problem,
7 * version 1.0 - May 10, 2002
9 * Copyright (C) 2002 Blair Tennessy (tennessy@cs.ubc.ca)
11 * Permission to use, copy, modify, distribute, and sell this software and its
12 * documentation for any purpose is hereby granted without fee, provided that
13 * the above copyright notice appear in all copies and that both that
14 * copyright notice and this permission notice appear in supporting
15 * documentation. No representations are made about the suitability of this
16 * software for any purpose. It is provided "as is" without express or
21 #define DEFAULTS "*delay: 20000 \n" \
22 "*showFPS: False \n" \
23 "*wireframe: False \n" \
25 # define refresh_queens 0
26 # include "xlockmore.h"
35 # include <X11/Xlib.h>
42 #endif /* HAVE_JWZGLES */
46 #include "gltrackball.h"
47 #include "chessmodels.h"
50 #define countof(x) (sizeof((x))/sizeof((*x)))
52 static XrmOptionDescRec opts[] = {
53 {"+rotate", ".queens.rotate", XrmoptionNoArg, "false" },
54 {"-rotate", ".queens.rotate", XrmoptionNoArg, "true" },
55 {"+flat", ".queens.flat", XrmoptionNoArg, "false" },
56 {"-flat", ".queens.flat", XrmoptionNoArg, "true" },
59 static int rotate, wire, clearbits, flat;
61 static argtype vars[] = {
62 {&rotate, "rotate", "Rotate", "True", t_Bool},
63 {&flat, "flat", "Flat", "False", t_Bool},
66 ENTRYPOINT ModeSpecOpt queens_opts = {countof(opts), opts, countof(vars), vars, NULL};
69 ModStruct queens_description =
70 {"queens", "init_queens", "draw_queens", "release_queens",
71 "draw_queens", "init_queens", NULL, &queens_opts,
72 1000, 1, 2, 1, 4, 1.0, "",
83 GLXContext *glx_context;
85 trackball_state *trackball;
90 int board[MAXBOARD][MAXBOARD];
91 int steps, colorset, BOARDSIZE;
97 static Queenscreen *qss = NULL;
99 /* definition of white/black colors */
100 static const GLfloat colors[COLORSETS][2][3] =
102 {{0.43, 0.54, 0.76}, {0.8, 0.8, 0.8}},
103 {{0.5, 0.7, 0.9}, {0.2, 0.3, 0.6}},
104 {{0.53725490196, 0.360784313725, 0.521568627451}, {0.6, 0.6, 0.6}},
105 {{0.15, 0.77, 0.54}, {0.5, 0.5, 0.5}},
106 {{0.9, 0.45, 0.0}, {0.5, 0.5, 0.5}},
110 queens_handle_event (ModeInfo *mi, XEvent *event)
112 Queenscreen *qs = &qss[MI_SCREEN(mi)];
114 if (gltrackball_event_handler (event, qs->trackball,
115 MI_WIDTH (mi), MI_HEIGHT (mi),
118 else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
120 qs->steps = 1024 - 1;
129 /* returns true if placing a queen on column c causes a conflict */
130 static int conflictsCols(Queenscreen *qs, int c)
134 for(i = 0; i < qs->BOARDSIZE; ++i)
141 /* returns true if placing a queen on (r,c) causes a diagonal conflict */
142 static int conflictsDiag(Queenscreen *qs, int r, int c)
147 int n = r < c ? r : c;
148 for(i = 0; i < qs->BOARDSIZE-abs(r-c); ++i)
149 if(qs->board[r-n+i][c-n+i])
153 n = r < qs->BOARDSIZE - (c+1) ? r : qs->BOARDSIZE - (c+1);
154 for(i = 0; i < qs->BOARDSIZE-abs(qs->BOARDSIZE - (1+r+c)); ++i)
155 if(qs->board[r-n+i][c+n-i])
161 /* returns true if placing a queen at (r,c) causes a conflict */
162 static int conflicts(Queenscreen *qs, int r, int c)
164 return !conflictsCols(qs, c) ? conflictsDiag(qs, r, c) : 1;
168 static void blank(Queenscreen *qs)
172 for(i = 0; i < MAXBOARD; ++i)
173 for(j = 0; j < MAXBOARD; ++j)
174 qs->board[i][j] = NONE;
177 /* recursively determine solution */
178 static int findSolution(Queenscreen *qs, int row, int col)
180 if(row == qs->BOARDSIZE)
183 while(col < qs->BOARDSIZE) {
184 if(!conflicts(qs, row, col)) {
185 qs->board[row][col] = 1;
187 if(findSolution(qs, row+1, 0))
190 qs->board[row][col] = 0;
199 /** driver for finding solution */
200 static void go(Queenscreen *qs) { while(!findSolution(qs, 0, random()%qs->BOARDSIZE)); }
202 /* lighting variables */
203 static const GLfloat front_shininess[] = {60.0};
204 static const GLfloat front_specular[] = {0.4, 0.4, 0.4, 1.0};
205 static const GLfloat ambient[] = {0.3, 0.3, 0.3, 1.0};
206 static const GLfloat diffuse[] = {0.8, 0.8, 0.8, 1.0};
208 /* configure lighting */
209 static void setup_lights(Queenscreen *qs)
212 /* setup twoside lighting */
213 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
214 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
215 glLightfv(GL_LIGHT0, GL_POSITION, qs->position);
216 glEnable(GL_LIGHTING);
219 /* setup material properties */
220 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
221 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
223 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
226 #define checkImageWidth 8
227 #define checkImageHeight 8
228 /*static GLubyte checkImage[checkImageWidth][checkImageHeight][3];*/
230 /* return alpha value for fading */
231 static GLfloat findAlpha(Queenscreen *qs)
233 return qs->steps < 128 ? qs->steps/128.0 : qs->steps < 1024-128 ?1.0:(1024-qs->steps)/128.0;
237 static int drawPieces(Queenscreen *qs)
242 for(i = 0; i < qs->BOARDSIZE; ++i) {
243 for(j = 0; j < qs->BOARDSIZE; ++j) {
244 if(qs->board[i][j]) {
245 glColor3fv(colors[qs->colorset][i%2]);
246 glCallList(qs->queen_list);
247 polys += qs->queen_polys;
250 glTranslatef(1.0, 0.0, 0.0);
253 glTranslatef(-1.0*qs->BOARDSIZE, 0.0, 1.0);
258 /** reflectionboard */
259 static int draw_reflections(Queenscreen *qs)
264 glEnable(GL_STENCIL_TEST);
265 glStencilFunc(GL_ALWAYS, 1, 1);
266 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
267 glColorMask(0,0,0,0);
268 glDisable(GL_CULL_FACE);
270 glDisable(GL_DEPTH_TEST);
273 /* only draw white squares */
274 for(i = 0; i < qs->BOARDSIZE; ++i) {
275 for(j = (qs->BOARDSIZE+i) % 2; j < qs->BOARDSIZE; j += 2) {
276 glVertex3f(i, 0.0, j + 1.0);
277 glVertex3f(i + 1.0, 0.0, j + 1.0);
278 glVertex3f(i + 1.0, 0.0, j);
279 glVertex3f(i, 0.0, j);
284 glEnable(GL_DEPTH_TEST);
286 glColorMask(1, 1, 1, 1);
287 glStencilFunc(GL_EQUAL, 1, 1);
288 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
291 glScalef(1.0, -1.0, 1.0);
292 glTranslatef(0.5, 0.001, 0.5);
293 glLightfv(GL_LIGHT0, GL_POSITION, qs->position);
294 polys += drawPieces(qs);
296 glDisable(GL_STENCIL_TEST);
299 glLightfv(GL_LIGHT0, GL_POSITION, qs->position);
301 glEnable(GL_CULL_FACE);
303 glColorMask(1,1,1,1);
308 static int drawBoard(Queenscreen *qs)
315 for(i = 0; i < qs->BOARDSIZE; ++i)
316 for(j = 0; j < qs->BOARDSIZE; ++j) {
317 int par = (i-j+qs->BOARDSIZE)%2;
318 glColor4f(colors[qs->colorset][par][0],
319 colors[qs->colorset][par][1],
320 colors[qs->colorset][par][2],
322 glNormal3f(0.0, 1.0, 0.0);
323 glVertex3f(i, 0.0, j + 1.0);
324 glVertex3f(i + 1.0, 0.0, j + 1.0);
325 glVertex3f(i + 1.0, 0.0, j);
326 glVertex3f(i, 0.0, j);
334 GLfloat w = qs->BOARDSIZE;
337 /* Give the board a slight lip. */
338 /* #### oops, normals are wrong here, but you can't tell */
340 glColor3f(0.3, 0.3, 0.3);
342 glVertex3f (0, 0, 0);
343 glVertex3f (0, -h, 0);
344 glVertex3f (0, -h, w);
345 glVertex3f (0, 0, w);
347 glVertex3f (0, 0, w);
348 glVertex3f (0, -h, w);
349 glVertex3f (w, -h, w);
350 glVertex3f (w, 0, w);
352 glVertex3f (w, 0, w);
353 glVertex3f (w, -h, w);
354 glVertex3f (w, -h, 0);
355 glVertex3f (w, 0, 0);
357 glVertex3f (w, 0, 0);
358 glVertex3f (w, -h, 0);
359 glVertex3f (0, -h, 0);
360 glVertex3f (0, 0, 0);
362 glVertex3f (0, -h, 0);
363 glVertex3f (w, -h, 0);
364 glVertex3f (w, -h, w);
365 glVertex3f (0, -h, w);
369 /* Fill in the underside of the board with an invisible black box
370 to hide the reflections that are not on tiles. Probably there's
371 a way to do this with stencils instead.
377 glTranslatef (off, 0, off);
378 glDisable(GL_LIGHTING);
381 glVertex3f (0, 0, 0);
382 glVertex3f (0, -h, 0);
383 glVertex3f (0, -h, w);
384 glVertex3f (0, 0, w);
386 glVertex3f (0, 0, w);
387 glVertex3f (0, -h, w);
388 glVertex3f (w, -h, w);
389 glVertex3f (w, 0, w);
391 glVertex3f (w, 0, w);
392 glVertex3f (w, -h, w);
393 glVertex3f (w, -h, 0);
394 glVertex3f (w, 0, 0);
396 glVertex3f (w, 0, 0);
397 glVertex3f (w, -h, 0);
398 glVertex3f (0, -h, 0);
399 glVertex3f (0, 0, 0);
401 glVertex3f (0, -h, 0);
402 glVertex3f (w, -h, 0);
403 glVertex3f (w, -h, w);
404 glVertex3f (0, -h, w);
409 glEnable(GL_LIGHTING);
415 static int display(Queenscreen *qs)
421 glMatrixMode(GL_MODELVIEW);
424 glRotatef(current_device_rotation(), 0, 0, 1);
426 /* setup light attenuation */
427 /* #### apparently this does nothing */
428 glEnable(GL_COLOR_MATERIAL);
429 glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.8/(0.01+findAlpha(qs)));
430 glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.06);
432 /** setup perspective */
433 glTranslatef(0.0, 0.0, -1.5*qs->BOARDSIZE);
434 glRotatef(30.0, 1.0, 0.0, 0.0);
435 gltrackball_rotate (qs->trackball);
436 glRotatef(qs->theta*100, 0.0, 1.0, 0.0);
437 glTranslatef(-0.5*qs->BOARDSIZE, 0.0, -0.5*qs->BOARDSIZE);
439 /* find light positions */
440 qs->position[0] = qs->BOARDSIZE/2.0 + qs->BOARDSIZE/1.4*-sin(qs->theta*100*M_PI/180.0);
441 qs->position[2] = qs->BOARDSIZE/2.0 + qs->BOARDSIZE/1.4*cos(qs->theta*100*M_PI/180.0);
442 qs->position[1] = 6.0;
445 glEnable(GL_LIGHTING);
446 glLightfv(GL_LIGHT0, GL_POSITION, qs->position);
450 /* Since the lighting attenuation trick up there doesn't seem to be working,
451 let's drop the old board down and drop the new board in. */
452 if (qs->steps < (max/8.0)) {
453 GLfloat y = qs->steps / (max/8.0);
454 y = sin (M_PI/2 * y);
455 glTranslatef (0, 10 - (y * 10), 0);
456 } else if (qs->steps > max-(max/8.0)) {
457 GLfloat y = (qs->steps - (max-(max/8.0))) / (GLfloat) (max/8.0);
458 y = 1 - sin (M_PI/2 * (1-y));
459 glTranslatef (0, -y * 15, 0);
462 /* draw reflections */
464 polys += draw_reflections(qs);
467 polys += drawBoard(qs);
471 glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.1);
473 glTranslatef(0.5, 0.0, 0.5);
474 polys += drawPieces(qs);
477 if(!qs->button_down_p)
480 /* zero out board, find new solution of size MINBOARD <= i <= MAXBOARD */
481 if(++qs->steps == max) {
484 qs->BOARDSIZE = MINBOARD + (random() % (MAXBOARD - MINBOARD + 1));
485 qs->colorset = (qs->colorset+1)%COLORSETS;
491 #define EPSILON 0.001
494 /** draws cylindermodel */
495 static int draw_model(int chunks, const GLfloat model[][3], int r)
500 glRotatef(-90.0, 1.0, 0.0, 0.0);
502 for(i = 0; i < chunks; ++i) {
503 if(model[i][0] > EPSILON || model[i][1] > EPSILON) {
504 polys += tube (0, 0, 0,
507 r, False, False, False);
508 /* gluCylinder(quadric, model[i][0], model[i][1], model[i][2], r, 1);
511 glTranslatef(0.0, 0.0, model[i][2]);
519 ENTRYPOINT void reshape_queens(ModeInfo *mi, int width, int height)
521 GLfloat h = (GLfloat) height / (GLfloat) width;
522 glViewport(0,0, width, height);
523 glMatrixMode(GL_PROJECTION);
525 gluPerspective(45, 1/h, 2.0, 30.0);
526 glMatrixMode(GL_MODELVIEW);
529 ENTRYPOINT void init_queens(ModeInfo *mi)
531 int screen = MI_SCREEN(mi);
533 int poly_counts[PIECES];
534 wire = MI_IS_WIREFRAME(mi);
536 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
541 !(qss = (Queenscreen *) calloc(MI_NUM_SCREENS(mi), sizeof(Queenscreen))))
545 qs->window = MI_WINDOW(mi);
547 if((qs->glx_context = init_GL(mi)))
548 reshape_queens(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
552 qs->trackball = gltrackball_init (False);
554 qs->BOARDSIZE = 8; /* 8 cuz its classic */
556 chessmodels_gen_lists(-1, poly_counts);
557 qs->queen_list = QUEEN;
558 qs->queen_polys = poly_counts[QUEEN];
560 /* find a solution */
564 ENTRYPOINT void draw_queens(ModeInfo *mi)
566 Queenscreen *qs = &qss[MI_SCREEN(mi)];
567 Window w = MI_WINDOW(mi);
568 Display *disp = MI_DISPLAY(mi);
573 glXMakeCurrent(disp, w, *(qs->glx_context));
576 glShadeModel(GL_FLAT);
578 clearbits = GL_COLOR_BUFFER_BIT;
580 glColorMaterial(GL_FRONT, GL_DIFFUSE);
581 glEnable(GL_COLOR_MATERIAL);
585 glEnable(GL_DEPTH_TEST);
586 clearbits |= GL_DEPTH_BUFFER_BIT;
587 clearbits |= GL_STENCIL_BUFFER_BIT;
588 glEnable(GL_CULL_FACE);
592 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
594 mi->polygon_count = display(qs);
595 mi->recursion_depth = qs->BOARDSIZE;
597 if(mi->fps_p) do_fps(mi);
599 glXSwapBuffers(disp, w);
602 ENTRYPOINT void release_queens(ModeInfo *mi)
611 XSCREENSAVER_MODULE ("Queens", queens)