Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1806 yogev_ezra 1
; 10.06.2010 staper@inbox.ru
2
 
3
;Магический квадрат - это набор таких целых чисел, что их суммы
4
;в строках, столбцах (реже в диагоналях) равны.
5
 
6
;Пример:
7
 
8
; 16 3  2  13
9
; 5  10 11 8
10
; 9  6  7  12
11
; 4  15 14 1
12
 
13
use32
14
org	0x0
15
 db	'MENUET01'
16
 dd	0x1, START, I_END, (D_END+100) and not 3,  (D_END+100) and not 3, 0, 0
17
 
18
;Цвета:
19
Bckgrd_clr	equ 0xffffff	;фон
20
Brdr_line_clr	equ 0xb0b0b0	;линии по границам
21
Inter_line_clr	equ 0xb0b0b0	;внутренние линии
22
Square_clr	equ 0xdddddd	;цвет курсора
23
Fix_nmb_clr	equ 0x83459	;статичное значение
24
Chg_nmb_clr	equ 0;x008d8d	;переменное значение
25
Text_clr	equ 0x000000	;текст
26
Message_clr	equ 0x0000ff	;сообщения
27
 
28
max_dif equ 10
29
min_dif equ 2
30
len_kub equ 30	;длина стороны квадратика
31
beg_x	equ 40	;координата Х левого верхнего угла
32
beg_y	equ 50	;координата Y левого верхнего угла
33
sym_x equ 20
34
sym_y equ 20
35
win_x equ 400	;ширина окна
36
win_y equ 400	;высота окна
37
 
38
 
39
DEBUG equ 0
40
 
41
macro dbg_dec num
42
{pushad
43
newline
44
debug_print_dec num
45
popad
46
}
47
 
1824 yogev_ezra 48
include '../../../macros.inc'
1806 yogev_ezra 49
;include 'debug.inc'
50
include 'editbox_ex.mac'
51
include 'lang.inc'
52
 
53
 
54
START:
55
	;mcall	40,7
56
 
57
redraw_all:
58
	mcall	12,1
59
	mcall	48,4
60
	add	eax,100*65536+win_x
61
	mov	ecx,eax
62
	mcall	0,100*65536+win_y,,(0x34000000+Bckgrd_clr),,title
63
	movzx	eax,[Difficult]
64
	mov	ebx,len_kub
65
	xor	edx,edx
66
	mul	ebx
67
	add	eax,beg_x*65536+beg_x
68
	xchg	eax,ebx
69
	mcall	38,,beg_y*65536+beg_y,Brdr_line_clr
70
	mov	edx,Inter_line_clr
71
	movzx	esi,[Difficult]
72
	dec	esi
73
  @@:	add	ecx,len_kub*65536+len_kub
74
	mcall
75
	dec	esi
76
	jnz	@b
77
	add	ecx,len_kub*65536+len_kub
78
	mcall	,,,Brdr_line_clr
79
 
80
	movzx	eax,[Difficult]
81
	mov	ecx,len_kub
82
	xor	edx,edx
83
	mul	ecx
84
	add	eax,(beg_y+1)*65536+beg_y-1
85
	xchg	eax,ecx
86
	mov	ebx,beg_x*65536+beg_x
87
	mcall	38,,,Brdr_line_clr
88
	mov	edx,Inter_line_clr
89
	movzx	esi,[Difficult]
90
	dec	esi
91
 
92
  @@:	add	ebx,len_kub*65536+len_kub
93
	mcall
94
	dec	esi
95
	jnz	@b
96
	add	ebx,len_kub*65536+len_kub
97
	mcall	,,,Brdr_line_clr
98
 
99
	mcall	8,<3,78>,<3,13>,2,0xbbbbbb
100
	mcall	4,<7,5>,(0x80000000+Text_clr),txt.clear
101
	mcall	,<105,5>,,txt.dif
102
 
103
 
104
	call	show_level
105
 
106
	push	dword Map;esi;  mov     esi,Map
107
	mcall	12,2
108
draw_pole:
109
	if DEBUG
110
	call	SysMsgBoardNum	;show esp
111
	endf
112
 
113
	movzx	eax,[Y]
114
	dec	al
115
	movzx	ebx,[Difficult]
116
	mul	bl
117
	mov	bl,[X]
118
	add	al,bl
119
	pop	esi	;       mov     esi,Map
120
	push	eax	;курсорчик
121
	movzx	eax,[Difficult]
122
	mov	ebx,eax
