Advertisement
plattina

BatalhaNavalSimplificada

Feb 22nd, 2025
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Batalhanaval {
  5.  
  6.     private static final int TAMANHO_TABULEIRO = 10;
  7.     private static final int AGUA = -1;
  8.     private static final int NAVIO = 1;
  9.     private static final int ERRO = 0;
  10.     private static final int ACERTO = 2;
  11.  
  12.     private static final int[][] NAVIOS_TAMANHOS = {{4, 1}, {3, 1}, {2, 2}, {2, 2}};
  13.  
  14.     private static int[][] tabuleiro = new int[TAMANHO_TABULEIRO][TAMANHO_TABULEIRO];
  15.     private static int jogador = 1;
  16.     private static int acertosJogador1 = 0, acertosJogador2 = 0;
  17.     private static int tentativas = 0;
  18.  
  19.     public static void main(String[] args) {
  20.         inicializaTabuleiro();
  21.         iniciaNavios();
  22.         System.out.println("Bem-vindo ao jogo de Batalha Naval!");
  23.         jogar();
  24.     }
  25.  
  26.     private static void inicializaTabuleiro() {
  27.         for (int i = 0; i < TAMANHO_TABULEIRO; i++) {
  28.             for (int j = 0; j < TAMANHO_TABULEIRO; j++) {
  29.                 tabuleiro[i][j] = AGUA;
  30.             }
  31.         }
  32.     }
  33.  
  34.     private static void iniciaNavios() {
  35.         Random rand = new Random();
  36.         for (int[] tamanho : NAVIOS_TAMANHOS) {
  37.             boolean posicionado = false;
  38.             while (!posicionado) {
  39.                 int linha = rand.nextInt(TAMANHO_TABULEIRO);
  40.                 int coluna = rand.nextInt(TAMANHO_TABULEIRO);
  41.                 boolean horizontal = rand.nextBoolean();
  42.                 if (podeColocarNavio(linha, coluna, tamanho[0], tamanho[1], horizontal)) {
  43.                     for (int i = 0; i < tamanho[0]; i++) {
  44.                         for (int j = 0; j < tamanho[1]; j++) {
  45.                             int x = linha + (horizontal ? 0 : i);
  46.                             int y = coluna + (horizontal ? j : 0);
  47.                             tabuleiro[x][y] = NAVIO;
  48.                         }
  49.                     }
  50.                     posicionado = true;
  51.                 }
  52.             }
  53.         }
  54.     }
  55.  
  56.     private static boolean podeColocarNavio(int linha, int coluna, int largura, int altura, boolean horizontal) {
  57.         if (horizontal && coluna + largura > TAMANHO_TABULEIRO) return false;
  58.         if (!horizontal && linha + altura > TAMANHO_TABULEIRO) return false;
  59.         for (int i = 0; i < largura; i++) {
  60.             for (int j = 0; j < altura; j++) {
  61.                 int x = linha + (horizontal ? 0 : i);
  62.                 int y = coluna + (horizontal ? j : 0);
  63.                 if (tabuleiro[x][y] == NAVIO) return false;
  64.             }
  65.         }
  66.         return true;
  67.     }
  68.  
  69.     private static void jogar() {
  70.         Scanner entrada = new Scanner(System.in);
  71.         int totalNavios = 11; // Soma dos tamanhos das embarcações
  72.  
  73.         do {
  74.             System.out.println("\nJogador " + jogador + ", sua vez!");
  75.             mostraTabuleiro();
  76.             int[] tiro = darTiro(entrada);
  77.  
  78.             tentativas++;
  79.  
  80.             if (tabuleiro[tiro[0]][tiro[1]] == NAVIO) {
  81.                 System.out.println("Você acertou um navio!");
  82.                 tabuleiro[tiro[0]][tiro[1]] = ACERTO;
  83.                 if (jogador == 1) acertosJogador1++;
  84.                 else acertosJogador2++;
  85.             } else {
  86.                 System.out.println("Você errou o tiro.");
  87.                 tabuleiro[tiro[0]][tiro[1]] = ERRO;
  88.                 jogador = (jogador == 1) ? 2 : 1; // Alterna jogador
  89.             }
  90.  
  91.             System.out.printf("Pontuação: Jogador 1 = %d | Jogador 2 = %d\n", acertosJogador1, acertosJogador2);
  92.  
  93.         } while (acertosJogador1 + acertosJogador2 < totalNavios);
  94.  
  95.         System.out.println("\n\nJogo terminado! Os navios foram afundados em " + tentativas + " tentativas.");
  96.         mostraTabuleiro();
  97.         entrada.close();
  98.     }
  99.  
  100.     private static int[] darTiro(Scanner entrada) {
  101.         int[] tiro = new int[2];
  102.         boolean tiroValido = false;
  103.  
  104.         while (!tiroValido) {
  105.             System.out.print("Linha (A-J): ");
  106.             char linhaChar = entrada.next().toUpperCase().charAt(0);
  107.             tiro[0] = linhaChar - 'A';
  108.  
  109.             System.out.print("Coluna (1-10): ");
  110.             tiro[1] = entrada.nextInt() - 1;
  111.  
  112.             if (tiro[0] < 0 || tiro[0] >= TAMANHO_TABULEIRO || tiro[1] < 0 || tiro[1] >= TAMANHO_TABULEIRO) {
  113.                 System.out.println("Coordenadas inválidas! Tente novamente.");
  114.             } else if (tabuleiro[tiro[0]][tiro[1]] == ERRO || tabuleiro[tiro[0]][tiro[1]] == ACERTO) {
  115.                 System.out.println("Você já atirou nessa posição! Escolha outra.");
  116.             } else {
  117.                 tiroValido = true;
  118.             }
  119.         }
  120.         return tiro;
  121.     }
  122.  
  123.     private static void mostraTabuleiro() {
  124.         System.out.print("\t");
  125.         for (int i = 1; i <= TAMANHO_TABULEIRO; i++) {
  126.             System.out.print(i + "\t");
  127.         }
  128.         System.out.println();
  129.  
  130.         for (int linha = 0; linha < TAMANHO_TABULEIRO; linha++) {
  131.             System.out.print((char) ('A' + linha) + "\t");
  132.             for (int coluna = 0; coluna < TAMANHO_TABULEIRO; coluna++) {
  133.                 if (tabuleiro[linha][coluna] == ERRO) {
  134.                     System.out.print("*\t"); // Tiro errado
  135.                 } else if (tabuleiro[linha][coluna] == ACERTO) {
  136.                     System.out.print("X\t"); // Acerto
  137.                 } else {
  138.                     System.out.print("~\t"); // Oceano
  139.                 }
  140.             }
  141.             System.out.println();
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement