Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Batalhanaval {
- private static final int TAMANHO_TABULEIRO = 10;
- private static final int AGUA = -1;
- private static final int NAVIO = 1;
- private static final int ERRO = 0;
- private static final int ACERTO = 2;
- private static final int[][] NAVIOS_TAMANHOS = {{4, 1}, {3, 1}, {2, 2}, {2, 2}};
- private static int[][] tabuleiro = new int[TAMANHO_TABULEIRO][TAMANHO_TABULEIRO];
- private static int jogador = 1;
- private static int acertosJogador1 = 0, acertosJogador2 = 0;
- private static int tentativas = 0;
- public static void main(String[] args) {
- inicializaTabuleiro();
- iniciaNavios();
- System.out.println("Bem-vindo ao jogo de Batalha Naval!");
- jogar();
- }
- private static void inicializaTabuleiro() {
- for (int i = 0; i < TAMANHO_TABULEIRO; i++) {
- for (int j = 0; j < TAMANHO_TABULEIRO; j++) {
- tabuleiro[i][j] = AGUA;
- }
- }
- }
- private static void iniciaNavios() {
- Random rand = new Random();
- for (int[] tamanho : NAVIOS_TAMANHOS) {
- boolean posicionado = false;
- while (!posicionado) {
- int linha = rand.nextInt(TAMANHO_TABULEIRO);
- int coluna = rand.nextInt(TAMANHO_TABULEIRO);
- boolean horizontal = rand.nextBoolean();
- if (podeColocarNavio(linha, coluna, tamanho[0], tamanho[1], horizontal)) {
- for (int i = 0; i < tamanho[0]; i++) {
- for (int j = 0; j < tamanho[1]; j++) {
- int x = linha + (horizontal ? 0 : i);
- int y = coluna + (horizontal ? j : 0);
- tabuleiro[x][y] = NAVIO;
- }
- }
- posicionado = true;
- }
- }
- }
- }
- private static boolean podeColocarNavio(int linha, int coluna, int largura, int altura, boolean horizontal) {
- if (horizontal && coluna + largura > TAMANHO_TABULEIRO) return false;
- if (!horizontal && linha + altura > TAMANHO_TABULEIRO) return false;
- for (int i = 0; i < largura; i++) {
- for (int j = 0; j < altura; j++) {
- int x = linha + (horizontal ? 0 : i);
- int y = coluna + (horizontal ? j : 0);
- if (tabuleiro[x][y] == NAVIO) return false;
- }
- }
- return true;
- }
- private static void jogar() {
- Scanner entrada = new Scanner(System.in);
- int totalNavios = 11; // Soma dos tamanhos das embarcações
- do {
- System.out.println("\nJogador " + jogador + ", sua vez!");
- mostraTabuleiro();
- int[] tiro = darTiro(entrada);
- tentativas++;
- if (tabuleiro[tiro[0]][tiro[1]] == NAVIO) {
- System.out.println("Você acertou um navio!");
- tabuleiro[tiro[0]][tiro[1]] = ACERTO;
- if (jogador == 1) acertosJogador1++;
- else acertosJogador2++;
- } else {
- System.out.println("Você errou o tiro.");
- tabuleiro[tiro[0]][tiro[1]] = ERRO;
- jogador = (jogador == 1) ? 2 : 1; // Alterna jogador
- }
- System.out.printf("Pontuação: Jogador 1 = %d | Jogador 2 = %d\n", acertosJogador1, acertosJogador2);
- } while (acertosJogador1 + acertosJogador2 < totalNavios);
- System.out.println("\n\nJogo terminado! Os navios foram afundados em " + tentativas + " tentativas.");
- mostraTabuleiro();
- entrada.close();
- }
- private static int[] darTiro(Scanner entrada) {
- int[] tiro = new int[2];
- boolean tiroValido = false;
- while (!tiroValido) {
- System.out.print("Linha (A-J): ");
- char linhaChar = entrada.next().toUpperCase().charAt(0);
- tiro[0] = linhaChar - 'A';
- System.out.print("Coluna (1-10): ");
- tiro[1] = entrada.nextInt() - 1;
- if (tiro[0] < 0 || tiro[0] >= TAMANHO_TABULEIRO || tiro[1] < 0 || tiro[1] >= TAMANHO_TABULEIRO) {
- System.out.println("Coordenadas inválidas! Tente novamente.");
- } else if (tabuleiro[tiro[0]][tiro[1]] == ERRO || tabuleiro[tiro[0]][tiro[1]] == ACERTO) {
- System.out.println("Você já atirou nessa posição! Escolha outra.");
- } else {
- tiroValido = true;
- }
- }
- return tiro;
- }
- private static void mostraTabuleiro() {
- System.out.print("\t");
- for (int i = 1; i <= TAMANHO_TABULEIRO; i++) {
- System.out.print(i + "\t");
- }
- System.out.println();
- for (int linha = 0; linha < TAMANHO_TABULEIRO; linha++) {
- System.out.print((char) ('A' + linha) + "\t");
- for (int coluna = 0; coluna < TAMANHO_TABULEIRO; coluna++) {
- if (tabuleiro[linha][coluna] == ERRO) {
- System.out.print("*\t"); // Tiro errado
- } else if (tabuleiro[linha][coluna] == ACERTO) {
- System.out.print("X\t"); // Acerto
- } else {
- System.out.print("~\t"); // Oceano
- }
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement