Last active
November 5, 2025 17:45
-
-
Save mdflores/b1c9a47d5f2a8b41789a2ae77d24e328 to your computer and use it in GitHub Desktop.
Linkedlist Game of Knights - Battle Royal of 1_000_000 linked knights and will try to eliminated their targets
This file contains hidden or 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
| class Knight { | |
| name: string | |
| hp: number | |
| target?: Knight | null | |
| constructor(name: string, hp: number, target?: Knight) { | |
| this.name = name; | |
| this.hp = hp; | |
| this.target = target | |
| } | |
| private getDamage() { | |
| return Math.floor(Math.random() * (maxDamage - minDamage)) + minDamage; | |
| } | |
| attack(): Knight | null { | |
| if (!this.target) { | |
| console.log(`Assign my target!!!`) | |
| return null | |
| } | |
| const damage = this.getDamage() | |
| this.target.takeDamage(damage) | |
| console.log(`${this.name} attacks ${this.target.name} of ${damage} HP: ${this.target.hp}`) | |
| if (!this.target.isDefeated()) { | |
| return null | |
| } | |
| return this.target | |
| } | |
| takeDamage(damage: number) { | |
| this.hp -= damage | |
| } | |
| isDefeated() { | |
| return this.hp <= 0 | |
| } | |
| } | |
| const maxDamage = 6 | |
| const minDamage = 1 | |
| class CirleOfKnights { | |
| numberOfPlayers: number | |
| maxHP: number | |
| players: Knight[] | |
| roundCounter: number = 1 | |
| constructor(numberOfPlayers: number, maxHP: number) { | |
| this.numberOfPlayers = numberOfPlayers | |
| this.maxHP = maxHP | |
| this.players = [] | |
| } | |
| initializePlayers() { | |
| if (this.players.length > 0) { | |
| console.log('Players initialized!!! call execute()') | |
| return | |
| } | |
| for (let x = 1; x <= this.numberOfPlayers; x++) { | |
| const player = new Knight(`Knight_${x}`, this.maxHP) | |
| this.players.push(player) | |
| console.log(`${player.name} have arrived in the circle`) | |
| console.log("\n===============\n===============") | |
| } | |
| this.linkTheCircle() | |
| } | |
| private linkTheCircle() { | |
| if (this.players.length == 1) { | |
| return | |
| } | |
| console.log("\n+++++++++++\n+++++++++++") | |
| console.log('Maintaining the circle to be linked') | |
| console.log("+++++++++++\n+++++++++++\n") | |
| for (let x = 0; x < this.players.length; x++) { | |
| const player = this.players[x] | |
| const targetIndex = (x + 1) % this.players.length; | |
| const target = this.players[targetIndex]; | |
| console.log(`Assigning ${player.name} target to ${target.name}`) | |
| player.target = target | |
| } | |
| console.log("\n+++++++++++\n+++++++++++") | |
| console.log('Circle Linking DONE!') | |
| console.log("+++++++++++\n+++++++++++\n") | |
| } | |
| private playerAttacks() { | |
| console.log(`_+_+_+_+_+_+\n\nEntering round ${this.roundCounter}\n\n_+_+_+_+_+_+`) | |
| for (let i = 0; i < this.players.length; i++) { | |
| let currentPlayer = this.players[i] | |
| if (currentPlayer.isDefeated()) { | |
| continue | |
| } | |
| let eliminatedPlayer = currentPlayer.attack() | |
| if (eliminatedPlayer) { | |
| console.log(`--------------\n\t${eliminatedPlayer.name} Eliminated!!!. \n--------------`) | |
| this.players = this.players.filter( | |
| knight => knight.name != eliminatedPlayer.name | |
| ) | |
| this.linkTheCircle() | |
| console.log(`!!!!!!!!\n\t${this.players.length} remaining players!!!. \n--------------`) | |
| } | |
| } | |
| this.roundCounter++ | |
| } | |
| execute() { | |
| if (this.players.length == 0) { | |
| console.log('There are no players in the circle - call initializePlayers()') | |
| return | |
| } | |
| while (this.players.length > 1) { | |
| this.playerAttacks() | |
| } | |
| const winner = this.players[0] | |
| console.log(`****************\n\t${winner.name} have conquered them all. After ${this.roundCounter} rounds\n******************`) | |
| } | |
| } | |
| /////// Usage ////// | |
| let circle = new CirleOfKnights(1_000_000, 6); | |
| circle.initializePlayers() | |
| circle.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment