Last active
March 5, 2018 20:04
-
-
Save goldensunliu/cf3e1fee4a402167afcc27ba16186ae7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function isPeer(a, b) { | |
if (!a || !b) return false; | |
const squareA = ((Math.floor(a.x / 3)) * 3) + Math.floor(a.y / 3); | |
const squareB = ((Math.floor(b.x / 3)) * 3) + Math.floor(b.y / 3); | |
return a.x === b.x || a.y === b.y || squareA === squareB; | |
} | |
class Game extends Compoent { | |
... | |
isConflict(i, j) { | |
const { value } = this.state.board.getIn(['puzzle', i, j]).toJSON(); | |
if (!value) return false; | |
const rowConflict = | |
this.state.board.getIn(['choices', 'rows', i, value]) > 1; | |
const columnConflict = | |
this.state.board.getIn(['choices', 'columns', j, value]) > 1; | |
const squareConflict = | |
this.state.board.getIn(['choices', 'squares', | |
((Math.floor(i / 3)) * 3) + Math.floor(j / 3), value]) > 1; | |
return rowConflict || columnConflict || squareConflict; | |
} | |
renderCell(cell, x, y) { | |
const { board } = this.state; | |
const selected = this.getSelectedCell(); | |
const { value, prefilled, notes } = cell.toJSON(); | |
const conflict = this.isConflict(x, y); | |
const peer = areCoordinatePeers({ x, y }, board.get('selected')); | |
const sameValue = !!(selected && selected.get('value') | |
&& value === selected.get('value')); | |
const isSelected = cell === selected; | |
return (<Cell | |
prefilled={prefilled} | |
notes={notes} | |
sameValue={sameValue} | |
isSelected={isSelected} | |
isPeer={peer} | |
value={value} | |
onClick={() => { this.selectCell(x, y); }} | |
key={y} | |
x={x} | |
y={y} | |
conflict={conflict} | |
/>); | |
} | |
renderPuzzle() { | |
const { board } = this.state; | |
return ( | |
<div className="puzzle"> | |
{board.get('puzzle').map((row, i) => ( | |
<div key={i} className="row"> | |
{ | |
row.map((cell, j) => this.renderCell(cell, i, j)).toArray() | |
} | |
</div> | |
)).toArray()} | |
<style jsx>{PuzzleStyle}</style> | |
</div> | |
); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment