}
function MiniGrid(props) {
+
+ const mini_grid = props.mini_grid;
+ const squares = mini_grid.squares;
+
function grid_square(j) {
- const value = props.squares[j];
+ const value = squares[j];
const last_move = props.last_moves.includes(j);
/* Even if the grid is active, the square is only active if
/* Even if my parent thinks I'm active because of the last move, I
* might not _really_ be active if I'm full. */
let occupied = 0;
- props.squares.forEach(element => {
+ mini_grid.squares.forEach(element => {
if (element.symbol)
occupied++;
});
if (props.active && occupied < 9)
class_name += " active";
+ let winner = null;
+ if (mini_grid.winner) {
+ winner = <div className="winner">{mini_grid.winner}</div>;
+ }
+
return (
<div className={class_name}>
{grid_square(0)}
{grid_square(6)}
{grid_square(7)}
{grid_square(8)}
+ {winner}
</div>
);
}
const last_moves = this.props.last_two_moves.filter(move => move[0] === i)
.map(move => move[1]);
- const squares = this.props.mini_grids[i].squares;
+ const mini_grid = this.props.mini_grids[i];
return (
<MiniGrid
- squares={squares}
+ mini_grid={mini_grid}
active={grid_active}
last_moves={last_moves}
onClick={(j) => this.props.onClick(i,j)}
new_obj.squares = obj.squares.slice();
return new_obj;
});
- new_mini_grids[mini_grid_index].squares[position] = {
+ const new_mini_grid = new_mini_grids[mini_grid_index];
+ new_mini_grid.squares[position] = {
symbol: symbol,
glyph: false
};
/* With the symbol added to the squares, we need to see if this
* newly-placed move forms a glyph or not. */
- const connected = this.find_connected(
- new_mini_grids[mini_grid_index].squares, position);
+ const connected = this.find_connected(new_mini_grid.squares, position);
const is_glyph = this.is_glyph(connected);
+ /* Either set (or clear) the glyph Boolean for each connected square. */
for (let i = 0; i < 9; i++) {
if (connected[i])
- new_mini_grids[mini_grid_index].squares[i].glyph = is_glyph;
+ new_mini_grid.squares[i].glyph = is_glyph;
+ }
+
+ /* If this is the last cell of played in a mini-grid then it's
+ * time to score it. */
+ const occupied = new_mini_grid.squares.reduce(
+ (acc, val) => acc + (val.symbol !== null ? 1 : 0), 0);
+ if (occupied === 9) {
+ for (let i = 0; i < 9; i++) {
+ if (new_mini_grid.squares[i].glyph) {
+ if (new_mini_grid.squares[i].symbol === '+')
+ new_mini_grid.score_plus++;
+ else
+ new_mini_grid.score_o++;
+ }
+ }
+ if (new_mini_grid.score_plus > new_mini_grid.score_o)
+ new_mini_grid.winner = '+';
+ else
+ new_mini_grid.winner = 'o';
}
/* And append the move to the list of moves. */