-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.ts
More file actions
87 lines (71 loc) · 2.2 KB
/
Copy pathsample.ts
File metadata and controls
87 lines (71 loc) · 2.2 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
import { Array } from "effect"
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
type Player = "Player 1" | "Player 2"
function initGame(root: Element) {
function updateUI() {
currentPlayerText.textContent = `Current player: ${currentPlayer}`
kText.textContent = `k: ${k}`
nText.textContent = `n: ${n}`
p1HistoryText.textContent = `Player 1: ${player1History}`
p2HistoryText.textContent = `Player 2: ${player2History}`
}
function chooseInteger(m: number) {
k *= m
if (currentPlayer === "Player 1") {
player1History = [...player1History, m]
} else {
player2History = [...player2History, m]
}
if (k >= n) {
root.innerHTML = `
<p>${currentPlayer} wins!</p>
<button id="play-again">Play again</button>
`
const playAgainButton = document.querySelector("button")
playAgainButton?.addEventListener("click", () => {
initGame(root)
})
} else {
currentPlayer = currentPlayer === "Player 1" ? "Player 2" : "Player 1"
}
updateUI()
}
let k = 1
let n = Math.floor(Math.random() * 9901) + 100
let currentPlayer: Player = "Player 1"
let player1History: number[] = []
let player2History: number[] = []
root.innerHTML = `
<h2 id="current-player"></h2>
<h2 id="k"></h2>
<h2 id="n"></h2>
<p id="history-p1"></p>
<p id="history-p2"></p>
`
const currentPlayerText = document.querySelector("#current-player")!
const kText = document.querySelector("#k")!
const nText = document.querySelector("#n")!
const p1HistoryText = document.querySelector("#history-p1")!
const p2HistoryText = document.querySelector("#history-p2")!
updateUI()
for (let m = 2; m <= 9; m++) {
const button = document.createElement("button")
button.textContent = `${m}`
button.addEventListener("click", async () => {
const buttons = [...document.querySelectorAll("button")]
Array.forEach(buttons, (b) => {
b.disabled = true
})
chooseInteger(m)
await sleep(500)
Array.forEach(buttons, (b) => {
b.disabled = false
})
})
root.appendChild(button)
}
}
function mains() {
const root = document.querySelector("#app")!
initGame(root)
}