-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRS.ml
More file actions
57 lines (49 loc) · 1.63 KB
/
SRS.ml
File metadata and controls
57 lines (49 loc) · 1.63 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
open Tetromino
open Utils
let next_orientation_cw = function
| Zero -> Right
| Right -> Two
| Two -> Left
| Left -> Zero
let next_orientation_ccw = function
| Right -> Zero
| Two -> Right
| Left -> Two
| Zero -> Left
let get_offsets tetromino from_o to_o =
let from_offs = List.assoc from_o (offsets tetromino) in
let to_offs = List.assoc to_o (offsets tetromino) in
List.map2 (
fun (from_x, from_y) (to_x, to_y) -> (from_x - to_x, from_y - to_y)
) from_offs to_offs
let check_valid board minos =
List.for_all (Board.is_valid board) minos
let try_offset board rotated_minos offset pos =
let board_minos = List.map (offset |> add_pos pos |> add_pos) rotated_minos in
check_valid board board_minos
let rec try_offsets board tetromino rotated_minos = function
| [] -> None
| offset :: offsets ->
if try_offset board rotated_minos offset (pos tetromino)
then Some offset
else try_offsets board tetromino rotated_minos offsets
let try_rotate board tetromino cw =
let from_o = orientation tetromino in
let to_o = if cw
then next_orientation_cw from_o
else next_orientation_ccw from_o in
let rotated_minos = rotate_minos to_o (initial_positions tetromino)
in
let offsets = get_offsets tetromino from_o to_o in
match try_offsets board tetromino rotated_minos offsets with
| None -> None
| Some offset -> Some (
tetromino
|> change_orientation to_o
|> move offset
)
let try_move board tetromino delta_pos =
let new_tetromino = move delta_pos tetromino in
let valid = check_valid board (minos_world_positions new_tetromino) in
if valid then Some new_tetromino
else None