-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnim.js
More file actions
133 lines (123 loc) · 3.29 KB
/
Copy pathnim.js
File metadata and controls
133 lines (123 loc) · 3.29 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let arr = [];
let playerTaken = {
cnt: 0,
row: -1,
};
function rand(l,r) {
return l+Math.floor(Math.random()*(r-l+1));
}
function sum(arr) {
let s = 0;
for(let x of arr) s += x;
return s;
}
function takeStone(row) {
--arr[row];
let stone = $(`#stone-${row}-${arr[row]}`);
if(arr[row] != 0)
stone.width('0px');
else
stone.css('opacity', '0');
$(`#cnt${row}`).text(`${arr[row]}`);
}
function playerTake(row) {
if(playerTaken == null) return;
if(arr[row] == 0) {
alert("You cannot take stone in a empty pile");
return;
}
if(playerTaken.row != -1 && playerTaken.row != row) {
alert("You can take one pile in one move");
return;
}
takeStone(row);
playerTaken.row = row;
playerTaken.cnt++;
if(arr[row] == 0) comTake();
}
function comCalc(arr) {
let xor_sum = 0;
for(let i = 0; i < arr.length; i++) xor_sum ^= arr[i];
let moves = [];
if(xor_sum == 0) {
for(let i = 0; i < arr.length; i++) for(let j = 1; j <= arr[i]; j++) {
moves.push({
cnt: j,
row: i,
});
}
}else {
for(let i = 0; i < arr.length; i++) if((arr[i]^xor_sum) < arr[i]) {
moves.push({
cnt: arr[i] - (arr[i]^xor_sum),
row: i,
});
}
}
return moves;
}
function comTake() {
if(playerTaken.cnt == 0) {
alert("You haven't move yet!");
return;
}
playerTaken = null;
$(`#ready`).hide();
if(sum(arr) == 0) {
alert("Player Win!");
return;
}
let moves = comCalc(arr);
let m = moves[rand(0,moves.length-1)];
let delay_time = 500 / m.cnt;
for(let i = 0; i < m.cnt; i++) {
setTimeout(takeStone, delay_time*(i+1), m.row);
}
setTimeout(function() {
if(sum(arr) == 0) alert("Computer Win!");
$(`#ready`).show();
playerTaken = {
cnt: 0,
row: -1,
};
}, delay_time*(m.cnt+1));
}
function showRule() {
window.open("https://hackmd.io/Wolwkk29QhWdPLOjvpcepQ");
}
function showTuto() {
window.open("https://hackmd.io/p8NFDRJ8TkqPxg4JLp51hQ");
}
function getHint() {
let arr = prompt("input some integers, split by spaces").split(' ');
let moves = comCalc(arr);
let m = moves[rand(0,moves.length-1)];
alert(arr[m.row] + " " + m.cnt);
}
function init(n,C) {
let sum = 0;
for(let i = 0; i < n-1; i++) {
let x = rand(1,C);
arr.push(x);
sum ^= x;
}
while(arr.length < n) {
let x = rand(1,C);
if((x^sum) != 0) arr.splice(rand(0,arr.length), 0, x);
}
for(let i = 0; i < n; i++) {
$(`body`).append(`<span class="badge badge-pill badge-success" id="cnt${i}" style="font-size: 40px">${arr[i]}</span>`);
let bgstyle = i&1 ? "badge-light" : "badge-dark";
$(`body`).append(`<div class="badge badge-pill ${bgstyle}" style="font-size: 40px" id="row${i}" onclick="playerTake(${i})">`);
for(let j = 0; j < arr[i]; j++) $(`#row${i}`).append(`<img src="cobbleStone.png" class="stone" id="stone-${i}-${j}">`);
$(`body`).append(`</div><br>`);
}
$(`body`).append(`
<button id="reset" class="btn btn-info" style="font-size: 25px" onclick="location.reload()"> Reset </button>
<button id="rules" class="btn btn-info" style="font-size: 25px" onclick="showRule()"> Rules </button>
<button id="hint" class="btn btn-info" style="font-size: 25px" onclick="getHint()"> Hint </button>
<button id="tutor" class="btn btn-info" style="font-size: 25px" onclick="showTuto()"> Tutorial </button>
<button id="ready" class="btn btn-info" style="font-size: 25px" onclick="comTake()"> OK! </button>
`);
}
init(6,9);