Skip to content

Commit

Permalink
quality of life
Browse files Browse the repository at this point in the history
  • Loading branch information
filipopo committed May 12, 2024
1 parent 9c12e3b commit 8476454
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
29 changes: 16 additions & 13 deletions src/components/catan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { Resource } from '../lib/catan_hex'
import CatanField from '../lib/catan_field'
import './catan.css'

function CatanBoard({catan}: {catan: Catan}) {
interface CatanBoardProps {
catan: Catan
}

function CatanBoard({catan}: CatanBoardProps) {
const resColor: {[k in Resource]: string} = {
'wood': '#7B863D',
'clay': '#F6A173',
Expand All @@ -16,24 +20,23 @@ function CatanBoard({catan}: {catan: Catan}) {

const wtf = (catan.field.midRow - 1) * Math.sqrt(3) * -10
const layout = new Layout(Layout.pointy, new Point(20, 20), new Point(wtf, 20))

const w = Math.sqrt(3) * 20 * catan.field.board[catan.field.midRow].length
const h = 30 * catan.field.board.length + 10

return (
<>
<svg width={w} height={h} xmlns="http://www.w3.org/2000/svg">
{catan.field.board.map(row => (
row.map(hex => {
const p = layout.polygonCorners(hex)
const t = layout.hexToPixel(hex)
return (
<g>
<polygon fill={resColor[hex.resource]} points={p} />
<text fill={[6, 8].includes(hex.number) ? 'red' : 'white'} x={t.x} y={t.y + 10}>{hex.number}</text>
</g>
)
})
))}
{catan.field.hexes.map(hex => {
const p = layout.polygonCorners(hex)
const t = layout.hexToPixel(hex)
return (
<g>
<polygon fill={resColor[hex.resource]} points={p} />
<text fill={[6, 8].includes(hex.number) ? 'red' : 'white'} x={t.x} y={t.y + 10}>{hex.number}</text>
</g>
)
})}
</svg>
<br/>
Top buildable spots:
Expand Down
47 changes: 24 additions & 23 deletions src/lib/catan_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ class CatanField extends HexField {
public pointA: number[],
public deserts: number[][],
public board: Array<CatanHex>[],
resources: Resource[])
{
resources: Resource[]
) {
super(board)

const a = pointA.join(' ')
if (!this.corners().includes(a))
pointA = [0, 0]

this.hexes = this.inwardSpiral(this.board[pointA[0]][pointA[1]], this.corners().indexOf(a)) as CatanHex[]
const boardWidth = this.board[this.midRow].length

if (!this.pointMapping[boardWidth])
return

if (!this.corners().includes(pointA.join(' ')))
pointA = [0, 0]

const hexes = this.inwardSpiral(this.board[pointA[0]][pointA[1]], this.corners().indexOf(pointA.join(' ')))
let num = 0

for (let hex of hexes as CatanHex[]) {
for (const hex of this.hexes) {
if (deserts.every(desert => (
desert.some((e, i) => e !== this.cordsToIndex(hex.cords)[i])
))) {
Expand All @@ -36,7 +37,7 @@ class CatanField extends HexField {
const board: Array<CatanHex>[] = []

if (boardWidth > 6 && boardWidth % 2 === 1) {
for (let row of HexField.makeBoard(Math.floor(boardWidth / 2)))
for (const row of HexField.makeBoard(Math.floor(boardWidth / 2)))
board.push(row.map(hex => new CatanHex(...hex.cords)))
return board
}
Expand All @@ -52,6 +53,8 @@ class CatanField extends HexField {
return board
}

public hexes

public randomise() {
const resourceAmount: {[k: number]: {[k in Resource]?: number}} = {
5: {'wood': 4, 'clay': 3, 'wheat': 4, 'sheep': 4, 'stone': 3},
Expand Down Expand Up @@ -93,29 +96,27 @@ class CatanField extends HexField {
public buildSpots() {
let buildHexes: number[][] | Set<string> = new Set<string>()

for (let row of this.board) {
for (let hex of row) {
for (let direction = 0; direction < 6; direction++) {
const combination = [hex.number]
for (const hex of this.hexes) {
for (let direction = 0; direction < 6; direction++) {
const combination = [hex.number]

for (let intersection of [direction, direction + 1]) {
const hexn = this.getHex(...hex.neighbor(intersection))
if (hexn) combination.push((hexn as CatanHex).number)
}
for (const intersection of [direction, direction + 1]) {
const hexn = this.getHex(...hex.neighbor(intersection))
if (hexn) combination.push((hexn as CatanHex).number)
}

combination.sort((a, b) => a - b)
const hashedCombination = combination.join(' ')
combination.sort((a, b) => a - b)
const hashedCombination = combination.join(' ')

if (!buildHexes.has(hashedCombination))
buildHexes.add(hashedCombination)
}
if (!buildHexes.has(hashedCombination))
buildHexes.add(hashedCombination)
}
}

buildHexes = Array.from(buildHexes).map(cords => cords.split(' ').map(e => Number(e)))
const buildSpots: [string, number][] = []

for (let combination of buildHexes) {
for (const combination of buildHexes) {
let key = ''
const value = +(combination.reduce((acc, cur) => {
key += `${cur} `
Expand Down

0 comments on commit 8476454

Please sign in to comment.