2021-06-29 03:29:46 +03:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { FunctionComponent, useState } from 'react';
|
|
|
|
|
import { Hole, Store, Game } from '../mancala';
|
|
|
|
|
|
2021-06-30 16:11:22 +03:00
|
|
|
type Theme = {
|
|
|
|
|
background : string,
|
|
|
|
|
boardColor: string
|
|
|
|
|
storeColor: string
|
|
|
|
|
holeColor: string
|
|
|
|
|
ballColor: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const theme: Theme = {
|
|
|
|
|
background : "#EEEEEE",
|
|
|
|
|
boardColor: "#4D606E",
|
|
|
|
|
storeColor: "#3FBAC2",
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HoleView: FunctionComponent<{ hole: Hole, color: string, onClick: () => void }> = ({ hole, color, onClick }) => {
|
2021-06-30 16:11:22 +03:00
|
|
|
const balls = [...range(hole.ballCount)].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<
|
|
|
|
|
{ store: Store, color: string, gridColumn: string, gridRow: string }> = ({ store, color, gridColumn, gridRow }) => {
|
2021-06-30 16:11:22 +03:00
|
|
|
const balls = [...range(store.ballCount)].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>)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const BoardView: FunctionComponent<{ game?: Game, userKey: string, onHoleSelect: (index: number, hole: Hole) => void }> = ({
|
|
|
|
|
game, userKey, onHoleSelect }) => {
|
|
|
|
|
|
|
|
|
|
const player1Holes = game?.board.player1Holes.map((hole) => (
|
2021-06-30 16:11:22 +03:00
|
|
|
<HoleView hole={hole} color={theme.holeColor} onClick={() => {
|
2021-06-29 03:29:46 +03:00
|
|
|
if (game.turn == "player1") onHoleSelect(game.board.player1Holes.indexOf(hole), hole)
|
|
|
|
|
}} />
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
const player2Holes = game!!.board.player2Holes.map((hole) => (
|
2021-06-30 16:11:22 +03:00
|
|
|
<HoleView hole={hole} color={theme.holeColor} onClick={() => {
|
2021-06-29 03:29:46 +03:00
|
|
|
if (game.turn == "player2") onHoleSelect(game.board.player2Holes.indexOf(hole), hole)
|
|
|
|
|
}} />
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
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: theme.boardColor
|
2021-06-29 03:29:46 +03:00
|
|
|
}}>
|
|
|
|
|
{
|
|
|
|
|
game.getPlayerNameByKey(userKey) == "player2" ? (
|
|
|
|
|
<>
|
2021-06-30 16:11:22 +03:00
|
|
|
<StoreView store={game!!.board.player1Store} color={theme.storeColor} gridColumn="1 / 2" gridRow="1 / 3" />
|
|
|
|
|
<StoreView store={game!!.board.player2Store} color={theme.storeColor} gridColumn="8 / 9" gridRow="1 / 3" />
|
2021-06-29 03:29:46 +03:00
|
|
|
{player1Holes.reverse()}
|
|
|
|
|
{player2Holes}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
2021-06-30 16:11:22 +03:00
|
|
|
<StoreView store={game!!.board.player2Store} color={theme.storeColor} gridColumn="1 / 2" gridRow="1 / 3" />
|
|
|
|
|
<StoreView store={game!!.board.player1Store} color={theme.storeColor} gridColumn="8 / 9" gridRow="1 / 3" />
|
2021-06-29 03:29:46 +03:00
|
|
|
{player2Holes.reverse()}
|
|
|
|
|
{player1Holes}
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default BoardView
|