Skip to content

Commit

Permalink
Merge pull request #78 from salif/refactor
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
salif authored Oct 10, 2023
2 parents 7d4ab9a + 7af12eb commit 3c42a92
Show file tree
Hide file tree
Showing 87 changed files with 11,187 additions and 9,247 deletions.
28 changes: 28 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# EditorConfig is awesome: https://EditorConfig.org
root = true

[*]
end_of_line = lf
charset = utf-8

[*.json]
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = false

[*.js]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true

[*.sh]
indent_style = tab
indent_size = 4
insert_final_newline = true

[*.html]
indent_style = tab
indent_size = 4
insert_final_newline = true
42 changes: 42 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Salif Mehmed
Seniru Pasan Indira
Toni500github
dnhuan
Natsumi H
Dany Shaanan
Kharis Isriyanto
Luis Martinez
PedroGuilherme
Alex
Aresiel
Kevin Unger
Mario Aleixo
Paul Clark
Pietro
Smollet777
AbhayKaushik
AllAwesome497
Berk Gureken
Braulio Gutiérrez
Cody Metz
Dmytro Bubela
Ece Sefercioğlu
Emanuele Lorenzetti
EverMarr
Jackeline Nascimento
José Victor
Justin Riley
Lee Dennis
Mantas
Mathieu LAURET
Moxieq
PapuleX
Ramon Bezerra
Rodrigo da Silva Ferreira
Sebastian Kießling
Stefanos Z
Toni500git
Valery Briz
Vasilhs Toskas
exiam
rafacazal
140 changes: 140 additions & 0 deletions Hangman/Hangman.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
class Colors {
constructor() {
this.red = 91
this.green = 92
this.blue = 34
this.cian = 96
this.yellow = 93
}
}

class Game {
constructor(word, colors, availChars) {
this.word = word
this.colors = colors
this.availChars = availChars
this.lives = 8
this.toGuess = 0
this.playedChars = new Set()
}
}

class Hangman {
constructor(args) {
this.db = args.locale
this.words = args.words.words
this.pics = args.pics
this.colors = args.colors
this.input = args.input
this.output = args.output
}

play() {
const availChars = this.getUniqueChars(this.words)
const randWord = () => this.words[Math.floor(Math.random() * this.words.length)]
this.game = new Game(randWord().split(""), this.colors, availChars)

this.output.write(this.clr(`\n ${this.db.hangman}\n`, this.game.colors.green))

this.game.playedChars.add(this.game.word[0])
this.game.playedChars.add(this.game.word[this.game.word.length - 1])

this.showPrompt(this.obf(this.game.word, this.game.playedChars), this.pic())
this.input.on('data', (data) => {
this.guess(data.trim().toLowerCase())
})
}

rePlay() {
this.input.on('data', (data) => {
this.output.write(this.clr("\n ++++++++++++++++++++++++++++++\n", this.game.colors.blue))
this.play()
})
}

showPrompt(obfWord, msg) {
this.output.write(this.clr("\n ++++++++++++++++++++++++++++++\n", this.game.colors.blue))
if (msg != null) {
this.output.write(msg + "\n")
}
this.output.write(this.clr(`\n ${this.db.letters} ${Array.from(this.game.playedChars).join(", ")}\n`, this.game.colors.green))
this.output.write(this.clr(`\n ${obfWord}\n`, this.colors.cian))

this.output.write(this.clr(`\n ${this.db.guess} `, this.colors.yellow))
}

getUniqueChars(arr) {
const uniqueChars = new Set()
for (const str of arr) {
for (const char of str) {
uniqueChars.add(char)
}
}
return uniqueChars
}

clr(text, color) {
return color == null ? text : "\x1b[" + color + "m" + text + "\x1b[0m"
}

obf(word, playedChars) {
const output = []
let toGuess = 0
for (const c of word) {
if (playedChars.has(c)) {
output.push(c)
} else if (c === " ") {
output.push(c)
} else {
toGuess += 1
output.push("_")
}
}
this.game.toGuess = toGuess
return output.join(" ")
}

pic() {
return "\n" + this.pics[this.game.lives].join('\n')
}

guess(c) {
if (this.game.playedChars.has(c)) {
this.showPrompt(this.obf(this.game.word, this.game.playedChars),
this.clr(`\n ${this.db.played}`, this.game.colors.red))
} else if (this.game.availChars.has(c)) {
this.game.playedChars.add(c)
if (this.game.word.includes(c)) {
const obfWord = this.obf(this.game.word, this.game.playedChars)
if (this.game.toGuess < 1) {
this.output.write(this.clr("\n ++++++++++++++++++++++++++++++\n", this.game.colors.blue))
this.output.write(this.clr(`\n ${this.game.word.join('')}\n`, this.colors.cian))
this.output.write(this.clr(`\n ${this.db.you_won}\n\n`, this.game.colors.green))
// process.exit(0)
this.rePlay()
} else {
this.showPrompt(obfWord, null)
}
} else {
this.game.lives -= 1
if (this.game.lives < 1) {
this.output.write(this.clr("\n ++++++++++++++++++++++++++++++\n", this.game.colors.blue))
this.output.write(this.clr(`\n ${this.game.word.join('')}\n`, this.colors.cian))
this.output.write(this.clr(`\n ${this.db.you_lose}\n\n`, this.game.colors.red))
// process.exit(0)
this.rePlay()
} else {
this.showPrompt(this.obf(this.game.word, this.game.playedChars), this.pic())
}
}
} else {
this.showPrompt(this.obf(this.game.word, this.game.playedChars),
this.clr(`\n ${this.db.one_char} `, this.game.colors.red) +
this.clr(`\n [${Array.from(this.game.availChars).sort().join(', ')}]`, this.colors.green))
}
}
}

if (typeof module !== 'undefined') {
module.exports = { Hangman: Hangman, Colors: Colors }
}
50 changes: 26 additions & 24 deletions Hangman/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,42 @@

## Play

```sh
$ python hangman.py
```

or
### Node.js

```sh
$ python hangman.py [language]
```
```bash
node index.js

#### Example
# specify language:
node index.js [language]
# e.g.
node index.js pt-br

```sh
$ python hangman.py pt-br
# specify words:
node index.js en code
```

## Node.js
### Python

```sh
$ node hangman.js
```bash
python hangman.py
# or specify language:
python hangman.py [language]
# e.g.
python hangman.py pt-br
```

or

```sh
$ node hangman.js [language]
```
## Contributing

#### Example
### Translate

```sh
$ node hangman.js pt-br
```bash
cp locales/en/locale.json locales/[language]/locale.json
# Translate locales/[language]/locale.json
```

## Contributing
### Add a word list

See [this issue](https://github.com/salifm/cli-games/issues/9)
```bash
echo '{"words": ["word 1", "word 2"]}' > locales/[language]/words/[name].json
# Edit locales/[language]/words/[name].json
```
Loading

0 comments on commit 3c42a92

Please sign in to comment.