-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
129 lines (106 loc) · 2.56 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
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function createBoxList() {
const list = [];
for (let i = 0; i < 100; i++) {
list.push(i);
}
return list;
}
function printBoxList(list) {
let toPrint = "";
for (let i = 0; i < list.length; i++) {
const value = list[i];
let indexStr = `${i + 1}`;
let valueStr = `${value + 1}`;
if (indexStr.length < 2) {
indexStr = ` ${indexStr}`;
}
if (indexStr.length < 3) {
indexStr = ` ${indexStr}`;
}
if (valueStr.length < 2) {
valueStr = ` ${valueStr}`;
}
if (valueStr.length < 3) {
valueStr = ` ${valueStr}`;
}
toPrint += `[N:${indexStr} | V:${valueStr}] `;
if ((i + 1) % 10 === 0 && i !== 0) {
toPrint += "\n";
}
}
console.log(toPrint);
}
function shuffleBoxList(list) {
let currentIndex = list.length;
let randomIndex;
// While there remain elements to shuffle.
while (currentIndex > 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[list[currentIndex], list[randomIndex]] = [
list[randomIndex],
list[currentIndex],
];
}
}
function attemptToFindNumber(startNumber, list) {
let boxToCheck = startNumber;
for (let i = 0; i < 50; i++) {
const value = list[boxToCheck];
if (value === startNumber) {
return [true, boxToCheck];
}
boxToCheck = value;
}
return [false, null];
}
function playGame(logList = false) {
const boxList = createBoxList();
if (logList) {
printBoxList(boxList);
}
shuffleBoxList(boxList);
if (logList) {
printBoxList(boxList);
}
for (let i = 0; i < 100; i++) {
const res = attemptToFindNumber(i, boxList);
if (!res[0]) {
return false;
}
}
return true;
}
function calcProdability() {
let winGames = 0;
let numberOfGames = 1_000;
let logList = false;
if (process.argv.length >= 3) {
numberOfGames = parseInt(process.argv[2]);
}
if (process.argv.length >= 4) {
logList = process.argv[3] === "true";
}
console.log("Number of attempts set to:", numberOfGames, `\n`);
const printPoint = parseInt(numberOfGames / 10);
for (let i = 0; i < numberOfGames; i++) {
const gameRes = playGame(logList);
if (gameRes) {
winGames++;
}
const round = i + 1;
if (round % printPoint === 0) {
console.log("PlayGames attempt done:", round);
}
}
console.log("\nWin rate:", (winGames / numberOfGames) * 100, "%");
}
/**
* Run
*/
calcProdability();