Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted coordutil.js to typescript #382

Merged
merged 4 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions src/client/scripts/esm/chess/logic/movesets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import colorutil from '../util/colorutil.js';
import type { gamefile } from './gamefile.js';
// @ts-ignore
import type { Piece } from './movepiece.js';
import type { Coords } from '../util/coordutil.js';

// TODO: move this to coordutil.js after that is converted to typescript.
type Coords = [number, number];

/**
* A Movesets object containing the movesets for every piece type in a game
Expand Down
92 changes: 0 additions & 92 deletions src/client/scripts/esm/chess/util/coordutil.js

This file was deleted.

115 changes: 115 additions & 0 deletions src/client/scripts/esm/chess/util/coordutil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

/**
* This script contains utility methods for working with coordinates [x,y].
*
* ZERO dependancies.
*/


// Type Definitions ------------------------------------------------------------


/** A length-2 array of coordinates: `[x,y]` */
type Coords = [number,number];

/**
* A pair of coordinates, represented in a string, separated by a `,`.
*
* This is often used as the key for a piece in piece lists.
*
* This is NOT compatible with "e" notation, so things will crash when
* coordinates go above Number.MAX_SAFE_INTEGER
*/
type CoordsKey = `${number},${number}`;


// Functions -------------------------------------------------------------------


/**
* Checks if both the x-coordinate and the y-coordinate of a point are integers.
*/
function areCoordsIntegers(coords: Coords): boolean {
return Number.isInteger(coords[0]) && Number.isInteger(coords[1]);
}

/**
* Returns the key string of the coordinates: [x,y] => 'x,y'
*/
function getKeyFromCoords(coords: Coords): CoordsKey {
return `${coords[0]},${coords[1]}`;
}

/**
* Returns a length-2 array of the provided coordinates
* @param key - 'x,y'
* @returns The coordinates of the piece, [x,y]
*/
function getCoordsFromKey(key: CoordsKey): Coords {
return key.split(',').map(Number) as Coords;
}

/**
* Returns true if the coordinates are equal.
*
* If one coordinate isn't provided, they are considered not equal.
*/
function areCoordsEqual(coord1?: Coords, coord2?: Coords): boolean {
if (!coord1 || !coord2) return false; // One undefined, can't be equal
return coord1[0] === coord2[0] && coord1[1] === coord2[1];
}

/**
* Returns true if the coordinates are equal
*/
function areCoordsEqual_noValidate(coord1: Coords, coord2: Coords): boolean {
return coord1[0] === coord2[0] && coord1[1] === coord2[1];
}

/**
* Adds two coordinate pairs together component-wise.
*/
function addCoordinates(coord1: Coords, coord2: Coords): Coords {
return [
coord1[0] + coord2[0],
coord1[1] + coord2[1]
];
}

/**
* Subtracts two coordinate pairs together component-wise.
* @param minuendCoord - The first coordinate pair [x1, y1] to start with.
* @param subtrahendCoord - The second coordinate pair [x2, y2] to subtract from the minuend.
* @returns The resulting coordinate pair after subtracting.
*/
function subtractCoordinates(minuendCoord: Coords, subtrahendCoord: Coords): Coords {
return [
minuendCoord[0] - subtrahendCoord[0],
minuendCoord[1] - subtrahendCoord[1]
];
}

/**
* Makes a deep copy of the provided coordinates
*/
function copyCoords(coords: Coords): Coords {
return [...coords] as Coords;
}



export default {
areCoordsIntegers,
getKeyFromCoords,
getCoordsFromKey,
areCoordsEqual,
areCoordsEqual_noValidate,
addCoordinates,
subtractCoordinates,
copyCoords
};

