|
|
1.1 ! root 1: /* ! 2: * ttt -- 3-dimensional tic-tac-toe. ! 3: */ ! 4: ! 5: #include <stdio.h> ! 6: ! 7: #define EMPTY 0 ! 8: #define MAN 1 ! 9: #define BEAST 5 ! 10: ! 11: /* ! 12: * This is a table of all winning ! 13: * combinations. ! 14: * I stole it out of Kilobaud, April ! 15: * 78. ! 16: * You can look there to see how it ! 17: * is ordered. ! 18: */ ! 19: char w[] = { ! 20: 0, 1, 2, 3, ! 21: 4, 5, 6, 7, ! 22: 8, 9, 10, 11, ! 23: 12, 13, 14, 15, ! 24: 0, 4, 8, 12, ! 25: 1, 5, 9, 13, ! 26: 2, 6, 10, 14, ! 27: 3, 7, 11, 15, ! 28: 0, 5, 10, 15, ! 29: 3, 6, 9, 12, ! 30: 16, 17, 18, 19, ! 31: 20, 21, 22, 23, ! 32: 24, 25, 26, 27, ! 33: 28, 29, 30, 31, ! 34: 16, 20, 24, 28, ! 35: 17, 21, 25, 29, ! 36: 18, 22, 26, 30, ! 37: 19, 23, 27, 31, ! 38: 16, 21, 26, 31, ! 39: 19, 22, 25, 28, ! 40: 32, 33, 34, 35, ! 41: 36, 37, 38, 39, ! 42: 40, 41, 42, 43, ! 43: 44, 45, 46, 47, ! 44: 32, 36, 40, 44, ! 45: 33, 37, 41, 45, ! 46: 34, 38, 42, 46, ! 47: 35, 39, 43, 47, ! 48: 32, 37, 42, 47, ! 49: 35, 38, 41, 44, ! 50: 48, 49, 50, 51, ! 51: 52, 53, 54, 55, ! 52: 56, 57, 58, 59, ! 53: 60, 61, 62, 63, ! 54: 48, 52, 56, 60, ! 55: 49, 53, 57, 61, ! 56: 50, 54, 58, 62, ! 57: 51, 55, 59, 63, ! 58: 48, 53, 58, 63, ! 59: 51, 54, 57, 60, ! 60: 0, 16, 32, 48, ! 61: 1, 17, 33, 49, ! 62: 2, 18, 34, 50, ! 63: 3, 19, 35, 51, ! 64: 4, 20, 36, 52, ! 65: 5, 21, 37, 53, ! 66: 6, 22, 38, 54, ! 67: 7, 23, 39, 55, ! 68: 8, 24, 40, 56, ! 69: 9, 25, 41, 57, ! 70: 10, 26, 42, 58, ! 71: 11, 27, 43, 59, ! 72: 13, 29, 45, 61, ! 73: 12, 28, 44, 60, ! 74: 14, 30, 46, 62, ! 75: 15, 31, 47, 63, ! 76: 0, 21, 42, 63, ! 77: 4, 21, 38, 55, ! 78: 8, 25, 42, 59, ! 79: 12, 25, 38, 51, ! 80: 1, 21, 41, 61, ! 81: 13, 25, 37, 49, ! 82: 2, 22, 42, 62, ! 83: 14, 26, 38, 50, ! 84: 3, 22, 41, 60, ! 85: 7, 22, 37, 52, ! 86: 11, 26, 41, 56, ! 87: 15, 26, 37, 48, ! 88: 0, 20, 40, 60, ! 89: 0, 17, 34, 51, ! 90: 3, 18, 33, 48, ! 91: 3, 23, 43, 63, ! 92: 12, 24, 36, 48, ! 93: 12, 29, 46, 63, ! 94: 15, 30, 45, 60, ! 95: 15, 27, 39, 51 ! 96: }; ! 97: ! 98: char b[64]; ! 99: char sep[] = "----------- ----------- ----------- -----------"; ! 100: ! 101: main(argc, argv) ! 102: char *argv[]; ! 103: { ! 104: if (yes("Print rules")) ! 105: rules(); ! 106: if (yes("Play first")) ! 107: man(); ! 108: for (;;) { ! 109: beast(); ! 110: man(); ! 111: } ! 112: } ! 113: ! 114: yes(s) ! 115: char *s; ! 116: { ! 117: char b[20]; ! 118: ! 119: for (;;) { ! 120: printf("%s? ", s); ! 121: if (gets(b) == NULL) ! 122: exit(0); ! 123: if (b[0] == 'y') ! 124: return (1); ! 125: if (b[0] == 'n') ! 126: return (0); ! 127: printf("Answer `yes' or `no'.\n"); ! 128: } ! 129: } ! 130: ! 131: rules() ! 132: { ! 133: printf("Three dimensional tic-tac-toe is played on a 4x4x4\n"); ! 134: printf("board. To win you must get 4 in a row. Your moves\n"); ! 135: printf("are specified as a 3 digit number; the first digit\n"); ! 136: printf("is the level, the second the row and the third the\n"); ! 137: printf("column. Levels and columns go from left to right\n"); ! 138: printf("from 0 to 3. Rows go from top to bottom with 0 on\n"); ! 139: printf("the top.\n"); ! 140: } ! 141: ! 142: man() ! 143: { ! 144: register i, j, t; ! 145: char buf[20]; ! 146: ! 147: board(); ! 148: for (;;) { ! 149: if (gets(buf) == NULL) ! 150: exit(0); ! 151: i = 16*(buf[0]-'0') + (buf[1]-'0') + 4*(buf[2]-'0'); ! 152: if (i>=0 && i<=63 && b[i]==EMPTY) ! 153: break; ! 154: printf("?\n"); ! 155: } ! 156: b[i] = MAN; ! 157: for (i=0; i<4*76; i+=4) { ! 158: t = 0; ! 159: for (j=0; j<4; ++j) ! 160: t += b[w[i+j]]; ! 161: if (t == 4*MAN) { ! 162: printf("You win.\n"); ! 163: exit(0); ! 164: } ! 165: } ! 166: } ! 167: ! 168: board() ! 169: { ! 170: register i, j; ! 171: ! 172: for (i=0; i<4; ++i) { ! 173: if (i != 0) ! 174: puts(sep); ! 175: for (j=0; j<64; j+=4) { ! 176: psq(i+j); ! 177: if (j==12 || j==28 || j==44) ! 178: putchar(' '); ! 179: else if (j >= 60) ! 180: putchar('\n'); ! 181: else ! 182: putchar('!'); ! 183: } ! 184: } ! 185: } ! 186: ! 187: psq(s) ! 188: { ! 189: register v; ! 190: ! 191: v = b[s]; ! 192: if (v == MAN) ! 193: printf("UU"); ! 194: else if (v == BEAST) ! 195: printf("CC"); ! 196: else ! 197: printf(" "); ! 198: } ! 199: ! 200: beast() ! 201: { ! 202: register i, j, t; ! 203: int s, bs, bt, v[76]; ! 204: ! 205: for (i=0; i<4*76; i+=4) { ! 206: t = 0; ! 207: for (j=0; j<4; ++j) ! 208: t += b[w[i+j]]; ! 209: v[i>>2] = t; ! 210: if (t == 3*BEAST) ! 211: break; ! 212: } ! 213: if (i < 4*76) { ! 214: for (j=0; j<4; ++j) ! 215: if (b[w[i+j]] == EMPTY) { ! 216: b[w[i+j]] = BEAST; ! 217: break; ! 218: } ! 219: board(); ! 220: printf("I win.\n"); ! 221: exit(0); ! 222: } ! 223: bt = 0; ! 224: for (s=0; s<64; ++s) { ! 225: if (b[s] != EMPTY) ! 226: continue; ! 227: t = 0; ! 228: for (i=0; i<4*76; i+=4) { ! 229: for (j=0; j<4; ++j) ! 230: if (w[i+j] == s) ! 231: break; ! 232: if (j != 4) { ! 233: if (v[i>>2] == 3*MAN) { ! 234: b[s] = BEAST; ! 235: return; ! 236: } ! 237: t += weight(v[i>>2]); ! 238: } ! 239: } ! 240: if (t > bt) { ! 241: bt = t; ! 242: bs = s; ! 243: } ! 244: } ! 245: if (bt != 0) ! 246: b[bs] = BEAST; ! 247: else { ! 248: for (s=0; s<64; ++s) ! 249: if (b[s] == EMPTY) ! 250: break; ! 251: if (s == 64) { ! 252: printf("Draw.\n"); ! 253: exit(0); ! 254: } ! 255: b[s] = BEAST; ! 256: } ! 257: } ! 258: ! 259: weight(t) ! 260: register t; ! 261: { ! 262: if (t == MAN) ! 263: return(1); ! 264: if (t == 2*MAN) ! 265: return(4); ! 266: if (t == BEAST) ! 267: return(1); ! 268: if (t == 2*BEAST) ! 269: return(2); ! 270: return(0); ! 271: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.