Skip to content
This repository was archived by the owner on Dec 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
921e1f0
Delete everything
adzialocha Aug 23, 2022
00c4176
Set up Rollup project
adzialocha Aug 23, 2022
93f0626
Correct .nvmrc format
adzialocha Aug 23, 2022
cfb55ab
Add a README.md
adzialocha Aug 23, 2022
aef56a0
Update dependencies
adzialocha Aug 23, 2022
411c619
Rename too ZooAdventures component
adzialocha Aug 23, 2022
30e9aff
Add nodemon to watch for changes
adzialocha Aug 23, 2022
5346e77
Install graphql-request dependencies
adzialocha Aug 23, 2022
891f522
Add CommonJS to build library
adzialocha Aug 23, 2022
41f9806
Initial script to build schema and first board
adzialocha Aug 23, 2022
a1dc4f5
Initialise key pair and pass over configuration
adzialocha Aug 23, 2022
0913969
Display game board
adzialocha Aug 23, 2022
6fc83c5
Update field when clicking
adzialocha Aug 23, 2022
f9b3531
Add linter for react hooks
adzialocha Aug 23, 2022
79f3509
Define animal emoji based on public key
adzialocha Aug 23, 2022
400a53e
A little bit of styling
adzialocha Aug 23, 2022
50b4568
Do not show hover when already set
adzialocha Aug 23, 2022
0050b46
Do not send request when field is already set
adzialocha Aug 23, 2022
5e1f28e
Frequently update game board
adzialocha Aug 23, 2022
ca7dd33
Block player if they just made a move
adzialocha Aug 23, 2022
363e576
Split up in multiple files
adzialocha Aug 23, 2022
b0395d9
Move React components into separate files
adzialocha Aug 23, 2022
003ac79
Add some comments, improve logic for blocking player
adzialocha Aug 23, 2022
e426504
Add some doc strings
adzialocha Aug 23, 2022
09c3d39
WIP detect winner
adzialocha Aug 23, 2022
97cac6a
Add some doc strings
adzialocha Aug 24, 2022
9af8c7d
Calculate win combinations once, represent them as strings
adzialocha Aug 24, 2022
da8a28b
Get current winners of board
adzialocha Aug 24, 2022
605fe60
Show winner on board
adzialocha Aug 24, 2022
dc4260f
Fix checking winning positions
adzialocha Aug 24, 2022
ddd8961
Update comment
adzialocha Aug 24, 2022
e86ae85
Show messages to the user
adzialocha Aug 24, 2022
caf50bd
Style Message component, move animal info to the bottom
adzialocha Aug 24, 2022
67d593b
Remove emojis which are not animals
adzialocha Aug 24, 2022
92caf0e
Change message on the bottom
adzialocha Aug 24, 2022
cee7e0b
Change winSize to 3, check for sane configs
adzialocha Aug 24, 2022
925924a
Upload screenshot
adzialocha Aug 24, 2022
93be9f9
Update README.md
adzialocha Aug 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Show winner on board
  • Loading branch information
adzialocha committed Aug 24, 2022
commit 605fe60dd185a7b4ac6e296e160034f18ab0f1c5
7 changes: 6 additions & 1 deletion src/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ export const Game: React.FC<Props> = ({ config }) => {
return (
<>
{fields && (
<GameBoard fields={fields} animal={animal} onSetField={onSetField} />
<GameBoard
onSetField={onSetField}
animal={animal}
fields={fields}
winners={winners}
/>
)}
</>
);
Expand Down
28 changes: 21 additions & 7 deletions src/GameBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ import styled from 'styled-components';

import { validAnimal } from './animals';

import type { Winner } from './types';

// All dimensions in pixels
const FIELD_SIZE = 60;
const GAP_SIZE = 17;
const ICON_SIZE = 28;

type Props = {
onSetField: (index: number) => void;
animal: string;
fields: string[];
onSetField: (index: number) => void;
winners: Winner[];
};

export const GameBoard: React.FC<Props> = ({ fields, onSetField, animal }) => {
export const GameBoard: React.FC<Props> = ({
onSetField,
animal,
fields,
winners,
}) => {
return (
<StyledGameBoard boardSize={Math.sqrt(fields.length)}>
{fields.map((field, index) => {
Expand All @@ -23,17 +31,23 @@ export const GameBoard: React.FC<Props> = ({ fields, onSetField, animal }) => {
// Was this field already set by this player?
const alreadySet = field === animal;

// Is this a winning field?
const winner = winners.some(({ player, combination }) => {
return field === player && combination.includes(index);
});

return (
<GameBoardField
key={`field-${fieldIndex}`}
alreadySet={alreadySet}
onClick={() => {
if (alreadySet) {
return;
}

onSetField(fieldIndex);
}}
alreadySet={alreadySet}
key={`field-${fieldIndex}`}
winner={winner}
>
{validAnimal(field) && field}
</GameBoardField>
Expand All @@ -52,9 +66,9 @@ const StyledGameBoard = styled.div<{ boardSize: number }>`
`repeat(${props.boardSize}, ${FIELD_SIZE}px)`};
`;

const GameBoardField = styled.div<{ alreadySet: boolean }>`
const GameBoardField = styled.div<{ alreadySet: boolean; winner: boolean }>`
align-content: center;
background-color: #efefef;
background-color: ${(props) => (props.winner ? '#ffdb9a' : '#efefef')};
border-radius: 50%;
cursor: ${(props) => (props.alreadySet ? 'normal' : 'pointer')};
display: inline-grid;
Expand All @@ -69,7 +83,7 @@ const GameBoardField = styled.div<{ alreadySet: boolean }>`

return `
&:hover {
background-color: #ddd;
background-color: ${props.winner ? '#ffc04d' : '#ddd'};
}
`;
}}
Expand Down
11 changes: 11 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ export type Configuration = {
/** Interval to fetch latest board game state from node in milliseconds */
updateIntervalMs: number;
};

/**
* Winning player with related combination on the board.
*/
export type Winner = {
/** Animal character of the player */
player: string;

/** Positions in the board which represent the winner */
combination: number[];
};
15 changes: 11 additions & 4 deletions src/winner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { validAnimal } from './animals';

import type { Winner } from './types';

const SEPARATOR = '|';

function horizontal(
Expand Down Expand Up @@ -123,8 +125,8 @@ export function winCombinations(boardSize: number, winSize: number): string[] {
export function detectWinner(
fields: string[],
combinations: string[],
): string[] {
const winners: string[] = [];
): Winner[] {
const winners: Winner[] = [];

// Gather all current players and their positions
const players = fields.reduce<{ [field: string]: string }>(
Expand Down Expand Up @@ -153,8 +155,13 @@ export function detectWinner(
const positions = players[player];
const combination = combinations[c];

if (positions.includes(combination) && !winners.includes(player)) {
winners.push(player);
if (positions.includes(combination)) {
winners.push({
player,
combination: combination.split(SEPARATOR).map((value) => {
return parseInt(value, 10);
}),
});
}
}
}
Expand Down