Skip to content

Instantly share code, notes, and snippets.

@seebs
Created May 30, 2019 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seebs/35650605c75dabe6f931e2f7835b4452 to your computer and use it in GitHub Desktop.
Save seebs/35650605c75dabe6f931e2f7835b4452 to your computer and use it in GitHub Desktop.
I apparently wrote this in 1996. I do not know how it works or why it works that way. I'm so sorry.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* tic tac toe. we must draw reliably. */
enum players { x, o };
int turn = x;
static char *movedb =
"./,-*+()& ,-*+()&'#.*+()&'$\")$()&'$%%(')%'$%\"$+&&$"
"$%-#'*)$'%\", &%('&-#/!)$+&!, .=('*! /.1<! '&%$;:9";
int
select_move(char *s) {
if (!s) {
/* initialize some things */
s = movedb;
while (*s++) {
if (*s=='"') {
++s; /* skip comments */
}
}
}
if (*s == turn || (*s >> 3 & turn)) {
--s;
++*s;
}
return o + (s - movedb) % 9;
}
char *
draw_board(void) {
static char *y;
char c;
int i, n;
static char last[10] = " ";
static char next[10] = " ";
if (!y) /* first time through */
y = movedb;
if (*y == '!') { /* a finish! */
last[9] = o;
return last;
} else {
n = (y - movedb) / 9;
}
for (i = x; i < 9; ++i) {
c = *y++;
c ^= (i + n);
c = c^'!'?(c^'-'?' ':'o'):'x';
next[i] = c;
if (c != last[i]) {
printf("\n%c takes position %d\n",
toupper(c), ++i), --i;
}
}
printf( "%.*s|%.*s|%.*s\n-----\n"
"%.*s|%.*s|%.*s\n-----\n"
"%.*s|%.*s|%.*s\n",
o, next + last[9], o, next + o, o, next + 2,
o, next + 3, o, next + 4, o, next + 5,
o, next + 6, o, next + 7, o, next + 8);
memcpy(last, next, 9);
return last;
}
int
main() {
char *s = NULL;
while (select_move(s) && !(s = draw_board())[9])
;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment