diff --git a/actions/board/grid.actions.js b/actions/board/grid.actions.js
index 59b11fe..5bba6c2 100644
--- a/actions/board/grid.actions.js
+++ b/actions/board/grid.actions.js
@@ -10,9 +10,10 @@ export const generateValues = (count, level) => ({
level: level
});
-export const updateValues = (i, level) => ({
+export const updateValues = (x, y, level) => ({
type: GRID_ACTION,
action: UPDATE,
- index: i,
+ x: x,
+ y: y,
level: level
});
diff --git a/actions/board/muncher.actions.js b/actions/board/muncher.actions.js
index b60bc3a..575b23b 100644
--- a/actions/board/muncher.actions.js
+++ b/actions/board/muncher.actions.js
@@ -26,7 +26,7 @@ export const moveDown = () => ({
action: DOWN
});
-export const munch = () => ({
+export const munch = (x, y) => ({
type: MUNCHER_ACTION,
action: MUNCH
});
diff --git a/actions/board/scorebar.actions.js b/actions/board/scorebar.actions.js
index 9d08391..77fb2cf 100644
--- a/actions/board/scorebar.actions.js
+++ b/actions/board/scorebar.actions.js
@@ -1,8 +1,9 @@
// Scorebar component actions and action creators.
export const SCOREBAR_ACTION = 'SCOREBAR_ACTION';
-export const UPDATE = 'UPDATE';
+export const MUNCH = 'MUNCH';
-export const update = () => ({
+export const munch = (success) => ({
type: SCOREBAR_ACTION,
- action: UPDATE
+ action: MUNCH,
+ success: success
});
diff --git a/components/board/board.component.js b/components/board/board.component.js
index 95319a7..20d549b 100644
--- a/components/board/board.component.js
+++ b/components/board/board.component.js
@@ -1,8 +1,10 @@
require('../../sass/board/board.scss');
import { Component } from 'react';
+import { connect } from 'react-redux';
+import { getState } from 'react-redux';
import Scorebar from './scorebar.component';
-// import Titlebar from './Titlebar';
+import Titlebar from './titlebar.component';
import Grid from './grid.component';
// import Message from './Message';
import Muncher from './muncher.component';
@@ -11,21 +13,29 @@ import * as GridActions from '../../actions/board/grid.actions';
import * as MuncherActions from '../../actions/board/muncher.actions';
import * as ScorebarActions from '../../actions/board/scorebar.actions';
+const countEmptyStrings = (acc, curr) => { return acc + (curr === ''); };
+
export default class Board extends Component {
- // A "munch" event affects several children. It needs a dispatch reference.
- munch() {
- this.props.dispatch(GridActions.updateValues(0, 0));
+ munch(x, y) {
+ // Before/after grid validation reveals successful munch.
+ const pre = this.props.grid.reduce(countEmptyStrings, 0);
+ this.props.dispatch(GridActions.updateValues(x, y, 0));
+ const post = this.props.grid.reduce(countEmptyStrings, 0);
+
+ this.props.dispatch(ScorebarActions.munch(pre !== post));
this.props.dispatch(MuncherActions.munch());
- this.props.dispatch(ScorebarActions.update());
};
render() {
- //
//
return (
+
-
+
);
};
};
+
+// Connect state for inspection to determine dispatching flow.
+export default connect((s) => s)(Board);
diff --git a/components/board/muncher.component.js b/components/board/muncher.component.js
index 92f8029..f015f79 100644
--- a/components/board/muncher.component.js
+++ b/components/board/muncher.component.js
@@ -3,7 +3,6 @@ require('../../sass/board/muncher.scss');
import { Component } from 'react';
import { connect } from 'react-redux';
import * as Actions from '../../actions/board/muncher.actions';
-import { SETTINGS } from '../../App';
let listener = null;
@@ -14,31 +13,22 @@ export class Muncher extends Component {
switch (e.keyCode) {
case 32:
- this.props.munch();
+ this.props.munch(x, y);
break;
case 37:
- if (this.props.x === 0) {
- return;
- }
this.props.dispatch(Actions.moveLeft());
break;
+
case 38:
- if (this.props.y === 0) {
- return;
- }
this.props.dispatch(Actions.moveUp());
break;
+
case 39:
- if (this.props.x === SETTINGS.GRID_WIDTH - 1) {
- return;
- }
this.props.dispatch(Actions.moveRight());
break;
+
case 40:
- if (this.props.y === SETTINGS.GRID_HEIGHT - 1) {
- return;
- }
this.props.dispatch(Actions.moveDown());
break;
}
diff --git a/components/board/scorebar.component.js b/components/board/scorebar.component.js
index e27e7a3..6b3efe7 100644
--- a/components/board/scorebar.component.js
+++ b/components/board/scorebar.component.js
@@ -3,7 +3,7 @@ require('../../sass/board/scorebar.scss');
import { Component } from 'react';
import { connect } from 'react-redux';
-export default class Scorebar extends Component {
+export class Scorebar extends Component {
render() {
var lives = [];
for (var i = 0; i < this.props.lives; i++) {
diff --git a/index.js b/index.js
index 4332794..dc4b039 100644
--- a/index.js
+++ b/index.js
@@ -11,7 +11,7 @@ import gridReducer from './reducers/board/grid.reducer';
import muncherReducer from './reducers/board/muncher.reducer';
import scorebarReducer from './reducers/board/scorebar.reducer';
-const combinedReducers = combineReducers({
+const reducers = combineReducers({
mode: modeReducer,
newgame: newgameReducer,
muncher: muncherReducer,
@@ -19,7 +19,7 @@ const combinedReducers = combineReducers({
grid: gridReducer
});
-const store = createStore(combinedReducers);
+const store = createStore(reducers);
ReactDOM.render(
diff --git a/reducers/board/grid.reducer.js b/reducers/board/grid.reducer.js
index cbb954b..3c02d24 100644
--- a/reducers/board/grid.reducer.js
+++ b/reducers/board/grid.reducer.js
@@ -1,5 +1,7 @@
-require('immutable');
+const Immutable = require('immutable');
+
import * as GridActions from '../../actions/board/grid.actions';
+import { SETTINGS } from '../../App';
import Values from '../Values';
const reducer = (state = [], action) => {
@@ -10,13 +12,16 @@ const reducer = (state = [], action) => {
switch (action.action) {
case GridActions.GENERATE:
return Values.generate(action.count, action.level);
+
case GridActions.UPDATE:
- // const valid = Values.validate(state[action.index], action.level);
- const results = state.slice(0);
- // if (valid) {
- results[action.index] = "";
- // }
- return results;
+ const results = Immutable.List(state);
+ const index = action.y * SETTINGS.GRID_HEIGHT + action.x;
+ const valid = Values.validate(state[index], action.level);
+
+ if (valid) {
+ return results.set(index, '').toArray();
+ }
+ break;
}
return state;
diff --git a/reducers/board/muncher.reducer.js b/reducers/board/muncher.reducer.js
index d27d567..c9b56e4 100644
--- a/reducers/board/muncher.reducer.js
+++ b/reducers/board/muncher.reducer.js
@@ -1,5 +1,5 @@
-require('immutable');
import * as MuncherActions from '../../actions/board/muncher.actions';
+import { SETTINGS } from '../../App';
const reducer = (state = { x: 0, y: 0 }, action) => {
if (action.type !== MuncherActions.MUNCHER_ACTION) {
@@ -8,13 +8,29 @@ const reducer = (state = { x: 0, y: 0 }, action) => {
switch (action.action) {
case MuncherActions.LEFT:
+ if (state.x === 0) {
+ return state;
+ }
return { x: state.x - 1, y: state.y };
+
case MuncherActions.RIGHT:
+ if (state.x === SETTINGS.GRID_WIDTH - 1) {
+ return state;
+ }
return { x: state.x + 1, y: state.y };
+
case MuncherActions.UP:
+ if (state.y === 0) {
+ return state;
+ }
return { x: state.x, y: state.y - 1 };
+
case MuncherActions.DOWN:
+ if (state.y === SETTINGS.GRID_HEIGHT - 1) {
+ return state;
+ }
return { x: state.x, y: state.y + 1 };
+
case MuncherActions.MUNCH:
console.log("Muncher's mouth moved!");
return state;
diff --git a/reducers/board/scorebar.reducer.js b/reducers/board/scorebar.reducer.js
index 60be97e..f1ad6c6 100644
--- a/reducers/board/scorebar.reducer.js
+++ b/reducers/board/scorebar.reducer.js
@@ -1,16 +1,24 @@
-require('immutable');
+const Immutable = require('immutable');
+
import * as ScorebarActions from '../../actions/board/scorebar.actions';
import Values from '../Values';
import { SETTINGS } from '../../App';
-const reducer = (state = { current: 100, high: 999, lives: SETTINGS.LIVES }, action) => {
+const reducer = (state = { current: 0, high: 999, lives: SETTINGS.LIVES }, action) => {
if (action.type !== ScorebarActions.SCOREBAR_ACTION) {
return state;
}
+ const result = Immutable.Map(state);
+
switch (action.action) {
- case ScorebarActions.UPDATE:
- return { current: state.current + 10, high: 999 };
+ case ScorebarActions.MUNCH:
+ if (action.success) {
+ return result.set('current', state.current + 10).toObject();
+ }
+ else {
+ return result.set('lives', state.lives - 1).toObject();
+ }
}
return state;
diff --git a/reducers/mode.reducer.js b/reducers/mode.reducer.js
index c58e09c..e502908 100644
--- a/reducers/mode.reducer.js
+++ b/reducers/mode.reducer.js
@@ -1,7 +1,6 @@
-require('immutable');
import * as ModeActions from '../actions/mode.actions';
-const reducer = (state = ModeActions.WELCOME, action) => {
+const reducer = (state = ModeActions.BOARD, action) => {
if (action.type !== ModeActions.MODE_ACTION) {
return state;
}
diff --git a/reducers/welcome/new-game.reducer.js b/reducers/welcome/new-game.reducer.js
index 894e38d..85c0869 100644
--- a/reducers/welcome/new-game.reducer.js
+++ b/reducers/welcome/new-game.reducer.js
@@ -1,4 +1,3 @@
-require('immutable');
import * as NewGameActions from '../../actions/welcome/new-game.actions';
const reducer = (state = { hidden: false }, action) => {