Fix AI multiple moves and player colors
This commit is contained in:
@@ -14,7 +14,8 @@ export class AIBot {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute AI turn - makes ONE move then ends turn
|
||||
* Execute AI turn - makes MULTIPLE moves until no valid moves remain, then ends turn
|
||||
* According to game rules, ANY cell with strength > 1 can move if it has valid targets
|
||||
*/
|
||||
async playTurn() {
|
||||
console.log(`[AI-BOT P${this.playerId}] === Turn started ===`);
|
||||
@@ -22,7 +23,7 @@ export class AIBot {
|
||||
|
||||
try {
|
||||
// Get all player cells
|
||||
const playerCells = this.map.getPlayerCells(this.playerId);
|
||||
let playerCells = this.map.getPlayerCells(this.playerId);
|
||||
|
||||
console.log(`[AI-BOT P${this.playerId}] Has ${playerCells.length} cells`);
|
||||
|
||||
@@ -33,40 +34,79 @@ export class AIBot {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find all possible moves
|
||||
const moves = this.findPossibleMoves(playerCells);
|
||||
let moveCount = 0;
|
||||
let consecutiveNoMoves = 0;
|
||||
const maxConsecutiveNoMoves = 3; // Prevent infinite loops
|
||||
const maxMovesPerTurn = 50; // Maximum moves per turn to prevent infinite loops in tests
|
||||
|
||||
console.log(`[AI-BOT P${this.playerId}] Found ${moves.length} possible moves`);
|
||||
// Loop: keep finding and executing moves until no more valid moves exist
|
||||
while (consecutiveNoMoves < maxConsecutiveNoMoves && moveCount < maxMovesPerTurn) {
|
||||
// Re-fetch player cells each iteration (board state changes)
|
||||
playerCells = this.map.getPlayerCells(this.playerId);
|
||||
|
||||
if (moves.length === 0) {
|
||||
// No moves available, end turn
|
||||
console.log(`[AI-BOT P${this.playerId}] No valid moves, ending turn`);
|
||||
if (playerCells.length === 0) {
|
||||
console.log(`[AI-BOT P${this.playerId}] No cells remaining, ending turn`);
|
||||
break;
|
||||
}
|
||||
|
||||
// Find all possible moves from current board state
|
||||
const moves = this.findPossibleMoves(playerCells);
|
||||
|
||||
console.log(`[AI-BOT P${this.playerId}] Found ${moves.length} possible moves (move #${moveCount + 1})`);
|
||||
|
||||
if (moves.length === 0) {
|
||||
// No moves available this iteration
|
||||
consecutiveNoMoves++;
|
||||
console.log(`[AI-BOT P${this.playerId}] No valid moves this iteration (${consecutiveNoMoves}/${maxConsecutiveNoMoves})`);
|
||||
|
||||
if (consecutiveNoMoves >= maxConsecutiveNoMoves) {
|
||||
console.log(`[AI-BOT P${this.playerId}] No more valid moves after ${moveCount} moves, ending turn`);
|
||||
break;
|
||||
}
|
||||
|
||||
// Small delay before re-checking
|
||||
await this.wait(200);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Reset counter when we find valid moves
|
||||
consecutiveNoMoves = 0;
|
||||
|
||||
// Check if we've reached max moves limit
|
||||
if (moveCount >= maxMovesPerTurn) {
|
||||
console.log(`[AI-BOT P${this.playerId}] Reached max moves limit (${maxMovesPerTurn}), ending turn`);
|
||||
break;
|
||||
}
|
||||
|
||||
// Sort moves by priority (attack > expand > reinforce)
|
||||
moves.sort((a, b) => this.movePriority(b) - this.movePriority(a));
|
||||
|
||||
// Execute best move
|
||||
const bestMove = moves[0];
|
||||
console.log(`[AI-BOT P${this.playerId}] Selected move: from (${bestMove.from.q},${bestMove.from.r}) to (${bestMove.to.q},${bestMove.to.r}), type=${bestMove.type}, attackStr=${bestMove.attackStrength}, defStr=${bestMove.defenseStrength}`);
|
||||
|
||||
// Wait for thinking time between moves
|
||||
await this.wait(this.thinkingTime);
|
||||
this.gameUI.endTurn();
|
||||
return;
|
||||
|
||||
// Execute the move
|
||||
this.gameUI.selectedCell = bestMove.from;
|
||||
this.gameUI.currentTarget = bestMove.to;
|
||||
this.gameUI.executeAttack();
|
||||
|
||||
moveCount++;
|
||||
console.log(`[AI-BOT P${this.playerId}] Move #${moveCount} executed`);
|
||||
|
||||
// Clear selection after move (if method exists)
|
||||
if (typeof this.gameUI.cancelSelection === 'function') {
|
||||
this.gameUI.cancelSelection();
|
||||
}
|
||||
}
|
||||
|
||||
// Sort moves by priority (attack > expand > reinforce)
|
||||
moves.sort((a, b) => this.movePriority(b) - this.movePriority(a));
|
||||
|
||||
// Execute best move
|
||||
const bestMove = moves[0];
|
||||
console.log(`[AI-BOT P${this.playerId}] Selected move: from (${bestMove.from.q},${bestMove.from.r}) to (${bestMove.to.q},${bestMove.to.r}), type=${bestMove.type}, attackStr=${bestMove.attackStrength}, defStr=${bestMove.defenseStrength}`);
|
||||
|
||||
// Wait for thinking time
|
||||
await this.wait(this.thinkingTime);
|
||||
|
||||
// Execute the move
|
||||
this.gameUI.selectedCell = bestMove.from;
|
||||
this.gameUI.currentTarget = bestMove.to;
|
||||
this.gameUI.executeAttack();
|
||||
|
||||
console.log(`[AI-BOT P${this.playerId}] Move executed`);
|
||||
|
||||
// End turn after executing move
|
||||
// End turn after all moves are executed
|
||||
console.log(`[AI-BOT P${this.playerId}] Total moves this turn: ${moveCount}`);
|
||||
console.log(`[AI-BOT P${this.playerId}] Calling endTurn()`);
|
||||
this.gameUI.endTurn();
|
||||
|
||||
|
||||
console.log(`[AI-BOT P${this.playerId}] === Turn completed ===`);
|
||||
} catch (error) {
|
||||
console.error(`[AI-BOT P${this.playerId}] Error during turn:`, error);
|
||||
|
||||
Reference in New Issue
Block a user