Created
June 23, 2021 04:19
-
-
Save blacksheep557/44e5791bd868579a437865bac5a3633f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const readline = require("readline"); | |
| let rlSync = require('readline-sync'); | |
| function calcScore(actual, guessed) { | |
| const result = { correctPos: 0, incorrectPos: 0 }; | |
| for (let i = 0; i < actual.length; i++) { | |
| if (actual[i] === guessed[i]) { | |
| actual.splice(i, 1); | |
| guessed.splice(i, 1); | |
| result.correctPos += 1; | |
| i--; | |
| } | |
| } | |
| for (let val of guessed) { | |
| if (actual.includes(val)) { | |
| actual.splice(actual.indexOf(val), 1) | |
| result.incorrectPos += 1 | |
| } | |
| } | |
| console.log(result); | |
| return result; | |
| } | |
| const getCode = () => new Array(4).fill(0).map(val => Math.floor(Math.random() * 6)) | |
| const gameObject = { | |
| code: null, | |
| rl: null, | |
| guessed: [], | |
| init: () => { | |
| this.code = getCode(); | |
| this.rl = readline.createInterface({ | |
| input: process.stdin, | |
| output: process.stdout | |
| }); | |
| }, | |
| getinput: () => { | |
| this.userGuess = rlSync.question("Please guess a 4 digit numeric code :"); | |
| }, | |
| isCorrect: () => this.code === this.guessed | |
| } | |
| function startGame() { | |
| console.log('MASTERMIND'); | |
| const game = gameObject | |
| game.init(); | |
| while (!game.isCorrect()) { | |
| game.getinput(); | |
| } | |
| } | |
| startGame() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment