-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjungo.js
140 lines (123 loc) · 2.78 KB
/
jungo.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
import board from './board.js';
let checkBox = document.getElementById("myCheck");
let showTable = document.getElementById("showTable");
// init game over
let isGameOver = false;
let gameOverCounter = 0;
// init Game
let game = new WGo.Game(7);
let numberOfBlack;
let numberOfWhite;
// sound
let soundsURL = [
"sound/moveSound.mp3",
"sound/capture0.mp3",
"sound/pass.mp3",
];
let sounds = new Array(soundsURL.length);
soundsURL.forEach((url, i) => {
sounds[i] = new Howl({
src: [url],
});
});
// init current step
let currentStep = 0;
let step_template = "";
/**
* show steps
*/
let showSteps = (currentStep) => {
step_template = `<p>現在手數: ${currentStep}</p>`;
document.querySelector("#current-step").innerHTML = step_template;
};
showSteps(currentStep);
/**
* show numbers of stones
*/
let showStones = (black, white) => {
let numberOfBlack = black;
let numberOfWhite = white;
document.querySelector("#black-stones").innerHTML = numberOfBlack;
document.querySelector("#white-stones").innerHTML = numberOfWhite;
};
// move one step
board.addEventListener("click", function (x, y) {
if (isGameOver) return;
if (!game.isValid(x, y)) return;
game.play(x, y);
gameOverCounter = 0;
board.addObject({
x: x,
y: y,
c: -game.turn,
});
sounds[0].play();
currentStep++;
showSteps(currentStep);
removeCapturedStones();
});
let removeCapturedStones = () => {
numberOfBlack = 0;
numberOfWhite = 0;
for (let m = 0; m < 7; m++) {
for (let n = 0; n < 7; n++) {
if (game.getPosition().get(m, n) == 0) {
board.removeObjectsAt(m, n);
} else if (game.getPosition().get(m, n) == 1) {
numberOfBlack++;
} else {
numberOfWhite++;
}
}
}
showStones(numberOfBlack, numberOfWhite);
};
/**
* Toggle table
*/
window.toggleTable = () => {
// If the checkbox is checked, display the output text
if (checkBox.checked == true) {
showTable.style.display = "table";
} else {
showTable.style.display = "none";
}
};
/**
* pass (虛手)
*/
window.passGame = () => {
if (isGameOver) return;
sounds[2].play();
if (gameOverCounter == 1) {
currentStep++;
showSteps(currentStep);
gameOver();
} else {
game.pass();
gameOverCounter++;
currentStep++;
showSteps(currentStep);
}
};
/**
* resign (認輸)
*/
window.resignGame = () => {
location.reload();
};
/**
* game over (結束遊戲)
*/
let gameOver = () => {
isGameOver = true;
showTable.style.display = "table";
checkBox.checked = true;
if (numberOfBlack > numberOfWhite) {
document.querySelector("#winner").innerHTML = "黑勝";
} else if (numberOfBlack < numberOfWhite) {
document.querySelector("#winner").innerHTML = "白勝";
} else {
document.querySelector("#winner").innerHTML = "平手";
}
};