-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from eeoooue/promotion
Implemented Promotion
- Loading branch information
Showing
9 changed files
with
245 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
export class PromotionCard { | ||
constructor(parent, game) { | ||
this.game = game; | ||
this.parent = parent; | ||
this.element = this.deriveEndCard(this.game); | ||
} | ||
deriveEndCard(game) { | ||
const position = this.game.getPromotingPawnPosition(); | ||
const piece = this.game.boardState[position.i][position.j]; | ||
const colour = (piece.colour == "b") ? "black" : "white"; | ||
const bigText = `Promoting ${colour} pawn.`; | ||
const subText = "Which piece should your pawn become?"; | ||
return this.createEndCard(bigText, subText); | ||
} | ||
createEndCard(bigText, subText) { | ||
const card = document.createElement("div"); | ||
card.classList.add("end-card"); | ||
const header1 = document.createElement("h2"); | ||
header1.innerText = bigText; | ||
card.appendChild(header1); | ||
const header2 = document.createElement("h3"); | ||
header2.innerText = subText; | ||
card.appendChild(header2); | ||
const options = []; | ||
options.push("Bishop"); | ||
options.push("Knight"); | ||
options.push("Rook"); | ||
options.push("Queen"); | ||
for (let i = 0; i < 4; i++) { | ||
const optionText = options[i]; | ||
const optionBtn = document.createElement("button"); | ||
optionBtn.innerText = optionText; | ||
optionBtn.addEventListener("click", () => { | ||
this.submitChoice(optionText); | ||
}); | ||
card.appendChild(optionBtn); | ||
} | ||
return card; | ||
} | ||
submitChoice(text) { | ||
this.game.submitPromotionChoice(text); | ||
this.element.remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,6 @@ | |
</div> | ||
</div> | ||
|
||
|
||
|
||
<script type="module" src="./main.js"></script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
import { WebChessGame } from "./webchessgame.js"; | ||
import { ChessGame } from "./chessgame.js"; | ||
import { BoardPosition } from "./BoardPosition.js"; | ||
import { Piece } from "./piece.js"; | ||
|
||
export class PromotionCard { | ||
|
||
public parent: WebChessGame; | ||
public game: ChessGame; | ||
public element: HTMLDivElement; | ||
|
||
constructor(parent: WebChessGame, game: ChessGame) { | ||
|
||
this.game = game; | ||
this.parent = parent; | ||
this.element = this.deriveEndCard(this.game); | ||
} | ||
|
||
deriveEndCard(game: ChessGame): HTMLDivElement { | ||
|
||
const position: BoardPosition = this.game.getPromotingPawnPosition(); | ||
|
||
const piece: Piece = this.game.boardState[position.i][position.j]; | ||
|
||
const colour: string = (piece.colour == "b") ? "black" : "white"; | ||
|
||
const bigText = `Promoting ${colour} pawn.` | ||
const subText = "Which piece should your pawn become?" | ||
|
||
return this.createEndCard(bigText, subText); | ||
} | ||
|
||
createEndCard(bigText: string, subText: string): HTMLDivElement { | ||
|
||
const card = document.createElement("div"); | ||
card.classList.add("end-card"); | ||
|
||
const header1 = document.createElement("h2"); | ||
header1.innerText = bigText; | ||
card.appendChild(header1); | ||
|
||
const header2 = document.createElement("h3"); | ||
header2.innerText = subText; | ||
card.appendChild(header2); | ||
|
||
const options: string[] = []; | ||
options.push("Bishop"); | ||
options.push("Knight"); | ||
options.push("Rook"); | ||
options.push("Queen"); | ||
|
||
for(let i = 0; i<4; i++){ | ||
|
||
const optionText = options[i]; | ||
const optionBtn = document.createElement("button"); | ||
optionBtn.innerText = optionText; | ||
|
||
optionBtn.addEventListener("click", () => { | ||
this.submitChoice(optionText); | ||
}) | ||
|
||
card.appendChild(optionBtn); | ||
} | ||
|
||
return card; | ||
} | ||
|
||
submitChoice(text: string){ | ||
|
||
this.game.submitPromotionChoice(text); | ||
this.element.remove(); | ||
} | ||
} |
Oops, something went wrong.