mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-21 05:18:06 +01:00
added quick&dirty game
This commit is contained in:
@@ -20,12 +20,17 @@ include_HEADERS = WINGs.h WUtil.h WINGsP.h
|
||||
|
||||
bin_SCRIPTS = get-wings-flags get-wutil-flags
|
||||
|
||||
noinst_PROGRAMS = wtest wmquery wmfile fontl testmywidget testcolorpanel connect
|
||||
noinst_PROGRAMS = wtest wmquery wmfile fontl testmywidget testcolorpanel\
|
||||
connect puzzle
|
||||
|
||||
testmywidget_SOURCES = testmywidget.c mywidget.c mywidget.h
|
||||
|
||||
testmywidget_LDADD = libWINGs.a $(LIBLIST)
|
||||
|
||||
puzzle_SOURCES = puzzle.c
|
||||
|
||||
puzzle_LDADD = libWINGs.a $(LIBLIST)
|
||||
|
||||
fontl_SOURCES = fontl.c
|
||||
|
||||
fontl_LDADD = libWINGs.a $(LIBLIST)
|
||||
|
||||
@@ -62,7 +62,6 @@ host_triplet = @host@
|
||||
AS = @AS@
|
||||
CC = @CC@
|
||||
CPP_PATH = @CPP_PATH@
|
||||
DFLAGS = @DFLAGS@
|
||||
DLLIBS = @DLLIBS@
|
||||
DLLTOOL = @DLLTOOL@
|
||||
GFXLIBS = @GFXLIBS@
|
||||
|
||||
191
WINGs/puzzle.c
Normal file
191
WINGs/puzzle.c
Normal file
@@ -0,0 +1,191 @@
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <WINGs.h>
|
||||
|
||||
|
||||
|
||||
#define MAX_SIZE 10*10
|
||||
|
||||
|
||||
WMWindow *win;
|
||||
WMButton *Button[MAX_SIZE];
|
||||
char Map[MAX_SIZE];
|
||||
int Size = 4;
|
||||
int MoveCount;
|
||||
|
||||
#define MAP(x,y) Map[(x)+(y)*Size]
|
||||
|
||||
int WinSize = 120;
|
||||
|
||||
|
||||
Bool CheckWin(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < Size*Size-1; i++) {
|
||||
if (Map[i] != i)
|
||||
return False;
|
||||
}
|
||||
|
||||
return True;
|
||||
}
|
||||
|
||||
|
||||
void MoveButton(int button, int x, int y)
|
||||
{
|
||||
WMMoveWidget(Button[button], x*(WinSize/Size), y*(WinSize/Size));
|
||||
}
|
||||
|
||||
|
||||
Bool SlideButton(int button)
|
||||
{
|
||||
int x, y, done = 0;
|
||||
|
||||
/* locate the button */
|
||||
for (y = 0; y < Size; y++) {
|
||||
for (x = 0; x < Size; x++) {
|
||||
if (MAP(x,y) == button) {
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (done)
|
||||
break;
|
||||
}
|
||||
|
||||
if (x > 0 && MAP(x-1, y) < 0) {
|
||||
MAP(x,y) = -1;
|
||||
MoveButton(button, x-1, y);
|
||||
MAP(x-1,y) = button;
|
||||
} else if (x < Size-1 && MAP(x+1, y) < 0) {
|
||||
MAP(x,y) = -1;
|
||||
MoveButton(button, x+1, y);
|
||||
MAP(x+1,y) = button;
|
||||
} else if (y > 0 && MAP(x, y-1) < 0) {
|
||||
MAP(x,y) = -1;
|
||||
MoveButton(button, x, y-1);
|
||||
MAP(x,y-1) = button;
|
||||
} else if (y < Size-1 && MAP(x, y+1) < 0) {
|
||||
MAP(x,y) = -1;
|
||||
MoveButton(button, x, y+1);
|
||||
MAP(x,y+1) = button;
|
||||
} else {
|
||||
return False;
|
||||
}
|
||||
return True;
|
||||
}
|
||||
|
||||
|
||||
void ResetGame(void)
|
||||
{
|
||||
int i, x, y;
|
||||
|
||||
MoveCount = 0;
|
||||
|
||||
memset(Map, -1, Size*Size);
|
||||
|
||||
for (i = 0; i < Size*Size-1; i++) {
|
||||
while (1) {
|
||||
int pos = rand()%(Size*Size);
|
||||
if (Map[pos] < 0) {
|
||||
Map[pos] = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (y = 0; y < Size; y++) {
|
||||
for (x = 0; x < Size; x++) {
|
||||
if (MAP(x,y) >= 0) {
|
||||
MoveButton(MAP(x,y), x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void buttonClick(WMWidget *w, void *ptr)
|
||||
{
|
||||
char buffer[300];
|
||||
|
||||
if (SlideButton((int)ptr)) {
|
||||
MoveCount++;
|
||||
|
||||
if (CheckWin()) {
|
||||
sprintf(buffer, "You finished the game in %i moves.", MoveCount);
|
||||
|
||||
if (WMRunAlertPanel(WMWidgetScreen(w), win, "You Won!", buffer,
|
||||
"Wee!", "Gah! Lemme retry!", NULL) == WAPRDefault) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
ResetGame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Display *dpy;
|
||||
WMScreen *scr;
|
||||
int x, y, i;
|
||||
|
||||
WMInitializeApplication("Puzzle", &argc, argv);
|
||||
|
||||
|
||||
dpy = XOpenDisplay("");
|
||||
if (!dpy) {
|
||||
printf("could not open display\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
scr = WMCreateScreen(dpy, DefaultScreen(dpy));
|
||||
|
||||
win = WMCreateWindow(scr, "puzzle");
|
||||
WMResizeWidget(win, WinSize, WinSize);
|
||||
WMSetWindowTitle(win, "zuPzel");
|
||||
|
||||
for (i = y = 0; y < Size && i < Size*Size-1; y++) {
|
||||
for (x = 0; x < Size && i < Size*Size-1; x++) {
|
||||
char buf[32];
|
||||
WMColor *color;
|
||||
RColor col;
|
||||
RHSVColor hsv;
|
||||
|
||||
hsv.hue = i*360/(Size*Size-1);
|
||||
hsv.saturation = 100;
|
||||
hsv.value = 180;
|
||||
|
||||
RHSVtoRGB(&hsv, &col);
|
||||
|
||||
color = WMCreateRGBColor(scr, col.red<<8, col.green<<8,
|
||||
col.blue<<8, False);
|
||||
|
||||
MAP(x,y) = i;
|
||||
Button[i] = WMCreateButton(win, WBTMomentaryLight);
|
||||
WMSetWidgetBackgroundColor(Button[i], color);
|
||||
WMReleaseColor(color);
|
||||
WMSetButtonAction(Button[i], buttonClick, (void*)i);
|
||||
WMResizeWidget(Button[i], WinSize/Size, WinSize/Size);
|
||||
WMMoveWidget(Button[i], x*(WinSize/Size), y*(WinSize/Size));
|
||||
sprintf(buf, "%i", i+1);
|
||||
WMSetButtonText(Button[i], buf);
|
||||
WMSetButtonTextAlignment(Button[i], WACenter);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
WMMapSubwidgets(win);
|
||||
WMMapWidget(win);
|
||||
WMRealizeWidget(win);
|
||||
|
||||
ResetGame();
|
||||
|
||||
WMScreenMainLoop(scr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -898,9 +898,11 @@ makeDraggableLabel(WMWidget *w, char *file, int x, int y)
|
||||
iconMouseStuff, label);
|
||||
|
||||
|
||||
if (image != NULL) {
|
||||
WMSetLabelImagePosition(label, WIPImageOnly);
|
||||
WMSetLabelImage(label, image);
|
||||
WMReleasePixmap(image);
|
||||
} else puts(file);
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user