Subversion Repositories Kolibri OS

Rev

Rev 2295 | Rev 5230 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2295 leency 1
//Leency 06.10.2011, Flood-it! v2.4, GPL
2276 leency 2
 
3
#include "lib\kolibri.h"
4
#include "lib\random.h"
5
 
6
#ifndef AUTOBUILD
7
#include "lang.h--"
8
#endif
9
 
10
system_colors sc;
11
proc_info Form;
12
dword stak[100]; // 
13
 
14
// 
15
int DIFFICULTY_LEV_PARAMS[]={ 28, 14, 25,  //
16
                              17, 28, 50}; //
17
 
18
//- ""
19
int BLOCK_SIZE = 28; // 
20
int BLOCKS_NUM = 14; //      Y
21
int MAX_CLICKS = 25; //    
22
 
23
int CLICKS = 0;      //    
24
 
25
#define USER_PANEL_WIDTH 119
26
 
27
//      ,   ()  ,
28
//     
29
dword FIELD_COLORS[]= {0xf18db6, 0x605ca8, 0xfddc80, 0xdc4a20, 0x46b1e2, 0x7e9d1e,   0x232323, 0};
30
char *BOARD_SIZES[]={ "S", "L", 0 };
31
 
32
 
33
#ifdef LANG_RUS
2283 leency 34
	char *BUTTON_CAPTIONS[]={ "  [F2]", "  [F1]", " 室 [Esc]", 0};
2276 leency 35
	char CLICKS_TEXT[]=" :   /";
36
	char LEVELS_TEXT[]=":";
37
 
38
	char HELP_WINDOW_CAPTION[]="";
39
	char *HELP_TEXT[]={	"   Flood-it?",
40
	"",
41
	"   梥⮬  ࠭祭 ᫮ 室.",
42
	" 稭  孥  ⪨. 롥 梥,    ",
43
	"⨪ ᫥,  ⪨  ⨬ 梥⮬ - ⠪  ᮥ",
44
	"ᥤ ⪨ ⮩  ᪨. 墠  㦭  쭮",
45
	"᫮ 室. 蠣 ⥣   ਭ樯 - ,",
2281 leency 46
	"⮡ !",
47
	"",
48
	" ⠪  蠬:",
49
	"[Q] [W] [E]",
50
	"[A] [S] [D]",
51
	0};
3933 kaitz 52
#elif LANG_EST
53
	char *BUTTON_CAPTIONS[]={ "Uus mng [F2]", "Abi      [F1]", "Vlju   [Esc]", 0};
54
	char CLICKS_TEXT[]="Klikki:   /";
55
	char LEVELS_TEXT[]="Vli:";
56
 
57
	char HELP_WINDOW_CAPTION[]="Help";
58
	char *HELP_TEXT[]={	"Kuidas mngida mngu Flood-it?",
59
	"",
60
	"Ujuta kogu mnguvli le he vrviga lubatud kikude arvuga.",
61
	"Mngu alustad lemisest vasakust nurgast ja edened valides he vrvi",
62
	"vajutades nuppudele vasakul. Kui sa muudad vrvi pragusel alal,",
63
	"siis iga kokkupuutuv sama vrv muutub samaks. Nii saad ujutada",
64
	"teised alad mnguvljal le. Valida saad 2 mnguvlja suuruse",
65
	"vahel. Proovi vli le ujutada etteandtud kikude arvuga!",
66
	"Kaasahaarav ja lbus!",
67
	"",
68
	"Mngida saab ka klaviatuuriga:",
69
	"[Q] [W] [E]",
70
	"[A] [S] [D]",
71
	0};
2276 leency 72
#else
73
	char *BUTTON_CAPTIONS[]={ "New Game [F2]", "Help     [F1]", "Exit    [Esc]", 0};
74
	char CLICKS_TEXT[]="Clicks:   /";
75
	char LEVELS_TEXT[]="Board:";
76
 
77
	char HELP_WINDOW_CAPTION[]="Help";
78
	char *HELP_TEXT[]={	"How to play Flood-it?",
79
	"",
80
	"Flood the whole board with one color within the allowed steps.",
81
	"You start from the top left corner and progress by selecting one",
2281 leency 82
	"of the colored buttons on the left. When you change your current area",
2276 leency 83
	"color, every adjacent square with the same color also changes, that",
84
	"way you can flood other areas of the board. Select from 3 sizes of",
85
	"the board and try to flood-it in the least amount of steps!",
2281 leency 86
	"Addictive and Fun!",
87
	"",
88
	"You can also play with keyboard:",
89
	"[Q] [W] [E]",
90
	"[A] [S] [D]",
91
	0};
