Adding boats dynamically to the board

0

I am developing an application using RMI that simulates the battle game. My code for now is dynamically generating the board and automatically positioning the ships on the board, my question is how do I choose where to add each type of vessel (destroyer, cruiser, hydroplane, aircraft carrier and submarine) on my board? * Note: I need to differentiate each type of boat in the board, according to the image: Clientside:

publicclassJogador{TocadorSomtsom=newTocadorSom();TocadorSomtsomTiro=newTocadorSom();TocadorSomtsomacertou=newTocadorSom();publicJogador(intcod)throwsRemoteException{Scannersc=newScanner(System.in);System.out.println("Informe o tamanho do tabuleiro \nQuantidade de linhas: ");
    int linhas = sc.nextInt();
    System.out.println("quantidade de colunas: ");
    int colunas = sc.nextInt();
    int[][] tabuleiro = new int[linhas][colunas];
    int[][] navios = new int[5][2];
    int[] tiro = new int[2];
    boolean vez = false;
    int tentativas = 0, acertos = 0;
    MsImpl ms = new MsImpl();

    tsom.play("intro");

    try {

        Ms c = (Ms) Naming.lookup("rmi://127.0.0.1:1090/batalhaNaval");
        System.out.println();
        inicializarNavios(navios);
        inicializarTabuleiro(tabuleiro, linhas, colunas);
        System.out.println("");

        do {
            mostrarTabuleiro(tabuleiro, linhas, colunas);
            darTiro(tiro);
            tentativas++;

            if (ms.acertou(tiro, navios)) {
                tsomacertou.play("acertou");
                acertos++;

            } else {
                dica(tiro, navios, tentativas);
                tsomTiro.play("agua");

            }
            alteraTabuleiro(tiro, navios, tabuleiro);

        } while (acertos != 5);

        if (acertos == 5) {
            mostrarTabuleiro(tabuleiro, linhas, colunas);
            System.out.println("Vencedor jogador: " + ms.vez(cod));
            tsomTiro.play("win");
        }

    } catch (Exception e) {
        System.out.println(e);
    }

}

public static void inicializarTabuleiro(int[][] tabuleiro, int linhas, int colunas) {

    for (int linha = 0; linha < linhas; linha++) {
        for (int coluna = 0; coluna < colunas; coluna++) {
            tabuleiro[linha][coluna] = -1;
        }
    }
}

public static void mostrarTabuleiro(int[][] tabuleiro, int linhas, int colunas) {

    for (int i = 1; i <= linhas; i++) {
        System.out.print("\t" + i);
    }
    System.out.println();

    for (int linha = 0; linha < linhas; linha++) {
        System.out.print((linha + 1) + "");
        for (int coluna = 0; coluna < colunas; coluna++) {

            if (tabuleiro[linha][coluna] == -1) {
                System.out.print("\t" + "~");
            } else if (tabuleiro[linha][coluna] == 0) {
                System.out.print("\t" + "*");
            } else if (tabuleiro[linha][coluna] == 1) {
                System.out.print("\t" + "x");
            }

        }
        System.out.println();
    }
}

public static void inicializarNavios(int[][] navios) {

    public static void inicializarNavios(int[][] navios) {

    for (int c = 0; c < 4; c++) {
        System.out.println("Informe onde deseja colocar o destroyer - \nLinha: ");
        navios[c][0] = entrada.nextInt();
        System.out.println("Coluna: ");
        navios[c][1] = entrada.nextInt();
        System.out.println(c);
    }
    for (int c = 4; c < 9; c++) {
        System.out.println("Informe onde deseja colocar o porta-aviao - \nLinha: ");
        navios[c][0] = entrada.nextInt();
        System.out.println("Coluna: ");
        navios[c][1] = entrada.nextInt();
        System.out.println(c);
    }
    for (int c = 9; c < 12; c++) {
        System.out.println("Informe onde deseja colocar o hidro-aviao - \nLinha: ");
        navios[c][0] = entrada.nextInt();
        System.out.println("Coluna: ");
        navios[c][1] = entrada.nextInt();
        System.out.println(c);
    }
    for (int c = 12; c < 15; c++) {
        System.out.println("Informe onde deseja colocar o cruzador - \nLinha: ");
        navios[c][0] = entrada.nextInt();
        System.out.println("Coluna: ");
        navios[c][1] = entrada.nextInt();
        System.out.println(c);
    }int c = 16;
    System.out.println("Informe onde deseja colocar o submarino - \nLinha: ");
    navios[c][0] = entrada.nextInt();
    System.out.println("Coluna: ");
    navios[c][1] = entrada.nextInt();
    System.out.println(c);
}       }

}

