We're having this project of implementing Greed (greedjs.com) in C. everytime user makes a move, this function should update. as @ moves, it would leave the particular element of my BOARD[ROW][COLUMN] = 0. how can I write it into file so that instead of 0 it would print a space?
here's my code:
void update_game_file() {
fboard = fopen("newgame.txt", "w+");
if(fboard==NULL){
printf("Error!");
exit(1);
}
/*
* print board to file
*/
for (int i=0; i<ROWS; i++) {
for (int j=0; j<COLS; j++) {
if(BOARD[i][j] == 64) {
char player = BOARD[i][j];
fprintf( fboard, "%c", player);
} else if (BOARD[i][j] == 0) {
char space = BOARD[i][j];
fprintf( fboard, "%c", space);
} else if (BOARD[i][j] !=64) {
fprintf( fboard, "%d", BOARD[i][j]);
}
}
fprintf( fboard, "\n");
}
fclose(fboard);
}
the following is one way to handle the problem:
for (int i=0; i<ROWS; i++)
{
for (int j=0; j<COLS; j++)
{
if (BOARD[i][j] == 0)
{
fprintf( fboard, " " );
}
else if ( BOARD[i][j] == '@' )
{
fprintf( fboard, "@" );
}
else
{
fprintf( fboard, "%d", BOARD[i][j]);
}
}
fprintf( fboard, "\n");
}
however, this will cause some problems when reading the file back.
suggest:
for (int i=0; i<ROWS; i++)
{
for (int j=0; j<COLS; j++)
{
if (BOARD[i][j] == 0)
{
fprintf( fboard, "%c", ' ' );
}
else if ( BOARD[i][j] == '@' )
{
fprintf( fboard, "%c", '@' );
}
else
{
fprintf( fboard, "%c", BOARD[i][j]);
}
}
fprintf( fboard, "\n");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.