Skip to content

Commit

Permalink
Fix seed in 'show seed'
Browse files Browse the repository at this point in the history
Added CI
  • Loading branch information
j-m committed Dec 16, 2020
1 parent 76ac859 commit 96fa76f
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 79 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/pre-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Pre-deploy

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/node_modules
/.pnp
.pnp.js
.eslintcache

# testing
/coverage
Expand Down
48 changes: 48 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Changelog

This project is following [Semantic Versioning](https://semver.org/)

## [1.4.1](https://github.com/j-m/word-guess/releases/tag/1.4.1) - Fix 'show seed' (December 16, 2020)

- Fix 'show seed' seed
- Added tests
- Added GitHub action

## [1.4.0](https://github.com/j-m/word-guess/releases/tag/1.4.0) - Start Menu Info (November 16, 2020)

- Show seed button
- Project version
- GitHub link

## [1.3.0](https://github.com/j-m/word-guess/releases/tag/1.3.0) - Continue Playing (November 16, 2020)

- Added continue playing button if game lost
- Added guess counter if playing in `overtime`
- Created Heroku deployment

## [1.2.0](https://github.com/j-m/word-guess/releases/tag/1.2.0) - Large Seed Fix (November 16, 2020)

- Swapped to `bigint` to allow for arbitrarily long seeds (any words/sentences longer than 10 characters)
- Better lose animation

## [1.1.0](https://github.com/j-m/word-guess/releases/tag/1.1.0) - Spaces Fix (November 15, 2020)

- Fixed seeds representing multi-word games
- Fixed lives
- Breaking change but only bumped minor because project was private

## [1.0.0](https://github.com/j-m/word-guess/releases/tag/1.0.0) - Seeding (November 15, 2020)

- Added seed calculation
- Allow seed input in start menu
- Detect if input is a numeric (seed), alpha + space (sentence) , or alphanumeric (invalid)
- Lives separated into its own component

## 0.1.0 - Proof of Concept

- Added start menu
- Added Sentence components
- Added Alphabet components
- Added lives
- Added celebration/losing animations
- Added replay
48 changes: 4 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
![word guess banner logo](./assets/banner.png)

## About
# ![word guess banner logo](./assets/banner.png) Word Guess

Hangman without the hanging man

Expand All @@ -23,45 +21,7 @@ Then simply run `npm i` and `npm start` and you're off!
- Leaderboards
- Compete mode

## Changelog

This project is following [Semantic Versioning](https://semver.org/)

### 1.4.0 - Start Menu Info (November 16, 2020)

- Show seed button
- Project version
- GitHub link

### 1.3.0 - Continue Playing (November 16, 2020)

- Added continue playing button if game lost
- Added guess counter if playing in `overtime`
- Created Heroku deployment

### 1.2.0 - Large Seed Fix (November 16, 2020)

- Swapped to `bigint` to allow for arbitrarily long seeds (any words/sentences longer than 10 characters)
- Better lose animation

### 1.1.0 - Spaces Fix (November 15, 2020)

- Fixed seeds representing multi-word games
- Fixed lives
- Breaking change but only bumped minor because project was private

### 1.0.0 - Seeding (November 15, 2020)

- Added seed calculation
- Allow seed input in start menu
- Detect if input is a numeric (seed), alpha + space (sentence) , or alphanumeric (invalid)
- Lives separated into its own component

### 0.1.0 - Proof of Concept
## Wiki

- Added start menu
- Added Sentence components
- Added Alphabet components
- Added lives
- Added celebration/losing animations
- Added replay
[Changelog](./CHANGELOG.md)
[Live deployment](https://j-m-word-guess.herokuapp.com/)
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "word-guess",
"version": "1.4.0",
"version": "1.4.1",
"private": true,
"main": "src/index.tsx",
"homepage": "./",
Expand Down
38 changes: 5 additions & 33 deletions src/components/Start/StartForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,7 @@ import { FaGithub } from 'react-icons/fa'

import SentenceInput from './SentenceInput'
import StartGameButton from './StartGameButton'

const BASE: bigint = 28n

function seed(input: string): bigint {
let seed: bigint = 0n
Object.values(input.split("")).forEach(letter => {
if (letter !== " ") {
seed += BigInt(letter.charCodeAt(0)) - 65n + 1n
}
seed *= BASE
})
return seed
}

function sentence(seed: bigint): string {
let sentence: string = ""
while (seed > BASE) {
seed /= BASE
const letter: number = Number(seed % BASE)
if (letter === 0) {
sentence += ' '
} else {
sentence += String.fromCharCode(letter + 65 - 1)
}
}
return sentence.split("").reverse().join("")
}
import { sentenceToSeed, seedToSentence } from '../../util/seed'

interface StartFormProps {
start: (seed: bigint, input: string) => void
Expand All @@ -57,18 +31,18 @@ export default class StartForm extends React.PureComponent<StartFormProps, Start
return
}
if (/^[A-Za-z\s]+$/.test(input)) {
this.props.start(seed(input), input)
this.props.start(sentenceToSeed(input), input)
return
}
if (/^\d+$/.test(input)) {
this.props.start(BigInt(input), sentence(BigInt(input)))
this.props.start(BigInt(input), seedToSentence(BigInt(input)))
return
}
this.setState({error: "Input should either be just letters and spaces or a numeric seed"})
}

showSeed = () => {
this.setState(state => ({...state, seed: seed(state.input)}))
this.setState(state => ({...state, seed: sentenceToSeed(state.input.toUpperCase())}))
}

onWordChange = (input: string) => {
Expand All @@ -91,9 +65,7 @@ export default class StartForm extends React.PureComponent<StartFormProps, Start
onClick={this.submit}
/>
</form>
{this.state.error
? <p className="error">{this.state.error}</p>
: undefined}
{this.state.error && <p className="error">{this.state.error}</p>}

<div style={{width: "100%", textAlign: 'center'}}>
<span>v{version}</span>
Expand Down
23 changes: 23 additions & 0 deletions src/util/seed.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {sentenceToSeed, seedToSentence} from './seed'
describe('Seed', () => {
test('with 1 word', () => {
const input = "TEST"
const seed = sentenceToSeed(input)
const output = seedToSentence(seed)
expect(output).toBe(input)
})

test('works with multiple words', () => {
const input = "THIS IS A TEST"
const seed = sentenceToSeed(input)
const output = seedToSentence(seed)
expect(output).toBe(input)
})

test('works with a long sentence', () => {
const input = "THIS IS A MUCH LONGER TEST THAN THE OTHERS BUT PROBABLY STILL IS NOT AS LONG AS A GOOD TEST SHOULD BE BUT HEY HO"
const seed = sentenceToSeed(input)
const output = seedToSentence(seed)
expect(output).toBe(input)
})
})
26 changes: 26 additions & 0 deletions src/util/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const BASE: bigint = 27n

export function sentenceToSeed(input: string): bigint {
let seed: bigint = 0n
Object.values(input.split("")).forEach(letter => {
if (letter !== " ") {
seed += BigInt(letter.charCodeAt(0)) - 65n + 1n
}
seed *= BASE
})
return seed
}

export function seedToSentence(seed: bigint): string {
let sentence: string = ""
while (seed > BASE) {
seed /= BASE
const letter: bigint = seed % BASE
if (letter === 0n) {
sentence += ' '
} else {
sentence += String.fromCharCode(Number(letter + 65n - 1n))
}
}
return sentence.split("").reverse().join("")
}

0 comments on commit 96fa76f

Please sign in to comment.