-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
executable file
·582 lines (537 loc) · 15.9 KB
/
Program.fs
File metadata and controls
executable file
·582 lines (537 loc) · 15.9 KB
1
2
3
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// For more information see https://aka.ms/fsharp-console-apps
open Printf
open System
open System.Drawing
open System.Windows.Forms
open System.Threading
open System.Timers
let piece_width = 4
let piece_height = 4
let field_width = 14
let field_height = 24
let field = [| for i in 1 ..field_width -> [| for j in 1 .. field_height -> 0 |] |]
let field_color = [| for i in 1 ..field_width -> [| for j in 1 .. field_height -> Color.Black |] |]
let first_piece = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |]
let first_color = ref Color.Transparent
let second_piece = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |]
let second_color = ref Color.Transparent
let third_piece = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |]
let third_color = ref Color.Transparent
(*
let first_piece = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |]
let first_color = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> Color.Transparent |] |]
let second_piece = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |]
let second_color = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> Color.Transparent |] |]
let third_piece = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |]
let third_color = ref [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> Color.Transparent |] |]
*)
let loc = ref (0,0)
let get_x loc = fst loc.contents
let get_y loc = snd loc.contents
type move_to =
| Left | Right | Down
let r = System.Random()
let create_piece () =
let piece = [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |] in
let color = ref Color.Transparent in
(
match r.Next(0,7) with
| 0 ->
//printf "%d" 0;
piece[1][1] <- 1;
piece[2][1] <- 1;
piece[1][2] <- 1;
piece[2][2] <- 1;
color.contents <- Color.White;
| 1 ->
//printf "%d" 1;
piece[1][0] <- 1;
piece[1][1] <- 1;
piece[1][2] <- 1;
piece[1][3] <- 1;
color.contents <- Color.Red;
| 2 ->
//printf "%d" 2;
piece[1][1] <- 1;
piece[1][2] <- 1;
piece[2][2] <- 1;
piece[1][3] <- 1;
color.contents <- Color.Orange;
| 3 ->
//printf "%d" 3;
piece[1][1] <- 1;
piece[2][1] <- 1;
piece[1][2] <- 1;
piece[1][3] <- 1;
color.contents <- Color.Blue;
| 4 ->
//printf "%d" 4;
piece[1][1] <- 1;
piece[2][1] <- 1;
piece[2][2] <- 1;
piece[2][3] <- 1;
color.contents <- Color.Purple;
| 5 ->
//printf "%d" 5;
piece[2][1] <- 1;
piece[1][2] <- 1;
piece[2][2] <- 1;
piece[1][3] <- 1;
color.contents <- Color.Green;
| 6 ->
//printf "%d" 6;
piece[1][1] <- 1;
piece[1][2] <- 1;
piece[2][2] <- 1;
piece[2][3] <- 1;
color.contents <- Color.Yellow;
| _ -> failwith "System.Random"
);
first_piece.contents <- Array.copy second_piece.contents;
first_color.contents <- second_color.contents;
second_piece.contents <- Array.copy third_piece.contents;
second_color.contents <- third_color.contents;
third_piece.contents <- Array.copy piece;
third_color.contents <- color.contents
let get_piece_top () =
let rec get_top_in_y n (array:int array) =
if n < piece_height then
if array[n] = 1 then
n
else
get_top_in_y (n + 1) array
else
piece_height - 1
in
let rec move_in_x n min =
if n < piece_width then
let y = get_top_in_y 0 first_piece.contents[n] in
if y < min then
move_in_x (n + 1) y
else
move_in_x (n + 1) min
else
min
in
move_in_x 0 (piece_height - 1)
let get_piece_bottom () =
let rec get_bottom_in_y n (array: int array) =
if 0 < n then
if array[n] = 1 then
n
else
get_bottom_in_y (n - 1) array
else
0
in
let rec move_in_x n max =
if n < piece_width then(
let y = get_bottom_in_y (piece_height - 1) first_piece.contents[n] in
//printf "%d" n;
if max < y then
move_in_x (n + 1) y
else
move_in_x (n + 1) max)
else
max
in
move_in_x 0 0
let get_piece_left () =
let rec exists_in_y n (array: int array) =
if n < piece_height then
if array[n] = 1 then
true
else
exists_in_y (n + 1) array
else
false
in
let rec move_in_x n =
if n < piece_width then
if exists_in_y 0 first_piece.contents[n] then
n
else
move_in_x (n + 1)
else
piece_width - 1
in
move_in_x 0
let get_piece_right () =
let rec exists_in_y n (array: int array) =
if n < piece_height then
if array[n] = 1 then
true
else
exists_in_y (n + 1) array
else
false
in
let rec move_in_x n =
if 0 < n then
if exists_in_y 0 first_piece.contents[n] then
n
else
move_in_x (n - 1)
else
0
in
move_in_x (piece_width - 1)
let move_piece move =
match move with
| Left ->
let left = get_piece_left () in
if get_x loc + left <= 0 then
false
else
let rec exists_in_y x y (array: int array) =
if y < piece_height then
if array[y] = 1 && get_x loc + x - 1 >= 0
&& get_y loc + y >= 0 && field[get_x loc + x - 1][get_y loc + y] = 1 then
false
else
exists_in_y x (y + 1) array
else
true
in
let rec move_in_x n =
if n < piece_width then
if exists_in_y n 0 first_piece.contents[n] then
move_in_x (n + 1)
else
false
else
true
in
if move_in_x 0 then(
loc := (get_x loc - 1, get_y loc);
true
)
else
false
| Right ->
let right = get_piece_right () in
if get_x loc + right >= field_width - 1 then
false
else
let rec exists_in_y x y (array: int array) =
if y < piece_height then
if array[y] = 1 && get_x loc + x + 1 <= field_width
&& get_y loc + y >= 0 && field[get_x loc + x + 1][get_y loc + y] = 1 then
false
else
exists_in_y x (y+ 1) array
else
true
in
let rec move_in_x n =
if n < piece_width then
if exists_in_y n 0 first_piece.contents[n] then
move_in_x (n + 1)
else
false
else
true
in
if move_in_x 0 then(
loc.contents <- (get_x loc + 1, get_y loc);
true
)
else
false
| Down ->
let bottom = get_piece_bottom () in
if get_y loc + bottom >= field_height - 1 then
false
else
let rec exists_in_y x y (array: int array) =
if y < piece_height then
if array[y] = 1 && get_y loc + y + 1 >= 0
&& get_y loc + y + 1 < field_height && field[get_x loc + x][get_y loc + y + 1] = 1 then
false
else
exists_in_y x (y + 1) array
else
true
in
let rec move_in_x n =
if n < piece_width then
if exists_in_y n 0 first_piece.contents[n] then
move_in_x (n + 1)
else
false
else
true
in
if move_in_x 0 then(
loc.contents <- (get_x loc, get_y loc + 1);
true
)
else
false
let turn_piece () =
let piece_turned = [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> 0 |] |] in
let color_turned = [| for i in 1 ..piece_width -> [| for j in 1 .. piece_height -> Color.Black |] |] in
let rec move_in_y x y =
if y < piece_height then(
piece_turned[(piece_height - 1) - y][x] <- first_piece.contents[x][y];
color_turned[(piece_height - 1) - y][x] <- first_color.contents;
move_in_y x (y + 1)
)
in
let rec move_in_x x =
if x < piece_width then(
move_in_y x 0;
move_in_x (x + 1)
)
in move_in_x 0;
let rec move_in_y x y =
if y < piece_height then
if piece_turned[x][y] = 1 then
let offset = (get_x loc + x, get_y loc + y) in
if fst offset < 0 || fst offset >= field_width
|| snd offset >= field_height
|| (snd offset >= 0 && field[fst offset][snd offset] = 1) then
false
else
move_in_y x (y + 1)
else
move_in_y x (y + 1)
else
true
in
let rec move_in_x x =
if x < piece_width then
if move_in_y x 0 then
move_in_x (x + 1)
else false
else
true
in
if move_in_x 0 then(
first_piece.contents <- piece_turned;
(*first_color.contents <- color_turned;*)
true
)
else
false
let piece_to_field () =
let rec move_in_y x y =
if y < piece_height then
//printf "%d " ((get_y loc) + y)
//printf "%b" (first_piece.contents[x][y] = 1);
//printf "%b" ((get_y loc) + y >= 0);
// printf "%b" ((first_piece.contents[x][y] = 1) && ((get_y loc) + y >= 0));
if first_piece.contents[x][y] = 1 && (get_y loc) + y >= 0 then(
field[get_x loc + x][get_y loc + y] <- first_piece.contents[x][y];
field_color[get_x loc + x][get_y loc + y] <- first_color.contents
//printf "x:%d, y:%d" x y;
);
move_in_y x (y + 1)
in
let rec move_in_x x =
//printf "%b" (x < piece_width);
if x < piece_width then(
//printf "%d" x;
move_in_y x 0;
move_in_x (x + 1)
)
in move_in_x 0;
let delete_line () =
let y = ref (field_height - 1) in
let rec move_in_y del_count =
if y.contents >= 0 then
let rec move_in_x x line_count =
if x < field_width then
let ret = move_in_x (x + 1) line_count
in
line_count + field[x][y.contents] + ret
else
line_count
in
let line_count = move_in_x 0 0
in
//printfn "line_count %d" line_count;
if line_count = 0 then
del_count
else if line_count <> field_width then
y.contents <- y.contents - 1;
move_in_y del_count
else
let x' = ref 0 in
let rec go x =
if x < field_width then
field[x][y.contents] <- 0;
//field_color[x][y.contents] <- Color.Black;
x'.contents <- x;
go (x + 1)
in
go 0;
//printfn "del %d" x'.contents;
y.contents <- y.contents - 1;
move_in_y (del_count + 1)
//printfn "%A" field;
else
del_count
in
move_in_y 0
let shift_line del_count =
let y = ref (field_height - 1)
in
let rec move_in_y ()=
if y.contents >= 0 && del_count.contents > 0 then
let rec move_in_x x line_count =
if x < field_width then
let ret = move_in_x (x + 1) line_count
in
line_count + field[x][y.contents] + ret
else
line_count
in
let line_count = move_in_x 0 0
in
//printfn "line_count %d" line_count;
if line_count <> 0 then
y.contents <- y.contents - 1;
move_in_y ()
else
del_count.contents <- del_count.contents - 1;
let rec go iy =
let x' = ref 0 in
if iy >= 0 then
let rec go' x =
if x < field_width then
x'.contents <- x;
if iy - 1 >= 0 then
//printfn "%d" (field[x][iy - 1]);
field[x][iy] <- field[x][iy - 1];
field_color[x][iy] <- field_color[x][iy - 1]
else
field[x][0] <- 0;
field_color[x][0] <- Color.Black
go' (x + 1)
in go' 0;
//printfn "shift %d" x'.contents;
go (iy - 1)
in
go y.contents;
move_in_y ()
//printfn "%A" field;
in
move_in_y ()
let square = 20
let box = new PictureBox()
box.Width <- field_width * square;
box.Height <- field_height * square;
//let canvas = new Bitmap(80,80)//(piece_width*square, piece_height*square)
//let g = Graphics.FromImage(canvas)
let form = new Form()
let draw ((*box:PictureBox*)) =
let canvas = new Bitmap(field_width * square, field_height * square)
let g = Graphics.FromImage(canvas)
let rec move_in_y x y =
if y < field_height then
let brush = new SolidBrush(field_color[x][y]) in
//MessageBox.Show (x.ToString() ^ y.ToString());
//printf "x:%d, y:%d" x y;
(* if (get_x loc <= x && x < get_x loc + 4) &&
(get_y loc <= y && y < get_y loc + 4) then
let brush = new SolidBrush(Color.Blue);
g.FillRectangle(brush,x * square, y * square, square, square);
else
(*printfn "%d;%d;%A" x y loc.contents;*)
*)
g.FillRectangle(brush,x * square, y * square, square, square);
brush.Dispose ();
move_in_y x (y + 1)
in
let rec move_in_x x =
if x < field_width then(
move_in_y x 0;
move_in_x (x + 1)
)
in
//printfn "%A" field_color;
move_in_x 0;
let rec move_in_y x y =
if y < piece_height then
if first_piece.contents[x][y] = 1 then
let brush = new SolidBrush(first_color.contents) in
//MessageBox.Show ((get_x loc).ToString() ^ (get_y loc).ToString());
//printf "x:%d, y:%d" x y;
g.FillRectangle(brush,(get_x loc + x) * square, (get_y loc + y) * square, square, square);
brush.Dispose ();
move_in_y x (y + 1)
in
let rec move_in_x x =
if x < piece_width then(
move_in_y x 0;
move_in_x (x + 1)
)
in
move_in_x 0;
box.Image <- canvas;
g.Dispose()
(*
let draw_piece ((*box:PictureBox*)) =
let canvas = new Bitmap(field_width * square, field_height * square)
let g = Graphics.FromImage(canvas)
let rec move_in_y x y =
if y < piece_height then
let brush = new SolidBrush(first_color.contents) in
//MessageBox.Show ((get_x loc).ToString() ^ (get_y loc).ToString());
//printf "x:%d, y:%d" x y;
g.FillRectangle(brush,(get_x loc + x) * square, (get_y loc + y) * square, square, square);
brush.Dispose ();
move_in_y x (y + 1)
in
let rec move_in_x x =
if x < piece_width then(
move_in_y x 0;
move_in_x (x + 1)
)
in
move_in_x 0;
box.Image <- canvas;
g.Dispose()
*)
[<STAThread; EntryPoint>]
let main argv =
create_piece();
create_piece();
create_piece();
create_piece();
//piece_to_field ();
//printf "aaa";
//field_color[5][6]<-Color.White;
draw ();
//draw_piece ();
form.Controls.Add(box);
form.Width <- (field_width + 1) * square;
form.Height <- (field_height + 2) * square;
form.Text <- "Tetris written in F#";
let timer = new Timer(1500);
let piece_down () =
if move_piece Down = false then
piece_to_field();
//shift_line (delete_line ());
let score = delete_line ()
in
(*
if score > 0 then
Thread.Sleep(1500)*)
draw ();
printfn "%d" score;
timer.Stop();
Thread.Sleep(1500);
timer.Start();
shift_line (ref score);
loc.contents <- (0,0);
create_piece()
timer.Elapsed.Add(fun e-> piece_down ();draw(););
timer.Enabled <- true;
form.KeyDown.Add(fun e-> if e.KeyCode = Keys.Up then ignore(turn_piece ());draw(););
form.KeyDown.Add(fun e-> if e.KeyCode = Keys.Down then timer.Stop(); piece_down ();draw();timer.Start());
form.KeyDown.Add(fun e-> if e.KeyCode = Keys.Left then ignore(move_piece Left);draw(););
form.KeyDown.Add(fun e-> if e.KeyCode = Keys.Right then ignore(move_piece Right);draw(););
Application.Run (form);
0