Skip to content

Commit

Permalink
Memory game refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sadanandpai committed Sep 1, 2024
1 parent c8dcd0b commit 16761f6
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 383 deletions.
12 changes: 0 additions & 12 deletions apps/react/src/challenges/memory-game/components/atoms/Cell.jsx

This file was deleted.

This file was deleted.

25 changes: 25 additions & 0 deletions apps/react/src/challenges/memory-game/components/game-controls.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import styles from '../styles.module.css';

export function GameControls({ level, isGameInProgress, isGameOver, onNext, onRestart }) {
if (isGameInProgress) {
return null;
}

if (isGameOver) {
return (
<div>
<button className={styles.button} onClick={onRestart}>
Restart
</button>
</div>
);
}

return (
<div>
<button className={styles.button} onClick={onNext}>
{level === 0 ? 'Start' : 'Next'}
</button>
</div>
);
}
23 changes: 23 additions & 0 deletions apps/react/src/challenges/memory-game/components/game.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import GridDisplay from './grid-display';

export default function Game({ level, size, onComplete, onError, life }) {
if (level === 0) {
return null;
}

return (
<section>
<h3>Level: {level}</h3>

<GridDisplay
rows={size.rows}
cols={size.cols}
onComplete={onComplete}
onError={onError}
disabled={life === 0}
/>

<p style={{ fontSize: '2rem' }}>Life Left: {life}</p>
</section>
);
}
68 changes: 68 additions & 0 deletions apps/react/src/challenges/memory-game/components/grid-display.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { useEffect, useRef, useState } from 'react';
import { getMemoryGrid } from '../utils/utils';
import { Grid } from './grid';
import { config } from '../config';

export default function GridDisplay({ rows, cols, onError, onComplete, disabled }) {
const [userGrid, setUserGrid] = useState([]);
const activeCells = useRef(0);
const gridRef = useRef();
const gameInProgress = useRef(false);

useEffect(() => {
const { grid, activeCount } = getMemoryGrid(rows, cols);
setUserGrid(grid);
gridRef.current = grid;
activeCells.current = activeCount;

const timeoutId = setTimeout(() => {
setUserGrid((prevGrid) => prevGrid.map((rows) => rows.map(() => false)));
gameInProgress.current = true;
}, config.timer);

return () => clearTimeout(timeoutId);
}, [rows, cols]);

function onCellClick(rowIndex, colIndex) {
// If the cell is already clicked or the game is not in progress
if (userGrid[rowIndex][colIndex] || !gameInProgress.current) {
return;
}

// If the cell is active
if (gridRef.current[rowIndex][colIndex]) {
setUserGrid((prevGrid) =>
prevGrid.map((rows, rIndex) =>
rows.map((cell, cIndex) => (rIndex === rowIndex && cIndex === colIndex ? true : cell))
)
);

gridRef.current[rowIndex][colIndex] = false;
activeCells.current = activeCells.current - 1;
} else {
// If the cell is not active
onError();
}

// If all the active cells are clicked
if (activeCells.current === 0) {
onComplete();
gameInProgress.current = false;
}
}

return (
<Grid rows={userGrid.length} cols={userGrid[0]?.length} width={0.9 * window.innerWidth}>
{userGrid.map((rows, rowIndex) =>
rows.map((cell, colIndex) => (
<button
disabled={disabled}
key={`${rowIndex}-${colIndex}`}
onClick={() => onCellClick(rowIndex, colIndex)}
style={{ backgroundColor: cell ? 'yellow' : 'white' }}
/>
))
)}
</Grid>
);
}
18 changes: 18 additions & 0 deletions apps/react/src/challenges/memory-game/components/grid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import styles from '../styles.module.css';

export function Grid({ rows, cols, width, children }) {
const gridStyle = {
gridTemplateRows: `repeat(${rows}, 1fr)`,
gridTemplateColumns: `repeat(${cols}, 1fr)`,
width: `${width - 20}px`,
height: `${((width - 20) / cols) * rows}px`,
maxHeight: `${rows * 100}px`,
maxWidth: `${cols * 100}px`,
};

return (
<div className={styles.grid} style={gridStyle}>
{children}
</div>
);
}

This file was deleted.

This file was deleted.

46 changes: 0 additions & 46 deletions apps/react/src/challenges/memory-game/components/pages/Game.jsx

This file was deleted.

38 changes: 0 additions & 38 deletions apps/react/src/challenges/memory-game/components/pages/Home.jsx

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions apps/react/src/challenges/memory-game/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const config = {
rows: 3,
cols: 3,
life: 5,
timer: 3000,
};
Loading

0 comments on commit 16761f6

Please sign in to comment.