I'm learning C, so decided to try and make a Tic Tac Toe game, using ASCII art as the table.
I don't have much yet...
#include <stdio.h>
#define WIDTH 2;
#define HEIGHT 2;
int main (int argc, char *argv[]) {
printf("Welcome to Tic Tac Toe!\n\n");
int width = WIDTH;
int height = HEIGHT;
// Make grid
for (int y = 0; y <= height; y++) {
for (int x = 0; x <= width; x++) {
printf("%d%d", x, y);
if (x != width) {
printf("||");
}
}
if (y != height) {
printf("\n");
for (int i = 0; i < (width + (width * 4)); i++) {
printf("=");
}
printf("\n");
} else {
printf("\n");
}
}
// Ask for user input
printf("Please enter the cell where you would like to place an X, e.g. for example the first top left cell is '00'\n");
}
When ran on the command line, I get this output
Welcome to Tic Tac Toe!
00||10||20
==========
01||11||21
==========
02||12||22
Please enter the cell where you would like to place an X, e.g. for example the first top left cell is '00'
Now, when I figure out how to get input of more than one char (I only know how to use getchar()
for getting individual chars so far, though that might work OK for this example), I'd like to loop through again and place an X for the corresponding cell.
Should I write a function for printing the table, which takes arguments such as 'int markerX, int markerY` to place the X?
How would I then store the position of the marker so I could check if the game is won or not?
Is my Choose which cell to place marker the best way to ask for user input for a game on the command line?
Thanks!