123
	mul	eax
124
	sub	eax,ebx
125
	mov	edi,eax
126
	pop	eax
127
	push	eax
128
;	mov	edi,81-9
129
	movzx	ebp,[Difficult]
130
	mov	ebx,beg_x*65536+beg_x+sym_x
131
	mov	ecx,beg_y*65536+beg_y+sym_y
132
	call	out_numbers
133
	pop	eax
134
	call	out_sum
135
 
136
still:
137
	mcall	10
138
 
139
	dec	al
140
	jz	redraw_all
141
	dec	al
142
	jz	key
143
	dec	al
144
	jnz	still
145
;button:
146
	mcall	17
147
	cmp	ah,1
148
	jne	@f
149
	mcall	-1
150
.clear:
151
@@:	mov	ecx,max_dif*max_dif
152
	mov	esi,Map
153
@@:	mov	byte [esi+ecx-1],0
154
	loop	@b
155
	jmp	redraw_all
156
 
157
key:
158
	mcall	2
159
 
160
.43:	cmp	ah,43		;+
161
	jne	.45
162
	cmp	[Difficult],max_dif
163
	je	still
164
	inc	[Difficult]
165
	jmp	redraw_all
166
	jmp	still
167
.45:				;-
168
	cmp	ah,45
169
	jne	.99
170
	cmp	[Difficult],min_dif
171
	je	still
172
	dec	[Difficult]
173
	jmp	redraw_all
174
	jmp	still
175
 
176
.99:	cmp	ah,0x58
177
	je	@f
178
	cmp	ah,0x78
179
	jne	.39
180
@@:	jmp	still.clear
181
 
182
.39:	cmp	ah,0x39
183
	ja	.110
184
	cmp	ah,0x30
185
	jb	still
186
	sub	ah,0x30
187
	mov	cl,ah
188
 
189
	movzx	eax,[Y]
190
	dec	al
191
	mov	ebx,max_dif;[Difficult]
192
	mul	bl
193
	mov	bl,[X]
194
	dec	bl
195
	add	al,bl
196
 
197
	mov	esi,Map
198
	bt	[flags],9
199
	jnc	@f
200
	mov	bl,[esi+eax]
201
	push	eax
202
	mov	ax,10
203
	xor	dl,dl
204
	mul	bl
205
	add	cl,al
206
	pop	eax
207
	mov	[esi+eax],cl
208
	btr	[flags],9
209
	jmp	.onedraw
210
   @@:	mov	[esi+eax],cl
211
	bts	[flags],9
212
	jmp	.onedraw
213
 
214
 
215
.110:	cmp	ah,110		;n
216
	jne	.176
217
.new_game:
218
	jmp	redraw_all
219
 
220
.176:	cmp	ah,176 ;курсоры
221
	jne	.177
222
	call	draw_one_symbol
223
	dec	[X]
224
	cmp	[X],1
225
	jge	@f
226
	push	eax
227
	movzx	eax,[Difficult]
228
	mov	[X],al
229
	pop	eax
230
@@:	btr	[flags],9
231
	jmp	.onedraw
232
.177:	cmp	ah,177
233
	jne	.178
234
	call	draw_one_symbol
235
	inc	[Y]
236
	push	eax
237
	movzx	eax,[Difficult]
238
	cmp	[Y],al
239
	jbe	@f
240
	mov	[Y],1
241
@@:	pop	eax
242
	btr	[flags],9
243
	jmp	.onedraw
244
.178:	cmp	ah,178
245
	jne	.179
246
	call	draw_one_symbol
247
	dec	[Y]
248
	cmp	[Y],1
249
	jge	@f
250
	push	eax
251
	movzx	eax,[Difficult]
252
	mov	[Y],al
253
	pop	eax
254
@@:	btr	[flags],9
255
	jmp	.onedraw
256
.179:	cmp	ah,179
257
	jne	still
258
	call	draw_one_symbol
259
	inc	[X]
260
	push	eax
261
	movzx	eax,[Difficult]
262
	cmp	[X],al
263
	jbe	@f
264
	mov	[X],1
265
@@:	btr	[flags],9
266
	pop	eax
267
.onedraw:
268
	bts	[flags],4
269
	call	draw_one_symbol
270
	call	out_one_sum
271
	jmp	still ;.todraw
272
 
273
show_level:
274
	movzx	ecx,[Difficult]
275
	mcall	47,0x10000,,<205,5>,(0x50000000+Text_clr),Bckgrd_clr
