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 (tennessb@unbc.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
20 #include <X11/Intrinsic.h>
23 # define PROGCLASS "Queens"
24 # define HACK_INIT init_queens
25 # define HACK_DRAW draw_queens
26 # define HACK_RESHAPE reshape_queens
27 # define HACK_HANDLE_EVENT queens_handle_event
28 # define EVENT_MASK PointerMotionMask
29 # define queens_opts xlockmore_opts
31 #define DEFAULTS "*delay: 20000 \n" \
32 "*showFPS: False \n" \
33 "*wireframe: False \n" \
35 # include "xlockmore.h"
44 #include "gltrackball.h"
47 #define countof(x) (sizeof((x))/sizeof((*x)))
49 static XrmOptionDescRec opts[] = {
50 {"+rotate", ".queens.rotate", XrmoptionNoArg, (caddr_t) "false" },
51 {"-rotate", ".queens.rotate", XrmoptionNoArg, (caddr_t) "true" },
52 /* {"-white", ".queens.white", XrmoptionSepArg, (cadd_t) NULL }, */
53 /* {"-black", ".queens.white", XrmoptionSepArg, (cadd_t) NULL }, */
56 int rotate, wire, clearbits;
58 static argtype vars[] = {
59 {(caddr_t *) &rotate, "rotate", "Rotate", "True", t_Bool},
62 ModeSpecOpt queens_opts = {countof(opts), opts, countof(vars), vars, NULL};
65 ModStruct queens_description =
66 {"queens", "init_queens", "draw_queens", "release_queens",
67 "draw_queens", "init_queens", NULL, &queens_opts,
68 1000, 1, 2, 1, 4, 1.0, "",
74 GLXContext *glx_context;
76 trackball_state *trackball;
80 static Queenscreen *qs = NULL;
88 #define M_PI 3.14159265
97 /* definition of white/black colors */
98 GLfloat colors[COLORSETS][2][3] =
100 {{0.43, 0.54, 0.76}, {0.8, 0.8, 0.8}},
101 {{0.5, 0.7, 0.9}, {0.2, 0.3, 0.6}},
102 {{0.53725490196, 0.360784313725, 0.521568627451}, {0.6, 0.6, 0.6}},
103 {{0.15, 0.77, 0.54}, {0.5, 0.5, 0.5}},
104 {{0.9, 0.45, 0.0}, {0.5, 0.5, 0.5}},
107 int board[MAXBOARD][MAXBOARD];
108 int steps = 0, colorset = 0, BOARDSIZE = 8; /* 8 cuz its classic */
111 queens_handle_event (ModeInfo *mi, XEvent *event)
113 Queenscreen *c = &qs[MI_SCREEN(mi)];
115 if (event->xany.type == ButtonPress &&
116 event->xbutton.button & Button1)
118 c->button_down_p = True;
119 gltrackball_start (c->trackball,
120 event->xbutton.x, event->xbutton.y,
121 MI_WIDTH (mi), MI_HEIGHT (mi));
124 else if (event->xany.type == ButtonRelease &&
125 event->xbutton.button & Button1)
127 c->button_down_p = False;
130 else if (event->xany.type == MotionNotify &&
133 gltrackball_track (c->trackball,
134 event->xmotion.x, event->xmotion.y,
135 MI_WIDTH (mi), MI_HEIGHT (mi));
144 /* returns true if placing a queen on column c causes a conflict */
145 int conflictsCols(int c) {
148 for(i = 0; i < BOARDSIZE; ++i)
155 /* returns true if placing a queen on (r,c) causes a diagonal conflict */
156 int conflictsDiag(int r, int c) {
160 int n = r < c ? r : c;
161 for(i = 0; i < BOARDSIZE-abs(r-c); ++i)
162 if(board[r-n+i][c-n+i])
166 n = r < BOARDSIZE - (c+1) ? r : BOARDSIZE - (c+1);
167 for(i = 0; i < BOARDSIZE-abs(BOARDSIZE - (1+r+c)); ++i)
168 if(board[r-n+i][c+n-i])
174 /* returns true if placing a queen at (r,c) causes a conflict */
175 int conflicts(int r, int c) {
176 return !conflictsCols(c) ? conflictsDiag(r, c) : 1;
183 for(i = 0; i < MAXBOARD; ++i)
184 for(j = 0; j < MAXBOARD; ++j)
188 /* recursively determine solution */
189 int findSolution(int row, int col) {
193 while(col < BOARDSIZE) {
194 if(!conflicts(row, col)) {
197 if(findSolution(row+1, 0))
209 /** driver for finding solution */
210 void go(void) { while(!findSolution(0, random()%BOARDSIZE)); }
212 /* lighting variables */
213 GLfloat front_shininess[] = {80.0};
214 GLfloat front_specular[] = {0.5, 0.5, 0.5, 1.0};
215 GLfloat ambient[] = {0.5, 0.5, 0.5, 1.0};
216 GLfloat ambient2[] = {0.1, 0.1, 0.1, 1.0};
217 GLfloat diffuse[] = {0.7, 0.7, 0.7, 1.0};
218 GLfloat position0[] = {0.0, 7.0, 0.0, 1.0};
219 GLfloat position1[] = {8.0, 7.0, 8.0, 1.0};
220 GLfloat lmodel_ambient[] = {0.6, 0.6, 0.6, 1.0};
221 GLfloat lmodel_twoside[] = {GL_TRUE};
223 /* configure lighting */
224 void setup_lights(void) {
226 /* setup twoside lighting */
227 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient2);
228 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
229 glLightfv(GL_LIGHT0, GL_POSITION, position0);
230 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient2);
231 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
232 glLightfv(GL_LIGHT1, GL_POSITION, position1);
233 glEnable(GL_LIGHTING);
237 /* setup material properties */
238 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
239 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
241 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
244 /* return alpha value for fading */
245 GLfloat findAlpha(void) {
246 return steps < 128 ? steps/128.0 : steps < 512-128 ? 1.0 : (512-steps)/128.0;
250 void drawPieces(void) {
253 for(i = 0; i < BOARDSIZE; ++i) {
254 for(j = 0; j < BOARDSIZE; ++j) {
256 glColor3fv(colors[colorset][i%2]);
260 glTranslatef(1.0, 0.0, 0.0);
263 glTranslatef(-1.0*BOARDSIZE, 0.0, 1.0);
267 /** reflectionboard */
268 void draw_reflections(void) {
271 glEnable(GL_STENCIL_TEST);
272 glStencilFunc(GL_ALWAYS, 1, 1);
273 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
274 glColorMask(0,0,0,0);
275 glDisable(GL_CULL_FACE);
277 glDisable(GL_DEPTH_TEST);
280 /* only draw white squares */
281 for(i = 0; i < BOARDSIZE; ++i) {
282 for(j = (BOARDSIZE+i) % 2; j < BOARDSIZE; j += 2) {
283 glVertex3f(i, 0.0, j + 1.0);
284 glVertex3f(i + 1.0, 0.0, j + 1.0);
285 glVertex3f(i + 1.0, 0.0, j);
286 glVertex3f(i, 0.0, j);
290 glEnable(GL_DEPTH_TEST);
292 glColorMask(1, 1, 1, 1);
293 glStencilFunc(GL_EQUAL, 1, 1);
294 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
297 glScalef(1.0, -1.0, 1.0);
298 glTranslatef(0.5, 0.001, 0.5);
299 glLightfv(GL_LIGHT0, GL_POSITION, position0);
300 glLightfv(GL_LIGHT1, GL_POSITION, position1);
303 glDisable(GL_STENCIL_TEST);
306 glLightfv(GL_LIGHT0, GL_POSITION, position0);
307 glLightfv(GL_LIGHT1, GL_POSITION, position1);
309 glEnable(GL_CULL_FACE);
311 glColorMask(1,1,1,1);
315 void drawBoard(void) {
320 for(i = 0; i < BOARDSIZE; ++i)
321 for(j = 0; j < BOARDSIZE; ++j) {
322 int par = (i-j+BOARDSIZE)%2;
323 glColor4f(colors[colorset][par][0],
324 colors[colorset][par][1],
325 colors[colorset][par][2],
327 glNormal3f(0.0, 1.0, 0.0);
328 glVertex3f(i, 0.0, j + 1.0);
329 glVertex3f(i + 1.0, 0.0, j + 1.0);
330 glVertex3f(i + 1.0, 0.0, j);
331 glVertex3f(i, 0.0, j);
339 void display(Queenscreen *c) {
342 glMatrixMode(GL_MODELVIEW);
345 glEnable(GL_LIGHTING);
346 glEnable(GL_COLOR_MATERIAL);
347 glLightfv(GL_LIGHT0, GL_POSITION, position0);
348 glLightfv(GL_LIGHT1, GL_POSITION, position1);
349 glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0/(0.01+findAlpha()));
350 glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.0/(0.01+findAlpha()));
352 /** setup perspectif */
353 glTranslatef(0.0, 0.0, -1.5*BOARDSIZE);
354 glRotatef(30.0, 1.0, 0.0, 0.0);
355 gltrackball_rotate (c->trackball);
356 glRotatef(theta*100, 0.0, 1.0, 0.0);
357 glTranslatef(-0.5*BOARDSIZE, 0.0, -0.5*BOARDSIZE);
359 /* draw reflections */
365 position1[0] = BOARDSIZE+2.0;
366 position1[2] = BOARDSIZE+2.0;
367 glLightfv(GL_LIGHT0, GL_POSITION, position0);
368 glLightfv(GL_LIGHT1, GL_POSITION, position1);
370 glTranslatef(0.5, 0.0, 0.5);
374 if(!c->button_down_p)
377 /* zero out board, find new solution of size MINBOARD <= i <= MAXBOARD */
381 BOARDSIZE = MINBOARD + (random() % (MAXBOARD - MINBOARD + 1));
382 colorset = (colorset+1)%COLORSETS;
388 GLfloat spidermodel[][3] =
407 #define EPSILON 0.001
409 /** draws cylindermodel */
410 void draw_model(int chunks, GLfloat model[][3], int r) {
412 GLUquadricObj *quadric = gluNewQuadric();
414 glRotatef(-90.0, 1.0, 0.0, 0.0);
416 for(i = 0; i < chunks; ++i) {
417 if(model[i][0] > EPSILON || model[i][1] > EPSILON)
418 gluCylinder(quadric, model[i][0], model[i][1], model[i][2], r, 1);
419 glTranslatef(0.0, 0.0, model[i][2]);
425 void reshape_queens(ModeInfo *mi, int width, int height) {
426 GLfloat h = (GLfloat) height / (GLfloat) width;
427 glViewport(0,0, width, height);
428 glMatrixMode(GL_PROJECTION);
430 gluPerspective(45, 1/h, 2.0, 30.0);
431 glMatrixMode(GL_MODELVIEW);
434 void init_queens(ModeInfo *mi) {
435 int screen = MI_SCREEN(mi);
437 wire = MI_IS_WIREFRAME(mi);
440 !(qs = (Queenscreen *) calloc(MI_NUM_SCREENS(mi), sizeof(Queenscreen))))
444 c->window = MI_WINDOW(mi);
445 c->trackball = gltrackball_init ();
447 if((c->glx_context = init_GL(mi)))
448 reshape_queens(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
452 glClearColor(0.0, 0.0, 0.0, 0.0);
453 glNewList(QUEEN, GL_COMPILE);
454 draw_model(schunks, spidermodel, 24);
457 clearbits = GL_COLOR_BUFFER_BIT;
459 glColorMaterial(GL_FRONT, GL_DIFFUSE);
460 glEnable(GL_COLOR_MATERIAL);
464 glEnable(GL_DEPTH_TEST);
465 clearbits |= GL_DEPTH_BUFFER_BIT;
466 clearbits |= GL_STENCIL_BUFFER_BIT;
467 glEnable(GL_CULL_FACE);
471 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
473 /* find a solution */
477 void draw_queens(ModeInfo *mi) {
478 Queenscreen *c = &qs[MI_SCREEN(mi)];
479 Window w = MI_WINDOW(mi);
480 Display *disp = MI_DISPLAY(mi);
485 glXMakeCurrent(disp, w, *(c->glx_context));
489 if(mi->fps_p) do_fps(mi);
491 glXSwapBuffers(disp, w);
494 void release_queens(ModeInfo *mi) {