From db778747865d96ca21cbdc43133389ee121166fb Mon Sep 17 00:00:00 2001 From: Artur Tamborski Date: Sun, 27 Dec 2020 19:58:48 +0100 Subject: [PATCH] components/Board: block selecting same answer twice --- src/components/Board/Board.scss | 3 --- src/components/Board/Board.tsx | 31 +++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/components/Board/Board.scss b/src/components/Board/Board.scss index 0a98f9a..4285410 100644 --- a/src/components/Board/Board.scss +++ b/src/components/Board/Board.scss @@ -22,12 +22,9 @@ } .Cell { - width: 60px; - height: 60px; font-weight: bold; font-size: x-large; font-family: monospace; - padding: 15px; background: ghostwhite; text-align: center; border: 5px solid #e5e5ea; diff --git a/src/components/Board/Board.tsx b/src/components/Board/Board.tsx index 389f481..c22d776 100644 --- a/src/components/Board/Board.tsx +++ b/src/components/Board/Board.tsx @@ -31,6 +31,7 @@ interface IBoardProps { interface IBoardState { score: number; + wasValid: boolean; wasSelecting: boolean; selections: Array; bar: Bar; @@ -39,6 +40,7 @@ interface IBoardState { export default class Board extends React.Component { private readonly cells: Array>; private readonly solutions: Array; + private readonly cellSize: number; constructor(props: IBoardProps) { super(props); @@ -46,6 +48,7 @@ export default class Board extends React.Component { const crosski: any = require(`../../constants/${props.numBoard}.json`) this.cells = crosski.cells; this.solutions = crosski.solutions; + this.cellSize = 60; const selections = [...new Array(this.solutions.length)].map(() => Object.assign({}, { @@ -57,6 +60,7 @@ export default class Board extends React.Component { this.state = { score: 0, selections, + wasValid: false, wasSelecting: false, bar: {start: {x: 0, y: 0}, end: {x: 0, y: 0}}, } @@ -67,8 +71,10 @@ export default class Board extends React.Component { } updateSelection(pos: Point, isSelecting: boolean, isMoving: boolean = false): void { + const [parse, str] = [JSON.parse, JSON.stringify]; + let score = this.state.score; - const selections = this.state.selections.slice(); + const selections: Array = parse(str(this.state.selections)); const s = selections.find(v => !v.isCorrect); // skip if there are no lines left or the mouse is moving but not selecting @@ -80,26 +86,37 @@ export default class Board extends React.Component { const isValidMove = x === 0 || y === 0 || x === y; const isMouseDown = isSelecting && !this.state.wasSelecting; const isMouseUp = !isSelecting && this.state.wasSelecting && isValidMove; + const isValidSolution = this.solutions.some(v => str(v.bar) === str(s.bar)); + const wasAlreadySelected = this.state.selections.some(v => str(v.bar) === str(s.bar)); + const isValid = isValidSolution && !wasAlreadySelected; const start: Point = isMouseDown ? pos : this.state.bar.start; const bar: Bar = {start, end: pos}; - const str = JSON.stringify; if (isValidMove) s.bar = bar; - if (isMouseUp && this.solutions.some(v => str(v.bar) === str(bar))) + if (isMouseUp && this.state.wasValid) [s.isCorrect, score] = [true, score + 1]; - this.setState({...this.state, score, bar, selections, wasSelecting: isSelecting}); + this.setState({ + ...this.state, + score, bar, selections, + wasValid: isValid, + wasSelecting: isSelecting + }); if (score === this.solutions.length) this.gameOver(); } renderCell(s: string, pos: Point): JSX.Element { + const size = `${this.cellSize}px`; + const padding = `${this.cellSize / 4}px`; + return (