mancala/src/components/BoardView.tsx

136 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-05-04 23:41:04 +03:00
import { Bank, MancalaGame, Pit } from 'mancala.js';
2021-06-29 03:29:46 +03:00
import * as React from 'react';
import { FunctionComponent, useState } from 'react';
2021-06-30 16:11:22 +03:00
type Theme = {
background : string,
boardColor: string
boardColorWhenPlayerTurn: string
2021-06-30 16:11:22 +03:00
storeColor: string
storeColorWhenPlayerTurn: string
2021-06-30 16:11:22 +03:00
holeColor: string
ballColor: string
}
const theme: Theme = {
background : "#EEEEEE",
boardColor: "#4D606E",
boardColorWhenPlayerTurn: "#84b8a6",
2021-06-30 16:11:22 +03:00
storeColor: "#3FBAC2",
storeColorWhenPlayerTurn: "#6cab94",
2021-06-30 16:11:22 +03:00
holeColor: "#D3D4D8",
ballColor: "#393E46",
}
const BallView: FunctionComponent<{color : string}> = ({color}) => {
2021-06-29 03:29:46 +03:00
return (<div style={{
2021-06-30 16:11:22 +03:00
background: color,
2021-06-29 03:29:46 +03:00
margin: "1px",
2021-06-30 16:11:22 +03:00
width: "1vw",
height: "1vw",
borderRadius: "10vw"
2021-06-29 03:29:46 +03:00
}} >
</div>)
}
function range(size: number) {
var ans = [];
for (let i = 0; i < size; i++) {
ans.push(i);
}
return ans;
}
2022-05-04 23:41:04 +03:00
const HoleView: FunctionComponent<{ hole: Pit, color: string, onClick: () => void }> = ({ hole, color, onClick }) => {
const balls = [...range(hole.stoneCount)].map((i) => <BallView color={theme.ballColor}/>)
2021-06-29 03:29:46 +03:00
return (<div
onClick={onClick}
style={{
background: color,
margin: "5px",
padding: "5px",
2021-06-30 16:11:22 +03:00
borderRadius: "10vw",
2021-06-29 03:29:46 +03:00
display: 'flex',
alignItems: 'center',
alignContent: 'center',
justifyContent: 'center',
justifyItems: 'center',
flexWrap: "wrap",
}} >
{balls}
</div>)
}
const StoreView: FunctionComponent<
2022-05-04 23:41:04 +03:00
{ store: Bank, color: string, gridColumn: string, gridRow: string }> = ({ store, color, gridColumn, gridRow }) => {
const balls = [...range(store.stoneCount)].map((i) => <BallView color={theme.ballColor}/>)
2021-06-29 03:29:46 +03:00
return (<div style={{
gridColumn: gridColumn,
gridRow: gridRow,
background: color,
2021-06-30 16:11:22 +03:00
margin: "5px",
borderRadius: "10vw",
2021-06-29 03:29:46 +03:00
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
2021-06-30 16:11:22 +03:00
alignContent: 'center',
2021-06-29 03:29:46 +03:00
flexWrap: "wrap",
}} >
{balls}
</div>)
}
2022-05-04 23:41:04 +03:00
const BoardView: FunctionComponent<{ game?: MancalaGame, userKey: string, onHoleSelect: (index: number, hole: Pit) => void }> = ({
2021-06-29 03:29:46 +03:00
game, userKey, onHoleSelect }) => {
2022-05-04 23:41:04 +03:00
const player1Pits = game?.board.player1Pits.map((hole) => (
2021-06-30 16:11:22 +03:00
<HoleView hole={hole} color={theme.holeColor} onClick={() => {
2022-05-04 23:41:04 +03:00
if (game.turnPlayerId === game.player1Id) onHoleSelect(game.board.player1Pits.indexOf(hole), hole)
2021-06-29 03:29:46 +03:00
}} />
))
2022-05-04 23:41:04 +03:00
const player2Pits = game!!.board.player2Pits.map((hole) => (
2021-06-30 16:11:22 +03:00
<HoleView hole={hole} color={theme.holeColor} onClick={() => {
2022-05-04 23:41:04 +03:00
if (game.turnPlayerId === game.player2Id) onHoleSelect(game.board.player2Pits.indexOf(hole), hole)
2021-06-29 03:29:46 +03:00
}} />
))
2022-05-04 23:41:04 +03:00
const isUserTurn = game.checkIsPlayerTurn(userKey)
const storeColor = isUserTurn ? theme.storeColor : theme.storeColorWhenPlayerTurn;
2021-06-29 03:29:46 +03:00
return (
<div style={{
margin: '10px',
2021-06-30 19:41:53 +03:00
padding: '20px',
2021-06-29 03:29:46 +03:00
display: 'grid',
2021-06-30 16:11:22 +03:00
gridTemplateColumns: 'repeat(8, 11vw)',
gridTemplateRows: 'repeat(2, 11vw)',
borderRadius: "3vw",
background: isUserTurn ? theme.boardColor : theme.boardColorWhenPlayerTurn
2021-06-29 03:29:46 +03:00
}}>
{
2022-05-04 23:41:04 +03:00
userKey === game.player2Id ? (
2021-06-29 03:29:46 +03:00
<>
2022-05-04 23:41:04 +03:00
<StoreView store={game!!.board.player1Bank} color={storeColor} gridColumn="1 / 2" gridRow="1 / 3" />
<StoreView store={game!!.board.player2Bank} color={storeColor} gridColumn="8 / 9" gridRow="1 / 3" />
{player1Pits.reverse()}
{player2Pits}
2021-06-29 03:29:46 +03:00
</>
) : (
<>
2022-05-04 23:41:04 +03:00
<StoreView store={game!!.board.player2Bank} color={storeColor} gridColumn="1 / 2" gridRow="1 / 3" />
<StoreView store={game!!.board.player1Bank} color={storeColor} gridColumn="8 / 9" gridRow="1 / 3" />
{player2Pits.reverse()}
{player1Pits}
2021-06-29 03:29:46 +03:00
</>
)
}
</div>
)
}
export default BoardView