10 #define MAX_SIZE 10*10
14 WMButton
*Button
[MAX_SIZE
];
19 #define MAP(x,y) Map[(x)+(y)*Size]
28 for (i
= 0; i
< Size
*Size
-1; i
++) {
37 void MoveButton(int button
, int x
, int y
)
39 WMMoveWidget(Button
[button
], x
*(WinSize
/Size
), y
*(WinSize
/Size
));
43 Bool
SlideButton(int button
)
47 /* locate the button */
48 for (y
= 0; y
< Size
; y
++) {
49 for (x
= 0; x
< Size
; x
++) {
50 if (MAP(x
,y
) == button
) {
59 if (x
> 0 && MAP(x
-1, y
) < 0) {
61 MoveButton(button
, x
-1, y
);
63 } else if (x
< Size
-1 && MAP(x
+1, y
) < 0) {
65 MoveButton(button
, x
+1, y
);
67 } else if (y
> 0 && MAP(x
, y
-1) < 0) {
69 MoveButton(button
, x
, y
-1);
71 } else if (y
< Size
-1 && MAP(x
, y
+1) < 0) {
73 MoveButton(button
, x
, y
+1);
82 #define SWAP(a,b) {int tmp; tmp=a; a=b; b=tmp;}
91 for (i
= 0; i
< Size
*Size
-1; i
++) {
97 for (i
= 0; i
< 1000; i
++) {
102 if (x
> 0) x
--; else ok
= 0;
105 if (x
< Size
-1) x
++; else ok
= 0;
108 if (y
> 0) y
--; else ok
= 0;
111 if (y
< Size
-1) y
++; else ok
= 0;
115 MoveButton(MAP(x
,y
), ox
, oy
);
117 SWAP(MAP(ox
, oy
), MAP(x
, y
));
119 while (XPending(WMScreenDisplay(WMWidgetScreen(win
)))) {
121 WMNextEvent(WMScreenDisplay(WMWidgetScreen(win
)), &ev
);
131 void buttonClick(WMWidget
*w
, void *ptr
)
135 if (SlideButton((int)ptr
)) {
139 sprintf(buffer
, "You finished the game in %i moves.", MoveCount
);
141 if (WMRunAlertPanel(WMWidgetScreen(w
), win
, "You Won!", buffer
,
142 "Wee!", "Gah! Lemme retry!", NULL
) == WAPRDefault
) {
152 static void resizeObserver(void *self
, WMNotification
*notif
)
154 WMSize size
= WMGetViewSize(WMWidgetView(win
));
157 WinSize
= size
.width
;
158 for (y
= 0; y
< Size
; y
++) {
159 for (x
= 0; x
< Size
; x
++) {
161 WMResizeWidget(Button
[(int)MAP(x
,y
)],
162 WinSize
/Size
, WinSize
/Size
);
163 WMMoveWidget(Button
[(int)MAP(x
,y
)],
164 x
*(WinSize
/Size
), y
*(WinSize
/Size
));
172 int main(int argc
, char **argv
)
178 WMInitializeApplication("Puzzle", &argc
, argv
);
181 dpy
= XOpenDisplay("");
183 printf("could not open display\n");
187 scr
= WMCreateScreen(dpy
, DefaultScreen(dpy
));
189 win
= WMCreateWindow(scr
, "puzzle");
190 WMResizeWidget(win
, WinSize
, WinSize
);
191 WMSetWindowTitle(win
, "zuPzel");
192 WMSetWindowMinSize(win
, 80, 80);
193 WMSetWindowAspectRatio(win
, 2, 2, 2, 2);
194 WMSetWindowResizeIncrements(win
, Size
, Size
);
195 WMSetViewNotifySizeChanges(WMWidgetView(win
), True
);
196 WMAddNotificationObserver(resizeObserver
, NULL
,
197 WMViewSizeDidChangeNotification
,
202 for (i
= y
= 0; y
< Size
&& i
< Size
*Size
-1; y
++) {
203 for (x
= 0; x
< Size
&& i
< Size
*Size
-1; x
++) {
209 hsv
.hue
= i
*360/(Size
*Size
-1);
210 hsv
.saturation
= 120;
213 RHSVtoRGB(&hsv
, &col
);
215 color
= WMCreateRGBColor(scr
, col
.red
<<8, col
.green
<<8,
219 Button
[i
] = WMCreateButton(win
, WBTMomentaryLight
);
220 WMSetWidgetBackgroundColor(Button
[i
], color
);
221 WMReleaseColor(color
);
222 WMSetButtonAction(Button
[i
], buttonClick
, (void*)i
);
223 WMResizeWidget(Button
[i
], WinSize
/Size
, WinSize
/Size
);
224 WMMoveWidget(Button
[i
], x
*(WinSize
/Size
), y
*(WinSize
/Size
));
225 sprintf(buf
, "%i", i
+1);
226 WMSetButtonText(Button
[i
], buf
);
227 WMSetButtonTextAlignment(Button
[i
], WACenter
);
232 WMMapSubwidgets(win
);
234 WMRealizeWidget(win
);
238 WMScreenMainLoop(scr
);