C语言二维数组在文件中读写的问题,谢谢
这是一个用二位数组写的五子棋小游戏的代码,我的思路是通过键盘输入坐标显示棋子,当输0 0时保存棋盘并结束游戏,下一次进入程序时,选择1.继续游戏;2.显示上局棋盘。但在文件保存方便遇到了问题。文件的读写我再代码中已经注释出来了,希望各位前辈能够解疑答惑,不胜感激。#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void printChessboard();
void playChess();
void Load();
int judge(int,int);
#define N 15
int chessboard[N + 1][N + 1]={0};
int i,j;
int whoseTurn = 0;
//读取棋盘
void Load()/*读取棋局*/
{
FILE* fp;
if( ( fp = fopen ("date.txt", "r" ) ) == NULL )
{
exit(0);
}
else
for (i = 0; i <= N+1; i++)
{
for (j = 0; j <= N+1; j++)
{
fscanf(fp,"%d",&chessboard[i][j]);
}
}
for (i = 0; i <= N+1; i++)
{
for (j = 0; j <= N+1; j++)
{
if (0 == i)
printf("%3d",j);
else if (j == 0)
printf("%3d",i);
else if (1 == chessboard[i][j])
printf(" X");
else if (2 == chessboard[i][j])
printf(" O");
else if(0 == chessboard[i][j])
printf(" *");
else
exit(0);
}
printf("\n");
}
}
void printChessboard()
{
int i, j;
for (i = 0; i <= N; i++)
{
for (j = 0; j <= N; j++)
{
if (0 == i)
printf("%3d",j);
else if (j == 0)
printf("%3d",i);
else if (1 == chessboard[i][j])
printf(" X");
else if (2 == chessboard[i][j])
printf(" O");
else if(0 == chessboard[i][j])
printf(" *");
else
exit(0);
}
printf("\n");
}
}
void playChess(void)
{
FILE *fp;
fp=fopen("date.txt","w");
printChessboard();
if (1 == whoseTurn % 2)
{
printf("轮到玩家1,请输入棋子的位置,格式为行号+空格+列号+1:");
scanf("%d%d", &i, &j);
for (int a = i; a<=i;a++)
{
for (int b= j; b <= j;b++)
{
scanf("%d",&chessboard[a][b]);
fprintf(fp,"%d",chessboard[i][j]);//保存棋盘
}
}
fclose(fp);
if(i!=0)
chessboard[i][j] = 1;
else
exit(0);
}
else
{
printf("轮到玩家2,请输入棋子的位置,格式为行号+空格+列号+2:");
scanf("%d%d", &i, &j);
for (int a = i; a<= i;a++)
{
for (int b= j; b <=j;b++)
{
scanf("%d",&chessboard[a][b]);
fprintf(fp,"%d",chessboard[i][j]);//保存棋盘
}
}
fclose(fp);
if(i!=0)
chessboard[i][j] = 2;
else
exit(0);
}
system("cls");
if (judge(i, j))
{
if(1 == whoseTurn % 2)
{
printf("玩家1胜!\n");
exit(0);
}
else
{
printf("玩家2胜!\n");
exit(0);
}
}
}
int judge(int x, int y)//判断输赢
{
int i, j;
int t = 2 - whoseTurn % 2;
for (i = x - 4, j = y; i <= x; i++)
{
if (t == chessboard[i][j] && t == chessboard[i + 1][j] && t == chessboard[i + 2][j] && t == chessboard[i + 3][j] && t == chessboard[i + 4][j])
return 1;
}
for (i = x, j = y - 4; j <= y; j++)
{
if (t == chessboard[i][j] && t == chessboard[i][j + 1] && t == chessboard[i][j + 2] && t == chessboard[i][j + 3] && t == chessboard[i][j + 4])
return 1;
}
for (i = x - 4, j = y - 4; i <= x, j <= y; i++, j++)
{
if (j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j + 1] && t == chessboard[i + 2][j + 2] && t == chessboard[i + 3][j + 3] && t == chessboard[i + 4][j + 4])
return 1;
}
for (i = x + 4, j = y - 4; i >= 1, j <= y; i--, j++)
{
if (j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i - 1][j + 1] && t == chessboard[i - 2][j + 2] && t == chessboard[i - 3][j + 3] && t == chessboard[i - 4][j + 4])
return 1;
}
return 0;
}
void main()
{
printf("1.新游戏 2.显示上局棋盘\n");
fflush(stdin);
char choice;
choice=getchar();
switch(choice)
{
case'1':
printf("欢迎进入游戏");
system("cls");
while(1)
{
whoseTurn++;
playChess();
}
break;
case'2':
Load();
}
}