| @@ -1,205 +1,205 @@ |
| 1 | | −#include <u.h> |
| 2 | | −#include <libc.h> |
| 3 | | −#include <draw.h> |
| 4 | | −#include <event.h> |
| 5 | | − |
| 6 | | −enum { |
| 7 | | − Ssmall, |
| 8 | | − Snormal, |
| 9 | | − Slarge, |
| 10 | | − |
| 11 | | − Flood = 1<<7, |
| 12 | | − ColorMask = 0x0f, |
| 13 | | − |
| 14 | | − NumColors = 6, |
| 15 | | − |
| 16 | | − ButtonSize = 32 |
| 17 | | −}; |
| 18 | | − |
| 19 | | −const int sizes[] = {14, 21, 28}; |
| 20 | | −const int turns[] = {25, 35, 50}; |
| 21 | | −const ulong srccolors[NumColors] = { |
| 22 | | − 0x6060a8ff, |
| 23 | | − 0xf6f61dff, |
| 24 | | − 0x46b0e0ff, |
| 25 | | − 0x7ea020ff, |
| 26 | | − 0xf070a0ff, |
| 27 | | − 0xdc4a20ff |
| 28 | | −}; |
| 29 | | −char *mstr[] = { |
| 30 | | − "14x14 / 25", |
| 31 | | − "25x25 / 35", |
| 32 | | − "28x28 / 50", |
| 33 | | − "exit", |
| 34 | | − 0 |
| 35 | | −}; |
| 36 | | − |
| 37 | | −static int size; |
| 38 | | −static int wait4click; |
| 39 | | −static int turnsleft; |
| 40 | | −static uchar cells[28*28]; // enough for maximal size |
| 41 | | −static Image *colors[NumColors]; |
| 42 | | −static Rectangle buttons[NumColors]; |
| 43 | | − |
| 44 | | −static void |
| 45 | | −floodneighbours(uchar color, int x, int y); |
| 46 | | − |
| 47 | | −static void |
| 48 | | −redraw(Image *screen) { |
| 49 | | − Rectangle r = screen->r; |
| 50 | | − draw(screen, r, display->white, nil, ZP); |
| 51 | | − |
| 52 | | − const uchar *cell = &cells[0]; |
| 53 | | − int w = Dx(r), h = Dy(r) - ButtonSize - 2; |
| 54 | | − int c = (w < h ? w : h) / size; |
| 55 | | − w = c*size; |
| 56 | | − |
| 57 | | − // cells |
| 58 | | − for(int x = r.min.x; x < r.min.x+size*c; x+=c) { |
| 59 | | − for(int y = r.min.y; y < r.min.y+size*c; y+=c) { |
| 60 | | − Rectangle r = Rect(x, y, x+c, y+c); |
| 61 | | − draw(screen, r, colors[*cell & ColorMask], nil, ZP); |
| 62 | | − cell++; |
| 63 | | − } |
| 64 | | − } |
| 65 | | − |
| 66 | | − // buttons |
| 67 | | − int startx = r.min.x + (w/2) - NumColors*ButtonSize/2; |
| 68 | | − int y = r.min.y + h; |
| 69 | | − for(int i = 0, x = startx; i < NumColors; i++, x += ButtonSize) { |
| 70 | | − buttons[i] = Rect(x, y, x+ButtonSize, y+ButtonSize); |
| 71 | | − draw(screen, buttons[i], colors[i], nil, ZP); |
| 72 | | − } |
| 73 | | − |
| 74 | | − flushimage(display, 1); |
| 75 | | −} |
| 76 | | − |
| 77 | | −static void |
| 78 | | −floodrecurse(uchar color, int x, int y) { |
| 79 | | − uchar *c = &cells[x + y*size]; |
| 80 | | − if((*c & Flood) == 0 && (*c & ColorMask) == color) { |
| 81 | | − *c = color | Flood; |
| 82 | | − floodneighbours(color, x, y); |
| 83 | | − } |
| 84 | | −} |
| 85 | | − |
| 86 | | −static void |
| 87 | | −floodneighbours(uchar color, int x, int y) { |
| 88 | | − cells[x + y*size] = color | Flood; |
| 89 | | − |
| 90 | | − if(x > 0) |
| 91 | | − floodrecurse(color, x-1, y); |
| 92 | | − if(x < size-1) |
| 93 | | − floodrecurse(color, x+1, y); |
| 94 | | − if(y > 0) |
| 95 | | − floodrecurse(color, x, y-1); |
| 96 | | − if(y < size-1) |
| 97 | | − floodrecurse(color, x, y+1); |
| 98 | | −} |
| 99 | | − |
| 100 | | −static int |
| 101 | | −reflood(uchar color) { |
| 102 | | − color &= ColorMask; |
| 103 | | − |
| 104 | | − int n = 0; |
| 105 | | − for(int x = 0; x < size; x++) |
| 106 | | − for(int y = 0; y < size; y++) |
| 107 | | − if(cells[x + y*size] & Flood) { |
| 108 | | − floodneighbours(color, x, y); |
| 109 | | − n++; |
| 110 | | − } |
| 111 | | − return n; |
| 112 | | −} |
| 113 | | − |
| 114 | | −static void |
| 115 | | −flood(uchar color) { |
| 116 | | − if((cells[0] & Flood) != 0 && (cells[0] & ColorMask) == color) |
| 117 | | − return; |
| 118 | | − |
| 119 | | − if(!turnsleft) |
| 120 | | − return; |
| 121 | | − |
| 122 | | − int n = reflood(color); |
| 123 | | − redraw(screen); |
| 124 | | − |
| 125 | | − if(n == size*size) { |
| 126 | | − // win |
| 127 | | − wait4click = 1; |
| 128 | | − } else if(--turnsleft == 0) { |
| 129 | | − // fail |
| 130 | | − wait4click = 1; |
| 131 | | − } |
| 132 | | −} |
| 133 | | − |
| 134 | | −static void |
| 135 | | −newgame(int sid) { |
| 136 | | − size = sizes[sid]; |
| 137 | | − turnsleft = turns[sid]; |
| 138 | | − |
| 139 | | − // randomize |
| 140 | | − uchar *c = &cells[0]; |
| 141 | | − for(int i = 0; i < size*size; i++) { |
| 142 | | − *c++ = nrand(NumColors); |
| 143 | | − } |
| 144 | | − |
| 145 | | − cells[0] |= Flood; |
| 146 | | − reflood(cells[0]); |
| 147 | | − redraw(screen); |
| 148 | | −} |
| 149 | | − |
| 150 | | −void |
| 151 | | −eresized(int new) |
| 152 | | −{ |
| 153 | | − if(new && getwindow(display, Refnone) < 0) |
| 154 | | − fprint(2, "can't reattach to window"); |
| 155 | | − redraw(screen); |
| 156 | | −} |
| 157 | | − |
| 158 | | −void main(int, char**) { |
| 159 | | − Menu menu; |
| 160 | | − |
| 161 | | − if(initdraw(0, 0, "cflood") < 0) |
| 162 | | − sysfatal("initdraw failed"); |
| 163 | | − |
| 164 | | − Rectangle r = Rect(0, 0, 1, 1); |
| 165 | | − for(int i = 0; i < NumColors; i++) { |
| 166 | | − colors[i] = allocimage(display, r, CMAP8, 1, srccolors[i]); |
| 167 | | − } |
| 168 | | − |
| 169 | | − einit(Emouse); |
| 170 | | − menu.item = mstr; |
| 171 | | − menu.lasthit = 0; |
| 172 | | − srand(time(0)); |
| 173 | | − |
| 174 | | − int sid = Snormal; |
| 175 | | − newgame(sid); |
| 176 | | − |
| 177 | | − for(int mold = 0;;) { |
| 178 | | − Event e; |
| 179 | | − int key = event(&e); |
| 180 | | − Mouse m = e.mouse; |
| 181 | | − |
| 182 | | − if(key != Emouse) |
| 183 | | − continue; |
| 184 | | − |
| 185 | | − if(m.buttons & 4) { |
| 186 | | − int p = emenuhit(3, &m, &menu); |
| 187 | | − if(p >= Ssmall && p <= Slarge) |
| 188 | | − newgame(sid = p); |
| 189 | | − else if(p > 0) |
| 190 | | − break; |
| 191 | | − } else if(wait4click && !mold && m.buttons != mold) { |
| 192 | | − wait4click = 0; |
| 193 | | − newgame(sid); |
| 194 | | − } else if(m.buttons & 1) { |
| 195 | | − for(int i = 0; i < NumColors; i++) { |
| 196 | | − if(ptinrect(m.xy, buttons[i])) { |
| 197 | | − flood(i); |
| 198 | | − break; |
| 199 | | − } |
| 200 | | − } |
| 201 | | − } |
| 202 | | − |
| 203 | | − mold = m.buttons; |
| 204 | | − } |
| 205 | | −} |
| 1 | +#include <u.h> |
| 2 | +#include <libc.h> |
| 3 | +#include <draw.h> |
| 4 | +#include <event.h> |
| 5 | + |
| 6 | +enum { |
| 7 | + Ssmall, |
| 8 | + Snormal, |
| 9 | + Slarge, |
| 10 | + |
| 11 | + Flood = 1<<7, |
| 12 | + ColorMask = 0x0f, |
| 13 | + |
| 14 | + NumColors = 6, |
| 15 | + |
| 16 | + ButtonSize = 32 |
| 17 | +}; |
| 18 | + |
| 19 | +const int sizes[] = {14, 21, 28}; |
| 20 | +const int turns[] = {25, 35, 50}; |
| 21 | +const ulong srccolors[NumColors] = { |
| 22 | + 0x6060a8ff, |
| 23 | + 0xf6f61dff, |
| 24 | + 0x46b0e0ff, |
| 25 | + 0x7ea020ff, |
| 26 | + 0xf070a0ff, |
| 27 | + 0xdc4a20ff |
| 28 | +}; |
| 29 | +char *mstr[] = { |
| 30 | + "14x14 / 25", |
| 31 | + "25x25 / 35", |
| 32 | + "28x28 / 50", |
| 33 | + "exit", |
| 34 | + 0 |
| 35 | +}; |
| 36 | + |
| 37 | +static int size; |
| 38 | +static int wait4click; |
| 39 | +static int turnsleft; |
| 40 | +static uchar cells[28*28]; // enough for maximal size |
| 41 | +static Image *colors[NumColors]; |
| 42 | +static Rectangle buttons[NumColors]; |
| 43 | + |
| 44 | +static void |
| 45 | +floodneighbours(uchar color, int x, int y); |
| 46 | + |
| 47 | +static void |
| 48 | +redraw(Image *screen) { |
| 49 | + Rectangle r = screen->r; |
| 50 | + draw(screen, r, display->white, nil, ZP); |
| 51 | + |
| 52 | + const uchar *cell = &cells[0]; |
| 53 | + int w = Dx(r), h = Dy(r) - ButtonSize - 2; |
| 54 | + int c = (w < h ? w : h) / size; |
| 55 | + w = c*size; |
| 56 | + |
| 57 | + // cells |
| 58 | + for(int x = r.min.x; x < r.min.x+size*c; x+=c) { |
| 59 | + for(int y = r.min.y; y < r.min.y+size*c; y+=c) { |
| 60 | + Rectangle r = Rect(x, y, x+c, y+c); |
| 61 | + draw(screen, r, colors[*cell & ColorMask], nil, ZP); |
| 62 | + cell++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // buttons |
| 67 | + int startx = r.min.x + (w/2) - NumColors*ButtonSize/2; |
| 68 | + int y = r.min.y + h; |
| 69 | + for(int i = 0, x = startx; i < NumColors; i++, x += ButtonSize) { |
| 70 | + buttons[i] = Rect(x, y, x+ButtonSize, y+ButtonSize); |
| 71 | + draw(screen, buttons[i], colors[i], nil, ZP); |
| 72 | + } |
| 73 | + |
| 74 | + flushimage(display, 1); |
| 75 | +} |
| 76 | + |
| 77 | +static void |
| 78 | +floodrecurse(uchar color, int x, int y) { |
| 79 | + uchar *c = &cells[x + y*size]; |
| 80 | + if((*c & Flood) == 0 && (*c & ColorMask) == color) { |
| 81 | + *c = color | Flood; |
| 82 | + floodneighbours(color, x, y); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +static void |
| 87 | +floodneighbours(uchar color, int x, int y) { |
| 88 | + cells[x + y*size] = color | Flood; |
| 89 | + |
| 90 | + if(x > 0) |
| 91 | + floodrecurse(color, x-1, y); |
| 92 | + if(x < size-1) |
| 93 | + floodrecurse(color, x+1, y); |
| 94 | + if(y > 0) |
| 95 | + floodrecurse(color, x, y-1); |
| 96 | + if(y < size-1) |
| 97 | + floodrecurse(color, x, y+1); |
| 98 | +} |
| 99 | + |
| 100 | +static int |
| 101 | +reflood(uchar color) { |
| 102 | + color &= ColorMask; |
| 103 | + |
| 104 | + int n = 0; |
| 105 | + for(int x = 0; x < size; x++) |
| 106 | + for(int y = 0; y < size; y++) |
| 107 | + if(cells[x + y*size] & Flood) { |
| 108 | + floodneighbours(color, x, y); |
| 109 | + n++; |
| 110 | + } |
| 111 | + return n; |
| 112 | +} |
| 113 | + |
| 114 | +static void |
| 115 | +flood(uchar color) { |
| 116 | + if((cells[0] & Flood) != 0 && (cells[0] & ColorMask) == color) |
| 117 | + return; |
| 118 | + |
| 119 | + if(!turnsleft) |
| 120 | + return; |
| 121 | + |
| 122 | + int n = reflood(color); |
| 123 | + redraw(screen); |
| 124 | + |
| 125 | + if(n == size*size) { |
| 126 | + // win |
| 127 | + wait4click = 1; |
| 128 | + } else if(--turnsleft == 0) { |
| 129 | + // fail |
| 130 | + wait4click = 1; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +static void |
| 135 | +newgame(int sid) { |
| 136 | + size = sizes[sid]; |
| 137 | + turnsleft = turns[sid]; |
| 138 | + |
| 139 | + // randomize |
| 140 | + uchar *c = &cells[0]; |
| 141 | + for(int i = 0; i < size*size; i++) { |
| 142 | + *c++ = nrand(NumColors); |
| 143 | + } |
| 144 | + |
| 145 | + cells[0] |= Flood; |
| 146 | + reflood(cells[0]); |
| 147 | + redraw(screen); |
| 148 | +} |
| 149 | + |
| 150 | +void |
| 151 | +eresized(int new) |
| 152 | +{ |
| 153 | + if(new && getwindow(display, Refnone) < 0) |
| 154 | + fprint(2, "can't reattach to window"); |
| 155 | + redraw(screen); |
| 156 | +} |
| 157 | + |
| 158 | +void main(int, char**) { |
| 159 | + Menu menu; |
| 160 | + |
| 161 | + if(initdraw(0, 0, "cflood") < 0) |
| 162 | + sysfatal("initdraw failed"); |
| 163 | + |
| 164 | + Rectangle r = Rect(0, 0, 1, 1); |
| 165 | + for(int i = 0; i < NumColors; i++) { |
| 166 | + colors[i] = allocimage(display, r, CMAP8, 1, srccolors[i]); |
| 167 | + } |
| 168 | + |
| 169 | + einit(Emouse); |
| 170 | + menu.item = mstr; |
| 171 | + menu.lasthit = 0; |
| 172 | + srand(time(0)); |
| 173 | + |
| 174 | + int sid = Snormal; |
| 175 | + newgame(sid); |
| 176 | + |
| 177 | + for(int mold = 0;;) { |
| 178 | + Event e; |
| 179 | + int key = event(&e); |
| 180 | + Mouse m = e.mouse; |
| 181 | + |
| 182 | + if(key != Emouse) |
| 183 | + continue; |
| 184 | + |
| 185 | + if(m.buttons & 4) { |
| 186 | + int p = emenuhit(3, &m, &menu); |
| 187 | + if(p >= Ssmall && p <= Slarge) |
| 188 | + newgame(sid = p); |
| 189 | + else if(p > 0) |
| 190 | + break; |
| 191 | + } else if(wait4click && !mold && m.buttons != mold) { |
| 192 | + wait4click = 0; |
| 193 | + newgame(sid); |
| 194 | + } else if(m.buttons & 1) { |
| 195 | + for(int i = 0; i < NumColors; i++) { |
| 196 | + if(ptinrect(m.xy, buttons[i])) { |
| 197 | + flood(i); |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + mold = m.buttons; |
| 204 | + } |
| 205 | +} |