Created
June 23, 2021 04:50
-
-
Save blacksheep557/4f1963a5685b940164d3de7df19e1c9b 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(); | |
| console.log(this.code) | |
| }, | |
| getinput: () => { | |
| this.guessed = rlSync.question("Please guess a 4 digit numeric code :") | |
| this.guessed = this.guessed.split('').map(num => Number(num)); | |
| }, | |
| isCorrect: () => { | |
| console.log(this.guessed) | |
| console.log(this.code) | |
| return (JSON.stringify(this.guessed) === JSON.stringify(this.code)) | |
| } | |
| } | |
| 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