Skip to content

Commit

Permalink
allow toggling hints in mahjong
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan4m1 committed Jan 1, 2024
1 parent eb7a70a commit 29689a1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
17 changes: 13 additions & 4 deletions src/components/mahjongBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function MahjongBoard({ images }) {
const boardRef = useRef(null);
const [solved, setSolved] = useState(false);
const [failed, setFailed] = useState(false);
const [showHints, setShowHints] = useState(false);
const [boardRect, setBoardRect] = useState(null);
const [activeTile, setActiveTile] = useState(null);
const [layout, setLayout] = useState(generateLayout('turtle'));
Expand Down Expand Up @@ -115,7 +116,7 @@ export default function MahjongBoard({ images }) {
}, [layout]);

useEffect(() => {
if (matches === 0) {
if (matches.length === 0) {
setFailed(true);
stopTimer();
}
Expand Down Expand Up @@ -163,8 +164,8 @@ export default function MahjongBoard({ images }) {
</Col>
<Col xs={2}>
<p className="mb-0">
{matches}{' '}
{matches === 1 && (
{matches.length}{' '}
{matches.length === 1 && (
<FontAwesomeIcon icon={faExclamationTriangle} color="orange" />
)}
</p>
Expand All @@ -190,7 +191,7 @@ export default function MahjongBoard({ images }) {
<Button disabled>
<FontAwesomeIcon icon={faFolderOpen} /> Load
</Button>
<Button disabled>
<Button onClick={() => setShowHints((prevVal) => !prevVal)}>
<FontAwesomeIcon icon={faHighlighter} /> Hint
</Button>
</ButtonGroup>
Expand All @@ -211,6 +212,14 @@ export default function MahjongBoard({ images }) {
}
y={tile.y * 64 - tile.layer * -5 + (boardRect?.top ?? 0)}
active={activeTile === tile.index}
hint={
showHints &&
Boolean(
matches.find(
([a, b]) => a.index === tile.index || b.index === tile.index
)
)
}
imageUrl={
images.nodes.find((node) =>
node.relativePath.endsWith(getTileImagePath(tile))
Expand Down
31 changes: 30 additions & 1 deletion src/components/mahjongTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import PropTypes from 'prop-types';

import { getTileFriendlyName } from 'utils/mahjong';

export default function MahjongTile({ tile, x, y, imageUrl, active, onClick }) {
export default function MahjongTile({
tile,
x,
y,
imageUrl,
active,
hint,
onClick
}) {
return (
<button
onClick={() => onClick(tile)}
Expand Down Expand Up @@ -39,6 +47,26 @@ export default function MahjongTile({ tile, x, y, imageUrl, active, onClick }) {
/>
</svg>
)}
{hint && (
<svg
width={53}
height={64}
viewBox="0 0 53 64"
style={{ zIndex: 10000 }}
>
<rect
x={0}
y={0}
height={64}
width={53}
fill="#00ff00"
fillOpacity={0.4}
stroke="#00ff00"
strokeWidth={2}
rx={8}
/>
</svg>
)}
</button>
);
}
Expand All @@ -47,6 +75,7 @@ MahjongTile.propTypes = {
tile: PropTypes.object.isRequired,
imageUrl: PropTypes.string.isRequired,
active: PropTypes.bool,
hint: PropTypes.bool,
onClick: PropTypes.func.isRequired,
x: PropTypes.number,
y: PropTypes.number
Expand Down
44 changes: 25 additions & 19 deletions src/utils/mahjong.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,31 @@ export function isMatch(aTile, bTile) {

return true;
}
function dedupeMatches(matches) {
return matches.flatMap(([a, b]) => {
const target = matches.find(
([searchA, searchB]) => searchA === b && searchB === a
);

return target ? [target] : [];
});
}
export function getAvailableMatches(layout) {
return (
layout.reduce(
(matches, tile) =>
matches +
layout.reduce((innerMatches, otherTile) => {
if (tile.index === otherTile.index) {
return innerMatches;
} else if (
isOpen(tile, layout) &&
isOpen(otherTile, layout) &&
isMatch(tile, otherTile)
) {
return innerMatches + 1;
} else {
return innerMatches;
}
}, 0),
0
) / 2
return dedupeMatches(
layout.flatMap((tile) => {
const match = layout.find(
(searchTile) =>
searchTile.index !== tile.index &&
isOpen(searchTile, layout) &&
isOpen(tile, layout) &&
isMatch(searchTile, tile)
);

if (match) {
return [[tile, match]];
} else {
return [];
}
})
);
}

0 comments on commit 29689a1

Please sign in to comment.