-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmatchsticks.js
More file actions
49 lines (39 loc) · 1.03 KB
/
matchsticks.js
File metadata and controls
49 lines (39 loc) · 1.03 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
var c = true
var s = 21
function main(args) {
console.log("there are 21 matchsticks on the table");
console.log("computer's first");
while (s != 0) {
if (c) {
console.log("computer's thinking")
var r = (s - 1) % 4
var t = (r == 0) ? 1 : r
console.log("computer takes $t")
takeOrEnd(t)
c = !c
} else {
console.log("it is your turn")
var t
do {
console.log("you can take 1, 2 or 3!")
t = prompt("so how many sticks will you take?")
} while (!(t == 1 || t == 2 || t == 3))
takeOrEnd(t)
c = !c
}
printSticks()
}
}
function takeOrEnd(t) {
if (s - t <= 0) {
console.log("game over")
console.log("the winner is " + ( (c) ? "you" : "computer"))
s = 0
} else {
s -= t
}
}
function printSticks() {
console.log(`there are ${s} matchstick(s) on the table`)
console.log("|".repeat(s))
}