-
Notifications
You must be signed in to change notification settings - Fork 0
/
artificial.js
203 lines (185 loc) · 4.85 KB
/
artificial.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
var stonePos2 = 0;
var i,j;
var rows_completed =0;
var AImode = false;
var cnt2 = 0;
//defining x y axis drawing pixel location for each block 10*20
var hasBlock = new Array(20);
for (i = 0; i < 20; i++) {
hasBlock[i] = new Array(10);
}
//store color map of each block
var blockColor = new Array(20);
for (i = 0; i < 20; i++) {
blockColor[i] = new Array(10);
}
var blockPositionMap_X = new Array(10);
var blockPositionMap_Y = new Array(20);
var xPosition = 10;
var yPosition = 10;
//******** init the block color and exsistency ******
for(i=0;i<20;i++)
for(j=0;j<10;j++){
hasBlock[i][j] = 0;
}
for(i=0;i<20;i++)
for(j=0;j<10;j++){
blockColor[i][j] = "#000000";//grey
}
// X position of each block in each line j, width is 30
for(j=0;j<10;j++){
blockPositionMap_X[j] = xPosition;
xPosition+=30;
}
// Y position of each block in each line j, height is 30
for(i=0;i<20;i++){
blockPositionMap_Y[i] = yPosition;
yPosition+=30;
}
/*function gameSpeed(){
setInterval(function() {
$('.Timer').text((new Date - start) / 1000 + " Seconds");
}, 1000);
}*/
function randomPick(){
//var myArray = ['drawLshape_left', 'drawIshape', 'drawSquare', 'drawLshape_right', 'drawTRIshape', 'drawLIshape_left','drawLIshape_right'];
var newShape = PIECES[Math.floor(Math.random() * PIECES.length)];
//alert(newShape[0].orientation);
return newShape;
}
function blockUpdate(){
rows_completed+=checkForCancelRows();
if(playerTurn==false || AImode == true){
if(AImode){
var shape = randomPick();
}
else{
var shape = lastShape;
}
GetDecisionLocation(shape);
playerTurn = true;
}
}
function GetDecisionLocation(shape){
//AI CODE GOES HERE
// Each shape has its belonging color, here we choose it color
var color = GetColorReference(shape);
// Pick the best move for this shape
var decision = pickDecision(shape);
//alert("hi");
if(decision.noMoveCanMake == true)
{
gameOver=true;
alert("It is impossible! Are you cheating?");
window.location.reload(false);
}
else
{
// update the real map with the placed shape
for (var i = 0; i < decision.orientation.length; i++) {
for (var j = 0; j < decision.orientation[i].length; j++) {
if(decision.orientation[i][j]==1)
{
hasBlock[decision.row-(decision.orientation.length-1)+i][decision.column+j]=1;
blockColor[decision.row-(decision.orientation.length-1)+i][decision.column+j]=color;
}
}
}
}
//return {
// 'placeLocation': [[5,5],[19,0],[19,1],[19,2],[19,3],[19,4],[19,5],[19,6],[19,7],[19,8],[19,9],[17,7],[17,8],[18,3],[18,4],[18,5],[18,6]]
//};
}
//color of the shape. Get the color when drawing for that shape(!!!Suffer from change depending orientation name)
function GetColorReference(shape){
switch(shape){
case PIECES[0]:
return "#99CC00";//green
break;
case PIECES[1]:
return"#009933";//dark green
break;
case PIECES[2]:
return"#FF6666";//pink
break;
case PIECES[3]:
return"#3399CC";//blue
break;
case PIECES[4]:
return"#CC3399";//purple
break;
case PIECES[5]:
return"#FFFF99";//white
break;
case PIECES[6]:
return"#FF9933";//brown
break;
default:
return"#000000";//grey
break;
}
}
// This function is used to check how many rows can be canceled in this turn
function checkForCancelRows(){
var rows_removed=0;
for(i=0;i<20;i++)
{
var cancel = true;
var rowCancel =-1;
for(j=0;j<10;j++)
{ //checking if this row is full to determine if needs to be cancelled
if(hasBlock[i][j]==0)
cancel=false;
else
rowCancel=i;
}
if(cancel==true)
{//if this row needs to be cancelled, shift all blocks above down by 1 block
rows_removed++;
for(m=rowCancel;m>0;m--){
for(n=0;n<10;n++)
{
hasBlock[m][n]=hasBlock[m-1][n];
blockColor[m][n]=blockColor[m-1][n];
}
}
//add player lines
if(!AImode && isArcade){
if(cnt2>(6-currentLevel)){
for(n=0;n<10;n++){
for(m=1;m<20;m++)
{
if(blockMap[m][n]!="#000000"){
if(n%2==stonePos2){
blockMap[m-1][n]="#999999";
} else {
blockMap[m-1][n]="#000000";
}
break;
}
}
}
cnt2=0;
}
cnt2++;
}
/* if(stoneCnt>)
for(m=0;m<19;m++){
for(n=0;n<10;n++)
{
blockMap[m][n]=blockMap[m+1][n];
}
}
for(n=0;n<10;n+=1){
if(n%2==stonePos2){
blockMap[19][n]="#999999";
} else {
blockMap[19][n]="#000000";
}
}
stonePos2=1-stonePos2;*/
}
stonePos2=1-stonePos2;
}
return rows_removed;
}