wordle-cli
Advanced tools
Comparing version 1.2.2 to 1.2.3
const getOptions = () => { | ||
const options = {} | ||
const options = {}; | ||
// Convert arguments to options | ||
process.argv.slice(2).map((flag) => { | ||
const flagSplit = flag.split('=') | ||
process.argv.slice(2).forEach((option) => { | ||
const flagSplit = option.split('='); | ||
if (/^--/.test(flagSplit[0])) { | ||
options[flagSplit[0].substring(2)] = flagSplit[1] || true | ||
options[flagSplit[0].substring(2)] = flagSplit[1] || true; | ||
} else if (/^-/.test(flagSplit[0])) { | ||
@@ -17,37 +17,39 @@ flagSplit[0] | ||
case 's': | ||
options['spoiler'] = true | ||
break | ||
options.spoiler = true; | ||
break; | ||
case 'h': | ||
options['hard'] = true | ||
break | ||
options.hard = true; | ||
break; | ||
case 'r': | ||
options['random'] = true | ||
break | ||
options.random = true; | ||
break; | ||
case 'u': | ||
options['unlimited'] = true | ||
break | ||
options.unlimited = true; | ||
break; | ||
case 'g': | ||
options['guesses'] = flagSplit[1] | ||
break | ||
[options.guesses] = flagSplit; | ||
break; | ||
case 'd': | ||
options['date'] = flagSplit[1] | ||
break | ||
[options.date] = flagSplit; | ||
break; | ||
case 'w': | ||
options['word'] = flagSplit[1] | ||
break | ||
[options.word] = flagSplit; | ||
break; | ||
default: | ||
break; | ||
} | ||
}) | ||
}); | ||
} | ||
}) | ||
}); | ||
// Reformat Date option | ||
if (options.date) options.date = new Date(options.date.split('-').reverse().join('-')) | ||
if (options.date) options.date = new Date(options.date.split('-').reverse().join('-')); | ||
// Check guesses option | ||
if (options.guesses == '0') options.unlimited = true | ||
if (!parseInt(options.guesses)) delete options.guesses | ||
if (options.guesses === '0') options.unlimited = true; | ||
if (!parseInt(options.guesses, 10)) delete options.guesses; | ||
return options | ||
} | ||
return options; | ||
}; | ||
module.exports = getOptions | ||
module.exports = getOptions; |
114
bin/index.js
#! /usr/bin/env node | ||
const { createInterface } = require('readline') | ||
const word = require('./wordFunctions') | ||
const getOptions = require('./getOptions') | ||
const messages = require('./messages') | ||
const { createInterface } = require('readline'); | ||
const word = require('./wordFunctions'); | ||
const getOptions = require('./getOptions'); | ||
const messages = require('./messages'); | ||
// Define variables | ||
let gameWon = false | ||
let history = [] | ||
const options = getOptions() | ||
const rounds = options.guesses || 6 | ||
const { wordOfTheDay, gameId } = word.get(options) | ||
const guessRegex = new RegExp(`^[a-z]{${wordOfTheDay.length}}$`, 'i') | ||
let gameWon = false; | ||
const history = []; | ||
const options = getOptions(); | ||
const rounds = options.guesses || 6; | ||
const { wordOfTheDay, gameId } = word.get(options); | ||
const guessRegex = new RegExp(`^[a-z]{${wordOfTheDay.length}}$`, 'i'); | ||
@@ -18,7 +18,7 @@ const wordle = createInterface({ | ||
output: process.stdout, | ||
}) | ||
}); | ||
// Start new round | ||
const round = () => { | ||
const result = new Array(wordOfTheDay.length).fill({}) | ||
const result = new Array(wordOfTheDay.length).fill({}); | ||
@@ -28,13 +28,15 @@ wordle.question('Guess a word: ', async (input) => { | ||
if (!guessRegex.test(input)) { | ||
console.log(`\nChoose a ${wordOfTheDay.length}-letter word!\n`) | ||
round() | ||
return | ||
} else if (!options.word && !word.validate(input.toLowerCase())) { | ||
console.log(`\n${input.toUpperCase()} is not a valid word!\n`) | ||
round() | ||
return | ||
console.log(`\nChoose a ${wordOfTheDay.length}-letter word!\n`); | ||
round(); | ||
return; | ||
} | ||
const userWord = input.toUpperCase().split('') | ||
if (!options.word && !word.validate(input.toLowerCase())) { | ||
console.log(`\n${input.toUpperCase()} is not a valid word!\n`); | ||
round(); | ||
return; | ||
} | ||
const userWord = input.toUpperCase().split(''); | ||
// Set correctly guessed letters to green | ||
@@ -47,5 +49,5 @@ userWord.forEach((letter, index) => { | ||
color: 'green', | ||
} | ||
}; | ||
} | ||
}) | ||
}); | ||
@@ -55,4 +57,4 @@ // Set wrongly placed letters to yellow | ||
if (!result[index].color && wordOfTheDay.includes(letter) && !options.hard) { | ||
const matchingResultLetters = result.filter((item) => item.letter === letter) | ||
const matchingWordOfTheDayLetters = wordOfTheDay.filter((item) => item === letter) | ||
const matchingResultLetters = result.filter((item) => item.letter === letter); | ||
const matchingWordOfTheDayLetters = wordOfTheDay.filter((item) => item === letter); | ||
@@ -64,6 +66,6 @@ if (matchingResultLetters.length < matchingWordOfTheDayLetters.length) { | ||
color: 'yellow', | ||
} | ||
}; | ||
} | ||
} | ||
}) | ||
}); | ||
@@ -77,23 +79,23 @@ // Set all other letters to none | ||
color: 'none', | ||
} | ||
}; | ||
} | ||
}) | ||
}); | ||
// If word is guessed, set gameWon to true | ||
if (result.map((letter) => letter.color).every((color) => color === 'green')) { | ||
gameWon = true | ||
gameWon = true; | ||
} | ||
await word.show(result) | ||
await word.show(result); | ||
history.push(result) | ||
history.push(result); | ||
// Start new round or end game | ||
if ((options.unlimited || history.length < rounds) && !gameWon) { | ||
round() | ||
round(); | ||
} else { | ||
wordle.close() | ||
wordle.close(); | ||
} | ||
}) | ||
} | ||
}); | ||
}; | ||
@@ -103,36 +105,32 @@ // Close game | ||
if (gameWon) { | ||
messages.youWon(gameId, { unlimited: options.unlimited, roundsPlayed: history.length, rounds }) | ||
messages.youWon(gameId, { unlimited: options.unlimited, roundsPlayed: history.length, rounds }); | ||
} else { | ||
console.log('Game over! The word was ' + wordOfTheDay.join('')) | ||
console.log(`Game over! The word was ${wordOfTheDay.join('')}`); | ||
} | ||
process.exit(0) | ||
}) | ||
process.exit(0); | ||
}); | ||
const start = async () => { | ||
if (options.help) { | ||
messages.title() | ||
messages.help() | ||
process.exit(0) | ||
messages.title(); | ||
messages.help(); | ||
process.exit(0); | ||
} else if (options.date && options.random) { | ||
console.log("You can't choose a date in combination with the random flag") | ||
process.exit(0) | ||
console.log("You can't choose a date in combination with the random flag"); | ||
process.exit(0); | ||
} else if (options.guesses && options.unlimited) { | ||
console.log("You can't choose an amount of guesses in combination with the unlimited flag") | ||
process.exit(0) | ||
console.log("You can't choose an amount of guesses in combination with the unlimited flag"); | ||
process.exit(0); | ||
} else if (options.word && options.random) { | ||
console.log("You can't choose a custom word in combination with the random flag") | ||
process.exit(0) | ||
console.log("You can't choose a custom word in combination with the random flag"); | ||
process.exit(0); | ||
} else if (options.spoiler) { | ||
await word.show( | ||
wordOfTheDay.map((letter) => { | ||
return { letter, color: 'green' } | ||
}) | ||
) | ||
process.exit(0) | ||
await word.show(wordOfTheDay.map((letter) => ({ letter, color: 'green' }))); | ||
process.exit(0); | ||
} else { | ||
messages.title() | ||
round() | ||
messages.title(); | ||
round(); | ||
} | ||
} | ||
}; | ||
start() | ||
start(); |
@@ -1,6 +0,6 @@ | ||
const { insertSpaces } = require('./utils') | ||
const { insertSpaces } = require('./utils'); | ||
module.exports = { | ||
title() { | ||
console.clear() | ||
console.clear(); | ||
console.log(` | ||
@@ -17,17 +17,20 @@ | ||
`) | ||
`); | ||
}, | ||
youWon(gameId, rounds) { | ||
let stats = [] | ||
let statsOutput = '' | ||
const stats = []; | ||
let statsOutput = ''; | ||
if (gameId) stats.push(`#${gameId}`) | ||
if (!rounds.unlimited) stats.push(`${rounds.roundsPlayed}/${rounds.rounds}`) | ||
if (gameId) stats.push(`#${gameId}`); | ||
if (!rounds.unlimited) stats.push(`${rounds.roundsPlayed}/${rounds.rounds}`); | ||
if (stats.length) statsOutput = `wordle ${stats.join(' ')}` | ||
if (stats.length) statsOutput = `wordle ${stats.join(' ')}`; | ||
const statsPadding = (23 - statsOutput.length) / 2 | ||
const statsPadding = (23 - statsOutput.length) / 2; | ||
statsOutput = insertSpaces(Math.ceil(statsPadding)) + statsOutput + insertSpaces(Math.floor(statsPadding)) | ||
const padStart = insertSpaces(Math.ceil(statsPadding)); | ||
const padEnd = insertSpaces(Math.floor(statsPadding)); | ||
statsOutput = padStart + statsOutput + padEnd; | ||
console.log(` | ||
@@ -55,3 +58,3 @@ _____________________________ | ||
`) | ||
`); | ||
}, | ||
@@ -92,4 +95,4 @@ help() { | ||
`) | ||
`); | ||
}, | ||
} | ||
}; |
module.exports = { | ||
insertSpaces(num) { | ||
return new Array(num + 1).join(' ') | ||
return new Array(num + 1).join(' '); | ||
}, | ||
} | ||
}; |
@@ -1,12 +0,12 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { insertSpaces } = require('./utils') | ||
const { insertSpaces } = require('./utils'); | ||
// GET WORD | ||
const formatWord = (word) => word.toUpperCase().split('') | ||
const formatWord = (word) => word.toUpperCase().split(''); | ||
let wordsBuffer = fs.readFileSync(path.join(__dirname, 'words.json')) | ||
let words = JSON.parse(wordsBuffer) | ||
const wordsBuffer = fs.readFileSync(path.join(__dirname, 'words.json')); | ||
const words = JSON.parse(wordsBuffer); | ||
@@ -19,3 +19,3 @@ const get = (options) => { | ||
gameId: false, | ||
} | ||
}; | ||
} | ||
@@ -25,3 +25,3 @@ | ||
if (options.random) { | ||
const gameId = Math.floor(Math.random() * words.length) | ||
const gameId = Math.floor(Math.random() * words.length); | ||
@@ -31,26 +31,24 @@ return { | ||
gameId, | ||
} | ||
}; | ||
} | ||
// Get word for specified date | ||
const date = options.date || Date.UTC(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()) | ||
const diff = new Date(date) - new Date(Date.UTC(2021, 5, 19)) | ||
const gameId = Math.floor(diff / 864e5) | ||
const today = Date.UTC(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()); | ||
const date = options.date || today; | ||
const diff = new Date(date) - new Date(Date.UTC(2021, 5, 19)); | ||
const gameId = Math.floor(diff / 864e5); | ||
return { | ||
wordOfTheDay: formatWord(words[gameId]), | ||
gameId, | ||
} | ||
} | ||
}; | ||
}; | ||
// VALIDATE WORD | ||
let validWordsBuffer = fs.readFileSync(path.join(__dirname, 'validWords.json')) | ||
let validWords = JSON.parse(validWordsBuffer) | ||
const validWordsBuffer = fs.readFileSync(path.join(__dirname, 'validWords.json')); | ||
const validWords = JSON.parse(validWordsBuffer); | ||
const validate = (word) => { | ||
if (words.includes(word) || validWords.includes(word)) { | ||
return true | ||
} | ||
} | ||
const validate = (word) => words.includes(word) || validWords.includes(word); | ||
@@ -64,32 +62,33 @@ // SHOW WORD | ||
reset: [0], | ||
} | ||
}; | ||
const format = (color = 'reset') => `\x1b[${formattingCodes[color].join(';')}m` | ||
const endTile = () => format() + ' ' | ||
const format = (color = 'reset') => `\x1b[${formattingCodes[color].join(';')}m`; | ||
const endTile = () => `${format()} `; | ||
const show = async (result) => { | ||
let topBottom = '' | ||
let middle = '' | ||
let breakpoints = [] | ||
let topBottom = ''; | ||
let middle = ''; | ||
const breakpoints = []; | ||
result.forEach(({ color, letter }) => { | ||
topBottom += format(color) + insertSpaces(7) + endTile() | ||
middle += format(color) + insertSpaces(3) + letter + insertSpaces(3) + endTile() | ||
breakpoints.push(middle.length) | ||
}) | ||
topBottom += format(color) + insertSpaces(7) + endTile(); | ||
middle += format(color) + insertSpaces(3) + letter + insertSpaces(3) + endTile(); | ||
breakpoints.push(middle.length); | ||
}); | ||
console.log('\n\n\n\n\n\n') | ||
console.log('\n\n\n\n\n\n'); | ||
for (let i = 0; i < result.length; i++) { | ||
process.stdout.moveCursor(0, -5) | ||
for (let i = 0; i < result.length; i += 1) { | ||
process.stdout.moveCursor(0, -5); | ||
process.stdout.write(topBottom.substring(0, breakpoints[i]) + '\n') | ||
process.stdout.write(middle.substring(0, breakpoints[i]) + '\n') | ||
process.stdout.write(topBottom.substring(0, breakpoints[i]) + '\n') | ||
process.stdout.write('\n\n') | ||
process.stdout.write(`${topBottom.substring(0, breakpoints[i])}\n`); | ||
process.stdout.write(`${middle.substring(0, breakpoints[i])}\n`); | ||
process.stdout.write(`${topBottom.substring(0, breakpoints[i])}\n`); | ||
process.stdout.write('\n\n'); | ||
// eslint-disable-next-line | ||
await new Promise((resolve) => setTimeout(resolve, 300)) | ||
} | ||
} | ||
}; | ||
module.exports = { show, get, validate } | ||
module.exports = { show, get, validate }; |
{ | ||
"name": "wordle-cli", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "CLI interpretation of the game Wordle", | ||
"main": "bin/index.js", | ||
"scripts": { | ||
"lint": "eslint ." | ||
}, | ||
"bin": { | ||
@@ -22,3 +25,7 @@ "wordle": "bin/index.js" | ||
"homepage": "https://github.com/robvanbakel/wordle-cli#readme", | ||
"license": "ISC" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"eslint": "^8.10.0", | ||
"eslint-config-airbnb-base": "^15.0.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155700
10
13269
2