-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetrAI.js
121 lines (100 loc) · 3.44 KB
/
tetrAI.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
let aiIntervall;
let aiSwitch = true;
function aiSwitchIntervall () {
if (aiSwitch){
aiIntervall = setInterval (makeMove, 1000/aiSpeed);
} else {
clearInterval(aiIntervall)
}
aiSwitch = !aiSwitch;
};
function makeMove(){
let main = loopAllPos();
switchHold(false);
let hold = loopAllPos();
if (main.points < hold.points) {
switchHold(false);
model = main.model;
points += POINTS[main.lines]
} else {
model = hold.model
points += POINTS[hold.lines]
}
newCertainTetrimo();
}
function loopAllPos() {
let best = {points:undefined, model:undefined, lines:undefined};
let firstMove = [];
for (let i = 0; i < 4; i++) {
let bigBack = JSON.parse(JSON.stringify(model));
let bigBackTetrCoord = certainTetrimo.coordinates
while (move(certainTetrimo, MOVE_DIER.left));
let morePosAvailable = true;
let first = true;
while (morePosAvailable) {
let back = JSON.parse(JSON.stringify(model));
let backTetrCoord = certainTetrimo.coordinates;
while (move(certainTetrimo, MOVE_DIER.down));
if (first) {
if (firstMove.includes(JSON.stringify(model))) break;
firstMove[firstMove.length] = JSON.stringify(model);
first = false;
}
//handle so it doesn't take all rotation for their one
let lines = clearFullLines(certainTetrimo);
let score = calculateScore(model, lines);
if(!(score >= best.points)){
best.points = score;
best.model = JSON.parse(JSON.stringify(model));
best.lines = lines
}
// -- calculate points --
model = back;
certainTetrimo.coordinates = backTetrCoord;
morePosAvailable = move(certainTetrimo, MOVE_DIER.right);
}
model = JSON.parse(JSON.stringify(bigBack));
certainTetrimo.coordinates = bigBackTetrCoord;
move(certainTetrimo, MOVE_DIER.down);
rotation(certainTetrimo, true);
move(certainTetrimo, MOVE_DIER.up);
}
return best;
};
//certainRotation = spinRight(tetrimo, certainRotation)
//move down - move(certainTetrimo, MOVE_DIER.down)
//move(certainTetrimo, MOVE_DIER.left)
//getting all posible Positions
function calculateScore(modelSnapshot, lines){
let height;
let holes = 0;
let cliff = 0;
modelSnapshot.forEach((element, y) => {
element.forEach((element, x) => {
if (element > 0){
if (!height) height = 20 - y;
holes += detectHole(modelSnapshot, y,x);
cliff += detectCliff(modelSnapshot,y,x + 1) + detectCliff(modelSnapshot,y,x -1);
}
})
});
let gameOver = 0;
if (height > 18) {
for(let j = 0; j < 2; j++){
for(let i = 0; i < 4; i++){
if(model[j][i + 3]) gameOver = 1;
};
};
};
let minClear = 4 - lines
let score = height * 0.4 + holes * 8 + cliff * 1.5 + minClear * 0.1 + gameOver * 18283375748390;
return score;
}
function detectHole (modelSnapshot,y,x){
return (modelSnapshot[y+1] != undefined && modelSnapshot[y+1][x] == 0)
}
function detectCliff (modelSnapshot,y,x){
let oldY = y;
while(modelSnapshot[y] != undefined && modelSnapshot[y][x] == 0 && oldY - y != -3) y ++;
return (oldY - y == -3);
}