Java版战舰游戏开发求助:实现简易AI
Hey there! No worries about your English at all—we’re all here to help, especially with tight deadlines like your Saturday due date. Let’s break down how to get that simple Battleship AI up and running for your Java project.
First, let’s outline what a basic Battleship AI needs to do:
- Randomly select unattackable coordinates (the simplest version)
- Track already attacked positions to avoid repeating hits
- (Optional but easy to add) If it hits a ship, prioritize attacking adjacent coordinates to "track" the rest of the ship
Assuming your existing code uses Spanish-named classes (like Tablero for board, Coordenada for coordinate, Barco for ship), here’s a sample AI class you can adapt:
import java.util.ArrayList; import java.util.List; import java.util.Random; public class JugadorAI { private List<Coordenada> coordenadasAtacadas; // Tracks all attacked spots private Random generadorAleatorio; private Tablero tableroEnemigo; // Reference to the enemy's board to check hit results public JugadorAI(Tablero tableroEnemigo) { this.tableroEnemigo = tableroEnemigo; coordenadasAtacadas = new ArrayList<>(); generadorAleatorio = new Random(); } // Basic random attack method public Coordenada realizarAtaque() { int fila, columna; Coordenada coordenadaElegida; // Keep generating coordinates until we find one that hasn't been attacked do { // Adjust the range if your board isn't 10x10 (e.g., 0-9 for 10 rows/columns) fila = generadorAleatorio.nextInt(10); columna = generadorAleatorio.nextInt(10); coordenadaElegida = new Coordenada(fila, columna); } while (coordenadasAtacadas.contains(coordenadaElegida)); coordenadasAtacadas.add(coordenadaElegida); // Call your existing attack logic to check if this hit a ship boolean impacto = tableroEnemigo.atacar(coordenadaElegida); if (impacto) { System.out.println("¡El AI golpeó un barco!"); } return coordenadaElegida; } }
To make the AI feel smarter, add logic to attack adjacent spots after a hit. Here’s how to modify the class:
// Add a new list to track potential adjacent targets after a hit private List<Coordenada> coordenadasSeguimiento; // Update the constructor to initialize the new list public JugadorAI(Tablero tableroEnemigo) { this.tableroEnemigo = tableroEnemigo; coordenadasAtacadas = new ArrayList<>(); generadorAleatorio = new Random(); coordenadasSeguimiento = new ArrayList<>(); } // Modify the attack method to prioritize tracking targets first public Coordenada realizarAtaque() { Coordenada coordenadaElegida; // First, check if we have any pending tracking targets (from a previous hit) if (!coordenadasSeguimiento.isEmpty()) { coordenadaElegida = coordenadasSeguimiento.remove(0); // Skip if this spot was already attacked while (coordenadasAtacadas.contains(coordenadaElegida)) { if (coordenadasSeguimiento.isEmpty()) break; coordenadaElegida = coordenadasSeguimiento.remove(0); } } else { // No tracking targets? Fall back to random selection do { fila = generadorAleatorio.nextInt(10); columna = generadorAleatorio.nextInt(10); coordenadaElegida = new Coordenada(fila, columna); } while (coordenadasAtacadas.contains(coordenadaElegida)); } coordenadasAtacadas.add(coordenadaElegida); boolean impacto = tableroEnemigo.atacar(coordenadaElegida); if (impacto) { // Add adjacent spots to the tracking list (up, down, left, right) agregarCoordenadasSeguimiento(coordenadaElegida); System.out.println("¡El AI golpeó un barco!"); } return coordenadaElegida; } // Helper method to add valid adjacent coordinates to the tracking list private void agregarCoordenadasSeguimiento(Coordenada centro) { int fila = centro.getFila(); int columna = centro.getColumna(); // Add up (if within board bounds) if (fila > 0) { coordenadasSeguimiento.add(new Coordenada(fila - 1, columna)); } // Add down if (fila < 9) { // Adjust if your board uses different indexing coordenadasSeguimiento.add(new Coordenada(fila + 1, columna)); } // Add left if (columna > 0) { coordenadasSeguimiento.add(new Coordenada(fila, columna - 1)); } // Add right if (columna < 9) { coordenadasSeguimiento.add(new Coordenada(fila, columna + 1)); } }
- Make sure your
Tableroclass has anatacar(Coordenada)method that returnstrueif the attack hits a ship - In your game’s main loop, instantiate
JugadorAIby passing the enemy player’sTablero - When it’s the AI’s turn, call
realizarAtaque()to get its move and execute it
If you can share snippets of your existing classes (like your Tablero or Barco code), we can tweak this even more to fit your exact setup. But this should give you a solid starting point to hit your Saturday deadline!
内容的提问来源于stack exchange,提问作者ZornXD

