/* preprocess 1 - remove comments / evaluate constants - Don Yang (uguu.org) 02/19/00 */ int board[1024]; int x, y, z, i, j, k, l, rnd; int size = 5; int vx[8] = { 1, 1, 1, 0, -1, -1, -1, 0}; int vy[8] = {-32, 0, 32, 32, 32, 0,-32,-32}; int weight[32] = { 2, 14, 70, 199, 799, 2, 8, 28, 90, 799, 2, 2, 4, 10, 799, 4, 18, 94, 348, 999, 2, 10, 44, 220, 999, 2, 2, 4, 10, 999 }; int bestmove[1024], moves; int maxscore, currentscore; void input() { printf("\n\n%% "); scanf(" %c%d", &x, &y); x = (x | 32) - 97; y--; } void output() { for(x = !printf("\n "); x < size; printf("%c ", 97 + x++)); for(y = 0; y < size;) for(x = !printf("\n%2d ", ++y); x < size; printf("%c ", board[(y + 3) * 32 + 4 + x++])); } void expandboard() { if( size < 24 ) for(i = 0; i < 2; i++) for(j = 0; j < size; j++) { if( board[(i + 4) * 32 + j + 4] - 46 ) { for(k = 1023; k >= 132; board[k--] = board[k - 33]); size++; expandboard(); return; } if( board[(j + 4) * 32 + size - i + 3] - 46 ) { for(k = 1023; k >= 132; board[k--] = board[k - 32]); size++; expandboard(); return; } if( board[(size - i + 3) * 32 + size - j + 3] - 46 ) { size++; expandboard(); return; } if( board[(size - j + 3) * 32 + i + 4] - 46 ) { for(k = 1023; k >= 132; board[k--] = board[k - 1]); size++; expandboard(); return; } } } void think() { maxscore = moves = 0; for(x = 132; x < 924; x++) if( x % 32 >= 4 && x % 32 < 28 && board[x] == 46 ) { currentscore = 0; for(y = 111; y < 121; y += 9) { board[x] = y; for(z = 0; z < 8; z++) { for(l = x - (j = vx[z] + vy[z]); board[l] == y; l -= j); k = board[l] - 46 ? 5 : 0; for(i = 0; board[l += j] == y && i < 5; i++); currentscore += weight[ (y - 120 ? 15 : 0) + (k += board[l] - 46 ? 5 : 0) + i - 1]; } } board[x] = 46; if( currentscore > maxscore ) { maxscore = currentscore; moves = 0; } if( currentscore == maxscore ) bestmove[moves++] = x; } } int victory() { for(x = j = 132; x < 924 && j - 5; x++) if( (z = board[x]) - 46 ) for(y = 0; y < 4 && j - 5; y++) for(i = j = 1; i < 5 && j < 5; j += board[x + (vx[y] + vy[y]) * i++] - z ? 0 : 1); return j == 5; } void main() { for(x = 0; x < 1024; board[x++] = 46); for(weight[(rnd = time(0)) & 31]++; !victory();) { expandboard(); output(); input(); board[(y + 4) * 32 + x + 4] = 120; rnd += x; if( rnd < 0 ) rnd = -rnd; if( victory() ) break; expandboard(); think(); board[bestmove[rnd % moves]] = 111; } x--; y--; for(i = 0; i < 5; board[x + (vx[y] + vy[y]) * i++] &= -33); output(); puts(z == 120 ? "\nYou win" : "\nYou lose"); }