服务器端客户端JAVA游戏动作

服务器端客户端JAVA游戏动作,java,sockets,server-communication,Java,Sockets,Server Communication,我对服务器客户机编码还不熟悉。我有一个制作3石头游戏的项目,我应该在客户端和服务器之间建立连接。我将客户端连接到服务器,但我不知道如何将客户端的移动(点的坐标)传递到服务器,然后将正确的消息从服务器返回到客户端。我不知道字节[]是什么,数据的大小也是一样的 你能解释一下为什么我还要使用byte[]和BUFFSIZE吗?我怎样才能在客户把石头放在黑板上的服务器上呢? 西兰特苷: public class ThreeStonesGameClientSide { private static

我对服务器客户机编码还不熟悉。我有一个制作3石头游戏的项目,我应该在客户端和服务器之间建立连接。我将客户端连接到服务器,但我不知道如何将客户端的移动(点的坐标)传递到服务器,然后将正确的消息从服务器返回到客户端。我不知道字节[]是什么,数据的大小也是一样的 你能解释一下为什么我还要使用byte[]和BUFFSIZE吗?我怎样才能在客户把石头放在黑板上的服务器上呢? 西兰特苷:

public class ThreeStonesGameClientSide {

    private static final int BUFSIZE = 32;
    private final static org.slf4j.Logger LOG = LoggerFactory.getLogger(ThreeStonesGameClientSide.class);
    private String ip;
    private int port;

    public ThreeStonesGameClientSide(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }
    public void connectToServer() throws IOException {
        Socket socket = new Socket(this.ip, this.port);  
        LOG.info("Connected to the server");
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();

       Point recivedPoint = new Point();
       int x = (int) recivedPoint.getX();
       int y = (int) recivedPoint.getY();

       byte[] data = new byte[BUFSIZE];
        // Receive the same string back from the server
        int totalBytesRcvd = 0;  // Total bytes received so far
        int bytesRcvd;           // Bytes received in last read
        while (totalBytesRcvd < data.length) {
            if ((bytesRcvd = in.read(data, totalBytesRcvd,
                    data.length - totalBytesRcvd)) == -1) {
                throw new SocketException("Connection closed prematurely");
            }
            totalBytesRcvd += bytesRcvd;
        }  // data array is full

        System.out.println("Received: " + new String(data));

        socket.close();  // Close the socket and its streams 

    }

}
public class 3stonesgameclientside{
专用静态最终int BUFSIZE=32;
私有最终静态org.slf4j.Logger LOG=LoggerFactory.getLogger(ThreeStonesGameClientSide.class);
私有字符串ip;
专用int端口;
公用三通客户端(字符串ip,内部端口){
this.ip=ip;
this.port=端口;
}
public void connectToServer()引发IOException{
套接字=新套接字(this.ip,this.port);
LOG.info(“连接到服务器”);
InputStream in=socket.getInputStream();
OutputStream out=socket.getOutputStream();
接收点=新点();
int x=(int)receivedpoint.getX();
int y=(int)receivedpoint.getY();
字节[]数据=新字节[BUFSIZE];
//从服务器接收相同的字符串
int totalBytesRcvd=0;//到目前为止接收到的总字节数
int bytesRcvd;//上次读取时接收的字节数
while(totalBytesRcvd
这是服务器端

public class ThreeStonesGameClientSide {

    private static final int BUFSIZE = 32;
    private final static org.slf4j.Logger LOG = LoggerFactory.getLogger(ThreeStonesGameClientSide.class);
    private String ip;
    private int port;

    public ThreeStonesGameClientSide(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }
    public void connectToServer() throws IOException {
        Socket socket = new Socket(this.ip, this.port);  
        LOG.info("Connected to the server");
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();

       Point recivedPoint = new Point();
       int x = (int) recivedPoint.getX();
       int y = (int) recivedPoint.getY();

       byte[] data = new byte[BUFSIZE];
        // Receive the same string back from the server
        int totalBytesRcvd = 0;  // Total bytes received so far
        int bytesRcvd;           // Bytes received in last read
        while (totalBytesRcvd < data.length) {
            if ((bytesRcvd = in.read(data, totalBytesRcvd,
                    data.length - totalBytesRcvd)) == -1) {
                throw new SocketException("Connection closed prematurely");
            }
            totalBytesRcvd += bytesRcvd;
        }  // data array is full

        System.out.println("Received: " + new String(data));

        socket.close();  // Close the socket and its streams 

    }

}
public class 3stonesgameclientside{
专用静态最终int BUFSIZE=32;
私有最终静态org.slf4j.Logger LOG=LoggerFactory.getLogger(ThreeStonesGameClientSide.class);
私有字符串ip;
专用int端口;
公用三通客户端(字符串ip,内部端口){
this.ip=ip;
this.port=端口;
}
public void connectToServer()引发IOException{
套接字=新套接字(this.ip,this.port);
LOG.info(“连接到服务器”);
InputStream in=socket.getInputStream();
OutputStream out=socket.getOutputStream();
接收点=新点();
int x=(int)receivedpoint.getX();
int y=(int)receivedpoint.getY();
字节[]数据=新字节[BUFSIZE];
//从服务器接收相同的字符串
int totalBytesRcvd=0;//到目前为止接收到的总字节数
int bytesRcvd;//上次读取时接收的字节数
while(totalBytesRcvd
这是董事会班

public class Board implements Serializable {

    public final IntegerProperty numOfWhiteStones;
    public final IntegerProperty numOfBlackStones;
    public final ObjectProperty<Stone[][]> board;

    private Point lastPoint;

    public Board(Player p1, Player p2) {
        this.numOfBlackStones = new SimpleIntegerProperty();
        this.numOfWhiteStones = new SimpleIntegerProperty();
        this.board = new SimpleObjectProperty<Stone[][]>(new Stone[11][11]);
    }

    /**
     * Checks if chosen point is along the same x and y axis as the last placed point.
     * @param placementPoint
     * @return 
     */
    public boolean checkSlot(Point placement) {
        Stone[][] board = getBoard();
        int x = placement.x;
        int y = placement.y;
        // Check if placement is within the limits of the inner arry
        if ((x < 0 || x > board.length - 1) && (y < 0 || y > board.length - 1)) {
            return false;
        }
        // Check if place on board is empty
        if (board[x][y] != Stone.EMPTY) {
            return false;
        }
        if (lastPoint == null){
            return true;
        }
        // Check if placement is within the same colum or row as the last played stone
        return (x == lastPoint.x ^ y == lastPoint.y);

    }

    public void placeStone(Point placement, Stone stone) {
        Stone[][] board = getBoard();
        int x = placement.x;
        int y = placement.y;
        board[x][y] = stone;
        if (stone == Stone.BLACK) {
            numOfBlackStones.add(1);
        } else {
            numOfWhiteStones.add(1);
        }
        lastPoint = placement;
    }

    public int calculatePointsFor(Stone stone) {
        Stone[][] board = this.board.get();
        int counter = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board.length; j++) {
                for (int k = 0; k < 4; k++) {
                    if (directionCheck(stone, new Point(i, j), k)){
                        counter++;
                    }
                }
            }
        }
        return counter;
    }

    boolean directionCheck(Stone stone, Point coord, int direction) {
        Stone[][] board = getBoard();
        int x = coord.x;
        int y = coord.y;
        switch (direction) {
            case 0:
                if (coord.x + 2 < board.length) {
                    return (board[x][y] == board[x + 1][y] && board[x + 1][y] == board[x + 2][y]);
                }
                break;
            case 1:
                if ((coord.x + 2 < board.length) && (coord.y + 2 < board.length)) {
                    return (board[x][y] == board[x + 1][y + 1] && board[x + 1][y + 1] == board[x + 2][y + 2]);
                }
                break;
            case 2:
                if (coord.y + 2 < board.length) {
                    return (board[x][y] == board[x][y + 1] && board[x][y + 1] == board[x][y + 2]);
                }
                break;
            case 3:
                if ((coord.x - 2 >= 0) && (coord.y + 2 < board.length)) {
                    return (board[x][y] == board[x - 1][y + 1] && board[x - 1][y + 1] == board[x - 2][y + 2]);
                }
                break;

        }
        return false;
    }

    public final Stone[][] getBoard() {
        return board.get();
    }

    public void setBoard(final Stone[][] isbn) {
        this.board.set(isbn);
    }

    public final ObjectProperty boardProperty() {
        return board;
    }

    public final int getNumOfWhiteStones() {

        return numOfWhiteStones.get();
    }

    public final IntegerProperty numOfWhiteStonesProperty() {
        return numOfWhiteStones;
    }

    public final int getNumOfBlackStones() {
        return numOfBlackStones.get();
    }

    public final IntegerProperty numOfBlackStonesProperty() {
        return numOfBlackStones;
    }

    public final Point getLastPoint() {
        return lastPoint;
    }

}
公共类板实现可序列化{
公共最终综合财产;
Blackstones的公共最终整合财产;
公共财产委员会;
私人点;
公用板(玩家p1、玩家p2){
this.numOfBlackStones=新的SimpleIntegerProperty();
this.numOfWhiteStones=新的SimpleIntegerProperty();
this.board=新的SimpleObject属性(新石头[11][11]);
}
/**
*检查所选点是否与最后放置的点沿相同的x轴和y轴。
*@param placementPoint
*@返回
*/
公共布尔校验槽(点放置){
石头[][]板=getBoard();
int x=位置.x;
int y=位置。y;
//检查放置是否在内棱的限制范围内
如果((x<0|x>board.length-1)和&(y<0|y>board.length-1)){
返回false;
}
//检查船上的位置是否为空
如果(板[x][y]!=Stone.EMPTY){
返回false;
}
if(lastPoint==null){
返回true;
}
//检查放置位置是否与上次玩的石头位于同一列或同一行内
返回(x==lastPoint.x^y==lastPoint.y);
}
公共空隙砂石(点位、石材){
石头[][]板=getBoard();
int x=位置.x;
int y=位置。y;
板[x][y]=石头;
如果(石头==石头。黑色){
加入(1);
}否则{
添加(1);
}
lastPoint=位置;
}
公共点计算点(石头){
Stone[]board=this.board.get();
int计数器=0;
对于(int i=0;i