276
ret
277
 
278
 
279
draw_one_symbol:
280
	movzx	eax,[X]
281
	mov	ebx,len_kub*65536+len_kub
282
	mul	ebx
283
	xchg	eax,ebx
284
	add	ebx,(beg_x*65536+beg_y-len_kub*65536+len_kub)
285
	movzx	eax,[Y]
286
	mov	ecx,len_kub*65536+len_kub
287
	mul	ecx
288
	xchg	eax,ecx
289
	add	ecx,(beg_y*65536+beg_y+sym_y-len_kub*65536+len_kub)
290
	movzx	eax,[Y]
291
	dec	al
292
	push	ebx
293
	movzx	ebx,[Difficult]
294
	mul	bl
295
	mov	bl,[X]
296
	add	al,bl
297
	dec	al
298
	pop	ebx
299
	mov	esi,Map
300
	add	esi,eax
301
	push	dword 0 ;не курсор
302
	bt	[flags],4
303
	jnc	@f
304
	mov	dword [esp],1 ;курсор
305
	btr	[flags],4
306
@@:	mov	edi,0
307
	mov	ebp,1
308
	call	out_numbers
309
	pop	eax
310
ret
311
 
312
 
313
out_numbers:
314
	push	ebx ecx esi
315
	shr	ebx,16
316
	inc	bx
317
	shl	ebx,16
318
	add	ebx,len_kub-1
319
	shr	ecx,16
320
	inc	cx
321
	shl	ecx,16
322
	add	ecx,len_kub-1
323
	mov	edx,Bckgrd_clr
324
	push	ebp
325
	dec	dword [esp+4*5]
326
	jnz	@f
327
	mov	edx,Square_clr
328
@@:	mcall	13
329
	pop	ebp
330
	pop	esi
331
 
332
	push	ebx edx
333
	mov	eax,esi
334
	sub	eax,Map-1
335
	xor	edx,edx
336
	movzx	ebx,[Difficult]
337
	div	ebx
338
	push	edx
339
	xor	edx,edx
340
	mov	ebx,max_dif
341
	mul	ebx
342
	pop	edx
343
	test	edx,edx
344
	jnz	@f
345
	movzx	edx,[Difficult]
346
	sub	eax,max_dif
347
@@:	add	eax,edx
348
	mov	al,[eax+Map-1]
349
	pop	edx ebx
350
	test	al,al
351
 
352
;	cmp	byte [esi],0
353
	jnz	.changeable_number
354
	jmp	.null
355
.end:
356
	inc	esi
357
	dec	ebp
358
	jnz	out_numbers
359
	test	edi,edi
360
	jz	@f
361
	push	ebx edx
362
	movzx	eax,[Difficult]
363
	sub	edi,eax
364
	mov	ebp,eax
365
	mov	ebx,len_kub*65536
366
	xor	edx,edx
367
	mul	ebx
368
	push	eax
369
	movzx	eax,[Difficult]
370
	mov	ebx,len_kub
371
	mul	ebx
372
	pop	ebx
373
	add	eax,ebx
374
	pop	edx
375
	pop	ebx
376
	sub	ebx,eax
377
;	add	ebx,-9*24*65536-9*24
378
	add	ecx,len_kub*65536+len_kub
379
	jmp	out_numbers
380
  @@:
381
ret
382
 
383
 
384
.null:
385
	pop	ecx ebx
386
	add	ebx,len_kub*65536+len_kub
387
	jmp	.end
388
.changeable_number:
389
	push	esi
390
	shr	ebx,16
391
	shr	ecx,16
392
	mov	dx,bx
393
	shl	edx,16
394
	mov	dx,cx
395
	add	edx,8*65536+4
396
 
397
	push	ebx edx
398
	mov	eax,esi
399
	sub	eax,Map-1
400
	xor	edx,edx
401
	movzx	ebx,[Difficult]
402
	div	ebx
403
	push	edx
404
	xor	edx,edx
405
	mov	ebx,max_dif
406
	mul	ebx
407
	pop	edx
408
	test	edx,edx
409
	jnz	@f
410
	movzx	edx,[Difficult]
411
	sub	eax,max_dif
412
@@:	add	eax,edx
413
	pop	edx ebx
414
 
415
	movzx	ebx,byte [eax+Map-1];[esi]
416
 
417
 
418
	push	esi ebp edi edx
419
	sub	edx,2*65536-3
420
	push	edx
