-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSave.c
More file actions
110 lines (110 loc) · 3.95 KB
/
Copy pathSave.c
File metadata and controls
110 lines (110 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include "Board.h"
#include "Game.h"
#include "Save.h"
#include "Movement.h"
void saveGame(void)//saves the game
{
FILE* save;
save = fopen("save.bin","wb");//opens the file for writing binary data
if(save == NULL)//checks if the file was opened successfully
{
printf("ERROR: failed to open file");
return;
}
fwrite(board,64,1,save);//writes the board in ascii mode
fwrite(&graveyard1Size,1,1,save);//writes the size of graveyard 1 in binary mode
fwrite(player1Graveyard,graveyard1Size,1,save);//writes graveyard 1 in ascii mode
fwrite(&graveyard2Size,1,1,save);//writes the size of graveyard 2 in binary mode
fwrite(player2Graveyard,graveyard2Size,1,save);//writes graveyard 2 in ascii mode
fwrite(¤tPlayer,1,1,save);//writes the current player
fclose(save);//closes the file
}
void loadGame(void)//the function that loads the game
{
FILE* load;
load = fopen("save.bin","rb");//opens the file for reading binary data
if(load == NULL)//checks if the file was opened successfully
{
printf("ERROR: failed to open file");
return;
}
fread(board,64,1,load);//reads the board
fread(&graveyard1Size,1,1,load);//reads the size of graveyard 1
fread(player1Graveyard,graveyard1Size,1,load);//reads graveyard 1
fread(&graveyard2Size,1,1,load);//reads the size of graveyard 2
fread(player2Graveyard,graveyard2Size,1,load);//reads graveyard 2
fread(¤tPlayer,1,1,load);//reads the current player
fclose(load);//closes the file
turn = 0;//sets the turn to 0 to reset the undo and redo data
maxTurn = turn;//sets maxTurn equal to turn
}
bool undo()//the function that does the undo operation
{
if(turn <= 0)//checks if there is a move to undo
{
return false;
}
else
{
currentPlayer = !currentPlayer ;//changes the current player
if( gameWin != none)//checks if the game state is checkmate or draw
gameWin = none;
loadUndoMove();//loads the move to undo
movePiece(command,board);//moves the piece
if(deadPieces[turn] != 0)//checks if a piece was captured this turn
{
board[command.currentY][command.currentX] = deadPieces[turn];//restores the captured piece
if(isupper(deadPieces[turn]))//checks which player owns the piece and reduces the size of his graveyard
{
graveyard1Size--;
}
else
{
graveyard2Size--;
}
}
if(promotion[turn] != 0)//checks if a pawn was promoted at this turn
{
if(currentPlayer == firstPlayer)//checks which player did the promotion
{
board[command.nextY][command.nextX] = 'P';//restores the piece
}
else
{
board[command.nextY][command.nextX] = 'p';//restores the piece
}
}
turn--;//reduces the turn by one
printBoard();//prints the board
}
return true;
}
void redo()//the function that does the redo operation
{
if(turn >= maxTurn || turn < 0)//checks if there is a move to redo
{
redoErrorPrint = true;//prints an error if there is no move to redo
}
else
{
loadRedoMove();//loads the move for redo
movePiece(command,board);//moves the piece
if(promotion[turn+1] != 0 )//checks if a promotion occurred in this turn
{
board[command.nextY][command.nextX] = promotion[turn+1];//does the promotion
}
if(deadPieces[turn+1] != 0)
{
if(isupper(deadPieces[turn+1]))
graveyard1Size++;//adds the dead pieces back to graveyard
else
graveyard2Size++;
}
currentPlayer = !currentPlayer;//changes the current player
turn++;//increases the turn by one
printBoard();//prints the board
}
}