[英]Greed on C Program trouble saving game to file
We're having this project of implementing Greed (greedjs.com) in C. everytime user makes a move, this function should update. 我们有一个在C中实现Greed(greedjs.com)的项目。每次用户移动时,此功能都应更新。 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? 当@移动时,它将保留我的BOARD [ROW] [COLUMN] = 0的特定元素。我如何将其写入文件,以便打印0而不是0?
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.