logoalt Hacker News

escanortoday at 7:42 AM2 repliesview on HN

pure random player:

  (async function(punch, delay) {
    async function sleep(ms) {
      return new Promise((resolve, _) => {
        setTimeout(resolve, ms)
      })
    }
    
    for (let i = 0; i < 100; i++) {
      punch(['L', 'R'][(Math.random() * 2) | 0])
      await sleep(delay)
    }
  })(punch, 150)
  
"cheating" player:

  (async function(oracle, punch, delay) {
    async function sleep(ms) {
      return new Promise((resolve, _) => {
        setTimeout(resolve, ms)
      })
    }
    
    for (let i = 0; i < 100; i++) {
      punch((i + 1) < oracle.minForPrediction ? ['L', 'R'][(Math.random() * 2) | 0] : oracle.predictNextPunch() === 'L' ? 'R' : 'L')
      
      await sleep(delay)
    }
  })(oracle, punch, 150)

is it possible to do any better? i haven't fully read frog/oracle code

Replies

emrtnntoday at 11:16 AM

Less fancy hack here, intercept the prediction function on every punch and force the frog to take the opposite side. const realPunch = punch;

punch = function(myMove) {

  oracle.predictNextPunch = function() {
    return myMove === 'L' ? 'R' : 'L'; 
  };
  
  realPunch(myMove);
};
escanortoday at 8:00 AM

it is:

  (async function(oracle, punch, delay) {
    async function sleep(ms) {
      return new Promise((resolve, _) => {
        setTimeout(resolve, ms)
      })
    }
    
    for (let i = 0; i < 100; i++) {
      const state = oracle._state
      const block = oracle.predictNextPunch()
      oracle._state = state
      
      punch(block === 'L' ? 'R' : 'L')
      
      await sleep(delay)
    }
  })(oracle, punch, 150)