-
Problem with output
I recently wrote this program that displays (command-line program) a tic-tac-toe format to be played. I had started the program and wrote the function to print out the board with the specified occupied spots (an array). I tested it out, and I got a weird printout with numbers instead of the ' ' char or the 'O' char. Can you please take a look and tell me what's wrong?:
Code:
/****************************************************
TicTacToe -- a program to simulate the game *
****************************************************/
#include <stdio.h>
/***Function Prototypes***/
void printbrd(int pieces[3][3]);
int main()
{
int pieces[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
/**print out the board for first time viewing**/
printbrd(pieces);
return 0;
}
void printbrd(int pieces[3][3])
{
char lines[3][20] = {"( ) ( ) ( )", "( ) ( ) ( )", "( ) ( ) ( )"};
char space[3][3];
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (pieces[i][j] == 1)
space[i][j] = 'O';
else if (pieces[i][j] == 0)
space[i][j] = ' ';
}
j = -3;
sprintf(lines[i], "(%c) (%c) (%c)", &space[i][j], &space[i][j+1], &space[j+2]);
}
/**print out the lines that are pre-configured to the right**/
printf("%s\n", lines[0]);
printf("%s\n", lines[1]);
printf("%s\n", lines[2]);
}
Thanks!
-
try this:
sprintf(lines[i], "(%c) (%c) (%c)", space[i][j], space[i][j+1], space[i][j+2]);
alex
-
Nope. Still doesn't work. Anything else that is wrong? Thanks for the reply!
-
Hi!
sprintf(lines[i], "(%c) (%c) (%c)", space[i][0], space[i][1], space[i][2]);
this should work...
alex
-
Nope, still doesn't work. The same distored output. But, thanks for the reply. I can't figure it out. It is probably something with my multidimensional array's definitons/implementations. Thanks, though!
-
I was quite convinced that that was the problem...
This code works flawlessly here:
Code:
#include <stdio.h>
/***Function Prototypes***/
void printbrd(int pieces[3][3]);
int main()
{
int pieces[3][3] = {{1,0,0}, {0,0,1}, {0,1,0}};
/**print out the board for first timeviewing**/
printbrd(pieces);
return 0;
}
void printbrd(int pieces[3][3])
{
char lines[3][20] = {"( ) ( ) ( )", "( ) ( ) ( )", "( ) ( ) ( )"};
char space[3][3];
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (pieces[i][j] == 1)
space[i][j] = 'O';
else if (pieces[i][j] == 0)
space[i][j] = ' ';
}
sprintf(lines[i], "(%c) (%c) (%c)", space[i][0],
space[i][1], space[i][2]);
}
/**print out the lines that are pre-configured to the right**/
printf("%s\n", lines[0]);
printf("%s\n", lines[1]);
printf("%s\n", lines[2]);
}
alex
-
Thanks for the code!!! Hmmm...I can't see it. What did you change from my code? I should know that for future reference so I don't make that mistake again. Thanks, again, for the code! I really appreciate it, alex.