public static void darTiro(int[] tiro) {

    Scanner entrada = new Scanner(System.in);

    System.out.print("Linha: ");
    tiro[0] = entrada.nextInt();
    tiro[0]--;

    System.out.print("Coluna: ");
    tiro[1] = entrada.nextInt();
    tiro[1]--;
}

public static void dica(int[] tiro, int[][] navios, int tentativas) {

    int linha = 0, coluna = 0;

    for (int c = 0; c < navios.length; c++) {
        if (navios[c][0] == tiro[0]) {
            linha++;
        }
        if (navios[c][1] == tiro[1]) {
            coluna++;
        }
    }

    System.out.print("\nLinha " + (tiro[0] + 1) + " -> navios " + linha + "\nColuna "
            + (tiro[1] + 1) + " -> navios " + coluna + "\n\n");

}

public static void alteraTabuleiro(int[] tiro, int[][] navios, int[][] tabuleiro) throws RemoteException {

    MsImpl ms = new MsImpl();

    if (ms.acertou(tiro, navios)) {
        tabuleiro[tiro[0]][tiro[1]] = 1;
    } else {
        tabuleiro[tiro[0]][tiro[1]] = 0;
    }
}
}

Client side:

 package Cliente;

 import java.rmi.RemoteException;

 public class J1 {

public static void main(String[] args) throws RemoteException {
    Jogador j1 = new Jogador(1);
}
}

Client side:

package Cliente;

import java.rmi.RemoteException;

public class J2 {

public static void main(String[] args) throws RemoteException {
    Jogador j2 = new Jogador(2);
}
}

Interface

package Interface;

import java.rmi.Remote;
import java.rmi.RemoteException;

 public interface BatalhaNaval extends Remote {

public boolean acertou(int[] tiro, int[][] navios) throws RemoteException;

public int jogou(String nome) throws RemoteException;

public boolean vez(int cod) throws RemoteException;
}

Server:

package Servidor;

import Interface.BatalhaNaval;
import implementacao.ServidorImpl;
import java.rmi.Naming;

public class Servidor {

public static void main(String[] args) {
    try {
        java.rmi.registry.LocateRegistry.createRegistry(1090);
        BatalhaNaval c = new ServidorImpl();
        Naming.rebind("rmi://127.0.0.1:1090/batalhaNaval", c);

        System.out.println("\nSevidor conectado!!");
    } catch (Exception e) {
        System.out.println(e);
    }
}
}

Implementation

package implementacao;

import Interface.BatalhaNaval;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ServidorImpl extends UnicastRemoteObject implements BatalhaNaval {

private static final long serialVersionUID = 1L;
public static boolean j2 = false;
public static boolean j1 = true;

public ServidorImpl() throws RemoteException {

}

@Override
public boolean acertou(int[] tiro, int[][] navios) throws RemoteException {

    for (int c = 0; c < navios.length; c++) {
        if (tiro[0] == navios[c][0] && tiro[1] == navios[c][1]) {
            System.out.println("Você acertou o tiro (" + (tiro[0] + 1) + " , " + (tiro[1] + 1) + ")");
            return true;
        }
    }System.out.println("Você errou o tiro (" + (tiro[0] + 1) + " , " + (tiro[1] + 1) + ")");
    return false;
}

@Override
public int jogou(String nome) throws RemoteException {

    if (nome.equals("Jogador1")) {
        return 1;
    }
    if (nome.equals("Jogador2")) {
        return 2;
    }
    return 0;
}

@Override
public boolean vez(int cod) throws RemoteException {

    if (cod == 1) {
        return j1;
    } else if (cod == 2) {
        return j2;
    }
    return false;
}
}
    
asked by anonymous 23.10.2017 / 21:41

0 answers