Reputation: 157
I am trying to create a tic-tac-toe board using an array of [7][3]
The idea is to input '|' at
[0][0] , [0][2] , [0][4] , [0][6]
[1][0] , [1][2] , [1][4] , [1][6]
[2][0] , [2][2] , [2][4] , [2][6]
and 'empty space' ( variable char XO
for the 'X' and 'O') at
[0][1] , [0][3] , [0][5]
[1][1] , [1][3] , [1][5]
[2][1] , [2][3] , [2][5]
This is the code:
#include <stdio.h>
char drawBoard()
{
char board[7][3], XO;
int rows, columns;
/*
0 * 2 = 0
1 * 2 = 2
2 * 2 = 4
3 * 2 = 6
0 * 2 + 1 = 1
1 * 2 + 1 = 3
2 * 2 + 1 = 5
*/
for ( rows = 0 ; rows < 3 ; rows++ )
for ( columns = 0 ; columns < 7 ; columns++ )
board[ rows ][ columns * 2 ] = '|';
for ( rows = 0 ; rows < 3 ; rows++ )
for ( columns = 0 ; columns < 6 ; columns++ )
board[ rows ][ columns *2 + 1 ] = XO;
for ( rows = 0 ; rows < 3 ; rows++ )
{
for ( columns = 0 ; columns < 7 ; columns++ )
{
printf( "%d ", board[rows][columns] );
}
printf("\n\n");
}
}/* end function drawboard */
int main()
{
drawBoard();
}/* end main */
When I run the program, the output are all numbers, which is well, disastrous.
I'm well aware I did not specify any input for variables 'XO', but shouldn't the '|' at least be printed :( ?
Upvotes: 2
Views: 16519
Reputation: 5575
Problems with your code:
Your declaration of 2d array is wrong. The first bracket should contain number of rows and 2nd bracket- the number of columns. ( you have it backwards in your code )
Rather than doing board[ rows ][ columns * 2 ] = '|';
.. its much simpler to just increment the loop counter by 2 ( see my code )
To print a character, you choose %c
descriptor not %d
Lastly not a big issue but it is good practice to do a return in main
Sorry another big problem with your code. You declare your drawBoard function as char
.. but you don't return any char
from it.. ( you compiler will complain if you have the -Wall flag on ) .. change it to void
instead
Here is the fixed code: ( see comments in the code to understand the problems with your code )
Also Right now I am populating the array with 'X' .. you can change that to suit your needs
#include <stdio.h>
void drawBoard()
{
char board[3][7], XO; // you were declaring your array wrong .. it rows in first bracket and columns in 2nd bracket
int rows, columns;
for ( rows = 0 ; rows < 3 ; rows++ ){
for ( columns = 0 ; columns < 7 ; columns=columns+2 ){ // to fill every second elemnt of the array .. just increment the counter by 2
board[ rows ][ columns ] = '|';
}
}
for ( rows = 0 ; rows < 3 ; rows++ ){
for ( columns = 1 ; columns < 7 ; columns = columns+2 ){
board[ rows ][ columns ] = 'X';
}
}
for ( rows = 0 ; rows < 3 ; rows++ )
{
for ( columns = 0 ; columns < 7 ; columns++ )
{
printf( "%c", board[rows][columns] ); // its supposed to be %c as you are printing a character
}
printf("\n");
}
}/* end function drawboard */
int main()
{
drawBoard();
return 0;
}
Output;
Sukhvir@Sukhvir-PC ~
$ gcc -Werror -g -o test test.c
Sukhvir@Sukhvir-PC ~
$ ./test
|X|X|X|
|X|X|X|
|X|X|X|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
My interpretation of the game: ( slightly edited as per suggestion from Maxime )
#include <stdio.h>
void drawBoard(char board[][3])
{
int rows, columns;
for ( rows = 0 ; rows < 3 ; rows++ )
{
for ( columns = 0 ; columns < 3 ; columns++ )
{
if(board[rows][columns]){
printf( "|%c", board[rows][columns] );
}else{
printf("| ");
}
}
printf("|\n");
}
}/* end function drawboard */
int main()
{
char game[3][3]={{0}};
int totalEntry =0,row,column;
char value;
while(totalEntry<=9){
printf("Enter (x) or (o): ");
scanf("%c",&value);
getchar();
printf("Enter row number: ");
scanf("%d",&row);
getchar();
printf("Enter Column number: ");
scanf("%d",&column);
getchar();
game[row][column] = value;
drawBoard(game);
}
return 0;
}
Output: ( stopped to keep it short .. but you get the idea )
$ ./test
Enter (X) or (O): x
Enter row number: 0
Enter Column number: 0
|x| | |
| | | |
| | | |
Enter (X) or (O): o
Enter row number: 1
Enter Column number: 1
|x| | |
| |o| |
| | | |
Enter (X) or (O): x
Enter row number: 2
Enter Column number: 2
|x| | |
| |o| |
| | |x|
Enter (X) or (O):
[4]+ Stopped
Upvotes: 4
Reputation: 18861
Regarding your main question:
Replace :
printf( "%d ", board[rows][columns] );
by :
printf( "%c ", board[rows][columns] );
Because currently, you're printing the ASCII value of the characters.
Other errors:
1) Be careful because you're also printing garbage because XO is not affected and not initialized.
2) Also, in this loop:
for ( columns = 0 ; columns < 7 ; columns++ )
board[ rows ][ columns * 2 ] = '|';
You are accessing to column 12 which is out of bound of the table. Multiple fixes are possible:
for ( columns = 0 ; columns < 7 ; columns+=2 )
board[ rows ][ columns ] = '|';
or
for ( columns = 0 ; columns < 4 ; columns++ )
board[ rows ][ columns * 2] = '|';
3) Your array declaration is wrong. You wrote :
char board[7][3]
instead of
char board[3][7]
It is not mandatory that the first dimension is the rows and the second the columns, you can decide of that. But, according to the following of your code, you are accessing your array with board[rows][columns]
so it should be char board[3][7]
since you have 3 rows and 7 columns.
4) Your function drawBoard
should not be declared as returning a char
if it returns nothing. Change the type to void
.
General comment:
1) You should probably not put the '|' in your array, it makes your code more complex. You should instead display them when you print the board. Imagine you want to change how the board should be printed (e.g. changing the '|' by " | "), you would need to change everything!
2) Compile with -Wall
.
Upvotes: 0
Reputation: 11860
Instead of:
printf( "%d ", board[rows][columns] );
Try:
printf( "%c ", board[rows][columns] );
This will print characters instead of numbers.
Upvotes: 0