From bb74b9dfce1d0a1ac0c3b030f43402337d52b8c1 Mon Sep 17 00:00:00 2001 From: Ben Burlingham Date: Sun, 27 Mar 2016 19:24:52 -1000 Subject: [PATCH] Evolved value generator. Started game state machine. --- js/Cell.js | 6 ++---- js/Character.js | 2 +- js/Game.js | 32 ++++++++++++++++++++++++++++++++ js/Grid.js | 29 +++++++++++++++++++++++------ js/Input.js | 8 ++++---- js/Values.js | 17 +++++++++-------- package.json | 4 ++++ webpack.config.js | 4 ++-- 8 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 js/Game.js diff --git a/js/Cell.js b/js/Cell.js index 38526fc..b19f3f7 100644 --- a/js/Cell.js +++ b/js/Cell.js @@ -2,11 +2,9 @@ var React = require('react'); var Cell = React.createClass({ render: function() { - var classname = 'cell'; - classname += ' x' + this.props.x; - classname += ' y' + this.props.y; + var classname = ['cell', 'x' + this.props.x, 'y' + this.props.y]; - return (
{this.props.value}
); + return (
{this.props.value}
); } }); diff --git a/js/Character.js b/js/Character.js index a7eea71..62301bc 100644 --- a/js/Character.js +++ b/js/Character.js @@ -26,7 +26,7 @@ var character = React.createClass({ return (
-
our
hero
+
); } diff --git a/js/Game.js b/js/Game.js new file mode 100644 index 0000000..20086da --- /dev/null +++ b/js/Game.js @@ -0,0 +1,32 @@ +var Values = require('./Values'); + +/** + * + */ +module.exports = { + level: 0, + + checkWin: function(values) { + var len = values.length; + var remaining = 0; + + for (var i = 0; i < len; i++) { + if (Values.validate(values[i], module.exports.level) === true) { + remaining++; + } + } + + return (remaining === 0); + }, + + checkLoss: function() { + + }, + + // State machine mediator. + state: { + nextLevel: function() { + module.exports.level++; + } + } +}; diff --git a/js/Grid.js b/js/Grid.js index 0210057..7f3f066 100644 --- a/js/Grid.js +++ b/js/Grid.js @@ -3,11 +3,16 @@ var Cell = require('./Cell'); var Character = require('./Character'); var UserInput = require('./Input'); var Values = require('./Values'); +var Game = require('./Game'); module.exports = React.createClass({ + generateValues() { + return Values.generate(this.props.width * this.props.height, Game.level); + }, + getInitialState() { return { - values: Values.generate(this.props.width, this.props.height) + values: this.generateValues() }; }, @@ -16,20 +21,32 @@ module.exports = React.createClass({ }, munch(x, y) { - if (this.state.values[x + '-' + y][1] === true) { - console.log('yum'); + var i = y * this.props.width + x; + + if (this.state.values[i] === "") { + return; } - else { - console.log('blech'); + + if (Values.validate(this.state.values[i], Game.level)) { + this.state.values[i] = ""; + this.setState({ values: this.state.values }); + } + + if (Game.checkWin(this.state.values) === true) { + Game.state.nextLevel(); + this.setState({ values: this.generateValues() }); } + //Game.checkLoss(); }, render() { var cells = []; + var i; for (var x = 0; x < this.props.width; x++) { for (var y = 0; y < this.props.height; y++) { - cells.push(); + i = y * this.props.width + x; + cells.push(); } } diff --git a/js/Input.js b/js/Input.js index 60dddca..6dbb58a 100644 --- a/js/Input.js +++ b/js/Input.js @@ -22,12 +22,12 @@ module.exports = { } break; case 39: // Right arrow - x < 2 ? - this.refs.ourhero1.setState({ x: x + 1 }) : - this.refs.ourhero1.setState({ x: 0 }) + if (x < this.props.width - 1) { + this.refs.ourhero1.setState({ x: x + 1 }); + } break; case 40: // Down arrow - if (y < 2) { + if (y < this.props.height - 1) { this.refs.ourhero1.setState({ y: y + 1 }); } } diff --git a/js/Values.js b/js/Values.js index 7ca87d7..985afe3 100644 --- a/js/Values.js +++ b/js/Values.js @@ -1,15 +1,16 @@ module.exports = { - generate: function(w, h) { - var values = {}; - var rand; + // Anagrams, multiples, equality + generate: function(n) { + var values = []; - for (var i = 0; i < w; i++) { - for (var j = 0; j < h; j++) { - rand = Math.ceil(Math.random() * 10); - values[i + '-' + j] = [rand, (rand % 2 === 0)]; - } + for (var i = 0; i < n; i++) { + values.push(Math.ceil(Math.random() * 1000)); } return values; + }, + + validate: function(value, level) { + return ((value || -1) % (level + 2) === 0); } }; diff --git a/package.json b/package.json index 75df529..e622872 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,15 @@ "babel-preset-react": "^6.5.0" }, "devDependencies": { + "css-loader": "^0.23.1", "express": "^4.13.4", "jsx-loader": "^0.13.2", + "node-sass": "^3.4.2", "react": "^0.14.7", "react-dom": "^0.14.7", "react-hot-loader": "^1.3.0", + "sass-loader": "^3.2.0", + "style-loader": "^0.13.1", "webpack": "^1.12.14", "webpack-dev-server": "^1.14.1" }, diff --git a/webpack.config.js b/webpack.config.js index e32eff9..c58556c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -6,7 +6,7 @@ function getEntrySources() { if (process.env.NODE_ENV !== 'production') { sources.push('webpack-dev-server/client?http://localhost:8080'); - sources.push('webpack/hot/only-dev-server'); + //sources.push('webpack/hot/only-dev-server'); } return sources; @@ -20,7 +20,7 @@ module.exports = { { test: /\.js$/, include: __dirname + '/js', - loaders: ['react-hot', 'babel'] + loaders: ['babel'] } ] },