export type {
Coords,
CoordsKey,
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/* eslint-disable max-depth */
// @ts-ignore
import formatconverter from "../logic/formatconverter.js";
// @ts-ignore
import coordutil from "../util/coordutil.js";

// Type definitions...

import type { Movesets } from "../logic/movesets.js";
import type { Position } from "./variant.js";
import type { Coords, CoordsKey } from "../util/coordutil.js";



'use strict';

const BOARDS_X = 8;
const BOARDS_Y = 8;

Expand All @@ -31,8 +29,7 @@ function genPositionOfFiveDimensional() {
const standardPosStr = 'P1,2+|P2,2+|P3,2+|P4,2+|P5,2+|P6,2+|P7,2+|P8,2+|p1,7+|p2,7+|p3,7+|p4,7+|p5,7+|p6,7+|p7,7+|p8,7+|R1,1+|R8,1+|r1,8+|r8,8+|N2,1|N7,1|n2,8|n7,8|B3,1|B6,1|b3,8|b6,8|Q4,1|q4,8|K5,1+|k5,8+';

// Store the standard position so we can reference it later
const standardPos = formatconverter.ShortToLong_Format(standardPosStr).startingPosition;

const standardPos: { [coordsKey: CoordsKey]: string } = formatconverter.ShortToLong_Format(standardPosStr).startingPosition;
const resultPos: Position = {};

// Loop through from the leftmost column that should be voids to the right most, and also vertically
Expand All @@ -46,9 +43,11 @@ function genPositionOfFiveDimensional() {
if ((i % BOARD_SPACING === 0) && (j % BOARD_SPACING) === 0
&& i !== MAX_X && j !== MAX_Y) {
for (const square in standardPos) {
resultPos[coordutil.getKeyFromCoords(coordutil.getCoordsFromKey(square).map((value: number, index: number) => {
const coords = coordutil.getCoordsFromKey(square as CoordsKey);
const key = coordutil.getKeyFromCoords(coords.map((value: number, index: number) => {
return value + [i, j][index]!;
}))] = standardPos[square];
}) as Coords);
resultPos[key] = standardPos[square as CoordsKey]!;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import { BufferModel, createModel } from '../buffermodel.js';

// @ts-ignore
import type gamefile from '../../../chess/logic/gamefile.js';

// TO DO: MOVE TO coordutil.ts ONCE THAT'S CONVERTED TO TS
type Coords = [number,number];
import type { Coords } from '../../../chess/util/coordutil.js';


// Functions -----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import board from '../board.js';
// @ts-ignore
import math, { BoundingBox } from '../../../util/math.js';
// @ts-ignore
import coordutil from '../../../chess/util/coordutil.js';
// @ts-ignore
import frametracker from '../frametracker.js';
// @ts-ignore
import preferences from '../../../components/header/preferences.js';
Expand All @@ -34,6 +32,7 @@ import legalmoveshapes from '../legalmoveshapes.js';
// @ts-ignore
import shapes from '../shapes.js';
import { BufferModel, BufferModelInstanced, createModel, createModel_Instanced } from '../buffermodel.js';
import coordutil from '../../../chess/util/coordutil.js';


// Type Definitions -----------------------------------------------------------------------------
Expand All @@ -47,9 +46,8 @@ import type { LegalMoves } from '../../chess/selection.js';
import type { Piece } from '../../../chess/logic/movepiece.js';
// @ts-ignore
import game from '../../chess/game.js';
import { Coords, CoordsKey } from '../../../chess/util/coordutil.js';

// TO DO: MOVE TO coordutil.ts ONCE THAT'S CONVERTED TO TS
type Coords = [number,number];
// TO DO: MOVE TO colorutil.ts ONCE THAT'S CONVERTED TO TS
type Color = [number,number,number,number];

Expand Down Expand Up @@ -430,7 +428,7 @@ function concatData_HighlightedMoves_Sliding(instanceData_NonCapture: number[],
const slideLines = Object.keys(legalMoves.sliding); // ['1,0','1,1', ...]

for (const lineKey of slideLines) { // '1,0'
const line: Coords = coordutil.getCoordsFromKey(lineKey); // [dx,dy]
const line: Coords = coordutil.getCoordsFromKey(lineKey as CoordsKey); // [dx,dy]
const C = organizedlines.getCFromLine(line, coords);

const corner1 = math.getAABBCornerOfLine(line, true); // "right"
Expand Down
4 changes: 1 addition & 3 deletions src/client/scripts/esm/game/rendering/legalmoveshapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import board from "./board.js";
import shapes from "./shapes.js";



type Coords = [number,number];

import type { Coords } from "../../chess/util/coordutil.js";


// Variables ------------------------------------------------------------------------------
Expand Down
Loading