-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
145 lines (131 loc) · 3.73 KB
/
main.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
const FROG = {
green: {
color: 'green',
img: 'imgs/frogGreen.png'
},
red: {
color: 'red',
img: 'imgs/frogRed.png'
}
};
const BLANK_SPACE = "blank";
let START_ARRAY = [];
let FINAL_ARRAY = [];
let posArray = [];
let prevArray = [];
let counter = 0;
setFrogNum($('#frogNumberSelector').children('option:selected').val());
function setFrogNum(num) {
let columns = Number((num * 2) + 1);
$('.frog').remove();
counter = 0;
buildArrays(num);
prevArray = [];
posArray = [];
prevArray.push(START_ARRAY.slice());
posArray = START_ARRAY.slice();
buildFrogs();
$('.frogGrid').css('grid-template-columns', 'repeat(' + columns + ', 1fr)');
displayFrogs();
}
function buildArrays(num) {
START_ARRAY = [BLANK_SPACE];
FINAL_ARRAY = [BLANK_SPACE];
for (let i = 0; i < num; i++) {
START_ARRAY.unshift(FROG.red.color);
START_ARRAY.push(FROG.green.color);
FINAL_ARRAY.unshift(FROG.green.color);
FINAL_ARRAY.push(FROG.red.color);
}
}
function buildFrogs() {
let list = document.getElementById('frogList');
for (let i = 0; i < START_ARRAY.length; i++) {
let listItem = document.createElement('li');
listItem.setAttribute('id', 'pos' + i);
listItem.setAttribute('class', 'frog');
document.getElementById('frogList').appendChild(listItem);
}
}
function displayFrogs() {
$('#counter').html(counter);
for (let i = 0; i < posArray.length; i++) {
if (posArray[i] == FROG.red.color) {
$('#pos' + i).html('<img class="clickable" src="' + FROG.red.img + '" alt="red frog" >');
} else if (posArray[i] == FROG.green.color) {
$('#pos' + i).html('<img class="clickable" src="' + FROG.green.img + '" alt="green frog" >');
} else {
$('#pos' + i).html('<img src="imgs/blankSpace.png" alt="" >');
}
}
}
function makeMove(index) {
if (!compareArrays(prevArray[prevArray.length - 1], posArray))
prevArray.push(posArray.slice());
switch (posArray[index]) {
case FROG.red.color:
if (posArray[index + 1] == BLANK_SPACE) {
posArray[index + 1] = FROG.red.color;
posArray[index] = BLANK_SPACE;
counter++;
} else if (posArray[index + 2] == BLANK_SPACE) {
posArray[index + 2] = FROG.red.color;
posArray[index] = BLANK_SPACE;
counter++;
}
break;
case FROG.green.color:
if (posArray[index - 1] == BLANK_SPACE) {
posArray[index - 1] = FROG.green.color;
posArray[index] = BLANK_SPACE;
counter++;
} else if (posArray[index - 2] == BLANK_SPACE) {
posArray[index - 2] = FROG.green.color;
posArray[index] = BLANK_SPACE;
counter++;
}
break;
};
displayFrogs();
if (compareArrays(posArray, FINAL_ARRAY)) {
alert('Congratulations, you did it!');
}
}
function resetFrogs() {
posArray = START_ARRAY.slice();
prevArray = [];
prevArray.push(START_ARRAY.slice());
counter = 0;
displayFrogs();
}
function compareArrays(x, y) {
if (x.length != y.length)
return false;
for (var i = 0; i < x.length; i++) {
if (x[i] != y[i])
return false;
}
return true;
}
function undo() {
if (prevArray[prevArray.length - 1] != null) {
if (compareArrays(prevArray[prevArray.length - 1], posArray) && prevArray.length > 1) {
prevArray.pop();
}
posArray = prevArray[prevArray.length - 1].slice();
if (prevArray.length > 1) {
prevArray.pop();
}
}
if (!compareArrays(prevArray, posArray) && counter > 0)
counter--;
displayFrogs();
}
$('#frogNumberSelector').change(function() {
setFrogNum($(this).children('option:selected').val());
});
$(document).on('click', '.frog', function() {
let pos = $(this).index();
// - 3 since the index of the list already has 3 elements for the buttons
makeMove(pos - 3);
});