Skip to content

Commit 9db23ee

Browse files
author
skedwards88
committed
reflect horizontal and vertical validity
1 parent e414fdf commit 9db23ee

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

src/App.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ a {
342342
font-size: calc(0.9 * var(--box-size));
343343
background-color: var(--dark-color);
344344
color: var(--light-color);
345+
position: relative;
345346
}
346347

347348
.letter.filled {
@@ -354,10 +355,31 @@ a {
354355
color: rgb(226 88 88);
355356
}
356357

358+
.letter.verticalValid::before{
359+
content: '';
360+
position: absolute;
361+
left: 18%;
362+
top: 38%;
363+
width: 1px;
364+
height: 6px;
365+
background-color: var(--light-color);
366+
}
367+
368+
.letter.horizontalValid::after{
369+
content: '';
370+
position: absolute;
371+
left: 10%;
372+
top: 45%;
373+
height: 1px;
374+
width: 6px;
375+
background-color: var(--light-color);
376+
}
377+
357378
.letter-border {
358379
box-sizing: border-box;
359380
width: calc(var(--box-size) + 1px);
360381
height: calc(var(--box-size) + 1px);
382+
z-index: 1;
361383
}
362384

363385
.borderTop {

src/components/Board.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React from "react";
22
import Piece from "./Piece";
33
import 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
611
export 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+
28106
export 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

src/components/Piece.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ function Letter({
66
pieceRowIndex,
77
pieceColIndex,
88
overlapping,
9+
isHorizontallyValid,
10+
isVerticallyValid,
911
gameIsSolved,
1012
dispatchGameState,
1113
}) {
@@ -28,6 +30,12 @@ function Letter({
2830
if (overlapping) {
2931
className += " overlapping";
3032
}
33+
if (isHorizontallyValid) {
34+
className += " horizontalValid";
35+
}
36+
if (isVerticallyValid) {
37+
className += " verticalValid";
38+
}
3139

3240
return (
3341
<div
@@ -75,6 +83,8 @@ export default function Piece({
7583
piece,
7684
where,
7785
overlapGrid,
86+
horizontalValidityGrid,
87+
verticalValidityGrid,
7888
gameIsSolved,
7989
dispatchGameState,
8090
}) {
@@ -100,6 +110,18 @@ export default function Piece({
100110
piece.boardLeft + colIndex
101111
] > 1
102112
}
113+
isHorizontallyValid={
114+
isOnBoard &&
115+
horizontalValidityGrid[piece.boardTop + rowIndex][
116+
piece.boardLeft + colIndex
117+
]
118+
}
119+
isVerticallyValid={
120+
isOnBoard &&
121+
verticalValidityGrid[piece.boardTop + rowIndex][
122+
piece.boardLeft + colIndex
123+
]
124+
}
103125
gameIsSolved={gameIsSolved}
104126
dispatchGameState={dispatchGameState}
105127
/>

0 commit comments

Comments
 (0)