#define true -1 #define false 0 #define byte unsigned char #define boolean unsigned char // constants #define CELL_EMPTY 2 char userinput; char board[9]; char convert[] = "XO_"; byte player, i, status_incomplete; void drawboard() { printf("\n"); for(i=0; i < 3; i++) { printf ("%c ",convert[board[i*3]]); printf ("%c ", convert[board[i*3 +1]]); printf ("%c\n",convert[board[i*3 +2]]); } printf("\nplayer %c: ", convert[player]); } int main () { // init the board // make this a function, so you can replay without powering down for (i=0; i < 9; i++){ board[i] = CELL_EMPTY; } player = 0x01; printf("TTT game\n"); printf("Grid is numbered 1-9\n"); printf("Press number, then return to make a move"); printf("\nplayer %c: ", convert[player]); for(;;) { do { status_incomplete = true; if (userinput = (char)getchar()) { userinput = userinput - 49; if (board[userinput] == CELL_EMPTY) { board[userinput] = player; // player is 0 or 1 player = player ^ 0x01; // toggle player if legal status_incomplete = false; drawboard(); } } } while (status_incomplete); } }