421
	xchg	eax,ebx
422
	mov	ebx,10
423
	xor	edx,edx
424
	div	ebx
425
	mov	ebx,edx
426
	pop	edx
427
 
428
	push	ebx
429
 
430
	test	eax,eax
431
	jz	.only_first_num
432
 
433
;	bt	[flags],9
434
;	jc	.only_first_num
435
 
436
	xchg	eax,ebx
437
 
438
	shl	bx,4
439
	add	ebx,FONT
440
	mov	ecx,8*65536+16
441
 
442
	mov	edi,Pltr.ch
443
	cmp	dword [esp+4*9],0
444
	jne	@f
445
	mov	edi,Pltr.chk
446
@@:	mov	esi,1
447
	mov	ebp,0
448
	mcall	65
449
 
450
.only_first_num:
451
	pop	ebx
452
	shl	bl,4
453
 
454
	add	ebx,FONT
455
	mov	ecx,8*65536+16
456
	add	edx,8*65536
457
	mov	edi,Pltr.ch
458
	cmp	dword [esp+4*8],0
459
	jne	@f
460
	mov	edi,Pltr.chk
461
@@:	mov	esi,1
462
	mov	ebp,0
463
	mcall	65
464
	pop	edx edi ebp esi
465
	pop	esi ecx ebx
466
	add	ebx,len_kub*65536+len_kub
467
	jmp	.end
468
 
469
out_sum:
470
	movzx	eax,[Difficult]
471
	push	eax
472
	mov	esi,Map
473
	mov	edi,out_sum_buf
474
.1:	xor	ebx,ebx
475
	xor	ecx,ecx
476
	xor	edx,edx
477
.2:	mov	dl,[ebx+esi]
478
	add	ecx,edx
479
	inc	ebx
480
	cmp	ebx,[esp]
481
	jne	.2
482
	mov	word [edi],cx
483
	add	edi,2
484
	add	esi,max_dif
485
	dec	eax
486
	jnz	.1
487
 
488
	push	out_sum_buf
489
	movzx	eax,[Difficult]
490
	mov	ebx,len_kub*65536
491
	xor	edx,edx
492
	mul	ebx
493
	add	eax,40*65536
494
	push	eax
495
	mov	edx,(beg_x-30)*65536+beg_y+10
496
	mov	ebx,0x30000
497
	mov	esi,0x50000000+Fix_nmb_clr
498
	mov	edi,Bckgrd_clr
499
 
500
.3:	mov	ecx,[esp+4]
501
	mov	cx,[ecx]
502
	shl	ecx,16
503
	shr	ecx,16
504
	mcall	47
505
	push	edx
506
	add	edx,[esp+4]
507
	mcall
508
	pop	edx
509
	add	edx,len_kub
510
	add	dword [esp+4],2
511
	dec	dword [esp+8]
512
	jnz	.3
513
	pop	eax
514
	pop	eax
515
	pop	eax
516
 
517
 
518
	movzx	eax,[Difficult]
519
	push	eax
520
	mov	esi,Map
521
	mov	edi,out_sum_buf
522
	xor	ebx,ebx
523
.4:	xor	ecx,ecx
524
	xor	edx,edx
525
	mov	ebx,[esp]
526
.5:	mov	dl,[esi]
527
	add	ecx,edx
528
	add	esi,max_dif
529
	dec	ebx
530
	jnz	.5
531
	mov	word [edi],cx
532
	add	edi,2
533
	push	eax
534
	mov	eax,max_dif
535
	xor	edx,edx
536
	mul	dword [esp+4]
537
	dec	eax
538
	sub	esi,eax
539
	pop	eax
540
	dec	eax
541
	jnz	.4
542
 
543
	push	out_sum_buf
544
	movzx	eax,[Difficult]
545
	mov	ebx,len_kub
546
	xor	edx,edx
547
	mul	ebx
548
	add	eax,35
549
	push	eax
550
	mov	edx,(beg_x+5)*65536+beg_y-20
551
	mov	ebx,0x30000
552
	mov	esi,0x50000000+Fix_nmb_clr
553
	mov	edi,Bckgrd_clr
554
 
555
.6:	mov	ecx,[esp+4]
556
	mov	ecx,[ecx]
557
	shl	ecx,16
558
	shr	ecx,16
559
	mcall	47
560
	push	edx
561
	add	edx,[esp+4]
562
	mcall
563
	pop	edx
564
	add	edx,len_kub*65536;[esp]
