Advertisement
Guest User

Untitled

a guest
May 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const readline = require("readline-sync")
  2.  
  3.  
  4. let debugLog = (text, val) => console.log(text + ":" + val)
  5. let getUserInput = (entry) => readline.question(entry + " ")
  6.  
  7. let gameText = {
  8.     startText: "Hi, can you guess the word",
  9.     errorText: "Sorry, try again",
  10.     lifeText: "Here your lifes:",
  11.     winText: "Good You've won",
  12.     loseText: "Sorry you've lost",
  13.     inputText: "please choose character:"
  14. }
  15.  
  16. class GameConfig {
  17.     static getWordList() {
  18.         return ["wordFirst", "wordSecond", "wordThird"]
  19.     }
  20.  
  21.     static getLifes() {
  22.         return 10
  23.     }
  24. }
  25.  
  26. class RandomWordGenerator {
  27.     static makeRandomWord(wordList) {
  28.         let len = wordList.length;
  29.         let rndNum = Math.floor(Math.random() * len)
  30.         return wordList[rndNum]
  31.     }
  32. }
  33.  
  34. class Game {
  35.     constructor() {
  36.         this.lifes = GameConfig.getLifes()
  37.         this.secretWordObj = new SecretWord()
  38.     }
  39.  
  40.     init() {
  41.         console.log(gameText.startText)
  42.        
  43.         this.gameLoop()
  44.     }
  45.  
  46.     gameLoop() {
  47.         while(this.isPlaying()) {
  48.             let secretWord = this.secretWordObj.getSecretWord()
  49.  
  50.             console.log(gameText.lifeText + this.lifes)
  51.             console.log(secretWord)
  52.            
  53.             let userInput = getUserInput(gameText.inputText)
  54.             let guessResult = this.secretWordObj.guessChar(userInput)
  55.  
  56.             console.clear()
  57.  
  58.             if(guessResult) {
  59.                 this.secretWordObj.insertChar(guessResult)
  60.             } else {
  61.                 this.lifes--
  62.                 console.log(gameText.errorText)
  63.             }
  64.            
  65.         }
  66.     }
  67.  
  68.     isPlaying() {
  69.         if(this.checkWinCondition()) {
  70.             console.log(gameText.winText)
  71.             return false
  72.         }
  73.  
  74.         if(this.checkLoseCondition()) {
  75.             console.log(gameText.loseText)
  76.             return false
  77.         }
  78.  
  79.         return true
  80.     }
  81.  
  82.     checkWinCondition() {
  83.         let length = this.secretWordObj.getSecretWord().length
  84.         let guessedChars = this.secretWordObj.getGuessedChars()
  85.        
  86.         if(length == guessedChars) {
  87.             return true
  88.         }
  89.         return false
  90.     }
  91.  
  92.     checkLoseCondition() {
  93.         if(!this.lifes) {
  94.             return true
  95.         }
  96.         return false
  97.     }
  98. }
  99.  
  100. class SecretWord {
  101.     constructor() {
  102.         this.word = []
  103.         this.secretWord = []
  104.         this.guessedChars = 0
  105.         let randomWord = RandomWordGenerator.makeRandomWord(GameConfig.getWordList())
  106.  
  107.         this._setWord(randomWord)
  108.         this._setSecretWord()
  109.     }
  110.  
  111.     _setWord(word) {
  112.         this.word = word.split("")
  113.     }
  114.  
  115.     _setSecretWord() {
  116.         for(let char in this.word) {
  117.             this.secretWord.push("*")
  118.         }
  119.     }
  120.  
  121.     getSecretWord() {
  122.         return this.secretWord.join("")
  123.     }
  124.  
  125.     getGuessedChars() {
  126.         return this.guessedChars
  127.     }
  128.  
  129.     guessChar(char) {
  130.         for(let item in this.word) {
  131.             if(this.word[item] == char) {
  132.                 let guessedChar = {
  133.                     char: this.word[item],
  134.                     index: item
  135.                 }
  136.                 delete this.word[item]
  137.                 return guessedChar
  138.             }
  139.         }
  140.         return false
  141.     }
  142.  
  143.     insertChar(charObj) {
  144.         this.secretWord[charObj.index] = charObj.char
  145.         this.guessedChars++
  146.     }
  147. }
  148.  
  149. new Game().init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement