Skip to content

Commit

Permalink
Added dice rolling and some basic piece dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Sofer authored and Dean Sofer committed May 13, 2024
1 parent 4f60630 commit 34361f3
Show file tree
Hide file tree
Showing 26 changed files with 65 additions and 69 deletions.
Binary file removed public/images/digit-1-black.png
Binary file not shown.
Binary file removed public/images/digit-1-white.png
Binary file not shown.
Binary file removed public/images/digit-2-black.png
Binary file not shown.
Binary file removed public/images/digit-2-white.png
Binary file not shown.
Binary file removed public/images/digit-3-black.png
Binary file not shown.
Binary file removed public/images/digit-3-white.png
Binary file not shown.
Binary file removed public/images/digit-4-black.png
Binary file not shown.
Binary file removed public/images/digit-4-white.png
Binary file not shown.
Binary file removed public/images/digit-5-black.png
Binary file not shown.
Binary file removed public/images/digit-5-white.png
Binary file not shown.
Binary file removed public/images/digit-6-black.png
Binary file not shown.
Binary file removed public/images/digit-6-white.png
Binary file not shown.
7 changes: 7 additions & 0 deletions src/Game/Dice/Dice.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.dice {
position: absolute;
left: 20%;
top: 50%;
transform: translateY(-50%);
z-index: 2;
}
File renamed without changes.
3 changes: 2 additions & 1 deletion src/Game/Dice/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MouseEventHandler } from 'react';
import * as IMAGES from './images';
import './Dice.css'

type DiceProps = {
onClick: MouseEventHandler,
Expand All @@ -9,6 +10,6 @@ type DiceProps = {
export default function Dice({ onClick, values }: DiceProps) {
return <div className="dice" onClick={onClick} style={{ cursor: 'pointer' }}>
<img src={IMAGES[`black${values[0] || 6}`]} />

Check failure on line 12 in src/Game/Dice/index.tsx

View workflow job for this annotation

GitHub Actions / deploy

Element implicitly has an 'any' type because expression of type '`black${number}`' can't be used to index type 'typeof import("/home/runner/work/PeaceInTheMiddleEast/PeaceInTheMiddleEast/src/Game/Dice/images/index")'.
<img src={IMAGES[`white${values[1] || 3}`]} />
<img src={IMAGES[`white${values[1] || 6}`]} />

Check failure on line 13 in src/Game/Dice/index.tsx

View workflow job for this annotation

GitHub Actions / deploy

Element implicitly has an 'any' type because expression of type '`white${number}`' can't be used to index type 'typeof import("/home/runner/work/PeaceInTheMiddleEast/PeaceInTheMiddleEast/src/Game/Dice/images/index")'.
</div>
}
22 changes: 1 addition & 21 deletions src/Game/Game.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
flex-wrap: wrap;
}

.dice {
position: absolute;
left: 20%;
top: 50%;
transform: translateY(-50%);
z-index: 2;
}

.bar {
background: lightblue;
flex-basis: 4%;
Expand All @@ -23,19 +15,6 @@
flex-basis: 4%;
}

.piece {
width: 20px;
height: 20px;

&.white {
background: white;
}

&.black {
background: black;
}
}

.point {
position: relative;
flex-basis: calc(100% / 14);
Expand All @@ -46,6 +25,7 @@
width: 0;
height: 0;
position: absolute;
z-index: -1;
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Game/Piece/Piece.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.piece {
width: 20px;
height: 20px;

&.white {
background: white;
}

&.black {
background: black;
}
}
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
32 changes: 32 additions & 0 deletions src/Game/Piece/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useCallback, type DragEventHandler } from "react";
import black from './images/piece-black-2.png';
import white from './images/piece-white-2.png';
import './Piece.css'

const IMAGES = { black, white }

type PointProps = {
pieces: number,
move: (from: number, to: number) => void,
position: number
}

export default function Point({ pieces, move, position }: PointProps) {

const onDragStart: DragEventHandler = useCallback((event) => event.dataTransfer?.setData('text', position.toString()), [])
const onDragOver: DragEventHandler = useCallback((event) => { event.preventDefault(); }, [])
const onDropListener: DragEventHandler = useCallback((event) => {
event.preventDefault();
let from = parseInt(event.dataTransfer?.getData("text")!)
return move(from, position)
}, [move])

const Pieces = Array.from({ length: Math.abs(pieces) });
const color = pieces > 0 ? 'white' : 'black';

return <div className="point" onDragOver={onDragOver} onDragStart={onDragStart} onDrop={onDropListener}>
{Pieces.map((value, index) => <div className={`piece ${color}`} key={index}>

Check failure on line 28 in src/Game/Piece/index.tsx

View workflow job for this annotation

GitHub Actions / deploy

'value' is declared but its value is never read.
<img src={IMAGES[color]} />
</div>)}
</div>
}
17 changes: 0 additions & 17 deletions src/Game/Point.tsx

This file was deleted.

41 changes: 11 additions & 30 deletions src/Game/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './Game.css';
import Dice from './Dice';
import Point from './Point';
import Point from './Piece';
import { useCallback, useState } from 'react';

const DEFAULT_BOARD = [
Expand All @@ -26,10 +26,16 @@ export default function Game() {
const fromPieces = board[from];
if (!fromPieces) return;

if (board[to] === 1) {
// TODO: Validate moves against dice

if (to === -1) {
// TODO: bear off piece
} else if (board[to] === 1) {
board[to] = -1;
// TODO: move piece to bar
} else if (board[to] === -1) {
board[to] = 1;
// TODO: move piece to bar
} else if (board[to] > 0 && fromPieces > 0) {
board[to]++;
board[from]--;
Expand All @@ -48,40 +54,15 @@ export default function Game() {
setDice([first, second])
}, [])

const onDrop = useCallback((position: number, event: DragEvent) => {
move(event.dataTransfer.getData('text'), position);
}, [move])

return <div id="board">
<Dice onClick={roll} values={dice} />

Check failure on line 59 in src/Game/index.tsx

View workflow job for this annotation

GitHub Actions / deploy

Type 'number[]' is not assignable to type '[(number | undefined)?, (number | undefined)?]'.
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>

{board.slice(0, 12).map((pieces, index) => <Point pieces={pieces} move={move} position={index} />)}
<div className="bar"></div>
<div className="bar"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
<div className="point"></div>
{board.slice(12).map((pieces, index) => <Point pieces={pieces} move={move} position={index} />)}
<div className="home"></div>
<div className="home"></div>
</div >;
return <>{this.state.board.map((pieces, position) => <Point onDrop={this.onDrop.bind(this, position)} pieces={pieces} position={position} />)}</>;
}

0 comments on commit 34361f3

Please sign in to comment.