type Card = {
name: string;
manaCost: number;
attack: number;
health: number;
ability?: (target: Card | Player) => void;
};
type Player = {
name: string;
health: number;
mana: number;
hand: Card[];
board: Card[];
};
class AIPlayer {
player: Player;
opponent: Player;
constructor(player: Player, opponent: Player) {
this.player = player;
this.opponent = opponent;
}
playTurn() {
console.log(`AI (${this.player.name}) is thinking...`);
// Replenish mana at start of turn (assuming max mana is 10)
this.player.mana = Math.min(10, this.player.mana + 1);
// Simple logic to play cards
this.playCards();
// Simple logic for attacking
this.attackOpponent();
console.log(`AI (${this.player.name}) has ended its turn.`);
}
playCards() {
for (let card of this.player.hand) {
if (card.manaCost <= this.player.mana) {
console.log(`AI plays ${card.name} from hand.`);
this.player.board.push(card);
this.player.mana -= card.manaCost;
}
}
// Remove played cards from hand
this.player.hand = this.player.hand.filter(card => !this.player.board.includes(card));
}
attackOpponent() {
for (let boardCard of this.player.board) {
if (this.opponent.board.length > 0) {
const targetCard = this.opponent.board[0]; // Simple target selection - first card
console.log(`AI attacks ${targetCard.name} with ${boardCard.name}.`);
this.resolveCombat(boardCard, targetCard);
} else {
console.log(`AI attacks opponent (${this.opponent.name}) directly with ${boardCard.name}.`);
this.opponent.health -= boardCard.attack;
}
}
}
resolveCombat(attacker: Card, defender: Card) {
defender.health -= attacker.attack;
if (defender.health <= 0) {
console.log(`${defender.name} is destroyed.`);
this.opponent.board = this.opponent.board.filter(card => card !== defender);
}
// Optionally resolve counterattack if needed:
attacker.health -= defender.attack;
if (attacker.health <= 0) {
console.log(`${attacker.name} is destroyed.`);
this.player.board = this.player.board.filter(card => card !== attacker);
}
}
}
// Example setup
const playerOne: Player = {
name: 'AI Player One',
health: 30,
mana: 0,
hand: [{ name: 'Wisp', manaCost: 0, attack: 1, health: 1 }],
board: []
};
const playerTwo: Player = {
name: 'Human Player',
health: 30,
mana: 0,
hand: [],
board: []
};
// Create AI
const aiPlayer = new AIPlayer(playerOne, playerTwo);
// Simulate AI Turn
aiPlayer.playTurn();