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);
88 memset(Map
, -1, Size
*Size
);
90 for (i
= 0; i
< Size
*Size
-1; i
++) {
92 int pos
= rand()%(Size
*Size
);
99 for (y
= 0; y
< Size
; y
++) {
100 for (x
= 0; x
< Size
; x
++) {
102 MoveButton(MAP(x
,y
), x
, y
);
110 void buttonClick(WMWidget
*w
, void *ptr
)
114 if (SlideButton((int)ptr
)) {
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
) {
131 int main(int argc
, char **argv
)
137 WMInitializeApplication("Puzzle", &argc
, argv
);
140 dpy
= XOpenDisplay("");
142 printf("could not open display\n");
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
++) {
159 hsv
.hue
= i
*360/(Size
*Size
-1);
160 hsv
.saturation
= 100;
163 RHSVtoRGB(&hsv
, &col
);
165 color
= WMCreateRGBColor(scr
, col
.red
<<8, col
.green
<<8,
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
);
182 WMMapSubwidgets(win
);
184 WMRealizeWidget(win
);
188 WMScreenMainLoop(scr
);