I have looked over the various other posts considering how to implement Tic-Tac-Toe in C but am unfortunately running into issues. I have two functions for initialising and drawing the grid, int init_grid(int gridsize)
and void draw_grid(int gridsize)
. These take the parameter gridsize
as the user is able to choose from a 3x3 to a 10x10 grid. The program compiles far so far, but when entering the size of the board, it prints the correct number of '.' characters but only in the first column.
The code is as follows:
init_grid
int init_grid(int gridsize) {
for (int row = 0; row < gridsize ; row++) {
for (int col = 0; col < gridsize; col++) {
grid[row][col] = '.';
}
}
if (gridsize > MaxGrid) {
puts("Error, gridsize too large.");
return 1;
}
else {
return 0;
}
}
draw_grid
void draw_grid(int gridsize) {
for (int row = 0; row < gridsize; row++)
{
for (int col = 0; row < gridsize; row++)
{
putchar (' ');
if (grid[row][col]) {
putchar (grid[row][col]);
}
else {
putchar ('.');
}
printf("\n");
}
}
}
main
int main() {
int gridsize = 0;
printf("Hello and welcome to Tic Tac Toe\n");
printf("Please enter the size of the grid you would like to play with (between 3 and 10):\n");
scanf("%d", &gridsize);
init_grid(gridsize);
draw_grid(gridsize);
return 0;
}
Output
Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and
10):
5
.
.
.
.
.
I hope I have made everything clear enough. I've tried a variety of different things now but just can't get it to print the board/grid correctly.
The comments above are all correct. I've put all the corrected code below and now the code does as expected.
I also added the code to label the rows and columns. Note that changes need to be made when the gridsize is greater than 9.
#include <stdio.h>
int init_grid(int gridsize);
void draw_grid(int gridsize);
char grid[25][25];
int MaxGrid = 25;
int init_grid(int gridsize)
{
for (int row = 0; row < gridsize ; row++)
{
for (int col = 0; col < gridsize; col++)
{
grid[row][col] = '.';
}
}
if (gridsize > MaxGrid)
{
puts("Error, gridsize too large.");
return 1;
}
else
{
return 0;
}
}
void draw_grid(int gridsize)
{
printf(" ");
for( int i=0; i < gridsize; i++ )
{
printf("%d ", i+1);
}
printf("\n");
for (int row = 0; row < gridsize; row++)
{
printf("%d ", row+1);
for (int col = 0; col < gridsize; col++)
{
putchar (' ');
if (grid[row][col])
{
putchar (grid[row][col]);
}
else
{
putchar ('.');
}
}
printf("\n");
}
}
int main()
{
int gridsize = 0;
printf("Hello and welcome to Tic Tac Toe\n");
printf("Please enter the size of the grid you would like to play with (between 3 and 10):\n");
scanf("%d", &gridsize);
init_grid(gridsize);
draw_grid(gridsize);
return 0;
}
Output:
jnorton@ubuntu:~/source$ ./a.out
Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and 10):
5
1 2 3 4 5
1 . . . . .
2 . . . . .
3 . . . . .
4 . . . . .
5 . . . . .
jnorton@ubuntu:~/source$ ./a.out
Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and 10):
3
1 2 3
1 . . .
2 . . .
3 . . .
jnorton@ubuntu:~/source$ ./a.out
Hello and welcome to Tic Tac Toe
Please enter the size of the grid you would like to play with (between 3 and 10):
10
1 2 3 4 5 6 7 8 9 10
1 . . . . . . . . . .
2 . . . . . . . . . .
3 . . . . . . . . . .
4 . . . . . . . . . .
5 . . . . . . . . . .
6 . . . . . . . . . .
7 . . . . . . . . . .
8 . . . . . . . . . .
9 . . . . . . . . . .
10 . . . . . . . . . .
jnorton@ubuntu:~/source$