-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
120 lines (106 loc) · 4.39 KB
/
script.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
localStorage.setItem("leaderBoard", localStorage.getItem("leaderBoard") || '[]');
let leaderboard = JSON.parse(localStorage.getItem("leaderBoard"));
let gamemode = "score";
function selectGamemode(gm) {
gamemode = gm;
if(gm == "score") {
document.querySelector(".gamemode-score").classList.add("active");
document.querySelector(".gamemode-time").classList.remove("active");
}
else {
document.querySelector(".gamemode-score").classList.remove("active");
document.querySelector(".gamemode-time").classList.add("active");
}
}
function genLeaderboard() {
if(leaderboard.length == 0) {
document.querySelector(".leaderboard-content").innerHTML = "No scores yet!";
return;
}
document.querySelector(".leaderboard-content").innerHTML = leaderboard.map((score, index) => `
<div class="leaderboard-item">
<div class="leaderboard-item__rank">${index + 1}</div>
<div class="leaderboard-item__name">${score.name}</div>
<div class="leaderboard-item__score">${score.score}</div>
</div>
`).join("");
}
genLeaderboard();
document.querySelector('.inp-screen input').addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
document.querySelector('.inp-screen button').click();
}
});
let name;
function finishGame(win, score) {
document.body.className = "inp-screen";
game.cleanup();
game = new GameStub();
if(!win) {
if(gamemode == "time") {
setTimeout(() => {
alert('Game Over! I mean you died. Very sad.');
}, 500)
return;
}
else {
setTimeout(() => {
alert('Game Over! Your score is: ' + score);
}, 500)
}
}
if(gamemode == "time") {
setTimeout(() => {
alert('Your score is ' + score + ' lines in 2 minutes!');
}, 500)
}
leaderboard.push({name, score});
leaderboard.sort((a, b) => b.score - a.score);
localStorage.setItem("leaderBoard", JSON.stringify(leaderboard));
console.log(leaderboard);
genLeaderboard();
}
class GameStub {
constructor() {
}
setDAS(DAS) {
localStorage.setItem("DAS", DAS);
}
setARR(ARR) {
localStorage.setItem("ARR", ARR);
}
setSDF(SDF) {
localStorage.setItem("SDF", SDF);
}
}
let game = new GameStub();
function startGame() {
document.body.className = "game-screen";
name = document.querySelector("body div input").value;
// Create a new game object
game = new Tetris(finishGame, gamemode, (score) => {document.querySelector(".score").innerHTML = score;});
}
// let game = new Tetris("asdf", null);
// error case
// let rootLayout = new Layout(4, 4);
// rootLayout.addConstraints(new ProportionalConstraint(['x', 1, 2], ['y', 1, 2], 1));
// rootLayout.addConstraints(new ProportionalConstraint(['x', 0, 3], ['x', 0, 1], 0.2));
// rootLayout.addConstraints(new ProportionalConstraint(['x', 0, 3], ['x', 0, 2], 0.4));
// rootLayout.addConstraints(new ProportionalConstraint(['y', 0, 3], ['y', 0, 1], 0.3));
// rootLayout.addConstraints(new ProportionalConstraint(['y', 0, 3], ['y', 0, 2], 0.7));
//not implemented case 2
// let rootLayout = new Layout(4, 4);
// rootLayout.addConstraints(new FixedConstraint(['y', 2, 3], 100));
// rootLayout.addConstraints(new ProportionalConstraint(['y', 0, 1], ['y', 1, 2], 3));//probelem here
// rootLayout.addConstraints(new FixedConstraint(['x', 0, 1], 100));
// rootLayout.addConstraints(new ProportionalConstraint(['x', 1, 2], ['x', 2, 3], 3));
// rootLayout.addConstraints(new ProportionalConstraint(['x', 1, 2], ['y', 1, 2], 1));
// let rootLayout = new Layout(3, 2);
// rootLayout.addConstraints(new ProportionalConstraint(['x', 0, 1], ['x', 1, 2], 1));
// rootLayout.addConstraints(new ProportionalConstraint(['x', 0, 2], ['y', 0, 1], 1));
// let rootLayout = new Layout(6, 2);
// rootLayout.addConstraints(new ProportionalConstraint(['x', 0, 1], ['x', 4, 5], 1));// error if first
// rootLayout.addConstraints(new ProportionalConstraint(['x', 1, 4], ['x', 1, 2], 0.2952));
// rootLayout.addConstraints(new ProportionalConstraint(['x', 1, 4], ['x', 1, 3], 0.7048));
// rootLayout.addConstraints(new ProportionalConstraint(['y', 0, 1], ['x', 1, 4], 1.2207));