Skip to content

Commit

Permalink
[#8] Changed Position.position to Position.coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Oct 21, 2023
1 parent 88b1b8d commit 943ca59
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 99 deletions.
14 changes: 7 additions & 7 deletions development/src/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class ChessBoard implements ChessBoardType {
pieces.forEach((piece) => {
const pieceElement = this.createPieceElement(piece);
const square = document.querySelectorAll(
`[square-id="${piece.position.position}"]`,
`[square-id="${piece.position.coordinates}"]`,
)[0];
square.appendChild(pieceElement);
});
Expand Down Expand Up @@ -132,21 +132,21 @@ export class ChessBoard implements ChessBoardType {

movePieceOnBoard(draggedPiece: Piece, targetSquare: Square) {
const draggedPieceSquareElement = this.boardElement.querySelector(
`[square-id="${draggedPiece.position.position.join(',')}"]`,
`[square-id="${draggedPiece.position.coordinates.join(',')}"]`,
) as HTMLElement;
const draggedPieceElement =
draggedPieceSquareElement?.firstElementChild as HTMLElement;

const targetSquareElement = this.boardElement.querySelector(
`[square-id="${targetSquare.position.position.join(',')}"]`,
`[square-id="${targetSquare.position.coordinates.join(',')}"]`,
) as HTMLElement;

targetSquareElement.appendChild(draggedPieceElement);
}

destroyPieceOnBoard(targetPiece: Piece) {
const targetPieceSquareElement = this.boardElement.querySelector(
`[square-id="${targetPiece.position.position.join(',')}"]`,
`[square-id="${targetPiece.position.coordinates.join(',')}"]`,
);
const targetPieceElement =
targetPieceSquareElement?.firstElementChild as HTMLElement;
Expand All @@ -156,7 +156,7 @@ export class ChessBoard implements ChessBoardType {

destroyItemOnBoard(targetItem: Item) {
const targetItemSquareElement = this.boardElement.querySelector(
`[square-id="${targetItem.position.position!.join(',')}"]`,
`[square-id="${targetItem.position.coordinates!.join(',')}"]`,
);
const targetItemElement =
targetItemSquareElement?.firstElementChild as HTMLElement;
Expand All @@ -167,15 +167,15 @@ export class ChessBoard implements ChessBoardType {
spawnPieceOnBoard(piece: Piece) {
const pieceElement = this.createPieceElement(piece);
const square = this.boardElement.querySelectorAll(
`[square-id="${piece.position.position}"]`,
`[square-id="${piece.position.coordinates}"]`,
)[0];
square.appendChild(pieceElement);
}

spawnItemOnBoard(item: Item) {
const itemElement = this.createItemElement(item);
const square = this.boardElement.querySelectorAll(
`[square-id="${item.position.position}"]`,
`[square-id="${item.position.coordinates}"]`,
)[0];
square.appendChild(itemElement);
}
Expand Down
2 changes: 1 addition & 1 deletion development/src/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Trap extends Item {
}

apply(piece: Piece) {
Logger.log(`${this.player.color} ${piece.name} placed a ${this.name} on ${piece.position.position}.`);
Logger.log(`${this.player.color} ${piece.name} placed a ${this.name} on ${piece.position.coordinates}.`);

this.position = piece.position;
items.push(this);
Expand Down
4 changes: 2 additions & 2 deletions development/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export class Logger {
}

static logMovement(draggedPiece: Piece, targetSquare: Square) {
const fromNotation = this.convertPositionToNotation(draggedPiece.position.position);
const toNotation = this.convertPositionToNotation(targetSquare.position.position);
const fromNotation = this.convertPositionToNotation(draggedPiece.position.coordinates);
const toNotation = this.convertPositionToNotation(targetSquare.position.coordinates);
this.log(`${draggedPiece.player.color} ${draggedPiece.name} moved from ${fromNotation} to ${toNotation}.`);
}

Expand Down
96 changes: 48 additions & 48 deletions development/src/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,38 @@ const whitePlayer = new Player('White');
const blackPlayer = new Player('Black');
export const players = [whitePlayer, blackPlayer];
export let pieces = [
new Rook({ position: [0, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Knight({ position: [1, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Bishop({ position: [2, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Queen({ position: [3, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new King({ position: [4, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Bishop({ position: [5, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Knight({ position: [6, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Rook({ position: [7, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [0, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [1, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [2, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [3, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [4, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [5, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [6, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [7, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ position: [0, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ position: [1, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ position: [2, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ position: [3, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ position: [4, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ position: [5, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ position: [6, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ position: [7, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Rook({ position: [0, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Knight({ position: [1, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Bishop({ position: [2, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Queen({ position: [3, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new King({ position: [4, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Bishop({ position: [5, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Knight({ position: [6, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Rook({ position: [7, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Rook({ coordinates: [0, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Knight({ coordinates: [1, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Bishop({ coordinates: [2, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Queen({ coordinates: [3, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new King({ coordinates: [4, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Bishop({ coordinates: [5, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Knight({ coordinates: [6, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Rook({ coordinates: [7, 0], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [0, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [1, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [2, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [3, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [4, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [5, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [6, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [7, 1], board: OVERWORLD_BOARD_ID }, players[1]),
new Pawn({ coordinates: [0, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ coordinates: [1, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ coordinates: [2, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ coordinates: [3, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ coordinates: [4, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ coordinates: [5, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ coordinates: [6, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Pawn({ coordinates: [7, 6], board: OVERWORLD_BOARD_ID }, players[0]),
new Rook({ coordinates: [0, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Knight({ coordinates: [1, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Bishop({ coordinates: [2, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Queen({ coordinates: [3, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new King({ coordinates: [4, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Bishop({ coordinates: [5, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Knight({ coordinates: [6, 7], board: OVERWORLD_BOARD_ID }, players[0]),
new Rook({ coordinates: [7, 7], board: OVERWORLD_BOARD_ID }, players[0]),
];

export let items: Array<Item> = [];
Expand All @@ -84,8 +84,8 @@ export function comparePositions(
secondPosition: Position,
): boolean {
return (
firstPosition.position[0] === secondPosition.position[0] &&
firstPosition.position[1] === secondPosition.position[1]
firstPosition.coordinates[0] === secondPosition.coordinates[0] &&
firstPosition.coordinates[1] === secondPosition.coordinates[1]
);
}

Expand All @@ -94,8 +94,8 @@ export function comparePositionsAndBoards(
secondPosition: Position,
): boolean {
const arePositionsEqual =
firstPosition.position[0] === secondPosition.position[0] &&
firstPosition.position[1] === secondPosition.position[1];
firstPosition.coordinates[0] === secondPosition.coordinates[0] &&
firstPosition.coordinates[1] === secondPosition.coordinates[1];
const areBoardsEqual = firstPosition.board === secondPosition.board;

return areBoardsEqual && arePositionsEqual;
Expand Down Expand Up @@ -132,7 +132,7 @@ export function onAction(
draggedElement.parentElement as HTMLElement;

const draggedElementPosition: Position = {
position: convertSquareIdToPosition(draggedElementParentElement.getAttribute('square-id')!),
coordinates: convertSquareIdToPosition(draggedElementParentElement.getAttribute('square-id')!),
board: board,
};

Expand All @@ -147,7 +147,7 @@ export function onAction(
const targetPiece: Piece | undefined = pieces.find((piece) => {
const squareElement = targetElement.parentElement!;
const targetElementPosition: Position = {
position: convertSquareIdToPosition(
coordinates: convertSquareIdToPosition(
squareElement.getAttribute('square-id')!,
),
board: board,
Expand All @@ -167,7 +167,7 @@ export function onAction(
}

const itemPosition: Position = {
position: convertSquareIdToPosition(
coordinates: convertSquareIdToPosition(
squareElement.getAttribute('square-id')!,
),
board: board,
Expand All @@ -181,7 +181,7 @@ export function onAction(
} else {
const targetSquare: Square = {
position: {
position: convertSquareIdToPosition(
coordinates: convertSquareIdToPosition(
targetElement.getAttribute('square-id')!,
),
board: board,
Expand All @@ -195,7 +195,7 @@ export function onFallOffTheBoard(draggedElement: HTMLElement, board: string) {
const draggedPiece: Piece | undefined = pieces.find((piece) => {
const squareElement = draggedElement.parentElement!;
const draggedElementPosition: Position = {
position: convertSquareIdToPosition(
coordinates: convertSquareIdToPosition(
squareElement.getAttribute('square-id')!,
),
board: board,
Expand Down Expand Up @@ -327,22 +327,22 @@ function castle(kingPiece: Piece, targetSquare: Square) {
);
});

const deltaX = targetSquare.position.position[0] - kingPiece.position.position[0];
const deltaX = targetSquare.position.coordinates[0] - kingPiece.position.coordinates[0];
// Depends on if it's Kingside or Queenside castling
const isKingsideCastling = deltaX > 0;
const rookFilter = (piece: Piece) =>
isKingsideCastling
? piece.position.position[0] > kingPiece.position.position[0]
: piece.position.position[0] < kingPiece.position.position[0];
? piece.position.coordinates[0] > kingPiece.position.coordinates[0]
: piece.position.coordinates[0] < kingPiece.position.coordinates[0];
const rookPiece = possibleRooks.find(rookFilter);
if (!rookPiece) return false;

const rookPieceTargetPosition: Position = {
position: [
coordinates: [
isKingsideCastling
? targetSquare.position.position[0] - 1
: targetSquare.position.position[0] + 1,
kingPiece.position.position[1],
? targetSquare.position.coordinates[0] - 1
: targetSquare.position.coordinates[0] + 1,
kingPiece.position.coordinates[1],
],
board: rookPiece.position.board,
};
Expand Down
Loading

0 comments on commit 943ca59

Please sign in to comment.