ref: 172d631819e492f3392a97ea4e78e0231f475c22
dir: /games/cflood.c/
#include <u.h> #include <libc.h> #include <draw.h> #include <event.h> enum{ /* sid */ Ssmall, Snormal, Slarge, Scustom, /* state */ Tgame, Twin, Tfail, Flood = 1<<7, ColorMask = 0x0f, NumColors = 6, ButtonSize = 32 }; static int sizes[] = { 14, 21, 28, 1 }; static int turns[] = { 25, 35, 50, 1 }; static const ulong srccolors[NumColors] = { 0x6060a8ff, 0xf6f61dff, 0x46b0e0ff, 0x7ea020ff, 0xf070a0ff, 0xdc4a20ff }; static char *mstr[] = { "14x14 / 25", "25x25 / 35", "28x28 / 50", "exit", 0 }; static int inv; static int size; static int sid; static int state; static int clickwait; static int turnsleft; static uchar *cells; static Image *colors[NumColors]; static Rectangle buttons[NumColors]; static void floodneighbours(uchar color, int x, int y); static void redraw(Image *screen) { int i, x, y, w, h, csize, left; Point p, sp, strsize; const uchar *c; Rectangle r; char s[64]; Font *f; Image *colora, *colorb; colora = colorb = display->black; if(inv) colorb = display->white; else colora = display->white; draw(screen, screen->r, colora, nil, ZP); if(state == Tgame) sprint(s, "%d", turnsleft); else if(state == Twin) sprint(s, "You won using %d turns (of %d)", turns[sid]-turnsleft, turns[sid]); else if(state == Tfail) sprint(s, "You failed"); f = display->defaultfont; strsize = stringsize(f, s); w = Dx(screen->r); h = Dy(screen->r) - ButtonSize - 4 - strsize.y; csize = (w < h ? w : h) / size; w = size*csize; left = screen->r.min.x + (Dx(screen->r) - w)/2; /* cells */ c = cells; for(x = 0; x < size; x++){ for(y = 0; y < size; y++){ r.min.x = left + x*csize; r.min.y = screen->r.min.y + 2 + y*csize; r.max.x = r.min.x + csize; r.max.y = r.min.y + csize; draw(screen, r, colors[*c & ColorMask], nil, ZP); c++; } } /* buttons */ x = left + (w - NumColors*ButtonSize)/2; y = screen->r.min.y + h + strsize.y; for(i = 0; i < NumColors; i++, x += ButtonSize){ buttons[i] = Rect(x, y, x+ButtonSize, y+ButtonSize); draw(screen, buttons[i], colors[i], nil, ZP); } /* caption */ p.x = left + w/2 - strsize.x/2; p.y = y - strsize.y; sp.x = sp.y = 0; string(screen, p, colorb, sp, f, s); flushimage(display, 1); } static void floodrecurse(uchar color, int x, int y) { uchar *c; c = &cells[x + y*size]; if((*c & Flood) == 0 && (*c & ColorMask) == color){ *c = color | Flood; floodneighbours(color, x, y); } } static void floodneighbours(uchar color, int x, int y) { cells[x + y*size] = color | Flood; if(x > 0) floodrecurse(color, x-1, y); if(x < size-1) floodrecurse(color, x+1, y); if(y > 0) floodrecurse(color, x, y-1); if(y < size-1) floodrecurse(color, x, y+1); } static int reflood(uchar color) { int n, x, y; color &= ColorMask; n = 0; for(x = 0; x < size; x++){ for(y = 0; y < size; y++){ if(cells[x + y*size] & Flood){ floodneighbours(color, x, y); n++; } } } return n; } static void flood(uchar color) { int n; if((cells[0] & Flood) != 0 && (cells[0] & ColorMask) == color) return; if(!turnsleft) return; turnsleft--; n = reflood(color); if(n == size*size){ state = Twin; clickwait = 1; }else if(!turnsleft){ state = Tfail; clickwait = 1; } redraw(screen); } static void newgame(int sid) { uchar *c; int i, maxsize; state = Tgame; size = sizes[sid]; turnsleft = turns[sid]; clickwait = 0; if(cells == nil){ maxsize = (size > sizes[Slarge]) ? size : sizes[Slarge]; cells = malloc(maxsize*maxsize); } /* randomize */ c = cells; for(i = 0; i < size*size; i++){ *c++ = nrand(NumColors); } cells[0] |= Flood; reflood(cells[0]); redraw(screen); } void eresized(int new) { if(new && getwindow(display, Refnone) < 0) sysfatal("can't reattach to window: %r"); redraw(screen); } static void usage(void) { fprint(2, "usage: cflood [-s size] [-t turns]\n"); exits("usage"); } void main(int argc, char** argv) { int key, p, i, oldbuttons; Event e; Mouse m; Menu menu; Rectangle r; if(initdraw(0, 0, "cflood") < 0) sysfatal("initdraw failed"); r = Rect(0, 0, 1, 1); for(i = 0; i < NumColors; i++) colors[i] = allocimage(display, r, CMAP8, 1, srccolors[i]); einit(Emouse); menu.item = mstr; menu.lasthit = 0; srand(time(0)); sid = Ssmall; inv = 0; ARGBEGIN{ case 'b': inv = 1; break; case 's': sid = Scustom; sizes[sid] = atoi(ARGF()); if(sizes[sid] < 1) usage(); break; case 't': sid = Scustom; turns[sid] = atoi(ARGF()); if(turns[sid] < 1) usage(); break; default: usage(); }ARGEND newgame(sid); for(oldbuttons = 0;;){ key = event(&e); m = e.mouse; if(key != Emouse) continue; if(m.buttons & 4){ p = emenuhit(3, &m, &menu); if(p >= Ssmall && p <= Slarge) newgame(sid = p); else if(p > 0) break; }else if(clickwait && !oldbuttons && m.buttons){ newgame(sid); }else if(m.buttons & 1){ for(i = 0; i < NumColors; i++){ if(ptinrect(m.xy, buttons[i])){ flood(i); break; } } } oldbuttons = m.buttons; } exits(nil); }