import Barco, { BarcoTipo } from './barco'; export enum EstadoCelda { Vacio = 0, Agua = -1, Golpe = -2, } export default class Mapa { celdas: number[][] = []; barcosMapa: Map = new Map(); constructor() { for (let i = 0; i < 10; i++) { this.celdas[i] = []; for (let j = 0; j < 10; j++) { this.celdas[i][j] = 0; } } } setBarcos(barcos: Barco | Barco[]) { const barcosArray = Array.isArray(barcos) ? barcos : [barcos]; for (const barco of barcosArray) { const incFila = barco.orientacion === 'VERTICAL' ? 1 : 0; const incColumna = barco.orientacion === 'HORIZONTAL' ? 1 : 0; for (let i = 0; i < barco.longitud; i++) { const fila = barco.y + i * incFila; const columna = barco.x + i * incColumna; this.celdas[fila][columna] = barco.id; } this.barcosMapa.set(barco.id, barco); } } getEstadoCelda(x: number, y: number): EstadoCelda { return this.celdas[x][y]; } getBarcoEnCelda(x: number, y: number): Barco | null { const barcoId = this.celdas[x][y]; if (barcoId > 0) { return this.barcosMapa.get(barcoId) || null; } return null; } marcaDisparo(x:number,y:number,valor:number){ this.celdas[x][y]=valor; } getInformacionBarcos(): Barco[] { return Array.from(this.barcosMapa.values()); } verificaDisparo(x: number, y: number): boolean { if (this.celdas[x][y] > 0) { const barco = this.getBarcoEnCelda(x,y); if (barco) { barco.recibirImpacto(); this.celdas[x][y] = EstadoCelda.Golpe; return true; } } else { this.celdas[x][y] = EstadoCelda.Agua; } return false; } }