Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1817 Albom 1
#include "system/kolibri.h"
2
#include "system/stdlib.h"
3
#include "system/string.h"
4
 
5
char CONSOLE[] = "/sys/lib/console.obj";
6
 
7
#define MENTION	_printf("Arrows to move left, up, right or down, or 'Esc' to exit: \n\n");
8
 
9
int** field;
10
int emptyCell_x, emptyCell_y;
11
 
12
char NOT_VALID_MOVE[] = {"Not valid move.\n\n"};
13
 
14
void (* _stdcall con_init)(unsigned w_w, unsigned w_h, unsigned s_w, unsigned s_h, const char* t);
15
void (* _cdecl _printf)(const char* format,...);
16
void (* _stdcall __exit)(char bCloseWindow);
17
 int (* _stdcall _getch)(void);
18
 
19
 
20
//------------------
21
void init()
22
{
23
	int x,y, i,j;
24
	srand( kol_system_time_get() );
25
	for(i=1; i<=15;)
26
	{
27
		x=rand()%4; y=rand()%4;
28
		if(field[x][y] == 0) field[x][y] = i++;
29
	}
30
 
31
	for(i=0; i<4; i++) //to find the empty cell
32
		for(j=0; j<4; j++)
33
			if(field[j][i] == 0)
34
			{
35
				emptyCell_x=j; emptyCell_y = i; return;
36
			}
37
 
38
}
39
 
40
//---------------------
41
void printField()
42
{
43
	int i,j;
44
	for(i=0; i<4; i++)
45
	{
46
		for(j=0; j<4; j++)
47
			if(field[j][i]) _printf("%3d", field[j][i]);
48
			else _printf("  _");
49
		_printf("\n\n");
50
	}
51
	_printf("\n\n");
52
}
53
 
54
//-----------------------
55
int notEndYet()
56
{
57
	int i,j;
58
	for(i=0; i<3; i++)
59
		for(j=0; j<4; j++)
60
			if(field[j][i] != 4*i+j+1) return 0; //go on play
61
	if(field[0][3] != 13) return 0;
62
 
63
	return 1; //victory!
64
}
65
 
66
//---------------   allows move the emply cell
67
int move()
68
{
69
 
70
unsigned short c;
71
 
72
while(1)
73
	{
74
	c = _getch();
75
	switch(c)
76
		{
77
		case 0x4d00:
78
			if(emptyCell_x==0)
79
				{
80
				_printf(NOT_VALID_MOVE);
81
				break;
82
				}
83
			else
84
				{
85
				field[emptyCell_x][emptyCell_y] = field[emptyCell_x-1][emptyCell_y];
86
				field[emptyCell_x-1][emptyCell_y] = 0;
87
				emptyCell_x--;
88
				return 1;
89
				}
90
			case 0x5000:
91
				if(emptyCell_y==0)
92
					{
93
					_printf(NOT_VALID_MOVE);
94
					break;
95
					}
96
				else
97
					{
98
					field[emptyCell_x][emptyCell_y] = field[emptyCell_x][emptyCell_y-1];
99
					field[emptyCell_x][emptyCell_y-1] = 0;
100
					emptyCell_y--;
101
					return 2;
102
					}
103
			case 0x4b00:
104
				if(emptyCell_x==3)
105
					{
106
					_printf(NOT_VALID_MOVE);
107
					break;
108
					}
109
				else
110
					{
111
					field[emptyCell_x][emptyCell_y] = field[emptyCell_x+1][emptyCell_y];
112
					field[emptyCell_x+1][emptyCell_y] = 0;
113
					emptyCell_x++;
114
					return 3;
115
					}
116
			case 0x4800:
117
				if(emptyCell_y==3)
118
					{
119
					_printf(NOT_VALID_MOVE);
120
					break;
121
					}
122
				else
123
					{
124
					field[emptyCell_x][emptyCell_y] = field[emptyCell_x][emptyCell_y+1];
125
					field[emptyCell_x][emptyCell_y+1] = 0;
126
					emptyCell_y++;
127
					return 4;
128
					}
129
			case 0x011b: __exit(1);
130
			default: MENTION
131
		}
132
	}
133
}
134
 
135
//----------------- main function
136
void kol_main()
137
{
138
 
139
int i;
140
kol_struct_import *imp;
141
 
142
imp = kol_cofflib_load(CONSOLE);
143
if (imp == NULL)
144
	kol_exit();
145
 
146
con_init = ( _stdcall  void (*)(unsigned, unsigned, unsigned, unsigned, const char*))
147
		kol_cofflib_procload (imp, "con_init");
148
if (con_init == NULL)
149
	kol_exit();
150
 
151
_printf = ( _cdecl void (*)(const char*,...))
152
		kol_cofflib_procload (imp, "con_printf");
153
if (_printf == NULL)
154
	kol_exit();
155
 
156
__exit = ( _stdcall void (*)(char))
157
		kol_cofflib_procload (imp, "con_exit");
158
 
159
if (__exit == NULL)
160
	kol_exit();
161
 
162
 
163
_getch = ( _stdcall int (*)(void))
164
		kol_cofflib_procload (imp, "con_getch2");
165
 
166
if (_getch == NULL)
167
	kol_exit();
168
 
169
con_init(-1, -1, -1, -1, "Console15 by O.Bogomaz");
170
 
171
field = (int**)malloc(4 * sizeof(int*));
172
 
173
for( i=0; i<4; i++)
174
	field[i] = (int*)malloc(4 * sizeof(int));
175
 
176
do
177
	init();
178
while(notEndYet());
179
 
180
MENTION
181
 
182
printField();
183
 
184
while(!notEndYet())
185
	{
186
	move();
187
	printField();
188
	}
189
 
190
_printf("\nYou win!\n");
191
__exit(0);
192
}