-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
323 lines (283 loc) · 9.96 KB
/
index.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100" rel="stylesheet">
<style>
* {
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
header{
height: 3vh;
width: 100%;
transition: all .3s ease-in-out 0s;
}
body {
overflow: hidden;
padding: 0;
margin: 0;
}
#title {
color: rgb(70, 70, 70);
font-size: 4.5vh;
margin-top: 3vh;
}
table {
width: 100%;
height: 75vh;
}
td {
cursor: pointer;
background: rgb(245, 245, 245);
transition: all .25s ease-in-out 0s;
border-radius: 3px;
border: 2px solid transparent;
}
td:active{
border-color: rgb(180, 180, 180);
}
h1 {
text-align: center;
font-family: Roboto;
letter-spacing: 1px;
font-size: 1.8em;
transition: opacity .25s ease-in-out 0s;
font-weight: 100;
cursor: default;
}
#info {
left: 0;
background: rgb(50, 50, 50);
opacity: 0;
width: 100%;
font-size: 5vh;
top: 50vh;
transform: translateY(-50%);
color: white;
padding-top: 2.5vh;
padding-bottom: 2.5vh;
position: absolute;
display: none;
letter-spacing: 1px;
transition: all .25s ease-in-out 0s;
}
button {
padding-top: 1.5vh;
padding-bottom: 1.5vh;
padding-left: 3%;
padding-right: 3%;
cursor: pointer;
outline: none;
margin-left: auto;
margin-right: auto;
margin-top: 3vh;
display: block;
background: #EEEEEE;
border-radius: 2vh;
color: rgb(50, 50, 50);
font-family: Roboto;
font-size: 3vh;
border: none;
transition: all .1s ease-in-out 0s;
box-shadow: 0px 0px 25px 0px rgba(176,176,176,0.19);
}
button:active {
transform: scale(.94);
box-shadow: 0px 0px 0px 0px rgba(176,176,176,0.22);
}
.cellBlue {
background: #29D2E4;
}
.cellRed {
background: #F73859;
}
.cellBlueHover {
background: rgba(41, 210, 228, .15);
}
.cellRedHover {
background: rgba(247, 56, 89, .15);
}
.animate {
transform: scale(1.05);
}
.cellBase {
opacity: 1;
}
.cellEnd {
opacity: .2;
}
.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>
<script type="text/javascript">
var bEnd = false;
var tCases = [];
var bPlayerOne = true;
var Rows;
var Columns;
function Initialize(cols, rows) {
Rows = rows;
Columns = cols;
bEnd = false;
tCases = [];
bPlayerOne = true;
var grid = document.getElementById('grid');
grid.innerHTML = "";
for (var r = 0; r < rows; ++r) {
var tr = grid.appendChild(document.createElement('tr'));
for (var c = 0; c < cols; ++c) {
var _case = new Case(c, r, 0);
_case.Create(tr);
tCases.push(_case);
}
}
document.getElementById('content').appendChild(grid);
}
function Case(X, Y, Clicker, id) {
this.Element = document.createElement('td');
this.Element.Clicker = Clicker;
this.Element.X = X;
this.Element.Y = Y;
this.Create = function (i) {
this.Element.onmouseover = function (e){
if(!bEnd)
Hover(e.target);
}
this.Element.onmouseout = function (e){
Unhover(e.target);
}
this.Element.onclick = function (e) {
if (!bEnd) {
var c = CountCasesColumn(e.target.X).Element;
if (c.Clicker === 0) {
c.className = '';
if (bPlayerOne) {
c.classList.add('cellBlue');
c.Clicker = 1;
} else {
c.classList.add('cellRed');
c.Clicker = 2;
}
c.classList.add('animate');
setTimeout(function () {
c.classList.remove('animate');
}, 200);
bPlayerOne = !bPlayerOne;
Hover(e.target);
var tDirections = [
tCases.filter(function (a) { return a.Element.X == c.X }),
tCases.filter(function (a) { return a.Element.Y == c.Y }),
tCases.filter(function (a) { return a.Element.X - c.X == a.Element.Y - c.Y }),
tCases.filter(function (a) { return -(a.Element.X - c.X) == a.Element.Y - c.Y })
];
for (var i = 0; i < tDirections.length; i++) {
var lWin = [];
for (var j = 0; j < tDirections[i].length; j++) {
if (tDirections[i][j].Element.Clicker == c.Clicker) {
lWin.push(tDirections[i][j]);
if (lWin.length >= 4) {
bEnd = true;
Message("Le joueur " + c.Clicker + " a gagné");
for (var i = 0; i < tCases.length; i++) {
if (lWin.indexOf(tCases[i]) < 0 && tCases[i].Element.Clicker > 0) {
tCases[i].Element.style.opacity = .2;
}
}
Unhover(e.target);
break;
}
} else lWin = [];
}
}
if (AllClicked() >= tCases.length && !bEnd) {
bEnd = true;
Message("Egalité");
for (var i = 0; i < tCases.length; i++) {
tCases[i].Element.style.opacity = .2;
}
Unhover(e.target);
}
}
}
};
i.appendChild(this.Element);
}
}
function Hover(e) {
var c = CountCasesColumn(e.X).Element
if(bPlayerOne)
c.classList.add('cellBlueHover');
else
c.classList.add('cellRedHover');
}
function Unhover(e) {
var c = CountCasesColumn(e.X).Element;
if(c.Clicker === 0)
c.className = '';
}
function CaseXY(x, y) {
for (var i = 0; i < tCases.length; i++) {
if (tCases[i].Element.X == x && tCases[i].Element.Y == y) {
return tCases[i];
}
}
return new Case(0, 0, -1);
}
function CountCasesColumn(Column) {
var Count = 0;
var Case;
for (var i = 0; i < tCases.length; i++) {
if (tCases[i].Element.X == Column && tCases[i].Element.Clicker > 0) {
Count++;
}
}
return CaseXY(Column, Rows - Count - 1);
}
function AllClicked() {
var Count = 0;
for (var i = 0; i < tCases.length; i++) {
if (tCases[i].Element.Clicker > 0) {
Count++;
}
}
return Count;
}
function Message(msg) {
var info = document.getElementById('info');
info.innerHTML = msg;
info.style.opacity = .8;
setTimeout(function () {
info.style.display = "block";
}, 350);
}
function CancelMessage() {
var info = document.getElementById('info');
info.style.opacity = 0;
setTimeout(function () {
info.style.display = "none";
}, 350);
}
function Reset(cols, rows) {
for (var i = 0; i < tCases.length; i++) {
tCases[i].Element.className = '';
}
CancelMessage();
setTimeout(function () {
Initialize(cols, rows);
}, 350);
}
</script>
</head>
<body onload="Initialize(7, 6)">
<div data-role="page" data-theme="b" class="noselect">
<h1 id="title">Puissance 4</h1>
<table id="grid"></table>
<h1 id="info"></h1>
<button onclick="Reset(7, 6)">Rejouer</button>
</div>
</body>
</html>