11import React from "react" ;
22import Piece from "./Piece" ;
33import DragShadow from "./DragShadow" ;
4+ import { getGridFromPieces } from "../logic/getGridFromPieces" ;
5+ import { isKnown } from "@skedwards88/word_logic" ;
6+ import { trie } from "../logic/trie" ;
7+ import { getWordsFromPieces } from "../logic/getWordsFromPieces" ;
8+ import { transposeGrid } from "@skedwards88/word_logic" ;
49
510// Returns a grid with the number of letters at each location in the grid
611export function countingGrid ( height , width , pieces ) {
@@ -25,6 +30,79 @@ export function countingGrid(height, width, pieces) {
2530 return grid ;
2631}
2732
33+ function getHorizontalValidityGrid ( { grid, originalWords} ) {
34+ // return a 2D array of bools indicating whether
35+ // the position corresponds to a letter on the board
36+ // that is part of a valid horizontal word
37+ const height = grid . length ;
38+ const width = grid [ 0 ] . length ;
39+
40+ const horizontalValidityGrid = Array ( height )
41+ . fill ( undefined )
42+ . map ( ( ) => Array ( width ) . fill ( false ) ) ;
43+
44+ for ( const [ rowIndex , row ] of grid . entries ( ) ) {
45+ let word = "" ;
46+ let indexes = [ ] ;
47+ for ( const [ columnIndex , letter ] of row . entries ( ) ) {
48+ if ( letter != "" ) {
49+ word += letter ;
50+ indexes . push ( columnIndex ) ;
51+ } else {
52+ if ( word . length > 1 ) {
53+ // If the word is one of the original words, always consider it valid (in case we updated the dictionary in the interim).
54+ // Otherwise, check whether it is a word in the trie.
55+ let isWord = originalWords . includes ( word ) ;
56+ if ( ! isWord ) {
57+ ( { isWord } = isKnown ( word , trie ) ) ;
58+ }
59+ if ( isWord ) {
60+ indexes . forEach (
61+ ( index ) => ( horizontalValidityGrid [ rowIndex ] [ index ] = true )
62+ ) ;
63+ }
64+ }
65+ word = "" ;
66+ indexes = [ ] ;
67+ }
68+ }
69+ // Also end the word if we reach the end of the row
70+ if ( word . length > 1 ) {
71+ // If the word is one of the original words, always consider it valid (in case we updated the dictionary in the interim).
72+ // Otherwise, check whether it is a word in the trie.
73+ let isWord = originalWords . includes ( word ) ;
74+ if ( ! isWord ) {
75+ ( { isWord } = isKnown ( word , trie ) ) ;
76+ }
77+ if ( isWord ) {
78+ indexes . forEach (
79+ ( index ) => ( horizontalValidityGrid [ rowIndex ] [ index ] = true )
80+ ) ;
81+ }
82+ }
83+ }
84+
85+ return horizontalValidityGrid ;
86+ }
87+
88+ function getWordValidityGrids ( { pieces, gridSize } ) {
89+ const originalWords = getWordsFromPieces ( {
90+ pieces,
91+ gridSize,
92+ solution : true ,
93+ } ) ;
94+
95+ const grid = getGridFromPieces ( { pieces, gridSize, solution : false } ) ;
96+
97+ const horizontalValidityGrid = getHorizontalValidityGrid ( { grid, originalWords} )
98+
99+ const transposedGrid = transposeGrid ( grid ) ;
100+ const horizontalTransposedValidityGrid = getHorizontalValidityGrid ( { grid : transposedGrid , originalWords} ) ;
101+ const verticalValidityGrid = transposeGrid ( horizontalTransposedValidityGrid ) ;
102+
103+ return [ horizontalValidityGrid , verticalValidityGrid ] ;
104+ }
105+
28106export default function Board ( {
29107 pieces,
30108 gridSize,
@@ -38,6 +116,7 @@ export default function Board({
38116 ) ;
39117
40118 const overlapGrid = countingGrid ( gridSize , gridSize , boardPieces ) ;
119+ const [ horizontalValidityGrid , verticalValidityGrid ] = getWordValidityGrids ( { pieces, gridSize } ) ;
41120 const pieceElements = boardPieces . map ( ( piece ) => (
42121 < Piece
43122 key = { piece . id }
@@ -46,6 +125,8 @@ export default function Board({
46125 overlapGrid = { overlapGrid }
47126 gameIsSolved = { gameIsSolved }
48127 dispatchGameState = { dispatchGameState }
128+ horizontalValidityGrid = { horizontalValidityGrid }
129+ verticalValidityGrid = { verticalValidityGrid }
49130 />
50131 ) ) ;
51132
0 commit comments