Skip to content

Commit

Permalink
Merge pull request #29 from eeoooue/repo-presentation
Browse files Browse the repository at this point in the history
Repo Presentation
  • Loading branch information
eeoooue committed Jun 8, 2023
2 parents 70e1e76 + 439be04 commit c8afb96
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 48 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# [chessboard](https://eeoooue.github.io/chessboard/)

moveable chess pieces on a chess board

attempt to mimic [chess.com](http://chess.com) UI, using [Cburnett's chess pieces](https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces)

# [Chess](https://eeoooue.github.io/chessboard/)

A plain implementation of Chess in TypeScript, based on the [chess.com](http://chess.com) UI, using [Cburnett's chess pieces](https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces).

![Chess Board](chessboard.png)
Binary file added chessboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions docs/board_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ export class BoardElement {
this.parent = parent;
this.boardContainer = boardContainer;
this.game = game;
this.game.attach(this);
this.paintTiles();
this.paintPieces(this.game.getPieces());
}
update(subject) {
repaint() {
this.clearPreviousBoard();
this.paintPieces(this.game.getPieces());
}
Expand Down
7 changes: 5 additions & 2 deletions docs/chess_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export class ChessGame {
this.moveTracker = new MoveTracker(this);
this.boardBuilder = new BoardBuilder(this);
this.state = "ongoing";
this.notify();
this.updateState();
}
submitPromotionChoice(choice) {
const pawn = this.getPromotingPawn();
pawn.promoteTo(choice);
this.state = "ongoing";
this.notify();
this.updateState();
}
getPromotingPawn() {
const i = (this.getTurnPlayer() == "b") ? 0 : 7;
Expand Down Expand Up @@ -83,6 +83,9 @@ export class ChessGame {
}
concludeTurn() {
this.turncount += 1;
this.updateState();
}
updateState() {
this.resetThreats();
this.notify();
this.checkGameOver();
Expand Down
1 change: 1 addition & 0 deletions docs/promotion_card.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ export class PromotionCard {
submitChoice(text) {
this.game.submitPromotionChoice(text);
this.element.remove();
this.parent.refresh();
}
}
32 changes: 17 additions & 15 deletions docs/web_chess_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ export class WebChessGame {
}
processMove(i, j) {
const move = new BoardPosition(i, j);
if (move) {
this.game.submitSelection(move);
if (this.game.moveTracker.active) {
const piece = this.game.boardState[move.i][move.j];
const tile = this.boardElement.grid[move.i][move.j];
tile.classList.add("highlighted");
const options = piece.possibleMoves;
this.boardElement.paintMoveOptions(options);
}
if (this.game.state == "checkmate" || this.game.state == "stalemate") {
this.showEndCard();
}
if (this.game.state == "promotion") {
this.askPromotionOption();
}
this.game.submitSelection(move);
this.refresh();
if (this.game.moveTracker.active) {
const piece = this.game.boardState[move.i][move.j];
const tile = this.boardElement.grid[move.i][move.j];
tile.classList.add("highlighted");
const options = piece.possibleMoves;
this.boardElement.paintMoveOptions(options);
}
}
refresh() {
this.boardElement.repaint();
if (this.game.state == "checkmate" || this.game.state == "stalemate") {
this.showEndCard();
}
if (this.game.state == "promotion") {
this.askPromotionOption();
}
}
askPromotionOption() {
Expand Down
6 changes: 2 additions & 4 deletions src/board_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { BoardPosition } from "./board_position.js";
import { Piece } from "./piece.js";
import { EmptyPiece } from "./pieces/empty_piece.js";
import { King } from "./pieces/king.js";
import { Observer } from "./observer.js";
import { Subject } from "./subject.js";

export class BoardElement implements Observer {
export class BoardElement {

public boardContainer: HTMLElement;
public grid: HTMLElement[][] = [];
Expand All @@ -20,13 +19,12 @@ export class BoardElement implements Observer {
this.parent = parent;
this.boardContainer = boardContainer;
this.game = game;
this.game.attach(this);

this.paintTiles()
this.paintPieces(this.game.getPieces())
}

update(subject: Subject): void {
repaint(): void {

this.clearPreviousBoard()
this.paintPieces(this.game.getPieces())
Expand Down
9 changes: 7 additions & 2 deletions src/chess_game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ChessGame implements Subject {
this.moveTracker = new MoveTracker(this);
this.boardBuilder = new BoardBuilder(this);
this.state = "ongoing";
this.notify();
this.updateState();
}

submitPromotionChoice(choice: string) {
Expand All @@ -33,7 +33,7 @@ export class ChessGame implements Subject {
pawn.promoteTo(choice);

this.state = "ongoing";
this.notify();
this.updateState();
}

getPromotingPawn(): Pawn {
Expand Down Expand Up @@ -123,6 +123,11 @@ export class ChessGame implements Subject {
concludeTurn(): void {

this.turncount += 1;
this.updateState();
}

updateState(): void {

this.resetThreats();
this.notify();
this.checkGameOver();
Expand Down
1 change: 1 addition & 0 deletions src/promotion_card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ export class PromotionCard {

this.game.submitPromotionChoice(text);
this.element.remove();
this.parent.refresh();
}
}
39 changes: 22 additions & 17 deletions src/web_chess_game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,28 @@ export class WebChessGame {

const move: BoardPosition = new BoardPosition(i, j);

if (move) {
this.game.submitSelection(move);
if (this.game.moveTracker.active){
const piece : Piece = this.game.boardState[move.i][move.j]
const tile = this.boardElement.grid[move.i][move.j]
tile.classList.add("highlighted")
const options: BoardPosition[] = piece.possibleMoves;
this.boardElement.paintMoveOptions(options);
}

if (this.game.state == "checkmate" || this.game.state == "stalemate"){
this.showEndCard();
}

if (this.game.state == "promotion"){
this.askPromotionOption();
}
this.game.submitSelection(move);
this.refresh();

if (this.game.moveTracker.active){
const piece : Piece = this.game.boardState[move.i][move.j]
const tile = this.boardElement.grid[move.i][move.j]
tile.classList.add("highlighted")
const options: BoardPosition[] = piece.possibleMoves;
this.boardElement.paintMoveOptions(options);
}
}

refresh(): void {

this.boardElement.repaint();

if (this.game.state == "checkmate" || this.game.state == "stalemate"){
this.showEndCard();
}

if (this.game.state == "promotion"){
this.askPromotionOption();
}
}

Expand Down

0 comments on commit c8afb96

Please sign in to comment.