565
	add	dword [esp+4],2
566
	dec	dword [esp+8]
567
	jnz	.6
568
	pop	eax
569
	pop	eax
570
	pop	eax
571
ret
572
 
573
 
574
 
575
out_one_sum:
576
	movzx	ecx,[Difficult]
577
	mov	esi,Map
578
	xor	edx,edx
579
 
580
	movzx	eax,[Y]
581
	dec	al
582
	mov	ebx,max_dif
583
	mul	bl
584
	add	esi,eax
585
@@:	mov	al,[esi]
586
	inc	esi
587
	add	edx,eax
588
	loop	@b
589
 
590
	mov	ecx,edx
591
	movzx	eax,[Difficult]
592
	mov	ebx,len_kub*65536
593
	xor	edx,edx
594
	mul	ebx
595
	add	eax,40*65536
596
	push	eax
597
	movzx	eax,[Y]
598
	dec	eax
599
	mov	ebx,len_kub
600
	xor	edx,edx
601
	mul	ebx
602
	mov	edx,(beg_x-30)*65536+beg_y+10
603
	add	edx,eax
604
	mov	ebx,0x30000
605
	mov	esi,0x50000000+Fix_nmb_clr
606
	mov	edi,Bckgrd_clr
607
 
608
	mcall	47
609
	add	edx,[esp]
610
	mcall
611
	pop	eax
612
 
613
 
614
 
615
 
616
	movzx	ecx,[Difficult]
617
	mov	esi,Map
618
	xor	edx,edx
619
 
620
	movzx	eax,[X]
621
	dec	al
622
	add	esi,eax
623
@@:	mov	al,[esi]
624
	add	esi,max_dif
625
	add	edx,eax
626
	loop	@b
627
 
628
	mov	ecx,edx
629
	movzx	eax,[Difficult]
630
	mov	ebx,len_kub
631
	xor	edx,edx
632
	mul	ebx
633
	add	eax,35
634
	push	eax
635
	movzx	eax,[X]
636
	dec	eax
637
	mov	ebx,len_kub*65536
638
	xor	edx,edx
639
	mul	ebx
640
	mov	edx,eax
641
	add	edx,(beg_x+5)*65536+beg_y-20
642
	mov	ebx,0x30000
643
	mov	esi,0x50000000+Fix_nmb_clr
644
	mov	edi,Bckgrd_clr
645
 
646
	mcall	47
647
	add	edx,[esp]
648
	mcall
649
 
650
	pop	edx
651
 
652
ret
653
 
654
 
655
 
656
 
657
if DEBUG
658
SysMsgBoardNum: ;warning: destroys eax,ebx,ecx,esi
659
	mov	ebx,esp
660
	mov	ecx,8
661
	mov	esi,(number_to_out+1)
662
.1:
663
	mov	eax,ebx
664
	and	eax,0xF
665
	add	al,'0'
666
	cmp	al,(10+'0')
667
	jb	@f
668
	add	al,('A'-'0'-10)
669
@@:
670
	mov	[esi+ecx],al
671
	shr	ebx,4
672
	loop	.1
673
	dec	esi
674
	mcall	71,1,number_to_out
675
ret
676
 
677
number_to_out	db '0x00000000',13,10,0
678
endf
679
 
680
 
681
 
682
 
683
 
684
if lang eq ru
685
title db 'Магический квадрат',0
686
txt:
687
.dif db "Сложность (+/-):",0
688
.clear db 'Очистить (x)',0
689
else
690
title db 'Magical square',0
691
txt:
692
.dif db "Difficult (+/-)",0
693
.clear db 'Clear (x)',0
694
endf
695
 
696
 
697
 
698
 
699
align 4
700
;Палитры:
701
Pltr:
702
.ch dd Bckgrd_clr,Chg_nmb_clr
703
.chk dd Square_clr,Chg_nmb_clr
704
.fx dd Bckgrd_clr,Fix_nmb_clr
705
.fxk dd Square_clr,Fix_nmb_clr
706
 
707
FONT file "MSquare.fnt"
708
 
709
X db 1
710
Y db 1
711
 
712
Difficult db 3
713
 
714
I_END:
715
align 16
716
Map	rb max_dif*max_dif
717
out_sum_buf rw max_dif
718
 
719
flags rw 1
720
 
721
D_END:
722
;бит 0: см. перед draw_pole
723
;2: в draw_pole и key
724
;4: in draw_one_symbol
725
;9: введёна первая цифра числа