# How to save game state in C in Allegro?

I'm developing my 1st board-game (go-game) in C with Allegro 5, for a course project. Part of my goal is to make a gamesave of the current gamestate. Let's say for 2 players if we exit the game, and log in the game, we want to reload the previous gamestate such that the position of the stones by 2 players to be preserved in their positions. How can I achieve this? I will appreciate any related reference, tutorial, or a git reference I can learn more about saving the states of the game.

P.S. I checked the old questions about saving game state, but there are guides only for other languages/libraries not in C/Allegro.

• What exactly is the part you are stuck on and can't figure out how to perform from Allegro in the questions you've already found here?
– user35344
Jan 20 '19 at 13:41
• I don't understand what exactly "state" means in game dev? and how it should be implemented?
– SarD
Jan 20 '19 at 15:03
• Since I am 1st-year BSc in CS, I dont have enough background in CS. My question was I don't understand what exactly "state" means ? I was trying to read gamedevelopment.tutsplus.com/articles/… but looks very complicated for me. I will appreciate if someone could explain it in a simple word with an example.
– SarD
Jan 20 '19 at 15:14
• "State" is just whatever information describes the current situation in your program. For instance, if the player just started level 5 and has 100 points, the game's state could be described as "level = 5, points = 100". It sounds like in your game, your state is the position of the ball. How have you tried saving and loading that position information to a file so far? Jan 20 '19 at 15:19
• Despite the title, gamedev.stackexchange.com/questions/25963/… contains good, language-agnostic answers. Unfortunate because save games is a language-agnostic problem. Jan 20 '19 at 22:55

The name of the method you're looking for is called 'serialization'. Whether it is output to a text file or a binary file doesn't matter. What matters is that you store the data necessary to recreate a game state and that you read it back the same way you save it.

In Allegro 5, you can use the https://liballeg.org/a5docs/trunk/file.html (FILE I/O) routines to save your text or to save binary data.

As for your specific situation, you have a board (a 2D array) of stone types (an enum).

The value of a position on the board can be in one of 3 different states ; 1) empty 2)black 3)white.

So first, make an enum for your data :

enum PIECE {
NONE = 0,
WHITE = 1,
BLACK = 2
};

const char[3] piecetypes = {'-' , 'O' , 'X'};



In GO, there are usually 19x19 pieces.

In serializing data like an array, we need to record its size somewhere.

First job is to open a file. Typical usage is of http://www.cplusplus.com/reference/cstdio/fopen/ fopen, or in Allegro 5, https://liballeg.org/a5docs/trunk/file.html#al_fopen al_fopen.

Once the file is open for writing (make sure to specify whether you're writing in text or binary mode!!!), write the board to the file.

Since we're using an enum with less than 8 values, and there aren't that many values (19x19 = 361), we'll represent our data in the file with a char (a single byte).

ALLEGRO_FILE* f = al_fopen("go.sav" , "w");/// Opening in text mode for writing
if (!f) {Bail();}
for (unsigned int y = 0 ; y < 19 ; ++y) {
for (unsigned int x = 0 ; x < 19 ; ++x) {
al_fputc(piecetypes[board[y][x]]);
}
al_fputc('\n');
}
al_fputc('\n');
al_fclose(f);
/// All done


Now our data should be saved something like this (open the text file in your favorite editor and verify the contents) :

-------------------
-------------------
-------------------
-------------------
-------------------
-----XX------------
------O------------
---------O---------
---------XX--------
----------OO-------
---------O---------
-------------------
-------------------
-------------------
-------------------
-------------------
-------------------
-------------------
-------------------


Now you need to be able to 'unserialize' the data, that is to say, to read it back from the file.

Open the file for reading and copy the data back into your board :

ALLEGRO_FILE* f = al_fopen("go.sav" , "r");
if (!f) {Bail();}
for (unsigned int y = 0 ; y < 19 ; ++y) {
for (unsigned int x = 0 ; x < 19 ; ++x) {
char c = al_fgetc(f);
if (c == '-') {board[y][x] = NONE;continue;}
board[y][x] = (c == 'O')?WHITE:BLACK;
}
al_fgetc(f);/// consume the newline
}
al_fclose(f);


First, decide what information of your game describes a "game state". In other words, what is the difference between a "new game" and what state the user currently is?

Examples of that are:

• Character's stats (Name, level, experience, strength, intelligence, money, etc)
• Quests (What quests have the user completed? What progress do they have on active ones?)
• Maps (Has the player visited a place before? Do they have fog of war that is explored next time they visit it? After the user has killed enemies on one stage, do they respawn or remain permanently dead?)

Once you have all this information down, it's time to write a file that includes all that information, which is going to be saved on the disc, so it remains there until someone removes it manually. You can pick a simple format like var=value and save all data like this:

character_name=john
character_level=4

quest_kill_dragon=0 # assuming -1 is not started, 0+ started and current state
quest_find_jewel=-1

map_mountain_explored=0 # maybe if you go to a map for the first time a cinematic plays
map_mountain_enemy_2=-1 # -1 on enemy is that they are dead and won't respawn


Please note I used # to display comments, but normally you wouldn't need comments as this file is not meant to be readable by humans. What data you want to save is up to you and what game you are making.

Alternatively you can use JSON to save data on a file, there are plenty of libraries online that help you do that. Unfortunately I haven't used any in the past, so I can't recommend any.

In case its needed, the manual of how to open/close files in C: GNU - Opening and Closing Files

• Thanks for the answer. I am afraid that I did not follow exactly what to do. Let me be more concrete: I am building a board-game called "go" with 2 players; In this game, "state" would mean: 1- the location of stones (black and white), and also 2- the points of player has got so far. Could you please clarify your response based on this scenario? Thanks, and apologize!
– SarD
Jan 22 '19 at 19:04
• @SarA if that detail is important to an answer, make sure it's in your question. Most of us aren't mind readers. ;) Also, make sure you explain how you've tried saving this information so far, and where you got stuck. eg. did you try writing out each players' points the same way TomTsagk shows writing out a character_level into a file in the example above? Jan 22 '19 at 20:21