UNPKG

battleship-ai

Version:

This repository contains a Battleship AI implementation built in TypeScript. The AI focuses on grid-based probability calculations, strategic ship placement, and targeted attack mechanisms to effectively play the game. This README explains the AI’s logic,

66 lines (62 loc) 1.78 kB
declare enum CellType { Empty = 0,// Water (empty) Miss = 2,// Missed shot Hit = 3 } interface Ship { size: number; positions: { x: number; y: number; }[]; } interface Coordinate { x: number; y: number; } interface AttackOutcome extends Coordinate { outcome: 'hit' | 'miss'; } declare class Grid { size: number; cells: CellType[][]; constructor(size: number); private init; updateCell(x: number, y: number, type: CellType): void; isMiss(x: number, y: number): boolean; isDamagedShip(x: number, y: number): boolean; } declare class BattleShipAI { private probGrid; private virtualGrid; private ships; constructor(gameSize: number, ships: number[]); private static readonly PROB_WEIGHT; private static readonly OPEN_LOW_MIN; private static readonly OPEN_LOW_MAX; private static readonly OPEN_MED_MIN; private static readonly OPEN_MED_MAX; private static readonly OPEN_HIGH_MIN; private static readonly OPEN_HIGH_MAX; private static readonly OPENINGS; private initProbs; private setProbs; private updateGrid; private resetGrid; private resetProbs; private isValidCell; private canPlaceShip; updateProbs(previousAttacks: AttackOutcome[]): void; private evaluateAdjacentTiles; private numHitCellsCovered; private getShipCoordinates; private evaluateCellForShips; getHighestProbabilityTarget(previousMoves: AttackOutcome[]): { x: number; y: number; }; getHeatmap(): number[][]; getRandomShipPlacements(): Ship[]; calculateOutcome(x: number, y: number, ships: Ship[]): "hit" | "miss"; } export { type AttackOutcome, BattleShipAI, CellType, type Coordinate, Grid, type Ship };