D
D
DarkByte20152017-06-05 15:50:48
Java
DarkByte2015, 2017-06-05 15:50:48

Through what to implement a very simple network game?

It is necessary to make a very simple network game (tic-tac-toe in the console). Through what will be the easiest way to make network interaction? Maybe spring? I'm not sure that I want to connect sockets here ... There are more problems with them than good. I want a level of abstraction.
upd. I was advised here by such libs: Mina and Netty. Maybe try them ... Googled, Mina certainly looks much simpler. Netty is a bit complicated.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alexandrov, 2017-06-05
@jamakasi666

You have an extremely simple game of tic-tac-toe. It turns out for a network game you need to send the following:
- New game event; //Package id 1
- Set tic/tac toe event(int "x" , "y", and for example boolean type isCircle)//Package id 2
- win event. //Package ID 3
- Error event (bad move) //Package ID 4
I.e. There are 4 types of data in total.
The network packet can be represented as follows:

"number_package_type_id; optional data; optional data"
; - delimiter(string delimiter)

Server side port=7777:
ServerSocket ss = new ServerSocket(7777); //создали сервер
Socket socket = ss.accept();//Ждем клиента
//Сюда попадаем только когда подключился клиента
DataInputStream in = new DataInputStream(socket.getInputStream(); //Получаем поток ввода
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); //Получаем поток вывода
String line = null;
while(true) {
     line = in.readUTF(); // Тут ждем и получаем строку от клиента
     String[] data = line.split(";"); //Разделяем полученную строку в массив строк
     if(data[0].equals("1")){
     //Сбрасываем уровень начинаем новую игру}
     if(data[0].equals("2")){
     //Ставим крестик\нолик по координатам
     int x =data[1]; int y=data[2];boolean isCircle=data[3] //Не забудь типы привести, ну там Integer.parse(data[номер])
     //Ну возможно отправить ответ клиенту
    out.writeUTF(line); // шлем его же строку обратно чтобы клиент у себя отрисовал фигуру или пакет с ошибкой
    out.flush(); // заканчиваем передачу в поток.
     }
     ....
}

At the client:
Socket socket = new Socket(InetAddress.getByName("IP сервера"), 7777);  //создаем сокет
DataInputStream in = new DataInputStream(socket.getInputStream(); //Получаем поток ввода
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); //Получаем поток вывода
String line = null;
while(true) {
... тут все аналогично серверу только с правками для клиента
}

If you want abstraction, then wrap the above pseudo-code in your class.
Spring does not serve these purposes at all, in this task it is like hammering nails with a microscope.
For your task, sockets are exactly what you need because everything is extremely simple and there can be no problems with sockets either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question