-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.ts
More file actions
169 lines (135 loc) · 4.82 KB
/
debug.ts
File metadata and controls
169 lines (135 loc) · 4.82 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
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
158
159
160
161
162
163
164
165
166
167
168
169
import readlineSync from 'readline-sync';
import evaluate from './heistotron/evaluator';
import { Output, deepClone } from './heistotron/solver';
import { Heist } from './game/types';
import createPath from './game/path';
// import readline from 'readline';
// const rl = readline.createInterface({
// input: process.stdin,
// output: process.stdout
// });
// function getUserInput(prompt: string): Promise<boolean> {
// return new Promise((resolve) => {
// rl.question(prompt, (answer) => {
// resolve(answer === 'y');
// });
// });
// }
type State = {
index: number;
diff: number;
stealth: number;
treasures: number;
score: number;
cards: {
id: string,
value: number,
selectable: boolean,
selected: boolean
}[];
};
export default function Debug (
heist: Heist,
state: Output
): void {
heist = deepClone(heist);
const [ fSc, fState ] = state;
const [ path, fS, fT ] = fState;
const iSc = evaluate(heist);
const iS = heist.thief.getValue();
const iT = heist.thief.getScore();
// ! Cache all states
let vals: { [key: string]: number } = { };
let prev = -1;
const states: State[] = Array(path.length).fill(null).map((_, i) => {
const index = path[i];
if(prev === index)
{
// ? TODO: We must deal the same rng cards we used before to find this path.
// clone.setDeck([]);
if(heist.path.isEnd())
{
heist.thief.setValue(heist.thief.getValue() < 10? 10 : heist.thief.getValue());
}
const path = heist.path.getPath();
path.forEach(i => heist.setCard(i));
heist.setCard(heist.thief._index);
heist.thief._index = path[path.length-1];
heist.setCard(heist.thief._index, heist.thief);
heist.path = createPath(heist);
heist.deal();
vals = {};
}
prev = index;
const card = heist.getCard(index);
vals[index] = card.getValue(heist);
heist.path.select(heist, card);
const score = evaluate(heist);
const diff = heist.path.getDiff();
const stealth = heist.thief.getValue();
const treasures = heist.thief.getScore();
const cards = Array(9).fill(null).map((e, i) => {
const card = heist.getCard(i);
return {
id: card.id,
value: vals[i] || card.getValue(heist),
selectable: vals[i]? false : card.isSelectable(heist),
selected: heist.path.getPath().includes(i)
};
});
return { index, score, diff, stealth, treasures, cards };
});
// ! Play with full control
for (let i = 0; i < states.length;)
{
const { score, diff, stealth, treasures, cards } = states[i];
console.clear();
console.log(`Path ${path.map((h, j) => i===j? `(${h})` : h).join(' > ')}`);
const dSc = Math.round((score - iSc)*100)/100;
const dS = stealth - iS;
const dT = treasures - iT;
// console.log(`Stealth: ${stealth}/${path.length===9? fS < 10? 10 : fS : fS} | Treasures: (${dT > 0? '+' : '-'}${dT})`);
console.log(`\nStealth: ${fS} (${dS > 0? '+' : ''}${dS}) | Treasures: ${fT} (${dT > 0? '+' : ''}${dT})`);
console.log(`Difficulty: ${diff}`);
console.log(`Score: ${Math.round(score*100)/100} (${dSc > 0? '+' : ''}${dSc}/${fSc})\n`);
const _cards = cards.map(({ id, value, selectable, selected }) => {
if(id === 'thief')
{
return `${id} {${stealth}, ${treasures}}`;
}
return `${id[0].toUpperCase() + id.slice(1, 5)} ${(selected? '<v>' : (selectable? '(v)' : `[v]`)).replace('v', value as any)}`;
});
console.log(_cards.slice(0,3).join('\t'));
console.log(_cards.slice(3,6).join('\t'));
console.log(_cards.slice(6,9).join('\t'));
console.log("\nUse <←> / <a> / <→> / <d> to navigate path. <q> to quit.");
let key = '';
let arrow = '';
while(true)
{
key = readlineSync.keyIn('', { hideEchoBack: true, mask: '' });
if (key === '[') {
const k = readlineSync.keyIn('', { hideEchoBack: true, mask: '' });
// < = D, ^ = A, > = C, v = B
if (k === 'D' || k === 'A' || k === 'C' || k === 'B') arrow = k;
}
break;
}
if(arrow === 'C' || key === 'd')
{
i++;
}
else if((arrow === 'D' || key === 'a') && i > 0)
{
i--;
}
if(key === 'q')
{
break;
}
}
console.log('Debugger has ended.');
};
function logState(state: State)
{
}