-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (103 loc) · 3.03 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
class P4 {
constructor(selector) {
this.COL = 7;
this.LGN = 6;
this.selector = selector;
this.player = 'red';
this.drawGame();
this.ecoute();
this.checkWin();
this.draw();
}
//Affichage du jeu
drawGame() {
const $jeu = $(this.selector);
for (let lgn = 0; lgn < this.LGN; lgn++) {
const $lgn = $('<div>').addClass('lgn');
for (let col = 0; col < this.COL; col++) {
const $col = $('<div>').addClass('col empty').attr("data-col", col).attr("data-lgn", lgn);
$lgn.append($col);
}
$jeu.append($lgn);
}
}
ecoute() {
const $jeu = $(this.selector);
const that = this
//dernière case libre
function lastCase(col) {
const $cells = $(`.col[data-col='${col}']`);
for (let i = $cells.length - 1; i >= 0; i--) {
const $cell = $($cells[i]);
if ($cell.hasClass('empty')) {
return $cell;
}
}
return null;
}
$jeu.on('mouseenter', '.col.empty', function () {
const $col = $(this).data('col');
const $last = lastCase($col);
if ($last != null) {
$last.addClass(`p${that.player}`);
}
});
$jeu.on('mouseleave', '.col', function () {
$('.col').removeClass(`p${that.player}`);
});
$jeu.on('click', '.col.empty', function () {
const col = $(this).data('col');
const $last = lastCase(col);
$last.addClass(`${that.player}`).removeClass(`empty p${that.player}`).data('player', `${that.player}`);
const winner = that.checkWin($last.data('lgn'), $last.data('col'));
that.player = (that.player === 'red') ? 'yellow' : 'red';
if (winner) {
window.alert(` les ${winner} ont gagné la partie`);
$('#restart').css('visibility', "visible");
return;
}
});
}
checkWin(lgn, col) {
const that = this;
function $getCell(i, j) {
return $(`.col[data-lgn='${i}'][data-col='${j}']`);
}
function checkDirection(direction) {
let total = 0;
let i = lgn + direction.i;
let j = col + direction.j;
let $next = $getCell(i, j);
while (i >= 0 && i < that.LGN && j >= 0 && j < that.COL && $next.data('player') === that.player) {
total++;
i += direction.i;
j += direction.j;
$next = $getCell(i, j);
}
return total;
}
function checkWin(directionA, directionB) {
const total = 1 + checkDirection(directionA) + checkDirection(directionB);
if (total >= 4) {
return that.player;
} else {
return null;
}
}
function checkHorizon() {
return checkWin({ i: 0, j: -1 }, { i: 0, j: 1 });
}
function checkvertical() {
return checkWin({ i: -1, j: 0 }, { i: 1, j: 0 });
}
function checkDiagonal1() {
return checkWin({ i: 1, j: 1 }, { i: -1, j: -1 });
}
function checkDiagonal2() {
return checkWin({ i: 1, j: -1 }, { i: -1, j: 1 });
}
return checkHorizon() || checkvertical() || checkDiagonal1() || checkDiagonal2();
}
draw() {
}
}