diff --git a/src/components/App/App.tsx b/src/components/App/App.tsx index de78745..bc39f80 100644 --- a/src/components/App/App.tsx +++ b/src/components/App/App.tsx @@ -5,22 +5,64 @@ import Board from '../Board/Board'; import './App.scss'; -export default function App(): JSX.Element { - return ( -
-
-
- +export type Point = { + x: number; + y: number; +} + +export type Bar = { + start: Point; + end: Point; +} + +export type Solution = { + bar: Bar; + answer: string; +} + +type Game = { + title: string; + description: string; + catchword: string; + cells: Array>; + solutions: Array; +} + +export default class App extends React.Component { + private game: Game; + private readonly answers: Array; + + constructor(props: object) { + super(props); + + this.game = require(`../../constants/1.json`) + this.answers = this.game.solutions.map(s => s.answer).map(k =>

{k}

); + } + + render() { + return ( +
+
- controls: 1231414214 + +
+

{this.game.title}

+

{this.game.description}

+
+
+
+
+
+ +
+
+ {this.answers}
-
-
-
- -
-
-
- ); + ); + } } diff --git a/src/components/Board/Board.tsx b/src/components/Board/Board.tsx index c22d776..c3b0f61 100644 --- a/src/components/Board/Board.tsx +++ b/src/components/Board/Board.tsx @@ -1,66 +1,44 @@ import React from 'react'; import Line from '../Line/Line'; +import {Point, Bar, Solution} from "../App/App"; import './Board.scss'; -type Point = { - x: number; - y: number; -} - -export type Bar = { - start: Point, - end: Point, -} - -type Solution = { +type Answer = { bar: Bar; - key: string; -} - -type Selection = { - bar: Bar, - isVisible: boolean; isCorrect: boolean; } interface IBoardProps { - numBoard: number; + cells: Array>; + solutions: Array; + cellSize: number; } interface IBoardState { score: number; - wasValid: boolean; + wasValidAnswer: boolean; wasSelecting: boolean; - selections: Array; + selections: Array; bar: Bar; } export default class Board extends React.Component { - private readonly cells: Array>; - private readonly solutions: Array; - private readonly cellSize: number; constructor(props: IBoardProps) { super(props); - 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(() => + const selections = [...new Array(props.solutions.length)].map(() => Object.assign({}, { bar: {start: {x: 0, y: 0}, end: {x: 0, y: 0}}, isCorrect: false, - isVisible: true, })); this.state = { score: 0, selections, - wasValid: false, + wasValidAnswer: false, wasSelecting: false, bar: {start: {x: 0, y: 0}, end: {x: 0, y: 0}}, } @@ -74,7 +52,7 @@ export default class Board extends React.Component { const [parse, str] = [JSON.parse, JSON.stringify]; let score = this.state.score; - const selections: Array = parse(str(this.state.selections)); + 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 @@ -86,32 +64,33 @@ 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}; if (isValidMove) s.bar = bar; - if (isMouseUp && this.state.wasValid) + if (isMouseUp && this.state.wasValidAnswer) [s.isCorrect, score] = [true, score + 1]; + const isValidSolution = this.props.solutions.some(v => str(v.bar) === str(s.bar)); + const wasAlreadySelected = this.state.selections.some(v => str(v.bar) === str(s.bar)); + const isValidAnswer = isValidSolution && !wasAlreadySelected; + this.setState({ ...this.state, score, bar, selections, - wasValid: isValid, - wasSelecting: isSelecting + wasValidAnswer: isValidAnswer, + wasSelecting: isSelecting, }); - if (score === this.solutions.length) + if (score === this.props.solutions.length) this.gameOver(); } renderCell(s: string, pos: Point): JSX.Element { - const size = `${this.cellSize}px`; - const padding = `${this.cellSize / 4}px`; + const size = `${this.props.cellSize}px`; + const padding = `${this.props.cellSize / 4}px`; return (