2276 leency 92
#endif
93
 
94
 
2283 leency 95
unsigned char color_matrix[28*28]; //    
2276 leency 96
 
2295 leency 97
unsigned char loss_matrix[14*14]={
2276 leency 98
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
99
2, 2, 2, 2, 2, 2, 2,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 4,
125
4, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 4, 1, 4,
126
4, 4, 1, 4, 1, 4, 4, 1, 4, 1, 4, 4, 1, 4,
127
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
128
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
129
};
130
 
131
void main()
132
{
133
	int key, id;
134
 
135
	new_game();
136
 
137
	loop()
138
	{
139
		switch(WaitEvent())
140
		{
141
			case evButton:
142
				id = GetButtonID();
2282 leency 143
				IF (id==1) || (id==4) ExitProcess();
144
				IF (id==2) goto _NEW_GAME_MARK;
145
				IF (id==3) goto _HELP_MARK;
2283 leency 146
				IF (id>=100)
147
				{
148
					make_turn(id-100);
149
					break;
150
				}
2276 leency 151
				if (id>=10)
152
				{
153
					id=id-10*3;
154
 
2282 leency 155
					IF (BLOCK_SIZE == DIFFICULTY_LEV_PARAMS[id]) break; //   
2276 leency 156
 
157
					BLOCK_SIZE = DIFFICULTY_LEV_PARAMS[id]; // 
158
					BLOCKS_NUM = DIFFICULTY_LEV_PARAMS[id+1]; //      Y
159
					MAX_CLICKS = DIFFICULTY_LEV_PARAMS[id+2]; //    
160
 
161
					new_game();
162
 
163
					MoveSize(-1, -1, BLOCK_SIZE*BLOCKS_NUM +14+USER_PANEL_WIDTH, BLOCK_SIZE*BLOCKS_NUM +GetSkinWidth()+14);
164
				}
165
				break;
166
			case evKey:
167
				key = GetKey();
2282 leency 168
				IF (key==027) //Escape
2276 leency 169
					 ExitProcess();
2282 leency 170
				IF (key==050) //F1
2276 leency 171
				{
172
					_HELP_MARK:
173
						CreateThread(#help,#stak);
174
				}
2282 leency 175
				IF (key==051) //F2
2276 leency 176
				{
177
					_NEW_GAME_MARK:
178
						new_game();
179
						draw_clicks_num();
180
						draw_field();
181
				}
2282 leency 182
				IF (key==113) make_turn(0); //Q
183
				IF (key==119) make_turn(1); //W
184
				IF (key==101) make_turn(2); //E
185
				IF (key==097) make_turn(3); //A
186
				IF (key==115) make_turn(4); //S
187
				IF (key==100) make_turn(5); //D
2276 leency 188
				break;
189
			case evReDraw:
190
				draw_window();
191
		}
192
	}
193
}
194
 
195
 
2281 leency 196
void make_turn(int turn_id)
197
{
2282 leency 198
	IF (color_matrix[0]==turn_id) return; //     ,   
199
	IF (CLICKS>=MAX_CLICKS) return; //  
2281 leency 200
 
201
	CLICKS++;
202
	draw_clicks_num();
203
 
204
	fill_field(turn_id);
205
	draw_field();
206
	check_for_end(); //  
207
}
208
 
209
 
2276 leency 210
void draw_window()
211
{
212
	int i, j;
213
	#define BUTTON_SIZE 28
214
 
215
	sc.get();
216
 
217
	DefineAndDrawWindow(300,176, BLOCK_SIZE*BLOCKS_NUM +14+USER_PANEL_WIDTH, BLOCK_SIZE*BLOCKS_NUM +GetSkinWidth()+14, 0x74,sc.work,0,0,"Flood-it!");
218
 
219
	//      
220
	GetProcessInfo(#Form, SelfInfo);
2295 leency 221
	IF (Form.status_window==4) return;
2276 leency 222
 
223
	//  ->  
224
	for (i=0;i<=4;i++)
225
	{
2282 leency 226
		IF (i<>4)
2276 leency 227
			DrawRegion(USER_PANEL_WIDTH+i-5,i, BLOCK_SIZE*BLOCKS_NUM +9-i-i, BLOCK_SIZE*BLOCKS_NUM+9-i-i, sc.work);
228
		else
229
			DrawRegion(USER_PANEL_WIDTH+i-5,i, BLOCK_SIZE*BLOCKS_NUM +9-i-i, BLOCK_SIZE*BLOCKS_NUM+9-i-i, sc.work_graph); //
230
	}
231
	DrawBar(0,0, USER_PANEL_WIDTH-5, BLOCK_SIZE*BLOCKS_NUM+10, sc.work);
232
 
233
	// 
234
	for (i=0;i<2;i++)
235
		for (j=0;j<3;j++)
236
			DefineButton(j*BUTTON_SIZE+17,i*BUTTON_SIZE+15,BUTTON_SIZE,BUTTON_SIZE, i*3+j+100,FIELD_COLORS[i*3+j]);
237
 
238
	// 
239
	for (j=0;j<3;j++)
240
	{
241
		DefineButton(17,j*25+120, 13*6+6, 20, j+2,sc.work_button);
242
		WriteText(17+4,j*25+127,0x80,sc.work_button_text,BUTTON_CAPTIONS[j],0);
243
	}
244
 
245
	//   
246
	WriteText(17,BLOCKS_NUM*BLOCK_SIZE-20+7,0x80,sc.work_text,#LEVELS_TEXT,0);
247
	for (j=0;j<2;j++)
248
	{
249
		DefineButton(j*25+56,BLOCKS_NUM*BLOCK_SIZE-20, 20,20, j+10,sc.work_button);
250
 
2282 leency 251
		IF (BLOCK_SIZE == DIFFICULTY_LEV_PARAMS[j*3]) EDI=0x800080;
2276 leency 252
			else EDI=sc.work_button_text;
253
 
254
		WriteText(j*25+56+8,BLOCKS_NUM*BLOCK_SIZE-20+7,0x80,EDI,BOARD_SIZES[j],0);
255
		WriteText(j*25+56+9,BLOCKS_NUM*BLOCK_SIZE-20+7,0x80,EDI,BOARD_SIZES[j],0);
256
	}
257
 
258
	draw_clicks_num();
259
 
260
	draw_field();
261
 
262
}
263
 
264
 
265
void new_game()
266
{
2283 leency 267
	int i;
2276 leency 268
 
269
	CLICKS = 0;
270
 
2283 leency 271
	for (i=0;i
272
			color_matrix[i] = random(6);
2276 leency 273
}
274
 
275
 
276
void fill_field(int new_color_id)
277
{
2283 leency 278
	int i, j,
2276 leency 279
	old_color_id=color_matrix[0],
280
	restart;
281
	#define MARKED 6
282
 
283
	color_matrix[0]=MARKED;
284
 
285
	_RESTART_MARK:
286
 
287
	restart=0;
288
 
289
	for (i=0;i
290
		for (j=0;j
291
		{
2282 leency 292
			IF (color_matrix[i*BLOCKS_NUM+j]<>old_color_id) continue; //      
293
			IF (color_matrix[i*BLOCKS_NUM+j]==MARKED) continue; //   ,  
2276 leency 294
 
2282 leency 295
			IF (j>0) && (color_matrix[i*BLOCKS_NUM+j-1]==MARKED) color_matrix[i*BLOCKS_NUM+j]=MARKED; // 
296
			IF (i>0) && (color_matrix[i-1*BLOCKS_NUM+j]==MARKED) color_matrix[i*BLOCKS_NUM+j]=MARKED; // 
297
			IF (j
298
			IF (i
2276 leency 299
 
2282 leency 300
			IF (color_matrix[i*BLOCKS_NUM+j]==MARKED) restart=1; //  ,        -    
2276 leency 301
		}
2282 leency 302
	IF (restart) goto _RESTART_MARK;
2276 leency 303
 
2283 leency 304
	for (i=0;i
305
			IF (color_matrix[i]==MARKED) color_matrix[i]=new_color_id;
2276 leency 306
}
307
 
308
 
309
int check_for_end()
310
{
311
	int i, j, ii, jj;
312
 
313
	if (CLICKS>=MAX_CLICKS) // 
314
	{
2282 leency 315
		IF (CLICKS==MAX_CLICKS) //   
2276 leency 316
		{
2283 leency 317
			for (i=0;i
2295 leency 318
					IF (color_matrix[i]<>color_matrix[0]) goto _loss_MARK;
2276 leency 319
			goto _WIN_MARK;
320
		}
321
 
2295 leency 322
		_loss_MARK:
2276 leency 323
 
324
		for (i=0;i<14;i++)
325
			for (j=0;j<14;j++)
326
			{
2282 leency 327
				IF (BLOCK_SIZE == DIFFICULTY_LEV_PARAMS[0])
2276 leency 328
				{
329
					ii=i;
330
					jj=j;
331
				}
332
				else
333
				{
334
					ii=i*2;
335
					jj=j*2;
336
				}
2295 leency 337
				color_matrix[ii*BLOCKS_NUM+jj]=loss_matrix[i*14+j];
338
				color_matrix[ii+1*BLOCKS_NUM+jj]=loss_matrix[i*14+j];
339
				color_matrix[ii*BLOCKS_NUM+jj+1]=loss_matrix[i*14+j];
340
				color_matrix[ii+1*BLOCKS_NUM+jj+1]=loss_matrix[i*14+j];
2276 leency 341
				draw_field();
342
				//Pause(5);
343
			}
344
 
345
		return 1;
346
	}
347
 
2283 leency 348
	for (i=0;i
349
			IF (color_matrix[i]<>color_matrix[0]) return 0;
2276 leency 350
 
351
	//       MAX_CLICKS -> 
352
 
353
	_WIN_MARK:
354
 
355
	for (i=0;i<25;i++)
356
	{
357
		new_game();
358
		draw_field();
359
		Pause(7);
360
	}
361
 
362
	CLICKS=MAX_CLICKS;
363
 
364
	for (i=0;i<14;i++)
365
		for (j=0;j<14;j++)
366
		{
2282 leency 367
			IF (BLOCK_SIZE == DIFFICULTY_LEV_PARAMS[0]) //     
2276 leency 368
			{
369
				ii=i;
370
				jj=j;
371
			}
372
			else
373
			{
374
				ii=i*2;
375
				jj=j*2;
376
			}
377
			color_matrix[ii*BLOCKS_NUM+jj]=win_matrix[i*14+j];
378
			color_matrix[ii+1*BLOCKS_NUM+jj]=win_matrix[i*14+j];
379
			color_matrix[ii*BLOCKS_NUM+jj+1]=win_matrix[i*14+j];
380
			color_matrix[ii+1*BLOCKS_NUM+jj+1]=win_matrix[i*14+j];
381
			draw_field();
382
			//Pause(5);
383
		}
384
	return 1;
385
}
386
 
387
 
388
void draw_clicks_num()
389
{
390
	#define TEXT_X 21
391
	#define TEXT_Y 92
392
 
393
	DrawBar(8*6+TEXT_X, TEXT_Y, 6*2,9, sc.work);
394
 
395
	WriteText(TEXT_X,TEXT_Y,0x80,sc.work_text,#CLICKS_TEXT,0);
396
 
2282 leency 397
	IF (CLICKS<10) EBX=9*6+TEXT_X;
2276 leency 398
		else EBX=8*6+TEXT_X;
399
 
400
	WriteText(EBX,TEXT_Y,0x80,sc.work_text,IntToStr(CLICKS),0);
401
 
402
	WriteText(11*6+TEXT_X,TEXT_Y,0x80,sc.work_text,IntToStr(MAX_CLICKS),0);
403
}
404
 
405
 
406
void draw_field()
407
{
408
	int i, j;
409
	int color_id;
410
 
411
	for (i=0;i
412
		for (j=0;j
413
		{
414
			color_id = color_matrix[i*BLOCKS_NUM+j];
415
			DrawBar(j*BLOCK_SIZE+USER_PANEL_WIDTH, i*BLOCK_SIZE+5, BLOCK_SIZE,BLOCK_SIZE, FIELD_COLORS[color_id]);
416
		}
417
}
418
 
419
 
420
void help()
421
{
422
	int i;
423
 
424
	loop()
425
	switch (WaitEvent())
426
	{
427
		CASE evButton:
2282 leency 428
				IF (GetButtonID()==1) ExitProcess();
2276 leency 429
				break;
430
		CASE evKey:
2282 leency 431
				IF (GetKey()==27) ExitProcess(); //Esc
2276 leency 432
				break;
433
		CASE evReDraw:
434
				for (i=0; HELP_TEXT[i]<>0; i++;) {};
435
 
2281 leency 436
				DefineAndDrawWindow(500,200,450,i*13+44,0x34,sc.work,0,0,#HELP_WINDOW_CAPTION);
2276 leency 437
 
438
				WriteText(6,12,0x80,sc.work_text,HELP_TEXT[0],0); //   
439
				for (i=0; HELP_TEXT[i]<>0; i++;) WriteText(5,i*13+12,0x80,sc.work_text,HELP_TEXT[i],0);
440
	}
441
}
442
 
443
 
444
stop: