java调用函数在服务器中设置数组,在客户端打印数组
所以我正在构建游戏战舰,我正在为棋盘使用一个2d数组,但问题是如果我调用函数来设置数组,我必须调用所有其他使用该数组的函数来获得相同的数组,否则,它将只使用一个新的数组。现在我正在为服务器内部的阵列调用setup函数,当我调用该函数将阵列打印到GUI时,它会打印出一个新的阵列,知道如何解决这个问题吗
服务器类:
public class Server {
private static PrintWriter writerSocket;
private static BufferedReader buffedReader;
public static String player1shipHit;
Controller controller = Controller.returnController();
private static String sharedMessage;
int shipsLeftPlayer1 = 4;
int shipsLeftPlayer2 = 4;
public static String player2hit;
private static InputStreamReader readerSocket;
private final Object lock = new Object();
void waitForLock() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void notifyLock() {
synchronized (lock) {
lock.notify();
}
}
public static void main(String[] args) throws IOException {
new Server().go();
}
void go() throws IOException {
ServerSocket server = new ServerSocket(5000);
Socket connectionOne = server.accept();
controller.player1.setUp(); //Setup
System.out.println("Server>> P1 connected");
writerSocket = new PrintWriter(connectionOne.getOutputStream());
writerSocket.println("Player1> Enter your guess in the format: X,Y");
writerSocket.flush();
new Thread(new JobOne(connectionOne)).start();
Socket connectionTwo = server.accept();
System.out.println("server>> P2 connected");
controller.player2.setUp();
writerSocket = new PrintWriter(connectionTwo.getOutputStream());
writerSocket.flush();
new Thread(new JobTwo(connectionTwo)).start();
}
private class JobOne implements Runnable {
Socket socket;
JobOne(Socket name) {
socket = name;
}
@Override
public void run() {
try {
readerSocket = new InputStreamReader(socket.getInputStream());
buffedReader = new BufferedReader(readerSocket);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Controller.state = false;
try {
sharedMessage = buffedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
String[] coordinates = sharedMessage.split(",");
int x = Integer.parseInt(coordinates[0]);
int y = Integer.parseInt(coordinates[1]);
if (controller.player2.board.testHit(x, y)) {
controller.player1.board.updateBoard("Hit", x, y);
sharedMessage = "Player 2 has been hit!";
writerSocket.println(sharedMessage);
writerSocket.flush();
if (controller.player2.testShip("C", x, y)) {
sharedMessage = "Player 2's" + player1shipHit + " is Down";
shipsLeftPlayer2--;
if (shipsLeftPlayer2 == 0) {
sharedMessage = "Player 1 has won!";
}
}
} else if (!controller.player2.board.testHit(x, y)) {
sharedMessage = "Player 1 misses!";
}
try {
writerSocket = new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
notifyLock();
writerSocket.println(sharedMessage);
writerSocket.flush();
waitForLock();
}
}
}
private class JobTwo implements Runnable {
Socket socket;
JobTwo(Socket name) {
socket = name;
}
@Override
public void run() {
try {
writerSocket = new PrintWriter((socket.getOutputStream()));
readerSocket = new InputStreamReader(socket.getInputStream());
buffedReader = new BufferedReader(readerSocket);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
waitForLock();
writerSocket.println(sharedMessage);
writerSocket.flush();
while (true) {
Controller.state1 = false;
writerSocket.println("Player2> Enter the coordinates in the format X,Y:");
writerSocket.flush();
try {
sharedMessage = buffedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
String[] coordinates = sharedMessage.split(",");
int x = Integer.parseInt(coordinates[0]);
int y = Integer.parseInt(coordinates[1]);
;
System.out.println("Player 2 X:" + x);
System.out.println("PLayer 2 Y:" + y);
Coordinate coordinate = new Coordinate(x, y);
if (controller.player1.board.testHit(x, y)) {
controller.player2.board.updateBoard("Hit", x, y);
sharedMessage = "Player 2 has a hit";
if (controller.player1.testShip(player2hit, x, y)) {
sharedMessage = "Player1s" + player2hit + "is down!" + 50;
shipsLeftPlayer1--;
if (shipsLeftPlayer1 == 0) {
sharedMessage = "PLayer 2 has won!";
}
}
} else {
controller.player2.board.updateBoard("Miss", x, y);
sharedMessage = "PLayer 2 has a miss.";
}
notifyLock();
}
}
}
}
}
董事会级别:
public class Board {
public int[][] board1 = null;
public int[][] board2;
public Board() {
board1 = new int[20][20];
board2 = new int[20][20];
}
public void updateBoard(String type, int x, int y) {
if (type.equals("hit")) {
board2[x][y] = 1;
} else if (type.equals("miss")) {
board2[x][y] = '0';
}
}
public boolean testHit(int x, int y) {
if (board1[x][y] == 1) {
board1[x][y] = 5;
System.out.println("Its a hit!");
return true;
} else {
System.out.println("It's a miss");
return false;
}
}
}
玩家等级:
public class Player {
Destroyer destroyer;
Battleship battleship;
Submarine submarine;
Board board;
int[][] board1;
Turn turn;
int playerId;
boolean guiState;
Carrier carrier;
ArrayList<String> ships = new ArrayList<String>(4);
public Player(int playerIdSet){
board1 = new int[20][20];
playerId = playerIdSet;
board = new Board();
turn = new Turn();
ships.add("Carrier");
ships.add("Destroyer");
ships.add("Battleship");
ships.add("Submarine");
}
public void setSubmarine(Submarine subSet){
submarine = subSet;
}
public Submarine getSubmarine(){
return submarine;
}
public void setBattleship(Battleship battleshipSet) {
battleship = battleshipSet;
}
public Battleship getBattleship() {
return battleship;
}
public void setDestroyer(Destroyer destroyerSet) {
destroyer = destroyerSet;
}
public void setCarrier(Carrier carrierSet){
carrier = carrierSet;
}
public Carrier getCarrier(){
return carrier;
}
public Destroyer getDestroyer() {
return destroyer;
}
public Board getBoard(){
return board;
}
public void setUp(){
for (String ship : ships) {
int x;
int y;
switch (ship) {
case "Destroyer" -> {
x = (int) (Math.random() * (9) + 0);
y = (int) (Math.random() * (9) + 0);
if (board.board1[x][y] == 0) {
Destroyer destroyer = new Destroyer(x, y);
setDestroyer(destroyer);
getDestroyer().createShip(playerId);
} else {
x = (int) (Math.random() * (9) + 0);
y = (int) (Math.random() * (9) + 0);
}
}
case "Carrier" -> {
x = (int) (Math.random() * (9) + 1);
y = (int) (Math.random() * (9) + 1);
if (board.board1[x][y] == 0) {
Carrier carrier = new Carrier(x, y);
setCarrier(carrier);
getCarrier().createShip(playerId);
} else {
x = (int) (Math.random() * (9) + 0);
y = (int) (Math.random() * (9) + 0);
}
}
case "Battleship" -> {
x = (int) (Math.random() * (9) + 0);
y = (int) (Math.random() * (9) + 0);
if (board.board1[x][y] == 0) {
System.out.println("X:" + x);
System.out.println("Y:" + y);
Battleship battleship = new Battleship(x, y);
setBattleship(battleship);
getBattleship().createShip(playerId);
} else {
x = (int) (Math.random() * (9) + 0);
y = (int) (Math.random() * (9) + 0);
}
}
case "Submarine" -> {
x = (int) (Math.random() * (9) + 0);
y = (int) (Math.random() * (9) + 0);
if (board.board1[x][y] == 0) {
System.out.println("X:" + x);
System.out.println("Y:" + y);
Submarine submarine = new Submarine(x, y);
setSubmarine(submarine);
getSubmarine().createShip(playerId);
} else {
x = (int) (Math.random() * (9) + 0);
y = (int) (Math.random() * (9) + 0);
System.out.println("Error checking...\n");
}
}
}
}
}
public void printBoard(){
System.out.println("Attempting to print");
for(int x = 0; x < board.board1.length; x++) {
for (int y =0; y < board.board1[x].length; y++){
System.out.println(board.board1[x][y]);
}
}
}
public void displayBoard(GUIController guiController) {
StringBuilder grid = new StringBuilder();
StringBuilder grid1 = new StringBuilder();
for(int x = 0; x < board.board1.length; x++) {
grid.append(x);
for (int y =0; y < board.board1[x].length; y++){
grid.append("[").append(board.board1[x][y]).append("]");
grid.append(" ");
}
grid.append("\n");
}
guiController.gui.board1Area.setText(String.valueOf(grid));
for(int i =0; i < board.board2.length; i++){
for(int j =0; j < board.board2.length; j++){
grid1.append("[").append(board.board2[i][j]).append("]");
grid.append(" ");
}
grid1.append("\n");
}
guiController.gui.board2Area.setText(String.valueOf(grid1));
}
public boolean testShip(String ship,int row, int col){
switch (ship){
case "Battleship" -> {
for (int[] ints : battleship.battleShipArray) {
for (int anInt : ints) {
return anInt == battleship.battleShipArray[row][col];
}
}
}
case "Carrier" -> {
for (int[] ints : carrier.carrierArray) {
for (int anInt : ints) {
return anInt == carrier.carrierArray[row][col];
}
}
}
case "Submarine" -> {
for (int[] ints : submarine.submarineArray) {
for (int anInt : ints) {
return anInt == submarine.submarineArray[row][col];
}
}
}
case "Destroyer" -> {
for (int[] ints : destroyer.destroyerArray) {
for (int anInt : ints) {
return anInt == destroyer.destroyerArray[row][col];
}
}
}
}
return false;
}
void markShipHit(int x, int y){
for(String ship : ships){
switch(ship){
/*case "Carrier" -> {
if(carrier.testHit(1,x,y)){
carrier.carrierArray[x][y] = 0;
Server.player1shipHit = "Carrier";
}
}*/
case "Battleship" -> {
if(battleship.testHit(1,x,y)){
battleship.battleShipArray[x][y] = 0;
Server.player1shipHit="Battleship";
}
}
case "Destroyer" ->{
if(destroyer.testHit(1,x,y)){
destroyer.destroyerArray[x][y] = 0;
Server.player1shipHit="Destroyer";
}
}
case "Submarine" ->{
if(submarine.testHit(1,x,y)){
submarine.submarineArray[x][y] = 0;
Server.player1shipHit="Submarine";
}
}
}
}
}
}
客户示例:
public class PlayerOne {
Socket socket;
InputStreamReader readerSocket;
PrintWriter writerSocket;
BufferedReader bufferedReader;
public static String input;
GUIController guiController;
boolean state = true;
Controller controller = Controller.returnController();
boolean setupState = false;
public static void main(String[] args){
new PlayerOne().go();
}
private void go(){
guiController = new GUIController();
try {
socket = new Socket("127.0.0.1", 5000);
receiveRead();
while(true) {
if (Controller.state) {
writeSend();
Controller.state = false;
} else {
receiveRead();
Controller.state = true;
}
}
}catch (IOException e){
e.printStackTrace();
}
}
void writeSend()throws IOException {
while (input == null) {
input = guiController.inputLine;
}
writerSocket = new PrintWriter(socket.getOutputStream());
writerSocket.println(input);
writerSocket.flush();
controller.player1.displayBoard(guiController);
}
void receiveRead () throws IOException {
readerSocket = new InputStreamReader(socket.getInputStream());
bufferedReader = new BufferedReader(readerSocket);
String line = bufferedReader.readLine();
guiController.gui.setOutputText(line);
controller.player1.displayBoard(guiController);
}
}
共 (0) 个答案