sarkanydavidhu

Kliens

Apr 16th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1. package hu.ppke.itk.progverseny2014.kliens;
  2.  
  3. import hu.ppke.itk.progverseny2014.Answer;
  4. import hu.ppke.itk.progverseny2014.Coord;
  5. import hu.ppke.itk.progverseny2014.PlayerMoveCommand;
  6. import hu.ppke.itk.progverseny2014.PlayerShootCommand;
  7. import hu.ppke.itk.progverseny2014.Ship;
  8. import hu.ppke.itk.progverseny2014.ShipMoveCommand;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.io.PrintStream;
  14. import java.io.PrintWriter;
  15. import java.net.ConnectException;
  16. import java.net.Socket;
  17. import java.util.ArrayList;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Random;
  21. import java.util.Set;
  22. import org.eclipse.swt.widgets.Display;
  23.  
  24. public class KliensThread
  25.   extends Thread
  26. {
  27.   private InputStream clientInput;
  28.   private OutputStream clientOutput;
  29.   private Socket socket;
  30.   private KliensJatektabla jatektabla;
  31.   private Display display;
  32.   String ip;
  33.   int port;
  34.   private PlayerMoveCommand lastMoveCommand;
  35.   private PlayerShootCommand lastShootCommand;
  36.   private final Random random = new Random();
  37.   private PrintWriter pw;
  38.   private Kliens kliens;
  39.  
  40.   public KliensThread(Kliens klines, Display display)
  41.   {
  42.     this.kliens = klines;
  43.     this.display = display;
  44.     try
  45.     {
  46.       this.pw = new PrintWriter("log_client_" + this.random.nextInt() + ".txt");
  47.     }
  48.     catch (FileNotFoundException e)
  49.     {
  50.       e.printStackTrace();
  51.     }
  52.     this.jatektabla = new KliensJatektabla(0, 0, this.pw);
  53.   }
  54.  
  55.   private Answer read(InputStream input)
  56.     throws IOException, InterruptedException
  57.   {
  58.     Answer lastAnswer = null;
  59.     while (input.available() >= Answer.getDataLength()) {
  60.       lastAnswer = new Answer(input);
  61.     }
  62.     return lastAnswer;
  63.   }
  64.  
  65.   private void process(Answer answer)
  66.   {
  67.     this.jatektabla.setWidth(answer.getFieldWidth());
  68.     this.jatektabla.setHeight(answer.getFieldHeight());
  69.     if (answer.getNewShipCount() != 0) {
  70.       this.jatektabla.availableShip(answer.getNewShipCount());
  71.     }
  72.     if ((this.lastMoveCommand != null) &&
  73.       (answer.getMoveSuccessCount() != this.lastMoveCommand.shipMoveCommands.size()))
  74.     {
  75.       this.pw.println("Hoppá, nem sikerült minden mozgatás, valami nem jó...");
  76.       System.err.println("Hoppá, nem sikerült minden mozgatás, valami nem jó...");
  77.     }
  78.     this.jatektabla.shoot(answer.getEnemyShootX(), answer.getEnemyShootY());
  79.    
  80.     this.lastMoveCommand = new PlayerMoveCommand();
  81.     for (int i = 0; i < this.jatektabla.getAvailableShipCount(); i++) {
  82.       if (this.random.nextInt(100) < 70)
  83.       {
  84.         Ship generated = this.jatektabla.generateRandomShip(this.jatektabla.getAvailableShipLength(i));
  85.         if (generated != null)
  86.         {
  87.           this.lastMoveCommand.add(new ShipMoveCommand(generated));
  88.           this.pw.println("Új hajó: " + generated.id);
  89.          
  90.           this.jatektabla.removeAvailable(generated.coords.size());
  91.           this.jatektabla.addShip(generated);
  92.         }
  93.       }
  94.     }
  95.     for (int i = 0; i < this.jatektabla.getShips().size(); i++)
  96.     {
  97.       boolean ok = true;
  98.       for (int j = 0; j < this.lastMoveCommand.shipMoveCommands.size(); j++) {
  99.         if (((ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(j)).shipId == ((Ship)this.jatektabla.getShips().get(i)).id) {
  100.           ok = false;
  101.         }
  102.       }
  103.       if (ok) {
  104.         this.lastMoveCommand.add(new ShipMoveCommand(((Ship)this.jatektabla.getShips().get(i)).id));
  105.       }
  106.     }
  107.     for (int i = 0; i < this.lastMoveCommand.shipMoveCommands.size(); i++)
  108.     {
  109.       int j = 0;
  110.       for (;;)
  111.       {
  112.         ShipMoveCommand act = (ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(i);
  113.         switch (this.random.nextInt(4 + j))
  114.         {
  115.         case 0:
  116.           act.moveDirection = 0; break;
  117.         case 1:
  118.           act.moveDirection = 1; break;
  119.         case 2:
  120.           act.moveDirection = 2; break;
  121.         case 3:
  122.           act.moveDirection = 8; break;
  123.         default:
  124.           act.moveDirection = 0;
  125.         }
  126.         Ship currentShip = this.jatektabla.getShip(act.shipId);
  127.         act.newPlace = new Coord(0, 0);
  128.         act.origPlace = new Coord(0, 0);
  129.         if ((currentShip != null) &&
  130.           (act.moveDirection == 0) && (currentShip.damaged.size() == 0) && (this.random.nextInt(j + 1) < 3))
  131.         {
  132.           int oldId = this.random.nextInt(currentShip.coords.size());
  133.           int newNeigh = this.random.nextInt(currentShip.coords.size());
  134.           Coord oldCoord = (Coord)currentShip.coords.get(oldId);
  135.           Coord newCoord = null;
  136.           switch (this.random.nextInt(4))
  137.           {
  138.           case 0:
  139.             newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX() + 1, ((Coord)currentShip.coords.get(newNeigh)).getY()); break;
  140.           case 1:
  141.             newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX() - 1, ((Coord)currentShip.coords.get(newNeigh)).getY()); break;
  142.           case 2:
  143.             newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX(), ((Coord)currentShip.coords.get(newNeigh)).getY() + 1); break;
  144.           case 3:
  145.             newCoord = new Coord(((Coord)currentShip.coords.get(newNeigh)).getX(), ((Coord)currentShip.coords.get(newNeigh)).getY() - 1);
  146.           }
  147.           act.origPlace = oldCoord;
  148.           act.newPlace = newCoord;
  149.         }
  150.         boolean moveShip = this.jatektabla.moveShip((ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(i));
  151.         if (moveShip) {
  152.           break;
  153.         }
  154.         j++;
  155.       }
  156.     }
  157.     this.lastShootCommand = new PlayerShootCommand(this.random.nextInt(this.jatektabla.getHeight()) + 1, this.random.nextInt(this.jatektabla.getWidth()) + 1);
  158.    
  159.  
  160.  
  161.  
  162.  
  163.     Set<Integer> movedShips = new HashSet();
  164.     for (int i = 0; i < this.lastMoveCommand.shipMoveCommands.size(); i++)
  165.     {
  166.       ShipMoveCommand shipMoveCommand = (ShipMoveCommand)this.lastMoveCommand.shipMoveCommands.get(i);
  167.       if (movedShips.contains(Integer.valueOf(shipMoveCommand.shipId))) {
  168.         throw new RuntimeException("Egy hajó kétszer");
  169.       }
  170.       movedShips.add(Integer.valueOf(shipMoveCommand.shipId));
  171.     }
  172.     for (int i = 0; i < this.jatektabla.getShips().size(); i++)
  173.     {
  174.       Ship ship = (Ship)this.jatektabla.getShips().get(i);
  175.       if (!this.jatektabla.isValidShip(ship)) {
  176.         throw new RuntimeException("Nem jó a tábla!");
  177.       }
  178.     }
  179.   }
  180.  
  181.   public void run()
  182.   {
  183.     try
  184.     {
  185.       this.socket = new Socket(this.ip, this.port);
  186.       this.clientInput = this.socket.getInputStream();
  187.       this.clientOutput = this.socket.getOutputStream();
  188.       do
  189.       {
  190.         Answer answer = read(this.clientInput);
  191.         if (answer != null)
  192.         {
  193.           process(answer);
  194.           if (this.display != null) {
  195.             this.display.wake();
  196.           }
  197.           sendMove();
  198.           sendShoot();
  199.           this.kliens.setJatektabla(this.jatektabla);
  200.           this.kliens.redraw();
  201.         }
  202.         else
  203.         {
  204.           sleep(20L);
  205.         }
  206.         if (isInterrupted()) {
  207.           break;
  208.         }
  209.       } while (!this.socket.isClosed());
  210.     }
  211.     catch (ConnectException e)
  212.     {
  213.       e.printStackTrace();
  214.     }
  215.     catch (IOException e)
  216.     {
  217.       System.out.println("Connection ended.");
  218.     }
  219.     catch (InterruptedException localInterruptedException) {}
  220.     try
  221.     {
  222.       if (this.socket != null) {
  223.         this.socket.close();
  224.       }
  225.     }
  226.     catch (IOException e)
  227.     {
  228.       e.printStackTrace();
  229.     }
  230.   }
  231.  
  232.   private void sendShoot()
  233.     throws IOException
  234.   {
  235.     byte[] data = this.lastShootCommand.getData();
  236.     this.clientOutput.write(data);
  237.    
  238.     this.pw.print("Lövés: ");
  239.     for (byte b : data) {
  240.       this.pw.print(Byte.toUnsignedInt(b) + " ");
  241.     }
  242.     this.pw.println();
  243.     this.pw.flush();
  244.   }
  245.  
  246.   private void sendMove()
  247.     throws IOException
  248.   {
  249.     byte[] data = this.lastMoveCommand.getData();
  250.     this.clientOutput.write(data);
  251.    
  252.  
  253.     this.pw.print("Mozgás: ");
  254.     for (byte b : data) {
  255.       this.pw.print(Byte.toUnsignedInt(b) + " ");
  256.     }
  257.     this.pw.println();
  258.     this.pw.flush();
  259.   }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment