-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
309 lines (288 loc) · 11.1 KB
/
player.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
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
var keycom={"38":"rotate()","40":"down()","37":"moveLeft()","39":"moveRight()"};
var nextShape = PIECES[Math.floor(Math.random() * PIECES.length)]
var currentShape = PIECES[Math.floor(Math.random() * PIECES.length)];
var currentOrientation = 0;
var currentColumn = 5;
var lastShape;
var MAXTIME = 10;
var timeLeft = MAXTIME;
var isArcade = false;
var stonePos = 0;
var cnt1=0;
var playerBlockPositionMap_X = new Array(10);
var playerBlockPositionMap_Y = new Array(20);
var playerXPosition = 610;
var playerYPosition = 10;
var playerTurn = true;
var blockMap = new Array(20);
var player_rows_completed = 0;
function initPlayer(){
// X position of each block in each line j, width is 30
playerTurn = true;
for(j=0;j<10;j++){
playerBlockPositionMap_X[j] = playerXPosition;
playerXPosition+=30;
}
// Y position of each block in each line j, height is 30
for(i=0;i<20;i++){
playerBlockPositionMap_Y[i] = playerYPosition;
playerYPosition+=30;
}
for (i = 0; i < 20; i++) {
blockMap[i] = new Array(10);
}
for(i=0;i<20;i++){
for(j=0;j<10;j++){
blockMap[i][j] = "#000000";//grey
}
}
prepareNextShape();
var currentRow = findUpperBoundryOfCurrentColumn();
drawBlockMap();
drawCurrentShape(currentRow);
document.getElementById("difficulty").disabled = true;
}
function drawBlockMap(){
c.strokeStyle = 'white';
c.lineWidth=1;
for(var row=0; row<20; row++){
for(var col=0; col<10; col++)
{
c.fillStyle = blockMap[row][col];
// Fill the block with color
c.fillRect(playerBlockPositionMap_X[col],playerBlockPositionMap_Y[row],30,30);
// Draw the boundry line of the block
if(blockMap[row][col]!="#000000"){//grey
c.beginPath();
c.fillStyle=blockMap[row][col].substr(0,4)+"000";
c.moveTo(playerBlockPositionMap_X[col]+3,playerBlockPositionMap_Y[row]+28);
c.lineTo(playerBlockPositionMap_X[col]+28,playerBlockPositionMap_Y[row]+28);
c.lineTo(playerBlockPositionMap_X[col]+28,playerBlockPositionMap_Y[row]+3);
c.lineTo(playerBlockPositionMap_X[col]+23,playerBlockPositionMap_Y[row]+8);
c.lineTo(playerBlockPositionMap_X[col]+23,playerBlockPositionMap_Y[row]+23);
c.lineTo(playerBlockPositionMap_X[col]+8,playerBlockPositionMap_Y[row]+23);
c.closePath();
c.fill();
//c.strokeRect(playerBlockPositionMap_X[col],playerBlockPositionMap_Y[row],30,30);
}
}
}
}
function putShapeIntoMap(currentRow){
heightOfShape = currentShape[currentOrientation].height;
widthOfShape = currentShape[currentOrientation].width;
colorOfShape = GetColorReference(currentShape);
for(var row=0; row<heightOfShape; row++){
for(var col=0; col<widthOfShape; col++)
{
if(currentShape[currentOrientation].orientation[row][col]==1)
{
blockMap[currentRow-heightOfShape+1+row][col+currentColumn] = colorOfShape;
}
}
}
}
// This function is used to check how many rows can be cleared in this turn
function checkClearedLines(){
for(i=0;i<20;i++)
{
var cancel = true;
var rowCancel = i;
for(j=0;j<10;j++)
{ //checking if this row is full to determine if needs to be cancelled
if(blockMap[i][j]=="#000000")//grey
cancel=false;
else
rowCancel=i;
}
if(cancel==true)
{//if this row needs to be cancelled, shift all blocks above down by 1 block
player_rows_completed+=1;
//remove player's line
for(m=rowCancel;m>0;m--){
for(n=0;n<10;n++)
{
blockMap[m][n]=blockMap[m-1][n];
}
}
//add ai's line
if(!AImode && isArcade){
if(cnt1>(6-currentLevel)){
for(n=0;n<10;n++){
for(m=1;m<20;m++)
{
if(hasBlock[m][n]==1){
if(n%2==stonePos){
hasBlock[m-1][n]=1;
blockColor[m-1][n]="#999999";
} else {
hasBlock[m-1][n]=0;
blockColor[m-1][n]="#000000";
}
break;
}
}
}
cnt1=0;
}
cnt1++;
}
/* for(m=0;m<19;m++){
for(n=0;n<10;n++)
{
hasBlock[m][n]=hasBlock[m+1][n];
blockColor[m][n]=blockColor[m+1][n];
}
}
for(n=0;n<10;n+=1){
if(n%2==stonePos){
hasBlock[19][n]=1;
blockColor[19][n]="#999999";
} else {
hasBlock[19][n]=0;
blockColor[19][n]="#000000";
}
}
stonePos=1-stonePos;*/
}
stonePos=1-stonePos;
}
}
function drawCurrentShape(currentRow){
c.strokeStyle = 'red';
c.lineWidth=2;
heightOfShape = currentShape[currentOrientation].orientation.length;
widthOfShape = currentShape[currentOrientation].orientation[0].length;
colorOfShape = GetColorReference(currentShape);
for(var row=0; row<heightOfShape; row++){
for(var col=0; col<widthOfShape; col++)
{
if(currentShape[currentOrientation].orientation[row][col]==1)
{
c.fillStyle = colorOfShape;
// Fill the block with color
//c.fillRect(playerBlockPositionMap_X[col+currentColumn],playerBlockPositionMap_Y[currentRow-heightOfShape+1+row],30,30);
// Draw the boundry line of the block
c.strokeRect(playerBlockPositionMap_X[col+currentColumn],playerBlockPositionMap_Y[currentRow-heightOfShape+1+row],30,30);
/*c.beginPath();
c.fillStyle=colorOfShape.substr(0,4)+"000";
c.moveTo(playerBlockPositionMap_X[col+currentColumn]+3,playerBlockPositionMap_Y[currentRow-heightOfShape+1+row]+28);
c.lineTo(playerBlockPositionMap_X[col+currentColumn]+28,playerBlockPositionMap_Y[currentRow-heightOfShape+1+row]+28);
c.lineTo(playerBlockPositionMap_X[col+currentColumn]+28,playerBlockPositionMap_Y[currentRow-heightOfShape+1+row]+3);
c.lineTo(playerBlockPositionMap_X[col+currentColumn]+23,playerBlockPositionMap_Y[currentRow-heightOfShape+1+row]+8);
c.lineTo(playerBlockPositionMap_X[col+currentColumn]+23,playerBlockPositionMap_Y[currentRow-heightOfShape+1+row]+23);
c.lineTo(playerBlockPositionMap_X[col+currentColumn]+8,playerBlockPositionMap_Y[currentRow-heightOfShape+1+row]+23);
c.closePath();
c.fill();*/
}
}
}
}
function rotate(){
lastOrientation = currentOrientation;
currentOrientation = (currentOrientation + 1) % currentShape.length;
if(currentColumn<=(10-currentShape[currentOrientation].width)){
if(playerTurn){
//currentOrientation = (currentOrientation + 1) % currentShape.length;
var currentRow = findUpperBoundryOfCurrentColumn();
drawBlockMap();
drawCurrentShape(currentRow);
}
}else{
currentOrientation = lastOrientation;
}
}
function down(){
if(playerTurn){
timeLeft = MAXTIME;
window.clearInterval(timer);
timer = setInterval(timeOut,1000);
var currentRow = findUpperBoundryOfCurrentColumn();
checkIfOver(currentRow);
putShapeIntoMap(currentRow);
drawBlockMap();
checkClearedLines();
drawBlockMap();
prepareNextShape();
currentRow = findUpperBoundryOfCurrentColumn();
drawCurrentShape(currentRow);
playerTurn = false;
if(isArcade){
if(player_rows_completed<15){
MAXTIME = 10;
currentLevel = 1;
} else if(player_rows_completed<50){
MAXTIME = 8;
currentLevel = 2;
} else if(player_rows_completed<100){
MAXTIME = 5;
currentLevel = 3;
} else if(player_rows_completed<300){
MAXTIME = 3;
currentLevel = 4;
} else if(player_rows_completed<500){
MAXTIME = 2;
currentLevel = 5;
}
}
}
}
function checkIfOver(currentRow){
if((currentRow-currentShape[currentOrientation].height)<=0){
alert("Sorry, You lose. Tetris AI is UNBEATABLE!");
window.location.reload(false);
}
}
function moveLeft(){
if(playerTurn){
if(currentColumn>0){
currentColumn--;
var currentRow = findUpperBoundryOfCurrentColumn();
drawBlockMap();
drawCurrentShape(currentRow);
}
}
}
function moveRight(){
if(playerTurn){
if(currentColumn<10-currentShape[currentOrientation].width){
currentColumn++;
var currentRow = findUpperBoundryOfCurrentColumn();
drawBlockMap();
drawCurrentShape(currentRow);
}
}
}
function findUpperBoundryOfCurrentColumn(){
//if(orientation.length-1)
//should fix a bug : block start from the middle where overlaps the exsistent block.
// We start from No.(orientation.length-1) row in the game, until we reach the last row on the bottom
heightOfShape = currentShape[currentOrientation].height;
widthOfShape = currentShape[currentOrientation].width;
//alert(currentShape[currentOrientation].orientation);
for (var row = heightOfShape-1; row < 20; row++) {
// test each row moving from top to bottom. if found conflict return row above
for (var i = 0; i < heightOfShape; i++) { // Again, i stands for row number of that matrix
for (var j = 0; j < widthOfShape; j++) { // j stands for col number of that matrix
// When the bottom of the shape overlap with current landscape, we return the number of previous row
// Can be further optimized, we just need to check the last row of shape
if((currentShape[currentOrientation].orientation[i][j] == 1) && (blockMap[row-(heightOfShape-1)+i][currentColumn+j] != "#000000")){
return row-1;
}
}
}
}
return 19;
}
function prepareNextShape(){
lastShape = currentShape;
currentShape = nextShape;
nextShape = PIECES[Math.floor(Math.random() * PIECES.length)];
currentOrientation = 0;
currentColumn = 5;
}
document.onkeydown=function(e){
// prevent browser scrolling the window
e.preventDefault();
eval(keycom[(e?e:event).keyCode]);
};