[Solved]-Trying Read Saved Game Connect4 Try Playing Press 0 Save Working Function Read Saved File Q37294900
i am trying to read from saved game Connect4. Try playing andthen press “0” to save; I am working on the function to read fromsaved file.
C computer language question
I have a text file that looks like this:
, , , , , , .
, , , , , , .
, , , ,R , , .
, , , , , , .
, , , , , , .
, ,R , ,Y, ,R.
I need to write a program that reads just the values(R or Y) andputs them in corresponding places in array[41]. So,array[6]=’R’,array[4]=’Y’.
I get weird output when I scan the array. the code i am usingand it doesn’t work.
#include <stdio.h>
int readGame(char*);
void displayBoard();
int legalMove(int, char *, char);
void displayBoard(char *);
int gameWon(char *, char);
int gameWonHor(char *work_array, char player_num);
int gameWonVer(char *work_array, char player_num);
int gameWonDiRight(char *work_array, char player_num);
int gameWonDiLeft(char *work_array, char player_num);
void writeGame(char*);
int main()
{
//introduce variables
int u=0;
int i, column, t, line;
char junk, legal;
char player_num=’R’;
char work_array[91];
char gamestart=’r’;
FILE * fp1;
fp1=fopen(“Connect4Test.txt”,”w”);
fclose(fp1);
printf(“Hi! you are entering Connect Four Game. Press ‘s’ tobegin or ‘r’ to start from a saved gamen”);
scanf(“%c%c”, &gamestart, &junk);
while (gamestart!=’r’&&gamestart!=’s’){
printf(“Invalid input. Try againn”);
//printf(“%c”,gamestart);
scanf(“%c”, &gamestart);}
if (gamestart==’r’){
readGame(work_array);}
else if (gamestart==’s’){
//intial board display
printf(” 1 2 3 4 5 6 7n”);
for (i=0;i<=41;i++){
work_array[i]=’ ‘;
if (i%7==0&&i>0){
printf(“|n”);
}
printf(“|%c”,work_array[i]);
}
printf(“|n”);
}
///starting the game with 2 players
do{
printf(“Player %c enter the column 1-7n”, player_num);
scanf(” %d”, &column);
while(legalMove(column, work_array, player_num)==0){
printf(“Illegal move! Player %c enter the column 1-7n”,player_num);
scanf(” %d”, &column);}
//printf(“%d legaln”,legal);
//printf(“check %d”,column);
displayBoard(work_array);
if (gameWon(work_array, player_num)==1){
printf(“Game won by player %cn”, player_num);
u=1000;
}
u++;
if (player_num==’R’){
player_num=’Y’;}
else player_num=’R’;
//printf(“%c playern”, player_num);
//printf(“won=%d, u=%d”, gameWon(work_array, player_num), u);
}while((legal=1)&&(u<=41)&&(column!=0));
printf(“Game Over!n”);
return(0);
}
///functions
int readGame(char* work_array){
FILE*saved_game = fopen(“Connect4Save.txt”, “r”);
if (saved_game){
//file exists and can be opened
printf(“file found, loading game…n”);
int i,k;
for (i=0;i<=41;i++){
work_array[i]=’ ‘;}
for (k=0;k<=41;k++){
fscanf(saved_game,”%c,%c,%c,%c,%c,%c,%c^.”,&work_array[7*k],&work_array[7*k+1],&work_array[7*k+2],&work_array[7*k+3],&work_array[7*k+4],&work_array[7*k+5],&work_array[7*k+6]);
//printf(“n.%c,%c,%c,%c,%c,%c,%c”,work_array[7*k],work_array[7*k+1],work_array[7*k+2],work_array[7*k+3],work_array[7*k+4],work_array[7*k+5],work_array[7*k+6]);
}
for (i=0;i<=41;i++){
printf(“%c”,work_array[i]);
fclose(saved_game);
displayBoard(work_array);
}
// close file when you’re done
}
else{
//file doesn’t exists or cannot be opened
printf(“file doesn’t exist. Restart the gamen”);
}
}
//void displayBoard(){}
int legalMove(int column, char *work_array, charplayer_num){
int legal=0;
int k;
if (column==0){
writeGame(work_array);
return(1);
}
else if ((column>=1)&&(column<=7)){
//printf(“function loopn”);
for (k=column-1;k<=column+34;k=k+7) {
//printf(“function loop2n”);
if (work_array[k]==’ ‘){
//printf(“function loop3n”);
legal=1;
work_array[k]=player_num;
work_array[43]=k;
//printf(“k=%dn”,k);
//printf(“%d function loop legal %dn”, k, legal);
//printf(“%dn”,k);
return(1);
}}}
else{
legal=0;
}
return(legal);
}
void displayBoard(char * work_array){
int i,k;
printf(” 1 2 3 4 5 6 7n”);
for (k=5;k>=0;k–){
//printf(“i=%d k=%d”,i,k);
for (i=7*k;i<=7*k+6;i++){
printf(“|%c”,work_array[i]);
}
printf(“|n”);
}
}
int gameWon(char *work_array, char player_num){
if (gameWonHor(work_array, player_num)||gameWonVer(work_array,player_num)||gameWonDiLeft(work_array,player_num)||gameWonDiRight(work_array, player_num)){
return(1);
}
else return(0);
}
int gameWonHor(char *work_array, char player_num){
int k,i;
for (k=0;k<=35;k=k+7){
//printf(“i=%d k=%d”, i, k);
for (i=k;i<=k+3;i++){
if(((work_array[i]==work_array[i+1])&&(work_array[i+1]==work_array[i+2])&&(work_array[i+2]==work_array[i+3])&&work_array[i+3]==’R’)||((work_array[i]==work_array[i+1])&&(work_array[i+1]==work_array[i+2])&&(work_array[i+2]==work_array[i+3])&&work_array[i+3]==’Y’)){
return(1);
}
}
}
//printf(“i=%d k=%d”, i, k);
return(0);
}
int gameWonVer(char *work_array, char player_num){
int k,i;
for (k=0;k<=14;k=k+7){
//printf(“i=%d k=%d”, i, k);
for (i=k;i<=k+6;i++){
if(((work_array[i]==work_array[i+7])&&(work_array[i+7]==work_array[i+14])&&(work_array[i+14]==work_array[i+21])&&work_array[i+21]==’R’)||((work_array[i]==work_array[i+7])&&(work_array[i+7]==work_array[i+14])&&(work_array[i+14]==work_array[i+21])&&(work_array[i+21])==’Y’)){
return(1);
}
}
}
//printf(“i=%d k=%d”, i, k);
return(0);
}
int gameWonDiLeft(char *work_array, char player_num){
int k,i;
for (k=0;k<=14;k=k+7){
//printf(“i=%d k=%d”, i, k);
for (i=k+3;i<=k+6;i++){
if((work_array[i]==work_array[i+6])&&(work_array[i+6]==work_array[i+12])&&(work_array[i+12]==work_array[i+18])&&(work_array[i+18]==player_num)){
return(1);
}
}
}
//printf(“i=%d k=%d”, i, k);
return(0);
}
int gameWonDiRight(char *work_array, char player_num){
int k,i;
for (k=0;k<=14;k=k+7){
//printf(“i=%d k=%d”, i, k);
for (i=k;i<=k+3;i++){
if((work_array[i]==work_array[i+8])&&(work_array[i+8]==work_array[i+16])&&(work_array[i+16]==work_array[i+24])&&(work_array[i+24]==player_num)){
return(1);
}
}
}
//printf(“i=%d k=%d”, i, k);
return(0);
}
void writeGame(char* work_array){
int i,k;
FILE*saved_game = fopen(“Connect4Save.txt”, “w”);
for (k=5;k>=0;k–){
for (i=7*k;i<=7*k+6;i++){
fprintf(saved_game,”%c”,work_array[i]);
if ((i-6)%7!=0){
fprintf(saved_game,”,”);
}
}
fprintf(saved_game, “.n”);
}
printf(“Your progress is saved. Here is what wasrecorded:n”);
fclose(saved_game);
}/* Use the function showMyFile(“filename”) to display yourfile!*/
35 36 37 38 39 40 41
28 29 30 31 32 33 34
21 22 23 24 25 26 27
14 15 16 17 18 19 20
7 8 9 10 11 12 13
0 1 2 3 4 5 6
Expert Answer
Answer to i am trying to read from saved game Connect4. Try playing and then press “0” to save; I am working on the function to re… . . .
OR

