-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.js
More file actions
112 lines (95 loc) · 4.07 KB
/
play.js
File metadata and controls
112 lines (95 loc) · 4.07 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
var win = 0;
var loose = 0;
function sendAjax() {
if (loose == 1 || win == 1) {
resetPage();
loose = 0;
win = 0;
}
var toSend = "colors=";
for (i = 0; i < 4; i++) {
if (i != 0)
toSend += "+";
toSend += document.getElementById("usrImg" + i).alt;
}
var xhttp = new XMLHttpRequest();
console.log("Send");
xhttp.onreadystatechange = function () {
console.log(this.readyState + " " + this.status);
if (this.readyState == 4 && this.status == 200) {
var tokens = this.responseText.split("+");
console.log("Answer received : " + tokens[0] + " " + tokens[1] + " " + tokens[2]);
var currentLine = tokens[0];
var color;
for (i = 0; i < 4; i++) {
color = document.getElementById("usrImg" + i).alt;
document.getElementById("game" + currentLine + i).alt = color;
document.getElementById("game" + currentLine + i).src = color + ".png";
}
for (i = 0; i < tokens[1]; i++) {
document.getElementById("results" + currentLine + i).alt = "red";
document.getElementById("results" + currentLine + i).src = "red.png";
}
for (i = tokens[1]; i < tokens[1] * 1 + tokens[2] * 1; i++) {
document.getElementById("results" + currentLine + i).alt = "white";
document.getElementById("results" + currentLine + i).src = "white.png";
}
if (tokens[1] == 4) {
window.alert("Congratulation ! You Won !");
win = 1;
}
else if (tokens[0] == 11) {
window.alert("Too bad, you lost :'(");
loose = 1;
}
}
};
xhttp.open("GET", "/request?" + toSend);
xhttp.send();
}
function resetPage() {
console.log("resetting...");
var color = "empty";
for (var j = 0; j < 12; j++) {
for (var i = 0; i < 4; i++) {
document.getElementById("game" + j + i).alt = color;
document.getElementById("game" + j + i).src = color + ".png";
document.getElementById("results" + j + i).alt = color;
document.getElementById("results" + j + i).src = color + ".png";
}
}
}
function changeColor(x) {
var element = document.getElementById("usrImg" + x);
var color = element.getAttribute("alt");
switch (color) {
case "white":
element.src = "black.png";
element.alt = "black";
break;
case "black":
element.src = "red.png";
element.alt = "red";
break;
case "red":
element.src = "blue.png";
element.alt = "blue";
break;
case "blue":
element.src = "yellow.png";
element.alt = "yellow";
break;
case "yellow":
element.src = "green.png";
element.alt = "green";
break
case "green":
element.src = "white.png";
element.alt = "white";
break;
default:
element.src = "white.png";
element.alt = "white";
break;
}
}