center on the screen

This commit is contained in:
Michael Peters 2024-08-04 12:41:23 -07:00
parent 77e6958c5d
commit efb4ddc014
3 changed files with 47 additions and 37 deletions

View File

@ -129,7 +129,7 @@ function createLabSnapshots(labs: SnakeGameTrainerLab[]): LabSnapshot[] {
export default function runCanvas(canvas: HTMLCanvasElement, pipeRef: MutableRefObject<PipeRef>) {
const ui = new UI(canvas);
const keys = new Keys();
const engine = new Engine({ stepsBeforeDelay: 1, updateDelay: 0 });
const engine = new Engine({ stepsBeforeDelay: 5, updateDelay: 0 });
// game logic --------------------------------------------------------------

View File

@ -1,12 +1,27 @@
#snake {
svg#board {
background-color: #333333;
height: 100vh;
align-items: center;
justify-content: center;
display: flex;
flex-flow: row nowrap;
.apple {
fill: #e01851;
.ui {
display: flex;
flex-flow: row nowrap;
align-items: flex-start;
justify-content: flex-start;
canvas#board {
margin-right: 20px;
}
.snake-part {
fill: #277edb;
.table-container {
max-height: 600px;
overflow-y: auto;
table#snaps {
width: 450px;
}
}
}
}

View File

@ -9,6 +9,15 @@ export interface PipeRef {
addTrainerSnap: (snap: TrainerSnapshot) => void;
}
function tableHead(...cols: (string | number)[]) {
const elements = cols.map((col, idx) => <th key={idx}>{col}</th>);
return <tr>{elements}</tr>;
}
function tableRow(...cols: (string | number)[]) {
const elements = cols.map((col, idx) => <td key={idx}>{col}</td>);
return <tr>{elements}</tr>;
}
const SnakePage: FC = () => {
const boardRef = useRef(null);
const [snaps, setSnaps] = useState<TrainerSnapshot[]>([]);
@ -29,6 +38,12 @@ const SnakePage: FC = () => {
runCanvas(boardRef.current, pipeRef);
}, []);
// future ideas:
// - center canvas & snap table
// - configure next iteration settings
// - save/load snakes
// - view single snake
const labSnapsTRs = useMemo(
() =>
snaps.map(snap => {
@ -44,42 +59,22 @@ const SnakePage: FC = () => {
const q95Len = labsRanked[Math.floor(labsRanked.length * 0.95)]!.snakeLength;
const maxLen = Math.max(...labsRanked.map(lab => lab.snakeLength));
return (
<tr>
<td>{snap.iteration}</td>
<td>{avgLen}</td>
<td>{minLen}</td>
<td>{q20Len}</td>
<td>{q40Len}</td>
<td>{q60Len}</td>
<td>{q80Len}</td>
<td>{q90Len}</td>
<td>{q95Len}</td>
<td>{maxLen}</td>
</tr>
);
return tableRow(snap.iteration, avgLen, minLen, q20Len, q40Len, q60Len, q80Len, q90Len, q95Len, maxLen);
}),
[snaps],
);
return (
<div id="snake">
<canvas id="board" ref={boardRef} width={BOARD_SIZE} height={BOARD_SIZE} />
<table>
<tr>
<th>Itr</th>
<th>Avg</th>
<th>Min</th>
<th>20%</th>
<th>40%</th>
<th>60%</th>
<th>80%</th>
<th>90%</th>
<th>95%</th>
<th>Max</th>
</tr>
{labSnapsTRs}
</table>
<div className="ui">
<canvas id="board" ref={boardRef} width={BOARD_SIZE} height={BOARD_SIZE} />
<div className="table-container">
<table id="snaps">
{tableHead('Itr', 'Avg', 'Min', '20%', '40%', '60%', '80%', '90%', '95%', 'Max')}
{labSnapsTRs}
</table>
</div>
</div>
</div>
);
};