-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
315 additions
and
527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import cx from 'clsx'; | ||
import { Cell, Game } from '../types'; | ||
import { observer } from 'mobx-react-lite'; | ||
|
||
export const HtmlRenderer = observer(function HtmlRenderer({ game }: { game: Game }) { | ||
return ( | ||
<main className="flex justify-center items-center flex-col h-screen"> | ||
<p className="mb-2">Points: {game.points}</p> | ||
<div className="flex flex-col gap-1.5 border-2 border-slate-300 rounded-md p-1"> | ||
{game.board.map((col, x) => ( | ||
<div className="flex gap-1.5" key={x}> | ||
{col.map((row, y) => ( | ||
<div | ||
className={cx('w-8 h-8 flex justify-center items-center rounded ', { | ||
'bg-green-300': row === Cell.Snake, | ||
'bg-green-300 eyes eyes-up': row === Cell.HeadUp, | ||
'bg-green-300 eyes eyes-right': row === Cell.HeadRight, | ||
'bg-green-300 eyes eyes-down': row === Cell.HeadDown, | ||
'bg-green-300 eyes eyes-left': row === Cell.HeadLeft, | ||
|
||
'bg-slate-50': row === Cell.Empty && (y - x) % 2 === 0, | ||
'bg-slate-100': row === Cell.Empty && (y - x) % 2 !== 0, | ||
|
||
'bg-red-500': row === Cell.Fruit, | ||
})} | ||
key={y} | ||
/> | ||
))} | ||
</div> | ||
))} | ||
</div> | ||
{game.phase === 'lost' && ( | ||
<div className="mt-12 flex gap-2 items-center"> | ||
<span>You Lose!</span> | ||
<button | ||
className="px-2 py-1 bg-slate-200 rounded hover:bg-slate-300 transition-colors" | ||
onClick={() => game.start()} | ||
> | ||
Play Again | ||
</button> | ||
</div> | ||
)} | ||
</main> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export type Phase = 'idle' | 'running' | 'lost'; | ||
|
||
export enum Direction { | ||
Up = 'up', | ||
Down = 'down', | ||
Left = 'left', | ||
Right = 'right', | ||
} | ||
|
||
export enum Cell { | ||
Empty = '_', | ||
Snake = 'S', | ||
HeadUp = 'HU', | ||
HeadRight = 'HR', | ||
HeadDown = 'HD', | ||
HeadLeft = 'HL', | ||
Fruit = 'F', | ||
} | ||
|
||
export interface Game { | ||
board: string[][]; | ||
phase: Phase; | ||
points: number; | ||
start: () => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import _ from 'lodash'; | ||
|
||
export const array2D = (count: number, cb: (x: number, y: number) => any) => | ||
_.times(count, (y) => _.times(count, (x) => cb(x, y))); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { redirect } from 'next/navigation'; | ||
|
||
export default function Page() { | ||
redirect('/snake'); | ||
redirect('/react-mobx'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
import { game } from './store'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { HtmlRenderer } from '@/app/_components/renderers/HtmlRenderer'; | ||
|
||
const Game = observer(() => { | ||
useEffect(() => { | ||
if (game.phase === 'idle') { | ||
game.setup(); | ||
game.start(); | ||
} | ||
}, []); | ||
|
||
return <HtmlRenderer game={game} />; | ||
}); | ||
|
||
export default Game; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.