-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.js
151 lines (124 loc) · 2.97 KB
/
pieces.js
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
/*
* This file contains the piece object's structure and its functionality
*/
function Piece(states)
{
// Constructor
this.states = states;
this.state = 0;
this.posY = 0;
this.posX = 1 + Math.floor(Math.random() * (20 - states[this.state][0].length));
// Iterate over the piece coordinates and apply the supplied function
this.eachPoint = function(action) {
var iPosX = this.posX;
var iPosY = this.posY;
for(var r = 0, len = this.states[this.state].length; r < len; r++)
{
for(var c = 0, len2 = this.states[this.state][r].length; c < len2; c++)
{
action(iPosX, iPosY, this.states[this.state][r][c]);
iPosX += 1;
}
iPosX = this.posX;
iPosY += 1;
}
};
// Check if the piece occupies a valid position on the grid (no overlaps/out of boundary)
this.isValid = function() {
var result = true;
this.eachPoint(function (x, y, isPlaced) {
if(x <= 0 || x >= (COLS - 1) || y >= (ROWS - 1)
// Check if this position is already occupied by another piece
|| (gridPoint(x, y) != undefined && gridPoint(x, y) != 0 && isPlaced != 0))
{
result = false;
}
})
return result;
}
// Check if the piece is in its final position (no more available actions)
this.isFinal = function()
{
// Move Left or Right
if(this.getMovedPiece(M_LEFT).isValid() || this.getMovedPiece(M_RIGHT).isValid()
// Rotate
|| this.getRotatedPiece(R_CW).isValid() || this.getRotatedPiece(R_CCW).isValid())
return true;
return false;
}
// Return a piece object that was moved left or right
this.getMovedPiece = function(direction)
{
var newPiece = Object.create(this);
newPiece.posX = this.posX + direction;
newPiece.posY = this.posY + 1;
return newPiece;
}
// Return a piece object that was rotated
this.getRotatedPiece = function(direction)
{
var newPiece = Object.create(this);
var len = this.states.length; // number of states
// This is done because JavaScript modulo operator (%)
// doesnt work correctly on the negative numbers
// This is a fix; thought it would be better than an if statement
newPiece.state = (((this.state + direction) % len) + len) % len;
newPiece.posY = this.posY + 1;
return newPiece;
}
}
function getNextPiece()
{
var pieceType = [
// LinePiece
[
[ [1, 1, 1, 1] ],
[ [1],
[1],
[1],
[1] ]
],
// LPiece
[
[ [1, 0],
[1, 0],
[1, 1] ],
[ [1, 1, 1],
[1, 0, 0] ],
[ [1, 1],
[0, 1],
[0, 1] ],
[ [0, 0, 1],
[1, 1, 1] ]
],
// ReverseLPiece
[
[ [0, 1],
[0, 1],
[1, 1] ],
[ [1, 0, 0],
[1, 1, 1] ],
[ [1, 1],
[1, 0],
[1, 0] ],
[ [1, 1, 1],
[0, 0, 1] ]
],
// ZPiece
[
[ [0, 1],
[1, 1],
[1, 0] ],
[ [1, 1, 0],
[0, 1, 1] ]
],
// Block Piece
[
[ [1, 1],
[1, 1] ]
]
];
// randomly generated number to select a piece type
var number = Math.floor(Math.random() * pieceType.length);
return new Piece(pieceType[number]);
}