added quick&dirty game
[wmaker-crm.git] / WINGs / puzzle.c
blob1d059f24ea4337b44a35c421055dba29ec5629e8
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <WINGs.h>
10 #define MAX_SIZE 10*10
13 WMWindow *win;
14 WMButton *Button[MAX_SIZE];
15 char Map[MAX_SIZE];
16 int Size = 4;
17 int MoveCount;
19 #define MAP(x,y) Map[(x)+(y)*Size]
21 int WinSize = 120;
24 Bool CheckWin(void)
26 int i;
28 for (i = 0; i < Size*Size-1; i++) {
29 if (Map[i] != i)
30 return False;
33 return True;
37 void MoveButton(int button, int x, int y)
39 WMMoveWidget(Button[button], x*(WinSize/Size), y*(WinSize/Size));
43 Bool SlideButton(int button)
45 int x, y, done = 0;
47 /* locate the button */
48 for (y = 0; y < Size; y++) {
49 for (x = 0; x < Size; x++) {
50 if (MAP(x,y) == button) {
51 done = 1;
52 break;
55 if (done)
56 break;
59 if (x > 0 && MAP(x-1, y) < 0) {
60 MAP(x,y) = -1;
61 MoveButton(button, x-1, y);
62 MAP(x-1,y) = button;
63 } else if (x < Size-1 && MAP(x+1, y) < 0) {
64 MAP(x,y) = -1;
65 MoveButton(button, x+1, y);
66 MAP(x+1,y) = button;
67 } else if (y > 0 && MAP(x, y-1) < 0) {
68 MAP(x,y) = -1;
69 MoveButton(button, x, y-1);
70 MAP(x,y-1) = button;
71 } else if (y < Size-1 && MAP(x, y+1) < 0) {
72 MAP(x,y) = -1;
73 MoveButton(button, x, y+1);
74 MAP(x,y+1) = button;
75 } else {
76 return False;
78 return True;
82 void ResetGame(void)
84 int i, x, y;
86 MoveCount = 0;
88 memset(Map, -1, Size*Size);
90 for (i = 0; i < Size*Size-1; i++) {
91 while (1) {
92 int pos = rand()%(Size*Size);
93 if (Map[pos] < 0) {
94 Map[pos] = i;
95 break;
99 for (y = 0; y < Size; y++) {
100 for (x = 0; x < Size; x++) {
101 if (MAP(x,y) >= 0) {
102 MoveButton(MAP(x,y), x, y);
110 void buttonClick(WMWidget *w, void *ptr)
112 char buffer[300];
114 if (SlideButton((int)ptr)) {
115 MoveCount++;
117 if (CheckWin()) {
118 sprintf(buffer, "You finished the game in %i moves.", MoveCount);
120 if (WMRunAlertPanel(WMWidgetScreen(w), win, "You Won!", buffer,
121 "Wee!", "Gah! Lemme retry!", NULL) == WAPRDefault) {
122 exit(0);
125 ResetGame();
131 int main(int argc, char **argv)
133 Display *dpy;
134 WMScreen *scr;
135 int x, y, i;
137 WMInitializeApplication("Puzzle", &argc, argv);
140 dpy = XOpenDisplay("");
141 if (!dpy) {
142 printf("could not open display\n");
143 exit(1);
146 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
148 win = WMCreateWindow(scr, "puzzle");
149 WMResizeWidget(win, WinSize, WinSize);
150 WMSetWindowTitle(win, "zuPzel");
152 for (i = y = 0; y < Size && i < Size*Size-1; y++) {
153 for (x = 0; x < Size && i < Size*Size-1; x++) {
154 char buf[32];
155 WMColor *color;
156 RColor col;
157 RHSVColor hsv;
159 hsv.hue = i*360/(Size*Size-1);
160 hsv.saturation = 100;
161 hsv.value = 180;
163 RHSVtoRGB(&hsv, &col);
165 color = WMCreateRGBColor(scr, col.red<<8, col.green<<8,
166 col.blue<<8, False);
168 MAP(x,y) = i;
169 Button[i] = WMCreateButton(win, WBTMomentaryLight);
170 WMSetWidgetBackgroundColor(Button[i], color);
171 WMReleaseColor(color);
172 WMSetButtonAction(Button[i], buttonClick, (void*)i);
173 WMResizeWidget(Button[i], WinSize/Size, WinSize/Size);
174 WMMoveWidget(Button[i], x*(WinSize/Size), y*(WinSize/Size));
175 sprintf(buf, "%i", i+1);
176 WMSetButtonText(Button[i], buf);
177 WMSetButtonTextAlignment(Button[i], WACenter);
178 i++;
182 WMMapSubwidgets(win);
183 WMMapWidget(win);
184 WMRealizeWidget(win);
186 ResetGame();
188 WMScreenMainLoop(scr);
190 return 0;