Classic games rewritten in simple functional style.
Support material for the screencast series Game Development with Functional Programming in JavaScript.
Functional JavasScript implementation of Snake for the web and terminal.
Classic games rewritten in simple functional style.
Support material for the screencast series Game Development with Functional Programming in JavaScript.
When running node cli I'm getting a syntax error on line 21 with the trailing comma. Removing the comma fixes the syntax error and the program runs normally.
I just watched your first episode on this and liked it.
Then I liked to take a closer look at the code.
https://github.com/chrokh/snake/blob/8aee671823904b0b948f90051b381af7efaa0450/base.js#L10
When I see this I wonder if I missed a reason why not to use literal object construction:
const objOf = k => v => ({ [k]: v })
Problems:
When moving right, and press down and left rapidly (within the same draw), the left will be rejected because it fails the check against the first item in the movement array. Expected behavior should be that rapid key press will perform both moves.
Valid move does not check if the move is the same as the current direction, so rapid presses of key fills up the array with junk. Expected behavior is that pressing the key in the direction of travel is completely ignored.
Proposed valid move function:
const validMove = move => state =>{
let indexLast = state.moves.length - 1
let OppositeDirection = state.moves[indexLast].x == -move.x || state.moves[indexLast].y == -move.y
let SameDirection = state.moves[indexLast].x == move.x && state.moves[indexLast].y == move.y
return (!SameDirection && !OppositeDirection)
}
The above should be placed into snake.js replacing the existing const validMove. It will make the game feel much more responsive.
PS indexLast can be turned into a method and placed in base.js for modularity
If I’m on a cell A going to cell B. While still on A I press UP the snake doesn’t go UP form A it goes to B first then UP.
This is happening because previous nextSnake
doesn't receive update moves
.
To fix:
const next = state => {
return {
rows: state.rows,
cols: state.cols,
moves: nextMoves(state),
- snake: nextSnake(state),
+ snake: nextSnake({...state, moves: nextMoves(state)}),
apple: nextApple(state)
}
}
This is improve reaction to key presses and make game feels less sluggish.
I'm trying to study your code, it is an ispiration for me.
I have a question, which is the difference between these three function?
const rnd = min => max => Math.floor(Math.random() * max) + min;
const rnd2 = (min, max) => { Math.floor(Math.random() * max) + min; }
function rnd3 (min, max) { return Math.floor(Math.random() * max) + min;}
Why you use the first one?
I try all three and they seem to have the same behavior.
I have looking for some explination but I don't understand.
I'm very nubie :(
The apple position can stand on a snake position.
const nextApple = state => willEat(state) ? validApplePos(state)(rndPos(state)) : state.apple
const validApplePos = (state) => (randPos) => state.snake.find( pointEq(randPos) ) === undefined ? randPos : validApplePos(state)(rndPos(state))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.