我目前正在尝试使用RMI进行战舰,并希望将操场转移到另一个客户,但它的工作
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class Design.Playground cannot be cast to class java.rmi.Remote (Design.Playground is in unnamed module of loader 'app'; java.rmi.Remote is in module java.rmi of loader 'bootstrap')
at Game.Game$3.mouseClicked(Game.java:100)
服务器是播放器1。
客户端是玩家2。
是否不可能将操场传输到客户端?
package RMI;
import Design.*;
import Game.Game;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject implements BattleshipServer {
private Playground p1 = new Playground();
private Playground p2 = new Playground();
private boolean hostTurn = true;
private boolean winner = false;
public Server() throws RemoteException {
super();
}
public static void main(String[] args) throws RemoteException, NotBoundException {
BattleshipServer server = new Server();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("BattleshipServer", server);
System.out.println("Server ready");
server.game(true);
}
@Override
public boolean getHostTurn() {
return hostTurn;
}
@Override
public void changeHostTurn() {
hostTurn = !hostTurn;
}
@Override
public boolean getWinner() {
return winner;
}
@Override
public void changeWinner() {
winner = true;
}
@Override
public String game(boolean host) throws RemoteException {
Game game = new Game(this);
game.game(host);
return "Yes";
}
@Override
public Game getGame() throws RemoteException {
return new Game(this);
}
@Override
public Playground getPlayground(int p) throws RemoteException {
if(p==1) return p1;
if(p==2) return p2;
return null;
}
@Override
public void sendPlayground(Playground playground, int p) throws RemoteException {
if(p==1) p1 = (Playground) UnicastRemoteObject.exportObject((Remote) playground,1099);
if(p==2) p2 = (Playground) UnicastRemoteObject.exportObject((Remote) playground,1099);
}
}
package RMI;
import Game.Game;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private final BattleshipServer server;
public Client(String ip, int port) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry(ip,port);
server = (BattleshipServer) registry.lookup("BattleshipServer");
}
public String method() throws RemoteException, NotBoundException {
Game game = server.getGame();
game.game(false);
//return server.game(false);
return "";
}
public static void main(String[] args) throws RemoteException, NotBoundException {
Client client = new Client("localhost", 1099);
System.out.println(client.method());
}
}
package RMI;
import Design.*;
import Game.Game;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface BattleshipServer extends Remote {
String game(boolean host) throws RemoteException, NotBoundException;
Game getGame() throws RemoteException;
Playground getPlayground(int p) throws RemoteException;
void sendPlayground(Playground playground, int p) throws RemoteException;
boolean getHostTurn() throws RemoteException;
void changeHostTurn() throws RemoteException;
boolean getWinner() throws RemoteException;
void changeWinner() throws RemoteException;
}
package Game;
import Design.*;
import RMI.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
public class Game {
private final Server server;
public Game(Server server){
this.server = server;
}
private int turn = 7;
private boolean firstHit = true;
private boolean hit = false;
private final BattleshipFrame frame = new BattleshipFrame();
//Playground erstellen und Schiffliste erstellen
private Playground playground = new Playground();
private Playground enemyPlayground = new Playground();
public void game(boolean host) {
frame.initialGUI(10, playground);
frame.initialGUI(21, enemyPlayground);
enemyPlayground.enabled(false);
final int[] zaehler = {0};
final int[] x1 = {0};
final int[] y1 = {0};
final int[] x2 = {0};
final int[] y2 = {0};
frame.getHostGameButton().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
frame.getJoinGameButton().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
for (int i = 0; i < playground.getPlayground().length; i++) {
for (int j = 0; j < playground.getPlayground()[i].length; j++) {
int finalI = i;
int finalJ = j;
playground.getPlayground()[i][j].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){
if(playground.getPlayground()[finalI][finalJ].isEnabled()) {
if (turn > -1) {
int size = 0;
if (turn == 7 || turn == 6) size = 4;
if (turn == 5 || turn == 4 || turn == 3) size = 3;
if (turn == 2 || turn == 1 || turn == 0) size = 2;
if (zaehler[0] == 0) {
frame.setText("Server playground kopiert");
frame.setText("Server playground kopiert");
frame.setText("Server playground kopiert");
frame.setText("Server playground kopiert");
x1[0] = finalI;
y1[0] = finalJ;
playground.getPlayground()[finalI][finalJ].setEnabled(false);
playground.changeButtons(finalI, finalJ, size, false);
}
if (zaehler[0] == 1) {
x2[0] = finalI;
y2[0] = finalJ;
}
zaehler[0]++;
if (zaehler[0] == 2) {
placeShip(x1[0], y1[0], x2[0], y2[0], size);
playground.changeButtons(finalI, finalJ, size, true);
if (turn != 6 && turn != 3) playground.disableNotPlaceable(size);
else playground.disableNotPlaceable(size - 1);
zaehler[0] = 0;
turn--;
if(turn == -1) {
enemyPlayground = enemyPlayground.copyPlayground(playground, 1);
enemyPlayground.enabled(false);
}
}
} else if (turn == -1) {
if (host && server.getHostTurn()) {
try {
server.sendPlayground(enemyPlayground, 1);
} catch (RemoteException ex) {
ex.printStackTrace();
}
server.changeHostTurn();
turn--;
System.out.println("Server playground kopiert");
} else if(!(host || server.getHostTurn())) {
try {
server.sendPlayground(enemyPlayground, 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
server.changeHostTurn();
turn--;
System.out.println("Client playground kopiert");
} else if(!host && server.getHostTurn()) System.out.println("Bitte auf Server warten");
} else if (turn == -2) {
if (host && server.getHostTurn()) {
try {
playground = playground.copyPlayground(server.getPlayground(2), 1);
server.changeHostTurn();
System.out.println("Client playground auf Server kopiert");
} catch (RemoteException ex) {
ex.printStackTrace();
}
turn--;
} else if(host && !server.getHostTurn()) System.out.println("Bitte auf Client warten");
else if (!(host || server.getHostTurn())) {
try {
playground = playground.copyPlayground(server.getPlayground(1), 1);
server.changeHostTurn();
System.out.println("Server playground auf Client kopiert");
} catch (RemoteException ex) {
ex.printStackTrace();
}
turn--;
} else if(!host && server.getHostTurn()) System.out.println("Bitte auf Server warten");
playground.enabled(true);
} else if (turn == -3) {
if (host && server.getHostTurn()) {
if(server.getWinner()) {
System.out.println("Du hast leider verloren!");
turn = Integer.MIN_VALUE;
} else {
try {
server.sendPlayground(playground, 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
playground.getPlayground()[finalI][finalJ].setEnabled(false);
hit(finalI, finalJ, playground);
}
if(firstHit && !hit) firstHit = false;
else if(!firstHit){
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(1), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
if(hit) hit = false;
else server.changeHostTurn();
} else if(host && !server.getHostTurn()) System.out.println("Bitte auf Client warten");
else if(!(host || server.getHostTurn())) {
if(server.getWinner()) {
System.out.println("Du hast leider verloren!");
turn = Integer.MIN_VALUE;
} else {
try {
server.sendPlayground(playground, 1);
} catch (RemoteException ex) {
ex.printStackTrace();
}
playground.getPlayground()[finalI][finalJ].setEnabled(false);
hit(finalI, finalJ, playground);
}
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(2), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
if(hit) hit = false;
else server.changeHostTurn();
} else if(!host && server.getHostTurn()) System.out.println("Bitte auf Server warten");
enemyPlayground.enabled(false);
//if(!(host || server.getHostTurn()) || (host && server.getHostTurn()))
playground.enabled((true));
if (playground.getShipList().isEmpty()) {
turn = Integer.MIN_VALUE;
if(host) {
System.out.println("HOST GEWONNEN");
server.changeHostTurn();
server.changeWinner();
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(1), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
} else {
System.out.println("CLIENT GEWONNEN");
server.changeHostTurn();
server.changeWinner();
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(2), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
}
}
}
}
});
}
}
}
public void placeShip(int x1, int y1, int x2, int y2, int size) {
int [][] pos = new int[2][size];
boolean vertical = y1 - y2 != 0;
for(int i = 0; i < size; i++) {
if(!vertical && x2 > x1) {
pos[0][i] = x1+i;
pos[1][i] = y1;
playground.getPlayground()[x1+i][y1].setEnabled(false);
playground.getPlayground()[x1+i][y1].setBackground(playground.getShipColor());
}
if(!vertical && x1 > x2) {
pos[0][i] = x1-i;
pos[1][i] = y1;
playground.getPlayground()[x1-i][y1].setEnabled(false);
playground.getPlayground()[x1-i][y1].setBackground(playground.getShipColor());
}
if(vertical && y2 > y1) {
pos[0][i] = x1;
pos[1][i] = y1+i;
playground.getPlayground()[x1][y1+i].setEnabled(false);
playground.getPlayground()[x1][y1+i].setBackground(playground.getShipColor());
}
if(vertical && y1 > y2) {
pos[0][i] = x1;
pos[1][i] = y1-i;
playground.getPlayground()[x1][y1-i].setEnabled(false);
playground.getPlayground()[x1][y1-i].setBackground(playground.getShipColor());
}
}
playground.getShipList().add(new Ship(size,pos));
}
public void shipDestroyed() {
for (int i = 0; i < playground.getShipList().size(); i++) {
boolean destroyed = true;
for (int j = 0; j < playground.getShipList().get(i).getSize(); j++) {
if (!playground.getPlayground()[playground.getShipList().get(i).getPos()[0][j]][playground.getShipList().get(i).getPos()[1][j]].getBackground().equals(Color.ORANGE)){
destroyed = false;
}
}
if (destroyed){
for (int j = 0; j < playground.getShipList().get(i).getSize(); j++) {
playground.getPlayground()[playground.getShipList().get(i).getPos()[0][j]][playground.getShipList().get(i).getPos()[1][j]].setBackground(Color.RED);
}
playground.getShipList().remove(i);
}
}
for (int i = 0; i < playground.getPlayground().length; i++) {
for (int j = 0; j < playground.getPlayground()[i].length; j++) {
if(playground.hasNeighbor(i,j,Color.RED) && !playground.getPlayground()[i][j].getBackground().equals(Color.RED)){
playground.getPlayground()[i][j].setEnabled(false);
playground.getPlayground()[i][j].setBackground(Color.GRAY);
}
}
}
}
public void hit(int x, int y, Playground playground) {
for (Ship ship : playground.getShipList()) {
for (int j = 0; j < ship.getSize(); j++) {
if (ship.getPos()[0][j] == x && ship.getPos()[1][j] == y) {
playground.getPlayground()[ship.getPos()[0][j]][ship.getPos()[1][j]].setBackground(Color.ORANGE);
hit = true;
shipDestroyed();
return;
}
}
}
playground.getPlayground()[x][y].setBackground(Color.WHITE);
}
}
package Design;
import Game.Ship;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class Playground implements Serializable {
private final JButton [][] playground = new JButton[10][10];
private final List<Ship> shipList = new ArrayList<>();
private final Color waterColor = new Color(80, 150, 255);
private final Color shipColor = new Color(0,0,0);
public Playground(){
for (int i = 0; i < playground.length; i++) {
for (int j = 0; j < playground[i].length; j++) {
playground[i][j] = new JButton();
playground[i][j].setPreferredSize(new Dimension(20,20));
playground[i][j].setBackground(waterColor);
}
}
}
public JButton[][] getPlayground() {
return playground;
}
public Color getShipColor(){
return shipColor;
}
public List<Ship> getShipList(){
return shipList;
}
public boolean hasNeighbor(int x, int y, Color color) {
boolean hasNeighbor = false;
try{
if (playground[x+1][y].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x+1][y+1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x+1][y-1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x][y+1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if ( playground[x][y-1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x-1][y].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x-1][y+1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x-1][y-1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
return hasNeighbor;
}
public void changeButtons(int x, int y, int size, boolean change) {
size--;
for (int i = 0; i < playground.length; i++) {
for (int j = 0; j < playground[i].length; j++) {
if(change){
if(!playground[i][j].getBackground().equals(shipColor) && !hasNeighbor(i,j,shipColor)) {
playground[i][j].setBackground(waterColor);
playground[i][j].setEnabled(true);
}
if(hasNeighbor(i,j,shipColor) && !playground[i][j].getBackground().equals(shipColor)) {
playground[i][j].setBackground(Color.GRAY);
playground[i][j].setEnabled(false);
}
}
else{
if(!((i==x+size && j==y)|| (i==x-size && j==y) || (i==x && j==y+size) || (i==x && j==y-size)) && !playground[i][j].getBackground().equals(shipColor)) {
playground[i][j].setBackground(Color.GRAY);
playground[i][j].setEnabled(false);
}
}
}
}
}
public void disableNotPlaceable(int size) {
size--;
for (int i = 0; i < playground.length; i++) {
for (int j = 0; j < playground[i].length; j++) {
boolean placeable = false;
try {
if (playground[i][j+size].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
try {
if (playground[i][j-size].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
try {
if (playground[i+size][j].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
try {
if (playground[i-size][j].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
if (!(placeable || playground[i][j].getBackground().equals(shipColor) || playground[i][j].getBackground().equals(Color.GRAY))) {
playground[i][j].setBackground(Color.GRAY);
playground[i][j].setEnabled(false);
}
}
}
}
public void clear(){
for (JButton[] jButtons : playground) {
for (JButton jButton : jButtons) {
jButton.setBackground(waterColor);
jButton.setEnabled(true);
}
}
for(int i=shipList.size()-1;i>=0;i--){
shipList.remove(i);
}
}
public Playground copyPlayground(Playground playground, int mode) {
clear();
for(int i=0;i<this.playground.length;i++) {
for(int j=0;j<this.playground[i].length;j++) {
if(mode == 1) {
if(playground.getPlayground()[i][j].getBackground().equals(shipColor)) {
this.getPlayground()[i][j].setBackground(playground.getPlayground()[i][j].getBackground());
}
} else if(mode == 2) this.getPlayground()[i][j].setBackground(playground.getPlayground()[i][j].getBackground());
}
}
for (int i=0;i<playground.getShipList().size();i++) {
if(playground.getShipList().size()>0) {
this.getShipList().add(playground.getShipList().get(i));
}
}
return this;
}
public void enabled(boolean enable){
for (JButton[] jButtons : this.playground) {
for (JButton jButton : jButtons) {
if(jButton.getBackground().equals(waterColor) && enable) jButton.setEnabled(true);
else if(!enable) jButton.setEnabled(false);
}
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class Design.Playground cannot be cast to class java.rmi.Remote (Design.Playground is in unnamed module of loader 'app'; java.rmi.Remote is in module java.rmi of loader 'bootstrap')
at Game.Game$3.mouseClicked(Game.java:100)
The Server is Player 1.
The Client is Player 2.
Is it not possible to transfer the Playground to the Client?
package RMI;
import Design.*;
import Game.Game;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject implements BattleshipServer {
private Playground p1 = new Playground();
private Playground p2 = new Playground();
private boolean hostTurn = true;
private boolean winner = false;
public Server() throws RemoteException {
super();
}
public static void main(String[] args) throws RemoteException, NotBoundException {
BattleshipServer server = new Server();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("BattleshipServer", server);
System.out.println("Server ready");
server.game(true);
}
@Override
public boolean getHostTurn() {
return hostTurn;
}
@Override
public void changeHostTurn() {
hostTurn = !hostTurn;
}
@Override
public boolean getWinner() {
return winner;
}
@Override
public void changeWinner() {
winner = true;
}
@Override
public String game(boolean host) throws RemoteException {
Game game = new Game(this);
game.game(host);
return "Yes";
}
@Override
public Game getGame() throws RemoteException {
return new Game(this);
}
@Override
public Playground getPlayground(int p) throws RemoteException {
if(p==1) return p1;
if(p==2) return p2;
return null;
}
@Override
public void sendPlayground(Playground playground, int p) throws RemoteException {
if(p==1) p1 = (Playground) UnicastRemoteObject.exportObject((Remote) playground,1099);
if(p==2) p2 = (Playground) UnicastRemoteObject.exportObject((Remote) playground,1099);
}
}
package RMI;
import Game.Game;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private final BattleshipServer server;
public Client(String ip, int port) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry(ip,port);
server = (BattleshipServer) registry.lookup("BattleshipServer");
}
public String method() throws RemoteException, NotBoundException {
Game game = server.getGame();
game.game(false);
//return server.game(false);
return "";
}
public static void main(String[] args) throws RemoteException, NotBoundException {
Client client = new Client("localhost", 1099);
System.out.println(client.method());
}
}
package RMI;
import Design.*;
import Game.Game;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface BattleshipServer extends Remote {
String game(boolean host) throws RemoteException, NotBoundException;
Game getGame() throws RemoteException;
Playground getPlayground(int p) throws RemoteException;
void sendPlayground(Playground playground, int p) throws RemoteException;
boolean getHostTurn() throws RemoteException;
void changeHostTurn() throws RemoteException;
boolean getWinner() throws RemoteException;
void changeWinner() throws RemoteException;
}
package Game;
import Design.*;
import RMI.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
public class Game {
private final Server server;
public Game(Server server){
this.server = server;
}
private int turn = 7;
private boolean firstHit = true;
private boolean hit = false;
private final BattleshipFrame frame = new BattleshipFrame();
//Playground erstellen und Schiffliste erstellen
private Playground playground = new Playground();
private Playground enemyPlayground = new Playground();
public void game(boolean host) {
frame.initialGUI(10, playground);
frame.initialGUI(21, enemyPlayground);
enemyPlayground.enabled(false);
final int[] zaehler = {0};
final int[] x1 = {0};
final int[] y1 = {0};
final int[] x2 = {0};
final int[] y2 = {0};
frame.getHostGameButton().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
frame.getJoinGameButton().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
for (int i = 0; i < playground.getPlayground().length; i++) {
for (int j = 0; j < playground.getPlayground()[i].length; j++) {
int finalI = i;
int finalJ = j;
playground.getPlayground()[i][j].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){
if(playground.getPlayground()[finalI][finalJ].isEnabled()) {
if (turn > -1) {
int size = 0;
if (turn == 7 || turn == 6) size = 4;
if (turn == 5 || turn == 4 || turn == 3) size = 3;
if (turn == 2 || turn == 1 || turn == 0) size = 2;
if (zaehler[0] == 0) {
frame.setText("Server playground kopiert");
frame.setText("Server playground kopiert");
frame.setText("Server playground kopiert");
frame.setText("Server playground kopiert");
x1[0] = finalI;
y1[0] = finalJ;
playground.getPlayground()[finalI][finalJ].setEnabled(false);
playground.changeButtons(finalI, finalJ, size, false);
}
if (zaehler[0] == 1) {
x2[0] = finalI;
y2[0] = finalJ;
}
zaehler[0]++;
if (zaehler[0] == 2) {
placeShip(x1[0], y1[0], x2[0], y2[0], size);
playground.changeButtons(finalI, finalJ, size, true);
if (turn != 6 && turn != 3) playground.disableNotPlaceable(size);
else playground.disableNotPlaceable(size - 1);
zaehler[0] = 0;
turn--;
if(turn == -1) {
enemyPlayground = enemyPlayground.copyPlayground(playground, 1);
enemyPlayground.enabled(false);
}
}
} else if (turn == -1) {
if (host && server.getHostTurn()) {
try {
server.sendPlayground(enemyPlayground, 1);
} catch (RemoteException ex) {
ex.printStackTrace();
}
server.changeHostTurn();
turn--;
System.out.println("Server playground kopiert");
} else if(!(host || server.getHostTurn())) {
try {
server.sendPlayground(enemyPlayground, 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
server.changeHostTurn();
turn--;
System.out.println("Client playground kopiert");
} else if(!host && server.getHostTurn()) System.out.println("Bitte auf Server warten");
} else if (turn == -2) {
if (host && server.getHostTurn()) {
try {
playground = playground.copyPlayground(server.getPlayground(2), 1);
server.changeHostTurn();
System.out.println("Client playground auf Server kopiert");
} catch (RemoteException ex) {
ex.printStackTrace();
}
turn--;
} else if(host && !server.getHostTurn()) System.out.println("Bitte auf Client warten");
else if (!(host || server.getHostTurn())) {
try {
playground = playground.copyPlayground(server.getPlayground(1), 1);
server.changeHostTurn();
System.out.println("Server playground auf Client kopiert");
} catch (RemoteException ex) {
ex.printStackTrace();
}
turn--;
} else if(!host && server.getHostTurn()) System.out.println("Bitte auf Server warten");
playground.enabled(true);
} else if (turn == -3) {
if (host && server.getHostTurn()) {
if(server.getWinner()) {
System.out.println("Du hast leider verloren!");
turn = Integer.MIN_VALUE;
} else {
try {
server.sendPlayground(playground, 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
playground.getPlayground()[finalI][finalJ].setEnabled(false);
hit(finalI, finalJ, playground);
}
if(firstHit && !hit) firstHit = false;
else if(!firstHit){
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(1), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
if(hit) hit = false;
else server.changeHostTurn();
} else if(host && !server.getHostTurn()) System.out.println("Bitte auf Client warten");
else if(!(host || server.getHostTurn())) {
if(server.getWinner()) {
System.out.println("Du hast leider verloren!");
turn = Integer.MIN_VALUE;
} else {
try {
server.sendPlayground(playground, 1);
} catch (RemoteException ex) {
ex.printStackTrace();
}
playground.getPlayground()[finalI][finalJ].setEnabled(false);
hit(finalI, finalJ, playground);
}
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(2), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
if(hit) hit = false;
else server.changeHostTurn();
} else if(!host && server.getHostTurn()) System.out.println("Bitte auf Server warten");
enemyPlayground.enabled(false);
//if(!(host || server.getHostTurn()) || (host && server.getHostTurn()))
playground.enabled((true));
if (playground.getShipList().isEmpty()) {
turn = Integer.MIN_VALUE;
if(host) {
System.out.println("HOST GEWONNEN");
server.changeHostTurn();
server.changeWinner();
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(1), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
} else {
System.out.println("CLIENT GEWONNEN");
server.changeHostTurn();
server.changeWinner();
try {
enemyPlayground = enemyPlayground.copyPlayground(server.getPlayground(2), 2);
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
}
}
}
}
});
}
}
}
public void placeShip(int x1, int y1, int x2, int y2, int size) {
int [][] pos = new int[2][size];
boolean vertical = y1 - y2 != 0;
for(int i = 0; i < size; i++) {
if(!vertical && x2 > x1) {
pos[0][i] = x1+i;
pos[1][i] = y1;
playground.getPlayground()[x1+i][y1].setEnabled(false);
playground.getPlayground()[x1+i][y1].setBackground(playground.getShipColor());
}
if(!vertical && x1 > x2) {
pos[0][i] = x1-i;
pos[1][i] = y1;
playground.getPlayground()[x1-i][y1].setEnabled(false);
playground.getPlayground()[x1-i][y1].setBackground(playground.getShipColor());
}
if(vertical && y2 > y1) {
pos[0][i] = x1;
pos[1][i] = y1+i;
playground.getPlayground()[x1][y1+i].setEnabled(false);
playground.getPlayground()[x1][y1+i].setBackground(playground.getShipColor());
}
if(vertical && y1 > y2) {
pos[0][i] = x1;
pos[1][i] = y1-i;
playground.getPlayground()[x1][y1-i].setEnabled(false);
playground.getPlayground()[x1][y1-i].setBackground(playground.getShipColor());
}
}
playground.getShipList().add(new Ship(size,pos));
}
public void shipDestroyed() {
for (int i = 0; i < playground.getShipList().size(); i++) {
boolean destroyed = true;
for (int j = 0; j < playground.getShipList().get(i).getSize(); j++) {
if (!playground.getPlayground()[playground.getShipList().get(i).getPos()[0][j]][playground.getShipList().get(i).getPos()[1][j]].getBackground().equals(Color.ORANGE)){
destroyed = false;
}
}
if (destroyed){
for (int j = 0; j < playground.getShipList().get(i).getSize(); j++) {
playground.getPlayground()[playground.getShipList().get(i).getPos()[0][j]][playground.getShipList().get(i).getPos()[1][j]].setBackground(Color.RED);
}
playground.getShipList().remove(i);
}
}
for (int i = 0; i < playground.getPlayground().length; i++) {
for (int j = 0; j < playground.getPlayground()[i].length; j++) {
if(playground.hasNeighbor(i,j,Color.RED) && !playground.getPlayground()[i][j].getBackground().equals(Color.RED)){
playground.getPlayground()[i][j].setEnabled(false);
playground.getPlayground()[i][j].setBackground(Color.GRAY);
}
}
}
}
public void hit(int x, int y, Playground playground) {
for (Ship ship : playground.getShipList()) {
for (int j = 0; j < ship.getSize(); j++) {
if (ship.getPos()[0][j] == x && ship.getPos()[1][j] == y) {
playground.getPlayground()[ship.getPos()[0][j]][ship.getPos()[1][j]].setBackground(Color.ORANGE);
hit = true;
shipDestroyed();
return;
}
}
}
playground.getPlayground()[x][y].setBackground(Color.WHITE);
}
}
package Design;
import Game.Ship;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class Playground implements Serializable {
private final JButton [][] playground = new JButton[10][10];
private final List<Ship> shipList = new ArrayList<>();
private final Color waterColor = new Color(80, 150, 255);
private final Color shipColor = new Color(0,0,0);
public Playground(){
for (int i = 0; i < playground.length; i++) {
for (int j = 0; j < playground[i].length; j++) {
playground[i][j] = new JButton();
playground[i][j].setPreferredSize(new Dimension(20,20));
playground[i][j].setBackground(waterColor);
}
}
}
public JButton[][] getPlayground() {
return playground;
}
public Color getShipColor(){
return shipColor;
}
public List<Ship> getShipList(){
return shipList;
}
public boolean hasNeighbor(int x, int y, Color color) {
boolean hasNeighbor = false;
try{
if (playground[x+1][y].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x+1][y+1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x+1][y-1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x][y+1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if ( playground[x][y-1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x-1][y].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x-1][y+1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
try{
if (playground[x-1][y-1].getBackground().equals(color)) {
hasNeighbor = true;
}
}
catch (ArrayIndexOutOfBoundsException ignored){}
return hasNeighbor;
}
public void changeButtons(int x, int y, int size, boolean change) {
size--;
for (int i = 0; i < playground.length; i++) {
for (int j = 0; j < playground[i].length; j++) {
if(change){
if(!playground[i][j].getBackground().equals(shipColor) && !hasNeighbor(i,j,shipColor)) {
playground[i][j].setBackground(waterColor);
playground[i][j].setEnabled(true);
}
if(hasNeighbor(i,j,shipColor) && !playground[i][j].getBackground().equals(shipColor)) {
playground[i][j].setBackground(Color.GRAY);
playground[i][j].setEnabled(false);
}
}
else{
if(!((i==x+size && j==y)|| (i==x-size && j==y) || (i==x && j==y+size) || (i==x && j==y-size)) && !playground[i][j].getBackground().equals(shipColor)) {
playground[i][j].setBackground(Color.GRAY);
playground[i][j].setEnabled(false);
}
}
}
}
}
public void disableNotPlaceable(int size) {
size--;
for (int i = 0; i < playground.length; i++) {
for (int j = 0; j < playground[i].length; j++) {
boolean placeable = false;
try {
if (playground[i][j+size].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
try {
if (playground[i][j-size].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
try {
if (playground[i+size][j].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
try {
if (playground[i-size][j].getBackground().equals(waterColor) && playground[i][j].getBackground().equals(waterColor)) {
placeable = true;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
if (!(placeable || playground[i][j].getBackground().equals(shipColor) || playground[i][j].getBackground().equals(Color.GRAY))) {
playground[i][j].setBackground(Color.GRAY);
playground[i][j].setEnabled(false);
}
}
}
}
public void clear(){
for (JButton[] jButtons : playground) {
for (JButton jButton : jButtons) {
jButton.setBackground(waterColor);
jButton.setEnabled(true);
}
}
for(int i=shipList.size()-1;i>=0;i--){
shipList.remove(i);
}
}
public Playground copyPlayground(Playground playground, int mode) {
clear();
for(int i=0;i<this.playground.length;i++) {
for(int j=0;j<this.playground[i].length;j++) {
if(mode == 1) {
if(playground.getPlayground()[i][j].getBackground().equals(shipColor)) {
this.getPlayground()[i][j].setBackground(playground.getPlayground()[i][j].getBackground());
}
} else if(mode == 2) this.getPlayground()[i][j].setBackground(playground.getPlayground()[i][j].getBackground());
}
}
for (int i=0;i<playground.getShipList().size();i++) {
if(playground.getShipList().size()>0) {
this.getShipList().add(playground.getShipList().get(i));
}
}
return this;
}
public void enabled(boolean enable){
for (JButton[] jButtons : this.playground) {
for (JButton jButton : jButtons) {
if(jButton.getBackground().equals(waterColor) && enable) jButton.setEnabled(true);
else if(!enable) jButton.setEnabled(false);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论