Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let states = Array(800).fill(0).map((_, n) => [Math.floor(n / 200) % 4, Math.floor(n / 20) % 10, Math.floor(n / 4) % 5 + 1, n % 4 + 1]);
- states = states.map(function(n) {
- return {
- state: n.join(","),
- val: 0
- };
- });
- // states[i] looks like
- // {
- // state: "(player health),(opponent health),(player combo),(opponent combo)",
- // val: 0
- // }
- states[780].val = 2 ** 11 * 3 ** 7; // initialize this many starting games
- // the number doesn't really matter, this just makes the results integers
- for (let a = 0; a < 50; a++) { // do 50 rounds to make sure all the possible games finish
- for (let i = 0; i < 800; i++) { // one round of simulations; 800 possible states
- let state = states[i].state.split(",").map(n => parseInt(n));
- if (state[0] === 0 || state[1] === 0) continue;
- let newStates = [
- [state[0], Math.max(state[1] - state[2], 0), state[2] + 1, 1], // win
- [Math.max(state[0] - state[3], 0), state[1], 1, state[3] + 1], // loss
- [state[0], state[1], 1, 1] // draw
- ];
- if (state[2] === 1 && state[3] === 1) {newStates = newStates.slice(0, 2)} // remove draws if both combos are 1
- let val = states[i].val;
- states[i].val = 0;
- newStates.map(n => states[n[0] * 200 + n[1] * 20 + n[2] * 4 + n[3] - 5].val += val / newStates.length);
- }
- }
- console.log(states.filter(n => n.val > 0).filter(n => n.state[0] === "0").reduce((a,b) => a + b.val, 0)); // number of times othermart lady wins
- console.log(states.filter(n => n.val > 0).filter(n => n.state[0] !== "0").reduce((a,b) => a + b.val, 0)); // number of times player wins
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement