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