>Daniel's Homepage

Posts

My posts about programming and things.
Date format is day/month/year because I'm sane.

demo recording and playback

27/4/2020

I decided to try adding demo recording/playback to the silly minesweeper game.

The demo is played back in realtime, meaning it will playback at the same speed as you.

You can record a demo using: ./ncsweeper -record demofille.dem
You can play a demo using: ./ncsweeper -play demofille.dem


This isn't portable, different compilers, endianness and cpu word size will produce incompatible demo files. I know these problems exist but I chose to not tackle them this time around.

The first structure written to file is a header that stores game field data, defined as:

struct demo_header
{
	int width;
	int height;
	int mine_count;
};
Next, a total of header.mine_count mines are written to file simply defined as:
struct demo_mine
{
	int x;
	int y;
};
Next written to file is the number of actions performed. These actions are then written to file in a structure defined as:
enum DEMO_ACTION_TYPE
{
	NONE = 0,
	GOUP,
	GODOWN,
	GOLEFT,
	GORIGHT,
	FLAG,
	REVEAL,
	QUIT,
};

struct demo_action
{
	double action_pre_delay;
	enum DEMO_ACTION_TYPE type;
	int start_x;
	int start_y;
};
This structure holds the starting position of the cursor for the action, the type of action (see the enum) and the time it took for the player to perform the action. These actions are recorded and loaded from file into a list which the game uses to play the demo back.



Here is a demo of my best time on a 50x50 playfield.

You can find the source code in my git repo.



RSS feed
FSF member

page generated 1/12/2024 using websitegenerator in C