/* Xcogitate, by PFF */ /* (c) 1995, 1996 */ /* Compile with */ /* Linux: 'gcc -o Xcogitate xcogitate.c -lX11 -lXpm' */ /* Solaris: 'gcc -o Xcogitate xcogitate.c -lX11 -lXpm -lsocket' */ /* X Window System version */ /* version 1.02 , registered */ /* X include files */ #include #include #include #include #include #include #include /* include pixmaps */ #include "bitmaps/title.h" #include "bitmaps/tiles1.h" #include "bitmaps/tiles2.h" #include "bitmaps/tiles3.h" #include "bitmaps/tiles4.h" #include "bitmaps/tiles5.h" #include "bitmaps/tiles6.h" #include "bitmaps/misc.h" #include "bitmaps/credits.h" #include "bitmaps/a.h" #include "bitmaps/buttons.h" #include "bitmaps/rules1.h" #include "bitmaps/rules2.h" #include "bitmaps/rules3.h" #include "bitmaps/rules4.h" #include "bitmaps/rules5.h" #include "bitmaps/rules6.h" #include "bitmaps/pffcredit.h" #include "bitmaps/pffcredit2.h" #include "bitmaps/quit.h" #include "bitmaps/hint.h" #include "bitmaps/final.h" #include "bitmaps/nomoves.h" #include "bitmaps/replay.h" #include "bitmaps/flash.h" /* declarations */ char title[] = {"Xcogitate v1.02 - Registered version"}; /* some global variables */ int winwidth = 35*12+120; int winheight = 35*8 + 10; int pouch[73][2]; int board[12][8]; int number; int score; int buttonstate[6]; Display *mydisplay; Window mywindow; GC mygc; XGCValues gcv; XEvent myevent; KeySym mykey; XSizeHints myhint; int myscreen; unsigned long myforeground, mybackground; Pixmap boxpix,boxshapemask, backpix, backshapemask, titlepix, titlemask, tilepix, tileshapemask, boxpix2, box2shapemask; Pixmap tiles[36], tilesmask[36]; Pixmap buttonup, buttondown, buttonupmask, buttondownmask; Pixmap quitup,quitdown, helpup,helpdown,rulesup,rulesdown,soundup,sounddown, aboutup,aboutdown,replayup,replaydown; Pixmap page,pff,pff2,quitbox,rulesa, rulesb, rulesc,rulesd, nomove,replaydialog; Pixmap rulese, rulesf, hintdialog, finaldialog; Pixmap flash,rulesmask; XpmAttributes att; Colormap cmap; char *displayname; int notshownyet = 1; make_pouch() { int i,j,k,a,b,c; for(i=0;i<6;i++) for(j=0;j<6;j++) { pouch[6*i+j][0] = i; pouch[6*i+j][1] = j; } for(i=0;i<36;i++) { pouch[i+36][0] = pouch[i][0]; pouch[i+36][1] = pouch[i][1]; } srand((unsigned int) time(NULL)); for(k=0;k<100;k++) { a = rand() % 72; b = rand() % 72; c = pouch[a][0]; pouch[a][0] = pouch[b][0]; pouch[b][0] = c; c = pouch[a][1]; pouch[a][1] = pouch[b][1]; pouch[b][1] = c; } } init_board() { int i,j; for(i=0;i<12;i++) for(j=0;j<8;j++) board[i][j] = -1; board[0][0] = 0; board[11][0] = 1; board[11][7] = 2; board[0][7] = 3; board[5][3] = 4; board[6][4] = 5; number = 6; notshownyet = 1; } draw_board(Display *display, Window window) { int i,j; XCopyArea(display, backpix, window, mygc, 0, 0, 550, 300, 0, 0); XCopyArea(display, boxpix, window, mygc, 0, 0, 99, 24, 12*35+10+6, 6); XCopyArea(display, titlepix, window, mygc, 0, 0, 100, 100, 12*35+10+5, 8*35+5-100); XCopyArea(display, boxpix2, window, mygc, 0, 0, 100, 60, 12*35+10+5, 40); /* Now draw the six buttons */ if(buttonstate[0] == 0) XCopyArea(display, quitup, window, mygc, 0, 0, 33, 30, 12*35+15+(0%3)*33, 110+(0/3)*30); else XCopyArea(display, quitdown, window, mygc, 0, 0, 33, 30, 12*35+15+(0%3)*33, 110+(0/3)*30); if(buttonstate[1] == 0) XCopyArea(display, helpup, window, mygc, 0, 0, 33, 30, 12*35+15+(1%3)*33, 110+(1/3)*30); else XCopyArea(display, helpdown, window, mygc, 0, 0, 33, 30, 12*35+15+(1%3)*33, 110+(1/3)*30); if(buttonstate[2] == 0) XCopyArea(display, rulesup, window, mygc, 0, 0, 33, 30, 12*35+15+(2%3)*33, 110+(2/3)*30); else XCopyArea(display, rulesdown, window, mygc, 0, 0, 33, 30, 12*35+15+(2%3)*33, 110+(2/3)*30); if(buttonstate[3] == 0) XCopyArea(display, replayup, window, mygc, 0, 0, 33, 30, 12*35+15+(3%3)*33, 110+(3/3)*30); else XCopyArea(display, replaydown, window, mygc, 0, 0, 33, 30, 12*35+15+(3%3)*33, 110+(3/3)*30); /* sound not supported in this version */ if(buttonstate[4] == 0) XCopyArea(display, sounddown, window, mygc, 0, 0, 33, 30, 12*35+15+(4%3)*33, 110+(4/3)*30); else XCopyArea(display, sounddown, window, mygc, 0, 0, 33, 30, 12*35+15+(4%3)*33, 110+(4/3)*30); if(buttonstate[5] == 0) XCopyArea(display, aboutup, window, mygc, 0, 0, 33, 30, 12*35+15+(5%3)*33, 110+(5/3)*30); else XCopyArea(display, aboutdown, window, mygc, 0, 0, 33, 30, 12*35+15+(5%3)*33, 110+(5/3)*30); for(i=0;i<=11;i++) for(j=0;j<=7;j++) { XCopyArea(display, tilepix, window, mygc, 0, 0, 35, 35, i*35+5, j*35+5); } for(i=0;i<12;i++) for(j=0;j<8;j++) if(board[i][j] != -1) XCopyArea(display, tiles[pouch[board[i][j]][0]*6 + pouch[ board[i][j] ] [1] ], window,mygc,0,0,34,34, 35*i+5+1, 35*j+5+1); XCopyArea(display, tiles[pouch[number][0]*6 + pouch[number][1]], window, mygc, 0, 0, 34, 34, 12*35+10+38, 65); write_score(); } init(int argc, char *argv[]) { int i; int done; /* initialisation */ score = 0; for(i=0;i<6;i++) buttonstate[i] = 0; buttonstate[2] = 0; /* set to 1 for screenshot only! */ mydisplay = XOpenDisplay(displayname); if(!mydisplay) { printf("Cannot open display\n"); return(0); } myscreen = DefaultScreen(mydisplay); /* default pixel values */ mybackground = WhitePixel(mydisplay, myscreen); myforeground = BlackPixel(mydisplay, myscreen); /* default program-specified window position and size */ myhint.x = 200; myhint.y = 300; myhint.width = winwidth; myhint.height = winheight; myhint.min_width = winwidth; myhint.min_height = winheight; myhint.max_width = winwidth; myhint.max_height = winheight; myhint.flags = PPosition | PSize | PMaxSize | PMinSize; /* window creation */ mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay), myhint.x, myhint.y, myhint.width, myhint.height, 5, myforeground, mybackground); XSetStandardProperties (mydisplay, mywindow, title, title, None, argv, argc, &myhint); /* if needed, install a private colormap */ if( (argc > 1 && !strcmp(argv[1],"-private") ) || (argc > 3 && !strcmp(argv[3],"-private") ) ) { cmap = XCreateColormap(mydisplay, mywindow, DefaultVisual(mydisplay,DefaultScreen(mydisplay)), AllocNone); XSetWindowColormap(mydisplay, mywindow, cmap); att.colormap = cmap; att.valuemask = XpmColormap ; } /* GC creation and initialisation */ mygc = XCreateGC(mydisplay, mywindow, 0, 0); XSetBackground(mydisplay, mygc, mybackground); XSetForeground(mydisplay, mygc, myforeground); /* Create Pixmap for xpm */ /* set attribute to the rightcolormap */ XpmCreatePixmapFromData(mydisplay, mywindow, tile1, &tiles[0], &tilesmask[0], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tile2, &tiles[1], &tilesmask[1], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tile3, &tiles[2], &tilesmask[2], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tile4, &tiles[3], &tilesmask[3], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tile5, &tiles[4], &tilesmask[4], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tile6, &tiles[5], &tilesmask[5], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles21, &tiles[6], &tilesmask[6], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles22, &tiles[7], &tilesmask[7], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles23, &tiles[8], &tilesmask[8], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles24, &tiles[9], &tilesmask[9], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles25, &tiles[10], &tilesmask[10], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles26, &tiles[11], &tilesmask[11], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles31, &tiles[12], &tilesmask[12], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles32, &tiles[13], &tilesmask[13], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles33, &tiles[14], &tilesmask[14], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles34, &tiles[15], &tilesmask[15], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles35, &tiles[16], &tilesmask[16], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles36, &tiles[17], &tilesmask[17], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles41, &tiles[18], &tilesmask[18], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles42, &tiles[19], &tilesmask[19], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles43, &tiles[20], &tilesmask[20], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles44, &tiles[21], &tilesmask[21], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles45, &tiles[22], &tilesmask[22], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles46, &tiles[23], &tilesmask[23], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles51, &tiles[24], &tilesmask[24], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles52, &tiles[25], &tilesmask[25], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles53, &tiles[26], &tilesmask[26], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles54, &tiles[27], &tilesmask[27], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles55, &tiles[28], &tilesmask[28], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles56, &tiles[29], &tilesmask[29], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles61, &tiles[30], &tilesmask[30], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles62, &tiles[31], &tilesmask[31], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles63, &tiles[32], &tilesmask[32], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles64, &tiles[33], &tilesmask[33], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles65, &tiles[34], &tilesmask[34], &att); XpmCreatePixmapFromData(mydisplay, mywindow, tiles66, &tiles[35], &tilesmask[35], &att); XpmCreatePixmapFromData(mydisplay, mywindow, boardtile, &tilepix, &tileshapemask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, box, &boxpix, &boxshapemask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, back, &backpix, &backshapemask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, credits, &titlepix, &titlemask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, box2, &boxpix2, &box2shapemask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, quit_up, &quitup, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, quit_down, &quitdown, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, help_up, &helpup, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, help_down, &helpdown, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules_up, &rulesup, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules_down, &rulesdown, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, replay_up, &replayup, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, replay_down, &replaydown, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, sound_up, &soundup, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, sound_down, &sounddown, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, about_up, &aboutup, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, about_down, &aboutdown, &buttondownmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules1, &rulesa, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules2, &rulesb, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules3, &rulesc, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules4, &rulesd, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules5, &rulese, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules6, &rulesf, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, hint, &hintdialog, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, rules1, &page, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, pffcredit, &pff, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, pffcredit2, &pff2, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, quit, &quitbox, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, nomoves, &nomove, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, replay, &replaydialog, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, final, &finaldialog, &rulesmask, &att); XpmCreatePixmapFromData(mydisplay, mywindow, flesh, &flash, &rulesmask, &att); /* input event selection */ XSelectInput(mydisplay, mywindow, ButtonPressMask | KeyPressMask | ExposureMask); /* set cursor */ XDefineCursor(mydisplay, mywindow, XCreateFontCursor(mydisplay, XC_draft_large)); /* window mapping */ XMapRaised(mydisplay, mywindow); } int inbox(XEvent myevent, int x1, int y1, int x2, int y2) { if((myevent.xbutton.x > x1) && (myevent.xbutton.x < x2) && (myevent.xbutton.y > y1) && (myevent.xbutton.y < y2) ) return(1); else return(0); } int valid_move(int x, int y) { int neigh, shape, colour, allmatch = 1, smatch, cmatch; if(board[x][y] != -1) return(0); /* count neighbours and colour/shape matches */ neigh = 0; shape = 0; colour = 0; if(x+1 < 12) if(board[x+1][y] != -1) { neigh++; shape += (smatch = (pouch[board[x+1][y]][0] == pouch[number][0])); colour += (cmatch = (pouch[board[x+1][y]][1] == pouch[number][1])); allmatch *= smatch || cmatch; } if(y+1 < 8) if(board[x][y+1] != -1) { neigh++; shape += (smatch = (pouch[board[x][y+1]][0] == pouch[number][0])); colour += (cmatch = (pouch[board[x][y+1]][1] == pouch[number][1])); allmatch *= smatch || cmatch; } if(x-1 >= 0) if(board[x-1][y] != -1) { neigh++; shape += (smatch = (pouch[board[x-1][y]][0] == pouch[number][0])); colour += (cmatch = (pouch[board[x-1][y]][1] == pouch[number][1])); allmatch *= smatch || cmatch; } if(y-1 >= 0) if(board[x][y-1] != -1) { neigh++; shape += (smatch = (pouch[board[x][y-1]][0] == pouch[number][0])); colour += (cmatch = (pouch[board[x][y-1]][1] == pouch[number][1])); allmatch *= smatch || cmatch; } if(neigh == 0) return(0); if(neigh == 1) if(shape == 1 || colour == 1) { score++; return(1); } else return(0); if(neigh == 2) if(shape != 0 && colour !=0 && allmatch) { score += 5; return(1); } else return(0); if(neigh == 3) if(shape*colour >= 2 && allmatch) { score += 20; return(1); } else return(0); if(neigh == 4) if(shape >= 2 && colour >=2 && allmatch) { score += 100; return(1); } else return(0); } write_score() { char sc[25]; sprintf(sc, "SCORE : %d,%d", score,number); XCopyArea(mydisplay, boxpix, mywindow, mygc, 0, 0, 99, 24, 12*35+10+6, 6); XDrawString(mydisplay, mywindow, mygc, 12*35+10+10, 22,sc, strlen(sc)); } int no_more_moves() { int a = 0, sco = score,i,j; for(i=0;i<12;i++) for(j=0;j<8;j++) a = a || valid_move(i,j); score = sco; return(!a); } wait_mouse(Pixmap pix) { int d = 1; while(d) { XNextEvent(mydisplay, &myevent); switch(myevent.type) { case Expose: if(myevent.xexpose.count == 0) { draw_board(myevent.xexpose.display, myevent.xexpose.window); XCopyArea(mydisplay, pix, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); } break; case ButtonPress: d = 0; break; } } } wait_help() { int d = 1; while(d) { XNextEvent(mydisplay, &myevent); switch(myevent.type) { case Expose: if(myevent.xexpose.count == 0) { draw_board(myevent.xexpose.display, myevent.xexpose.window); } break; case ButtonPress: d = 0; break; } } } do_flash(int i,int j) { XCopyArea(mydisplay,flash,mywindow,mygc, 0,0,33,33,i*35+5+1,j*35+5+1); wait_help(); XCopyArea(mydisplay, tilepix,mywindow,mygc, 0,0,34,34,i*35+5,j*35+5); } do_hint() { int sco,i,j,d=1,yes=1; /* first save background */ XCopyArea(mydisplay,mywindow,page,mygc, 6*35-100+5,4*35-100+5, 200,200, 0,0 ); /* now copy dialog */ XCopyArea(mydisplay, hintdialog, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ while(d) { XNextEvent(mydisplay, &myevent); switch(myevent.type) { case Expose: if(myevent.xexpose.count == 0) { draw_board(myevent.xexpose.display, myevent.xexpose.window); } break; case ButtonPress: if(inbox(myevent, 143, 184, 200, 208)) { d=0; yes=1; } if(inbox(myevent, 224, 184, 285, 208)) { d=0; yes=0; } break; } } /* now restore board */ XCopyArea(mydisplay,page,mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); if(yes) { sco=score; for(i=0;i<12;i++) for(j=0;j<8;j++) if(valid_move(i,j)) do_flash(i,j); score=sco; } } do_rules() { /* first save background */ XCopyArea(mydisplay,mywindow,page,mygc, 6*35-100+5,4*35-100+5, 200,200, 0,0 ); /* now copy rules page 1 */ XCopyArea(mydisplay, rulesa, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(rulesa); XCopyArea(mydisplay, rulesb, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(rulesb); XCopyArea(mydisplay, rulesc, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(rulesc); XCopyArea(mydisplay, rulesd, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(rulesd); XCopyArea(mydisplay, rulese, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(rulese); XCopyArea(mydisplay, rulesf, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(rulesf); /* now restore background */ XCopyArea(mydisplay,page,mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* return */ } do_credit() { /* first save background */ XCopyArea(mydisplay,mywindow,page,mygc, 6*35-100+5,4*35-100+5, 200,200, 0,0 ); /* now page 1 */ XCopyArea(mydisplay, pff, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(pff); XCopyArea(mydisplay, pff2, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ wait_mouse(pff2); /* now restore background */ XCopyArea(mydisplay,page,mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* return */ } do_replay() { int r,d= 1; /* first save background */ XCopyArea(mydisplay,mywindow,page,mygc, 6*35-100+5,4*35-100+5, 200,200, 0,0 ); /* now copy dialog */ XCopyArea(mydisplay, replaydialog, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ /* and test whether a box is selected */ while(d) { XNextEvent(mydisplay, &myevent); switch(myevent.type) { case Expose: if(myevent.xexpose.count == 0) { draw_board(myevent.xexpose.display, myevent.xexpose.window); XCopyArea(mydisplay, replaydialog, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); } break; case ButtonPress: if(inbox(myevent, 161,142,266,199)) if(105 - (myevent.xbutton.x-161) > 2*(myevent.xbutton.y-142)) { r = 1; d = 0; } else { r = 0; d = 0; } break; } } /* now restore background */ XCopyArea(mydisplay,page,mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* return */ return(r); } do_end() { int r,d= 1; /* first save background */ XCopyArea(mydisplay,mywindow,page,mygc, 6*35-100+5,4*35-100+5, 200,200, 0,0 ); /* now copy dialog */ XCopyArea(mydisplay, nomove, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ /* and test whether a box is selected */ while(d) { XNextEvent(mydisplay, &myevent); switch(myevent.type) { case Expose: if(myevent.xexpose.count == 0) { draw_board(myevent.xexpose.display, myevent.xexpose.window); XCopyArea(mydisplay, nomove, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); } break; case ButtonPress: if(inbox(myevent, 175,135,253,154)) /* Replay */ { r=2; d=0; } if(inbox(myevent, 175, 168, 253, 188)) /* New Game */ { r=1; d=0; } if(inbox(myevent, 175, 203, 253, 223)) /* Quit */ { r=0; d=0; } break; } } /* now restore background */ XCopyArea(mydisplay,page,mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* return */ return(r); } do_quit() { int r,d= 1; /* first save background */ XCopyArea(mydisplay,mywindow,page,mygc, 6*35-100+5,4*35-100+5, 200,200, 0,0 ); /* now copy dialog */ XCopyArea(mydisplay, quitbox, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* now wait on a single mouseclick */ /* and test whether a box is selected */ while(d) { XNextEvent(mydisplay, &myevent); switch(myevent.type) { case Expose: if(myevent.xexpose.count == 0) { draw_board(myevent.xexpose.display, myevent.xexpose.window); XCopyArea(mydisplay, quitbox, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); } break; case ButtonPress: if(inbox(myevent, 138,172,200,190)) { r=1; d=0; } if(inbox(myevent,228,172,290,190)) { r=0; d=0; } break; } } /* now restore background */ XCopyArea(mydisplay,page,mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* return */ return(r); } do_final() { /* first save background */ XCopyArea(mydisplay,mywindow,page,mygc, 6*35-100+5,4*35-100+5, 200,200, 0,0 ); /* now copy dialog */ XCopyArea(mydisplay, finaldialog, mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); wait_mouse(finaldialog); XCopyArea(mydisplay,page,mywindow,mygc, 0,0,200,200, 6*35-100+5,4*35-100+5); /* return */ return(do_replay()); } /* handle_mouse return value is 'done' in run_it loop */ int handle_mouse(XEvent myevent) { int x,y; int i,ii,jj; int q; if(inbox(myevent, 12*35+15, 110, 12*35+15+33, 110+30)) { if(buttonstate[0] == 0) { XCopyArea(myevent.xbutton.display, quitdown, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15, 110); buttonstate[0] = 1; q=do_quit(); buttonstate[0] = 0; XCopyArea(myevent.xbutton.display, quitup, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15, 110); return(q); } } if(inbox(myevent, 12*35+15+33, 110, 12*35+15+2*33, 110+30)) { if(buttonstate[1] == 0) { XCopyArea(myevent.xbutton.display, helpdown, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+33, 110); buttonstate[1] = 1; do_hint(); buttonstate[1] = 0; XCopyArea(myevent.xbutton.display, helpup, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+33, 110); } } if(inbox(myevent, 12*35+15+2*33, 110, 12*35+15+3*33, 110+30)) { if(buttonstate[2] == 0) { XCopyArea(myevent.xbutton.display, rulesdown, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+2*33, 110); buttonstate[2] = 1; do_rules(); buttonstate[2] = 0; XCopyArea(myevent.xbutton.display, rulesup, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+2*33, 110); } } if(inbox(myevent, 12*35+15, 110+30, 12*35+15+33, 110+60)) { if(buttonstate[3] == 0) { XCopyArea(myevent.xbutton.display, replaydown, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15, 110+30); buttonstate[3] = 1; if(do_replay()) { for(ii=0;ii<12;ii++) for(jj=0;jj<8;jj++) board[ii][jj] = -1; board[0][0] = 0; board[11][0] = 1; board[11][7] = 2; board[0][7] = 3; board[5][3] = 4; board[6][4] = 5; number = 6; score = 0; draw_board(mydisplay,mywindow); } buttonstate[3] = 0; XCopyArea(myevent.xbutton.display, replayup, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15, 110+30); } } if(inbox(myevent, 12*35+15+33, 110+30, 12*35+15+2*33, 110+60)) { if(buttonstate[4] == 0) { XCopyArea(myevent.xbutton.display, sounddown, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+33, 110+30); buttonstate[4] = 1; } else { /* sound not supported in this version */ buttonstate[4] = 0; XCopyArea(myevent.xbutton.display, sounddown, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+33, 110+30); } } if(inbox(myevent, 12*35+15+2*33, 110+30, 12*35+15+3*33, 110+60)) { if(buttonstate[5] == 0) { XCopyArea(myevent.xbutton.display, aboutdown, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+2*33, 110+30); buttonstate[5] = 1; do_credit(); buttonstate[5] = 0; XCopyArea(myevent.xbutton.display, aboutup, myevent.xbutton.window, mygc, 0, 0, 33, 30, 12*35+15+2*33, 110+30); } } if(inbox(myevent, 5,5,12*35+5,8*35+5)) { /* make a move */ x = (myevent.xbutton.x - 5)/35; y = (myevent.xbutton.y - 5)/35; if(valid_move(x,y)) { XCopyArea(myevent.xbutton.display, tiles[pouch[number][0]*6+pouch[number][1]], myevent.xbutton.window, mygc, 0, 0, 34, 34, 35*x+5+1, 35*y+5+1); /* uncomment the following line for debugging */ /* printf("%d %d\n", pouch[number][0], pouch[number][1]); */ board[x][y] = number++; if(number<72) XCopyArea(myevent.xbutton.display, tiles[pouch[number][0]*6 + pouch[number][1]], myevent.xbutton.window, mygc, 0, 0, 34, 34, 35*12+10+38, 65); write_score(); if (no_more_moves()) return(1); } } return(0); } run_it() { int done,i,r=0, reallydone,ii,jj,won=0; char text[10]; /* main event-reading loop */ while(no_more_moves()) { make_pouch(); } draw_board(mydisplay,mywindow); done = 0; reallydone = 0; while(reallydone == 0) { while (done == 0 && !won) { /* read the next event */ XNextEvent(mydisplay, &myevent); switch(myevent.type) { /* repaint window on expose events */ case Expose: if(myevent.xexpose.count == 0) draw_board(myevent.xexpose.display, myevent.xexpose.window); break; /* process keyboard mapping changes */ case MappingNotify: XRefreshKeyboardMapping(&myevent); break; /* process mouse-button presses */ case ButtonPress: done = handle_mouse(myevent); break; } /* switch */ if(number == 72) { score += 250; write_score(); if(do_final()) { for(ii=0;ii<12;ii++) for(jj=0;jj<8;jj++) board[ii][jj] = -1; board[0][0] = 0; board[11][0] = 1; board[11][7] = 2; board[0][7] = 3; board[5][3] = 4; board[6][4] = 5; number = 6; score = 0; draw_board(mydisplay,mywindow); } else { won = 1; r = 1; } } } /* while */ /* termination ?*/ reallydone = 1; if(!won && no_more_moves()) { r=do_end(); if(r == 2) /* Replay */ { for(ii=0;ii<12;ii++) for(jj=0;jj<8;jj++) board[ii][jj] = -1; board[0][0] = 0; board[11][0] = 1; board[11][7] = 2; board[0][7] = 3; board[5][3] = 4; board[6][4] = 5; number = 6; score = 0; draw_board(mydisplay,mywindow); done = 0; reallydone = 0; } } } XFreeGC(mydisplay, mygc); XDestroyWindow(mydisplay, mywindow); XCloseDisplay(mydisplay); return(r); } parse(int argc, char *argv[]) { displayname = '\0'; if(argc > 2) { if(!strcmp(argv[1], "-display")) displayname = argv[2]; else if(argc>3 && !strcmp(argv[2],"-display")) displayname = argv[3]; else printf("Option not supported\n"); } } main(int argc, char *argv[]) { int i=1; parse(argc,argv); while(i) { make_pouch(); init_board(); i = init(argc,argv); if(i) i=run_it(); } }