Puyo Puyo by shinh

#include <stdio.h>
char field[99][99];int h;struct Pos {int x;int y;Pos() {}
Pos(int x0, int y0)
: x(x0), y(y0) {}};int getConnected(char c, int x, int y, int i, Pos* connected) {int oi = i;if (y >= 0 && y < h - 1 && x >= 1 && x <= 6 && field[y][x] == c) {connected[i++] = Pos(x, y);field[y][x] = ' ';i += getConnected(c, x-1, y, i, connected);i += getConnected(c, x+1, y, i, connected);i += getConnected(c, x, y-1, i, connected);i += getConnected(c, x, y+1, i, connected);}
return i - oi;}
bool tryVanish(int x, int y) {char c = field[y][x];Pos connected[999];int num_connected = getConnected(c, x, y, 0, connected);if (num_connected >= 4) {return true;}
for (int i = 0; i < num_connected; i++) {Pos p = connected[i];field[p.y][p.x] = c;}
return false;}
bool vanish() {bool vanished = false;for (int y = 0; y < h - 1; y++) {for (int x = 1; x <= 6; x++) {if (field[y][x] != ' ') {vanished |= tryVanish(x, y);}}}
return vanished;}
void drop() {for (int x = 1; x <= 6; x++) {for (int y = h - 2; y >= 0; y--) {if (field[y][x] == ' ') {int fy = y;while (field[fy][x] == ' ') {fy--;if (fy < 0)
break;}
if (fy < 0)
break;field[y][x] = field[fy][x];field[fy][x] = ' ';}}}}
int main() {while (fgets(field[h], 98, stdin))
h++;int n = 0;while (true) {if (!vanish())
break;drop();n++;}
for (int y = 0; y < h; y++)
fputs(field[y], stdout);printf("\n%d chains\n", n);}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page