-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocks.f90
146 lines (133 loc) · 3.87 KB
/
blocks.f90
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
module blocks
implicit none
public
! Stores the shape of the blocks at each of their rotations
! http://tetris.wikia.com/wiki/ORS
! LINE BLOCK
integer, parameter :: line(4,4,2) = reshape( &
(/ 0, 0, 0, 0, &
1, 1, 1, 1, &
0, 0, 0, 0, &
0, 0, 0, 0, &
0, 0, 1, 0, &
0, 0, 1, 0, &
0, 0, 1, 0, &
0, 0, 1, 0 /), &
shape(line))
! T BLOCK
integer, parameter :: tee(4,4,4) = reshape( &
(/ 0, 0, 0, 0, &
1, 1, 1, 0, &
0, 1, 0, 0, &
0, 0, 0, 0, &
0, 1, 0, 0, &
1, 1, 0, 0, &
0, 1, 0, 0, &
0, 0, 0, 0, &
0, 1, 0, 0, &
1, 1, 1, 0, &
0, 0, 0, 0, &
0, 0, 0, 0, &
0, 1, 0, 0, &
0, 1, 1, 0, &
0, 1, 0, 0, &
0, 0, 0, 0 /), &
shape(tee))
! L BLOCK
integer, parameter :: ell(4,4,4) = reshape( &
(/ 0, 0, 0, 0, &
1, 1, 1, 0, &
1, 0, 0, 0, &
0, 0, 0, 0, &
1, 1, 0, 0, &
0, 1, 0, 0, &
0, 1, 0, 0, &
0, 0, 0, 0, &
0, 0, 0, 0, &
0, 0, 1, 0, &
1, 1, 1, 0, &
0, 0, 0, 0, &
0, 1, 0, 0, &
0, 1, 0, 0, &
0, 1, 1, 0, &
0, 0, 0, 0 /), &
shape(ell))
! J BLOCK
integer, parameter :: jay(4,4,4) = reshape( &
(/ 0, 0, 0, 0, &
1, 1, 1, 0, &
0, 0, 1, 0, &
0, 0, 0, 0, &
0, 1, 0, 0, &
0, 1, 0, 0, &
1, 1, 0, 0, &
0, 0, 0, 0, &
0, 0, 0, 0, &
1, 0, 0, 0, &
1, 1, 1, 0, &
0, 0, 0, 0, &
0, 1, 1, 0, &
0, 1, 0, 0, &
0, 1, 0, 0, &
0, 0, 0, 0 /), &
shape(jay))
! S BLOCK
integer, parameter :: ess(4,4,2) = reshape( &
(/ 0, 0, 0, 0, &
0, 1, 1, 0, &
1, 1, 0, 0, &
0, 0, 0, 0, &
1, 0, 0, 0, &
1, 1, 0, 0, &
0, 1, 0, 0, &
0, 0, 0, 0 /), &
shape(ess))
! Z BLOCK
integer, parameter :: zee(4,4,2) = reshape( &
(/ 0, 0, 0, 0, &
1, 1, 0, 0, &
0, 1, 1, 0, &
0, 0, 0, 0, &
0, 0, 1, 0, &
0, 1, 1, 0, &
0, 1, 0, 0, &
0, 0, 0, 0 /), &
shape(zee))
! SQUARE BLOCK
integer, parameter :: square(4,4) = reshape( &
(/ 0, 1, 1, 0, &
0, 1, 1, 0, &
0, 0, 0, 0, &
0, 0, 0, 0 /), &
shape(square))
contains
! Mutates rotation
function get_shape(block_type, rotation)
integer :: get_shape(4,4)
integer, intent(inout) :: rotation
integer, intent(in) :: block_type
select case (block_type)
case (0)
get_shape = line(:,:,rotation+1)
rotation = modulo(rotation, 2) ! pass by reference (intent(inout)) is convenient
case (1)
get_shape = tee(:,:,rotation+1)
rotation = modulo(rotation, 4)
case (2)
get_shape = ell(:,:,rotation+1)
rotation = modulo(rotation, 4)
case (3)
get_shape = jay(:,:,rotation+1)
rotation = modulo(rotation, 4)
case (4)
get_shape = ess(:,:,rotation+1)
rotation = modulo(rotation, 2)
case (5)
get_shape = zee(:,:,rotation+1)
rotation = modulo(rotation, 2)
case (6)
get_shape = square
rotation = 0
end select
end function get_shape
end module blocks