-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
164 lines (126 loc) · 3.98 KB
/
main.js
File metadata and controls
164 lines (126 loc) · 3.98 KB
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
// JS FILE PIANO TILES GAME
let sequence = [];
let humanSequence = [];
let level = 0;
let vis=[];
count=0;
temp = new Array(16).fill(0);
seqflag = new Array(16).fill(0);
const startButton = document.querySelector('.js-start');
const info = document.querySelector('.js-info');
const heading = document.querySelector('.js-heading');
const tileContainer = document.querySelector('.js-container');
function resetGame(text) {
seqflag = new Array(16).fill(0);
alert(text);
sequence = [];
humanSequence = [];
level = 0;
startButton.classList.remove('hidden');
info.classList.add('hidden');
tileContainer.classList.add('unclickable');
}
function humanTurn(level) {
temp = new Array(16).fill(0);
console.log("ujjawal's turn");
tileContainer.classList.remove('unclickable');
info.textContent = `Your turn: ${level} Tap${level > 1 ? 's' : ''}`;
}
function activateTile(color) {
// computer's part
console.log("Tiles activating");
const tile = document.querySelector(`[data-tile='${color}']`);
tile.classList.add('activated');
setTimeout(() => {
tile.classList.remove('activated');
}, 200);
}
function playRound(nextSequence) {
nextSequence.forEach((color, index) => {
setTimeout(() => {
activateTile(color);
}, (index + 1) * 300);
});
}
// k=16;
function nextStep() {
ujjawal=true;
console.log("Returning random\nLevel ",level,"Starting...");
const tiles = ['row1', 'row2', 'row3', 'row4', 'row5', 'row6', 'row7', 'row8', 'row9', 'row10', 'row11', 'row12', 'row13', 'row14', 'row15', 'row16'];
while(ujjawal){
b1=Math.floor(Math.random() * tiles.length);
if(seqflag[b1]==0)
{seqflag[b1]=1;break;
}
}
console.log(tiles[b1]);
return tiles[b1];
}
function nextRound() {
if(level==16){
console.log("You Win!");
temp = new Array(16).fill(0);}
level += 1;
tileContainer.classList.add('unclickable');
info.textContent = "Bot's Turn";
heading.textContent = `Level ${level} of 16`;
const nextSequence = [...sequence];
nextSequence.push(nextStep());
playRound(nextSequence);
sequence = [...nextSequence];
setTimeout(() => {
humanTurn(level);
}, level * 600 + 500);
}
// in this function we have to fix random clicks
var flag=false;// flag=1 for false
const vis2 = ['row1', 'row2', 'row3', 'row4', 'row5', 'row6', 'row7', 'row8', 'row9', 'row10', 'row11', 'row12', 'row13', 'row14', 'row15', 'row16'];
function handleClick(tile) {
flag=true;
console.log("inside handleclick")
const index = humanSequence.push(tile) - 1;
a1=vis2.indexOf(tile);
if(temp[a1]==1)
flag=false;
else
temp[a1]=1;
count++;
if(seqflag[a1]!=temp[a1])
flag=0;
const remainingTaps = sequence.length - humanSequence.length;
if (!flag) {
count--;
resetGame(`Oops! Game over, you pressed the wrong tile/repeated tile and :: NO. OF LEVEL U PASEED: ${level-1} & YOUR SCORE IS: ${(level-1)*100}`);
heading.textContent=`PIANO TILES GAME`;
console.log('Your Score is' ,count);
console.log('ujjawal has pressed incorrect tile/ repeated tile henceGame over!');
return;
}
if (humanSequence.length === sequence.length) {
if (humanSequence.length == 16) {
resetGame('Congrats! You completed all level sucessesfully');
return;
}
humanSequence = [];
info.textContent = 'Success! Keep going!';
setTimeout(() => {
nextRound();
}, 500);
return;
}
info.textContent = `Your turn: ${remainingTaps} Tap${
remainingTaps > 1 ? 's' : ''
}`;
}
function startGame() {
console.log("Game started");
startButton.classList.add('hidden');
info.classList.remove('hidden');
info.textContent = "Bot's Turn";
nextRound();
}
startButton.addEventListener('click', startGame);
tileContainer.addEventListener('click', event => {
const { tile } = event.target.dataset;
if (tile) handleClick(tile);
});