Advertisement
plattina

BatalhaNaval

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