-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
154 lines (126 loc) · 4.4 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
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
const originArray = ['A', 'B', 'C', 'D', 'E'];
const stateArray = [...originArray];
let count = 0;
// Rules and close button event handlers
const restartBtn = document.getElementById('retart-btn');
const message = document.getElementById('message');
restartBtn.addEventListener('click', startGame);
// Handling boxes drag and drop events
const boxes = document.querySelectorAll(".box");
function startGame(){
message.classList.remove('show');
mixAlphabet();
mixBoxesContent();
}
function mixAlphabet(){
for (let i = stateArray.length-1; i > 0; i--){
const randomIndex = Math.floor(Math.random() * i);
console.log(i, randomIndex);
switchStateArrayValue(i, randomIndex);
}
console.log('Start the game data: ', stateArray);
}
function switchStateArrayValue(firstIndex, secondIndex){
console.log('test');
const temporaryValue = stateArray[firstIndex];
stateArray[firstIndex] = stateArray[secondIndex];
stateArray[secondIndex] = temporaryValue;
}
// Add mixed array content to the boxes
function mixBoxesContent(){
for (var k = 0; k < stateArray.length; k++){
boxes[k].querySelector("span").textContent = stateArray[k].toString();
}
}
startGame();
// Add event to the box elements
boxes.forEach(boxElement => {
boxElement.addEventListener("dragstart", dragStart);
boxElement.addEventListener("dragenter", dragEnter);
boxElement.addEventListener("dragover", dragOver);
boxElement.addEventListener("dragleave", dragLeave);
boxElement.addEventListener("dragend", dragEnd);
boxElement.addEventListener("drop", drop);
});
function dragStart(ev){
console.log('user starts to drag an element...')
ev.target.classList.add("drag-start");
ev.dataTransfer.setData("text", ev.target.id);
}
function dragEnter(ev){
console.log('the dragged element enters the drop target...')
if(!ev.target.classList.contains("drag-start")) {
ev.target.classList.add("drag-enter");
}
}
function dragOver(ev){
console.log('the dragged element is over the drop target...')
ev.preventDefault();
}
function dragLeave(ev){
console.log('the dragged element leaves the drop target...')
ev.target.classList.remove("drag-enter");
}
function dragEnd(ev){
console.log('user has finished dragging the element...');
ev.target.classList.remove("drag-start");
}
function drop(ev) {
console.log('the dragged element is dropped on the drop target...');
ev.preventDefault();
ev.target.classList.remove("drag-enter");
// get data of source and target
const sourceBoxId = ev.dataTransfer.getData("text");
const targetBoxId = ev.target.id;
console.log(sourceBoxId,targetBoxId);
if(sourceBoxId != targetBoxId){
// collect the source box ID
const sourceBox = document.getElementById(sourceBoxId);
// collect the target box data and save to variables
const targetBackground = ev.target.style.backgroundColor;
const targetContent = ev.target.querySelector("span").textContent;
// change the background color and span text content of the TARGET box
ev.target.style.backgroundColor = sourceBox.style.backgroundColor;
ev.target.querySelector("span").textContent = sourceBox.querySelector("span").textContent;
ev.target.id = sourceBoxId;
// change the background color and span text content of the SOURCE box
sourceBox.style.backgroundColor = targetBackground;
sourceBox.querySelector("span").textContent = targetContent;
sourceBox.id = targetBoxId;
}
}
// Check if two alphabet array is linear equal
function checkResult(firstArray, secondArray)
{
let check = true;
// if length is not equal
if(firstArray.length != secondArray.length){
console.log('Two array is not equal length, false');
return "False";
}
else
// comapring each element of array
for(var i=0; i<firstArray.length; i++) {
console.log(firstArray[i], secondArray[i]);
if(firstArray[i]!= secondArray[i]) {
check = false;
}
}
return check;
}
function isEqual (){
// Update the array stateArray with the result of sorting by the player
for (var k = 0; k < stateArray.length; k++){
stateArray[k] = boxes[k].querySelector("span").textContent;
}
console.log(stateArray);
// Check if user finish the game
let checkEqual = checkResult(originArray, stateArray);
console.log('Check player result: ', checkEqual);
if (checkEqual){
alert ('You WIN! Play Again');
startGame();
// message.classList.add('show');
}
}
setInterval(isEqual, 2000);