-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
159 lines (143 loc) · 5.69 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
$(document).ready(initializeApp);
function initializeApp() {
makeBoardArray();
defaultPieces();
findOppositeColor();
checkPossibleMoves();
defineWin();
displayCurrentPlayer();
totalDiscCount();
applyClickHandler();
}
function applyClickHandler() {
$(".reset").click(gameReset)
}
var playerBlack = true;
var gameBoardArray = [];
var currentDiscs = [];
var neighborCells = [];
var validCells = [];
function makeBoardArray() {
var row = $(".row");
for (var rowIndex = 0; rowIndex < row.length; rowIndex++) {
var container = $(row[rowIndex]).find(".container");
gameBoardArray.push(container);
}
}
function findOppositeColor() {
for (var yIndex = 0; yIndex < gameBoardArray.length; yIndex++) {
for (var xIndex = 0; xIndex < gameBoardArray.length; xIndex++) {
if (playerBlack) {
if ($(gameBoardArray[yIndex][xIndex]).find(".disc").hasClass('white')) {
var discCoordinates = [yIndex, xIndex];
currentDiscs.push(discCoordinates);
}
} else {
if ($(gameBoardArray[yIndex][xIndex]).find(".disc").hasClass('black')) {
var discCoordinates = [yIndex, xIndex];
currentDiscs.push(discCoordinates)
}
}
}
}
}
function checkPossibleMoves() {
for (var i = 0; i < currentDiscs.length; i++) {
var discPosition = currentDiscs[i];
var coordinateY = parseInt(discPosition[0]);
var coordinateX = parseInt(discPosition[1]);
for (var column = -1; column < 2; column++) {
for (var row = -1; row < 2; row++) {
var cordY = coordinateY + (column);
var cordX = coordinateX + (row);
if (!$(gameBoardArray[cordY][cordX]).find(".disc").hasClass('white') && !$(gameBoardArray[cordY][cordX]).find(".disc").hasClass('black')) {
var neighborCoordinates = [cordY, cordX];
neighborCells.push(neighborCoordinates)
}
}
}
}
for (var i = 0; i < neighborCells.length; i++) {
var potentialPosition = neighborCells[i];
var discCoordinateY = parseInt(potentialPosition[0]);
var discCoordinateX = parseInt(potentialPosition[1]);
for (var column = -1; column < 2; column++) {
for (var row = -1; row < 2; row++) {
var cordY = discCoordinateY + (column);
var cordX = discCoordinateX + (row);
if (playerBlack) {
if (cordY > -1 && cordY < gameBoardArray.length - 1 && cordX > -1 && cordX < gameBoardArray.length - 1) {
if ($(gameBoardArray[cordY][cordX]).find(".disc").hasClass('white')) {
while (cordY > -1 && cordY < gameBoardArray.length - 1 && cordX > -1 && cordX < gameBoardArray.length - 1) {
if ($(gameBoardArray[cordY][cordX]).find(".disc").hasClass('black')) {
var validCoordinates = [discCoordinateY, discCoordinateX];
validCells.push(validCoordinates);
$(gameBoardArray[discCoordinateY][discCoordinateX]).addClass('highlight');
}
cordY += column;
cordX += row;
}
}
}
}
else {
if (cordY > -1 && cordY < gameBoardArray.length - 1 && cordX > -1 && cordX < gameBoardArray.length - 1) {
if ($(gameBoardArray[cordY][cordX]).find(".disc").hasClass('black')) {
while (cordY > -1 && cordY < gameBoardArray.length - 1 && cordX > -1 && cordX < gameBoardArray.length - 1) {
if ($(gameBoardArray[cordY][cordX]).find(".disc").hasClass('white')) {
var validCoordinates = [discCoordinateY, discCoordinateX];
validCells.push(validCoordinates);
$(gameBoardArray[discCoordinateY][discCoordinateX]).addClass('highlight');
console.log(validCells)
}
cordY += column;
cordX += row;
}
}
}
}
}
}
}
}
function winCondition() {
var blackCells = $(".black").length;
var whiteCells = $(".white").length;
if (blackCells > whiteCells) {
alert("Player B Wins!");
} else {
alert("Player W Wins!");
}
}
function gameReset() {
$(".container").removeClass("white black highlight");
initializeApp();
}
function totalDiscCount() {
var blackCells = $(".black").length;
var whiteCells = $(".white").length;
$(".value1 > .count").text(blackCells);
$(".value2 > .count").text(whiteCells);
}
function defaultPieces() {
$('div[row=3][col=3]').addClass("white");
$('div[row=3][col=4]').addClass("black");
$('div[row=4][col=3]').addClass("black");
$('div[row=4][col=4]').addClass("white");
}
function defineWin() {
var blackCells = $(".black").length;
var whiteCells = $(".white").length;
if (blackCells + whiteCells === 64) {
winCondition();
}
}
function displayCurrentPlayer() {
if (playerBlack) {
$(".playerIndicator1").show("blackTurn");
$(".arrow2").hide("arrow2")
} else {
$(".playerIndicator2").show("whiteTurn");
$(".arrow1").hide("arrow1")
}
}