-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
157 lines (122 loc) · 4.76 KB
/
index.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<title>the war calculator</title>
<meta name = "viewport" content = "width = device-width, initial-scale = 1"/>
<meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<link rel = "stylesheet" href = "styles.css">
<link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet">
</head>
<body>
<h1>the war calculator</h1>
<form id = "the_only_form">
Number of players:<br>
<input type = "text" name = "players"><br><br>
Card Thrown:<br>
<input type = "text" name = "card">
</form><br>
<button onclick="main_function ()">calculate win percentage!</button>
<p id="result"></p>
<script>
const result = document.getElementById ("result");
function main_function ()
{
var form = document.forms["the_only_form"];
var number_of_players = parseInt (form.elements[0].value, 10);
var card_thrown = input_to_card (form.elements[1].value.toLowerCase ());
if (card_thrown < 1 || card_thrown > 13)
result.innerHTML = "error - not a card";
else
{
if (number_of_players < 2)
result.innerHTML = "error - not enough players";
else
{
if (number_of_players > 52)
result.innerHTML = "woah woah woah wayyy too many players!";
else
result.innerHTML = "win percentage: " + war_calculator (number_of_players - 1, card_thrown) + "%";
}
}
}
function war_calculator (number_players, card_thrown)
{
if (card_thrown === 2)
{
var top = choose (48, number_players);
var bottom = choose (52, number_players);
return Math.round (10000 * (1 - (top / bottom))) / 100;
}
else
{
var top = choose (52 - number_of_winners (card_thrown), number_players);
var bottom = choose (52, number_players);
return Math.round (10000 * (top / bottom)) / 100;
}
}
function factorial (input)
{
var result = input;
if (input === 0 || input === 1)
return 1;
else
{
while (input > 1)
{
input--;
result *= input;
}
return result;
}
}
function choose (n, k)
{
var output = factorial (n) / (factorial (k) * factorial (n - k));
return output;
}
function number_of_winners (input)
{
switch (input)
{
case 1:
return 4;
break;
case 3:
return 48;
break;
default:
return (14 - input) * 4;
}
}
function input_to_card (input)
{
switch (input)
{
case "1":
case "14":
case "a":
case "ace":
return 1;
break;
case "11":
case "j":
case "jack":
return 11;
break;
case "12":
case "q":
case "queen":
return 12;
break;
case "13":
case "k":
case "king":
return 13;
break;
default:
return parseInt (input, 10);
}
}
</script>
</body>
</html>