Slider

Check-in [9d9c71b5b3]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added a simple count of the number of moves taken.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256:9d9c71b5b36ec13b0bbcd0ebaa37bb2efb624df9c14c7fcabeb1ff77e6e2dac3
User & Date: LesFarrell 2017-07-16 22:06:39
Context
2017-07-16
22:06
Added a simple count of the number of moves taken. Leaf check-in: 9d9c71b5b3 user: LesFarrell tags: trunk
2017-07-15
15:26
First commit of a SDL puzzle game written in C which compiles with the Tiny C Compiler. check-in: a7565be355 user: LesFarrell tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to compile.bat.

1
2
3
4
@echo off
c:\tcc\tcc -Wl,-subsystem=gui slider.c sdl2.def -o slider.exe -v -bench
REM c:\tcc\tcc slider.c sdl2.def -o slider.exe -v -bench
pause

|


1
2
3
4
@echo off
c:\tcc\tcc -Wall -Wl,-subsystem=gui slider.c sdl2.def -o slider.exe -v -bench
REM c:\tcc\tcc slider.c sdl2.def -o slider.exe -v -bench
pause

Changes to slider.c.

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
..
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
111

112
113
114
115
116
117




118
119
120



121
122
123
124
125
126
127

#define SDL_MAIN_HANDLED	1
#include "SDL2\SDL.h"

SDL_Window	*window;
SDL_Surface *surface;
SDL_Surface *image;


enum {UP, DOWN, LEFT, RIGHT};

char board[5][5]={'A','B','C','D','E'
								 ,'F','G','H','I','J'
								 ,'K','L','M','N','O'
				  			 ,'P','Q','R','S','T'
				  			 ,'U','V','W','X',' '};

void move(int *px, int *py, int direction) {
	char c = 0;
	int lx = *px, ly = *py;

	if (direction == LEFT)	if (*px > 0) (*px)--;
	if (direction == RIGHT)	if (*px < 4) (*px)++;
	if (direction == DOWN) 	if (*py < 4) (*py)++;
	if (direction == UP)		if (*py > 0) (*py)--;

	c = board[*py][*px];
	board[*py][*px] = ' ';
	board[ly][lx] = c;
}

void shuffle(int *px, int *py) {
	int direction = 0;
................................................................................
	for (int x = 0; x < 5; x++)
		for (int y = 0; y < 5; y++) if (board[x][y] == count) count++;
	if (count == 89 && *px == 4 && *py==4) return 1; else return 0;
}

int solvedmessage(void) {
	int buttonid = 0;


	const SDL_MessageBoxButtonData buttons[] = {
		{0, 0, "No" }, { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "Yes" },      
	};
  

	const SDL_MessageBoxData messageboxdata = {
		SDL_MESSAGEBOX_INFORMATION,
		NULL,
		"Congratulations ",
		"Would you like another game?",
		SDL_arraysize(buttons),
		buttons,
		NULL
	};

	SDL_ShowMessageBox(&messageboxdata, &buttonid);
	return buttonid;				
}

int main(int argc, char* argv[])
{
	int px = 4, py = 4;

	SDL_Event		event;

	SDL_Init(SDL_INIT_VIDEO);
	
	window = SDL_CreateWindow( "Slider", SDL_WINDOWPOS_UNDEFINED, 
		SDL_WINDOWPOS_UNDEFINED, 260, 260, SDL_WINDOW_OPENGL );

	surface = SDL_GetWindowSurface(window);
	image = SDL_LoadBMP( "tiles.bmp" );

	shuffle(&px, &py);

	
	for(;;) {
		if (SDL_PollEvent(&event) == 0 ) {
			drawboard();
			if (solved(&px, &py) == 1) {				
				if (solvedmessage() == 1) shuffle(&px, &py); else break;




 			}
		} 
		else {



			if (event.type == SDL_KEYDOWN) {
      	switch( event.key.keysym.sym ){
        	case SDLK_LEFT:  
        		move(&px, &py, LEFT);
            break;
					case SDLK_RIGHT: 
						move(&px, &py, RIGHT);







>












>
|
|
|
|
>







 







>





>




|












>











>





|
>
>
>
>



>
>
>







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
..
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

#define SDL_MAIN_HANDLED	1
#include "SDL2\SDL.h"

SDL_Window	*window;
SDL_Surface *surface;
SDL_Surface *image;
int movestaken = 0;

enum {UP, DOWN, LEFT, RIGHT};

char board[5][5]={'A','B','C','D','E'
								 ,'F','G','H','I','J'
								 ,'K','L','M','N','O'
				  			 ,'P','Q','R','S','T'
				  			 ,'U','V','W','X',' '};

void move(int *px, int *py, int direction) {
	char c = 0;
	int lx = *px, ly = *py;
	
	if (direction == LEFT)	if (*px > 0) {(*px)--; movestaken++;}
	if (direction == RIGHT)	if (*px < 4) {(*px)++; movestaken++;}
	if (direction == DOWN) 	if (*py < 4) {(*py)++; movestaken++;}
	if (direction == UP)		if (*py > 0) {(*py)--; movestaken++;}
	
	c = board[*py][*px];
	board[*py][*px] = ' ';
	board[ly][lx] = c;
}

void shuffle(int *px, int *py) {
	int direction = 0;
................................................................................
	for (int x = 0; x < 5; x++)
		for (int y = 0; y < 5; y++) if (board[x][y] == count) count++;
	if (count == 89 && *px == 4 && *py==4) return 1; else return 0;
}

int solvedmessage(void) {
	int buttonid = 0;
	char buffer[512] = {'\0'};

	const SDL_MessageBoxButtonData buttons[] = {
		{0, 0, "No" }, { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "Yes" },      
	};
  
  sprintf(buffer,"Well done, you took %d moves\nWould you like another game?",movestaken);
	const SDL_MessageBoxData messageboxdata = {
		SDL_MESSAGEBOX_INFORMATION,
		NULL,
		"Congratulations ",
		buffer,
		SDL_arraysize(buttons),
		buttons,
		NULL
	};

	SDL_ShowMessageBox(&messageboxdata, &buttonid);
	return buttonid;				
}

int main(int argc, char* argv[])
{
	int px = 4, py = 4;
	char buffer[80] = {'\0'};
	SDL_Event		event;

	SDL_Init(SDL_INIT_VIDEO);
	
	window = SDL_CreateWindow( "Slider", SDL_WINDOWPOS_UNDEFINED, 
		SDL_WINDOWPOS_UNDEFINED, 260, 260, SDL_WINDOW_OPENGL );

	surface = SDL_GetWindowSurface(window);
	image = SDL_LoadBMP( "tiles.bmp" );

	shuffle(&px, &py);
	movestaken = 0;
	
	for(;;) {
		if (SDL_PollEvent(&event) == 0 ) {
			drawboard();
			if (solved(&px, &py) == 1) {				
				if (solvedmessage() == 1) {
					shuffle(&px, &py); 
					movestaken = 0;
				}
					else break;
 			}
		} 
		else {
			sprintf(buffer,"Slider - Move: %d", movestaken);
			SDL_SetWindowTitle(window,buffer);
			
			if (event.type == SDL_KEYDOWN) {
      	switch( event.key.keysym.sym ){
        	case SDLK_LEFT:  
        		move(&px, &py, LEFT);
            break;
					case SDLK_RIGHT: 
						move(&px, &py, RIGHT);

Added slider.exe.

cannot compute difference between binary files

Changes to tiles.bmp.

cannot compute difference between binary files