Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hu.ppke.itk.progverseny2014.kliens;
- import hu.ppke.itk.progverseny2014.Answer;
- import hu.ppke.itk.progverseny2014.Coord;
- import hu.ppke.itk.progverseny2014.PlayerMoveCommand;
- import hu.ppke.itk.progverseny2014.PlayerShootCommand;
- import hu.ppke.itk.progverseny2014.Ship;
- import hu.ppke.itk.progverseny2014.ShipMoveCommand;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.net.ConnectException;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Random;
- import java.util.Set;
- import org.eclipse.swt.widgets.Display;
- public class KliensThread
- extends Thread
- {
- private InputStream clientInput;
- private OutputStream clientOutput;
- private Socket socket;
- private KliensJatektabla jatektabla;
- private Display display;
- String ip;
- int port;
- private PlayerMoveCommand lastMoveCommand;
- private PlayerShootCommand lastShootCommand;
- private final Random random = new Random();
- private PrintWriter pw;
- private Kliens kliens;
- public KliensThread(Kliens klines, Display display)
- {
- this.kliens = klines;
- this.display = display;
- try
- {
- this.pw = new PrintWriter("log_client_" + this.random.nextInt() + ".txt");
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- this.jatektabla = new KliensJatektabla(0, 0, this.pw);
- }
- private Answer read(InputStream input)
- throws IOException, InterruptedException
- {
- Answer lastAnswer = null;
- while (input.available() >= Answer.getDataLength()) {
- lastAnswer = new Answer(input);
- }
- return lastAnswer;
- }
- private void process(Answer answer)
- {
- this.jatektabla.setWidth(answer.getFieldWidth());
- this.jatektabla.setHeight(answer.getFieldHeight());
- if (answer.getNewShipCount() != 0) {
- this.jatektabla.availableShip(answer.getNewShipCount());
- }
- if ((this.lastMoveCommand != null) &&
- (answer.getMoveSuccessCount() != this.lastMoveCommand.shipMoveCommands.size()))
- {
- this.pw.println("Hoppá, nem sikerült minden mozgatás, valami nem jó...");
- System.err.println("Hoppá, nem sikerült minden mozgatás, valami nem jó...");
- }
- this.jatektabla.shoot(answer.getEnemyShootX(), answer.getEnemyShootY());
- this.lastMoveCommand = new PlayerMoveCommand();
- for (int i = 0; i < this.jatektabla.getAvailableShipCount(); i++) {
- if (this.random.nextInt(100) < 70)
- {
- Ship generated = this.jatektabla.generateRandomShip(this.jatektabla.getAvailableShipLength(i));
- if (generated != null)
- {
- this.lastMoveCommand.add(new ShipMoveCommand(generated));
- this.pw.println("Új hajó: " + generated.id);
- this.jatektabla.removeAvailable(generated.coords.size());
- this.jatektabla.addShip(generated);
- }
- }
- }
- for (int i = 0; i < this.jatektabla.getShips().size(); i++)
- {
- boolean ok = true;
- for (int j = 0; j < this.lastMoveCommand.shipMoveCommands.size(); j++) {
- if (((ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(j)).shipId == ((Ship)this.jatektabla.getShips().get(i)).id) {
- ok = false;
- }
- }
- if (ok) {
- this.lastMoveCommand.add(new ShipMoveCommand(((Ship)this.jatektabla.getShips().get(i)).id));
- }
- }
- for (int i = 0; i < this.lastMoveCommand.shipMoveCommands.size(); i++)
- {
- int j = 0;
- for (;;)
- {
- ShipMoveCommand act = (ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(i);
- switch (this.random.nextInt(4 + j))
- {
- case 0:
- act.moveDirection = 0; break;
- case 1:
- act.moveDirection = 1; break;
- case 2:
- act.moveDirection = 2; break;
- case 3:
- act.moveDirection = 8; break;
- default:
- act.moveDirection = 0;
- }
- Ship currentShip = this.jatektabla.getShip(act.shipId);
- act.newPlace = new Coord(0, 0);
- act.origPlace = new Coord(0, 0);
- if ((currentShip != null) &&
- (act.moveDirection == 0) && (currentShip.damaged.size() == 0) && (this.random.nextInt(j + 1) < 3))
- {
- int oldId = this.random.nextInt(currentShip.coords.size());
- int newNeigh = this.random.nextInt(currentShip.coords.size());
- Coord oldCoord = (Coord)currentShip.coords.get(oldId);
- Coord newCoord = null;
- switch (this.random.nextInt(4))
- {
- case 0:
- newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX() + 1, ((Coord)currentShip.coords.get(newNeigh)).getY()); break;
- case 1:
- newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX() - 1, ((Coord)currentShip.coords.get(newNeigh)).getY()); break;
- case 2:
- newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX(), ((Coord)currentShip.coords.get(newNeigh)).getY() + 1); break;
- case 3:
- newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX(), ((Coord)currentShip.coords.get(newNeigh)).getY() - 1);
- }
- act.origPlace = oldCoord;
- act.newPlace = newCoord;
- }
- boolean moveShip = this.jatektabla.moveShip((ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(i));
- if (moveShip) {
- break;
- }
- j++;
- }
- }
- this.lastShootCommand = new PlayerShootCommand(this.random.nextInt(this.jatektabla.getHeight()) + 1, this.random.nextInt(this.jatektabla.getWidth()) + 1);
- Set<Integer> movedShips = new HashSet();
- for (int i = 0; i < this.lastMoveCommand.shipMoveCommands.size(); i++)
- {
- ShipMoveCommand shipMoveCommand = (ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(i);
- if (movedShips.contains(Integer.valueOf(shipMoveCommand.shipId))) {
- throw new RuntimeException("Egy hajó kétszer");
- }
- movedShips.add(Integer.valueOf(shipMoveCommand.shipId));
- }
- for (int i = 0; i < this.jatektabla.getShips().size(); i++)
- {
- Ship ship = (Ship)this.jatektabla.getShips().get(i);
- if (!this.jatektabla.isValidShip(ship)) {
- throw new RuntimeException("Nem jó a tábla!");
- }
- }
- }
- public void run()
- {
- try
- {
- this.socket = new Socket(this.ip, this.port);
- this.clientInput = this.socket.getInputStream();
- this.clientOutput = this.socket.getOutputStream();
- do
- {
- Answer answer = read(this.clientInput);
- if (answer != null)
- {
- process(answer);
- if (this.display != null) {
- this.display.wake();
- }
- sendMove();
- sendShoot();
- this.kliens.setJatektabla(this.jatektabla);
- this.kliens.redraw();
- }
- else
- {
- sleep(20L);
- }
- if (isInterrupted()) {
- break;
- }
- } while (!this.socket.isClosed());
- }
- catch (ConnectException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- System.out.println("Connection ended.");
- }
- catch (InterruptedException localInterruptedException) {}
- try
- {
- if (this.socket != null) {
- this.socket.close();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- private void sendShoot()
- throws IOException
- {
- byte[] data = this.lastShootCommand.getData();
- this.clientOutput.write(data);
- this.pw.print("Lövés: ");
- for (byte b : data) {
- this.pw.print(Byte.toUnsignedInt(b) + " ");
- }
- this.pw.println();
- this.pw.flush();
- }
- private void sendMove()
- throws IOException
- {
- byte[] data = this.lastMoveCommand.getData();
- this.clientOutput.write(data);
- this.pw.print("Mozgás: ");
- for (byte b : data) {
- this.pw.print(Byte.toUnsignedInt(b) + " ");
- }
- this.pw.println();
- this.pw.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment