Trying to make a simple tic tac toe game and I'm stumped, something is not working right.
The grid is drawn easily, but I can't alter the grid for some reason or printf the proper character after getting input. It's probably something very simple and stupid, but its been a long time since doing any programming.
Code:
#include <stdio.h>
/* draw the grid */
void draw_grid(char *grid) {
printf("\n");
printf("%c -- %c -- %c\n",grid[0],grid[1],grid[2]);
printf("%c -- %c -- %c\n",grid[3],grid[4],grid[5]);
printf("%c -- %c -- %c\n",grid[6],grid[7],grid[8]);
printf("\n");
}
int get_move(void) { /* Bugs out if more than 1 character is entered, but have to do this to learn */
int x = '\0'; /* ansi-C and not mess with platform specific solutions. */
int new_line = '\0';
x = getchar();
new_line = getchar();
return x;
}
int main(void) {
int x = 0;
char grid[9];
/* initialize the grid */
for(x = 0; x < 9; x++) { /*this part works due to the initial grid being printed properly */
grid[x] = '.';
}
while(1) {
draw_grid(grid);
x = get_move();
printf("Value of X: %c\n",x);
printf("Value of grid[x]: %c",grid[x]); /* random garbage being printed here instead of '-' character. */
if(grid[x] != 'X') { /* this doesn't appear to work at all */
grid[x] = 'X';
}
}
return 0;
}