zork1.js 707 B

123456789101112131415161718192021
  1. const VM = require('./lib/vm')
  2. const fs = require('fs')
  3. const data = fs.readFileSync('./games/ZORK1.DAT')
  4. const saveFile = './zork1.sav'
  5. const main = async () => {
  6. let state
  7. if (fs.existsSync(saveFile)) {
  8. state = JSON.parse(fs.readFileSync(saveFile, 'utf8'))
  9. } else {
  10. state = await VM.play(data)
  11. }
  12. while (state) {
  13. fs.writeFileSync(saveFile, JSON.stringify(state), 'utf8')
  14. if (typeof state.output === 'string') process.stdout.write(state.output)
  15. const text = (await new Promise(resolve => process.stdin.once('data', resolve))).toString()
  16. state = await VM.play(data, state.save, text.toString().trim())
  17. }
  18. }
  19. main().catch(console.error)