forked from wu814/PayoutPro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoker.js
151 lines (118 loc) · 4.06 KB
/
poker.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class MaxHeap {
constructor() {
this.heap = [];
}
parent(i) {
return Math.floor((i - 1) / 2);
}
left(i) {
return 2 * i + 1;
}
right(i) {
return 2 * i + 2;
}
heapify_up(i) {
while (i > 0 && this.heap[this.parent(i)][1] < this.heap[i][1]) {
[this.heap[this.parent(i)], this.heap[i]] = [this.heap[i], this.heap[this.parent(i)]];
i = this.parent(i);
}
}
heapify_down(i) {
let max_index = i;
let l = this.left(i);
let r = this.right(i);
if (l < this.heap.length && this.heap[l][1] > this.heap[max_index][1]) max_index = l;
if (r < this.heap.length && this.heap[r][1] > this.heap[max_index][1]) max_index = r;
if (i !== max_index) {
[this.heap[i], this.heap[max_index]] = [this.heap[max_index], this.heap[i]];
this.heapify_down(max_index);
}
}
insert(key, value) {
this.heap.push([key, value]);
this.heapify_up(this.heap.length - 1);
}
get_max() {
if (this.heap.length === 0) {
console.log("Heap is empty");
return ["", -1];
}
return this.heap[0];
}
extract_max() {
if (this.heap.length === 0) {
console.log("Heap is empty");
return ["", -1];
}
let max = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapify_down(0);
return max;
}
print_heap() {
console.log(this.heap.map(i => i[0] + ":" + i[1]).join(" "));
}
print_loss() {
let stri = this.heap.map(i => i[0] + " owes $" + i[1] + ".").join("<br>");
return stri;
}
print_gain() {
let stri = this.heap.map(i => i[0] + " earns $" + i[1] + ".").join("<br>");
return stri;
}
}
function rearrange(greater, less) {
greater.insert(greater.get_max()[0], greater.get_max()[1] - less.get_max()[1]);
greater.extract_max();
less.insert(less.get_max()[0], 0);
less.extract_max();
}
function main() {
const loss = new MaxHeap();
const gain = new MaxHeap();
const nameArray = ["p1name", "p2name", "p3name", "p4name", "p5name", "p6name", "p7name", "p8name", ];
const buyInArray = ["p1total", "p2total", "p3total", "p4total", "p5total", "p6total", "p7total", "p8total"];
const chipsArray = ["p1worth", "p2worth", "p3worth", "p4worth", "p5worth", "p6worth", "p7worth", "p8worth"];
// call to change each element
for (let i = 0; i < 8; i++) {
nameArray[i] = document.getElementById(nameArray[i]).value;
chipsArray[i] = document.getElementById(chipsArray[i]).value;
buyInArray[i] = document.getElementById(buyInArray[i]).value;
}
for (let i = 0; i < 8; i++) {
// try {
// chips = parseFloat(chipStr);
// buyIn = parseFloat(buyStr);
// } catch (e) {
// console.log(`Invalid input: ${e.message}`);
// process.exit(1);
// }
let net = parseFloat(chipsArray[i]) - parseFloat(buyInArray[i]);
if (nameArray[i] === "none") {
net = 0;
}
if (net > 0) {
gain.insert(nameArray[i], net);
}
else if (net < 0) {
loss.insert(nameArray[i], -1 * net);
}
}
let summary = loss.print_loss() + "<br>" + "<br>" + gain.print_gain();
//console.log(summary);
document.getElementById("sum").innerHTML = summary;
let trans = "";
while (gain.get_max()[1] !== 0 && loss.get_max()[1] !== 0) {
if ( gain.get_max()[1] < loss.get_max()[1]) {
trans = trans + `${loss.get_max()[0]} pays ${gain.get_max()[0]} $${gain.get_max()[1]}.` + "<br>";
rearrange(loss, gain);
}
else {
trans = trans + `${loss.get_max()[0]} pays ${ gain.get_max()[0]} $${loss.get_max()[1]}.` + "<br>";
rearrange( gain, loss);
}
}
//console.log(trans);
document.getElementById("trans").innerHTML = trans;
return 0;
}