Answer the question
In order to leave comments, you need to log in
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
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)
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(); // заканчиваем передачу в поток.
}
....
}
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) {
... тут все аналогично серверу только с правками для клиента
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question