Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.68 KB | None | 0 0
  1. package Client;
  2.  
  3. import javafx.application.Platform;
  4. import javafx.concurrent.*;
  5. import javafx.event.ActionEvent;
  6. import javafx.fxml.*;
  7. import javafx.geometry.Pos;
  8. import javafx.scene.Parent;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.Label;
  12. import javafx.scene.control.MenuItem;
  13. import javafx.scene.effect.Lighting;
  14. import javafx.scene.image.Image;
  15. import javafx.scene.layout.AnchorPane;
  16. import javafx.scene.layout.GridPane;
  17. import javafx.scene.paint.Paint;
  18. import javafx.scene.shape.Circle;
  19. import javafx.scene.shape.Polygon;
  20. import javafx.stage.Modality;
  21. import javafx.stage.Stage;
  22.  
  23. import java.io.IOException;
  24. import java.net.URL;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.ResourceBundle;
  28.  
  29. /*
  30.  *  TODO: Finish implementing movement
  31.  *  TODO: Write startTurn()
  32.  */
  33. public class Controller implements Initializable
  34. {
  35.     private Game game;
  36.     private Client client;
  37.     private List<Circle> pawnsGUI = new ArrayList();
  38.     private int playerNum = -1;
  39.     private int currentX;
  40.     private int currentY;
  41.  
  42.     @FXML MenuItem twoPlayers;
  43.     @FXML MenuItem threePlayers;
  44.     @FXML MenuItem fourPlayers;
  45.     @FXML MenuItem sixPlayers;
  46.     @FXML MenuItem startGame;
  47.  
  48.     @FXML AnchorPane mainPane;
  49.     @FXML GridPane boardGrid;
  50.     @FXML MenuItem exitMI;
  51.     @FXML Button endTurnB;
  52.     @FXML Label redPoints, bluePoints, greenPoints, yellowPoints, blackPoints, whitePoints;
  53.  
  54.  
  55.  
  56.     @Override // Initializer for our GUI Controller
  57.     public void initialize(URL location, ResourceBundle resources)
  58.     {
  59.         connectToServer();
  60.         boardGrid.setAlignment(Pos.CENTER);
  61.     }
  62.  
  63.     public void connectToServer()
  64.     {
  65.         client = new Client(this);
  66.         client.start();
  67.     }
  68.  
  69.     // Refreshing board
  70.     private void refresh()
  71.     {
  72.         boardGrid.getChildren().clear();
  73.  
  74.         for(int i = 0; i <= 18; i++)
  75.         {
  76.             for(int j = 0; j <= 14; j++)
  77.             {
  78.                 if(game.getBoard().getField(i, j).getClass() == AccessibleField.class)
  79.                 {
  80.                     if(i % 2 == 1)
  81.                     {
  82.                         boardFill(i, j, false);
  83.                     }
  84.                     else
  85.                     {
  86.                         boardFill(i, j, true);
  87.                     }
  88.                 }
  89.                 else if(game.getBoard().getField(i, j).getClass() == WinningField.class)
  90.                 {
  91.                     if(i % 2 == 1)
  92.                     {
  93.                         boardFill(i, j, false);
  94.                     }
  95.                     else
  96.                     {
  97.                         boardFill(i, j, true);
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.  
  103.         fillPawns();
  104.     }
  105.  
  106.     // Filling board with proper colored pawns and fields
  107.     private void boardFill(int i, int j, boolean shifted)
  108.     {
  109.         Polygon poly = new Polygon(27*(1), 27*(0),
  110.                 27*(0.5), 27*(0.86602540378),
  111.                 27*(-0.5), 27*(0.86602540378),
  112.                 27*(-1), 27*(0),
  113.                 27*(-0.5), 27*(-0.86602540378),
  114.                 27*(0.5), 27*(-0.86602540378));
  115.         if(shifted)
  116.         {
  117.             poly.translateYProperty().set(-23);
  118.         }
  119.         poly.setOnMouseClicked(e -> fieldClicked(i, j));
  120.         if(game.getBoard().getField(i, j).getClass() == WinningField.class)
  121.         {
  122.             for(int var = 0; var < game.getPlayers().length; var++)
  123.             {
  124.                 if(game.getBoard().getField(i, j).getOwner() != null){
  125.                     if(game.getBoard().getField(i, j).getOwner().equals(game.getPlayers()[var]))
  126.                 {
  127.                     switch (var) {
  128.                         case 0:
  129.                             poly.setFill(Paint.valueOf("RED"));
  130.                             break;
  131.                         case 1:
  132.                             poly.setFill(Paint.valueOf("BLUE"));
  133.                             break;
  134.                         case 2:
  135.                             poly.setFill(Paint.valueOf("GREEN"));
  136.                             break;
  137.                         case 3:
  138.                             poly.setFill(Paint.valueOf("YELLOW"));
  139.                             break;
  140.                         case 4:
  141.                             poly.setFill(Paint.valueOf("#4f4f4f"));
  142.                             break;
  143.                         case 5:
  144.                             poly.setFill(Paint.valueOf("WHITE"));
  145.                             break;
  146.  
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.         else
  153.         {
  154.             poly.setFill(Paint.valueOf("#d6d6d6"));
  155.         }
  156.  
  157.         poly.setStroke(Paint.valueOf("BLACK"));
  158.         boardGrid.add(poly, i, j);
  159.     }
  160.  
  161.     private void fieldClicked(int x, int y)
  162.     {
  163.         for(int i = 0; i < pawnsGUI.size(); i++)
  164.         {
  165.             if (pawnsGUI.get(i).getEffect() != null)
  166.             {
  167.                 movePawn(currentX, currentY, x, y);
  168.                 client.sendMessage("M "+currentX+" "+currentY+" "+x+" "+y+" "+playerNum);
  169.                 endTurn();
  170.             }
  171.         }
  172.     }
  173.  
  174.     private void fillPawns()
  175.     {
  176.         for(int i = 0; i < game.getPlayers().length ; i++)
  177.         {
  178.             for(int j = 0; j < 10; j++)
  179.             {
  180.                 int x = game.getPlayers()[i].getPawns().get(j).getCoordinateX();
  181.                 int y = game.getPlayers()[i].getPawns().get(j).getCoordinateY();
  182.  
  183.                 Circle circle = new Circle(15);
  184.                 if(x % 2 != 1)
  185.                 {
  186.                     circle.translateYProperty().set(-23);
  187.                 }
  188.                 circle.translateXProperty().set(14);
  189.                 pawnsGUI.add(circle);
  190.                 circle.setOnMouseClicked(event -> pawnClicked(circle, x, y));
  191.  
  192.                 switch (i) {
  193.                     case 0:
  194.                         circle.setFill(Paint.valueOf("RED"));
  195.                         break;
  196.                     case 1:
  197.                         circle.setFill(Paint.valueOf("BLUE"));
  198.                         break;
  199.                     case 2:
  200.                         circle.setFill(Paint.valueOf("GREEN"));
  201.                         break;
  202.                     case 3:
  203.                         circle.setFill(Paint.valueOf("YELLOW"));
  204.                         break;
  205.                     case 4:
  206.                         circle.setFill(Paint.valueOf("BLACK"));
  207.                         break;
  208.                     case 5:
  209.                         circle.setFill(Paint.valueOf("WHITE"));
  210.                         break;
  211.                 }
  212.                 circle.setStroke(Paint.valueOf("BLACK"));
  213.                 boardGrid.add(circle, x, y);
  214.             }
  215.         }
  216.     }
  217.  
  218.     private void pawnClicked(Circle circle, int x, int y)
  219.     {
  220.         if(game.getBoard().getField(x, y).getPawn().getOwner().equals(game.getPlayers()[playerNum]))
  221.         {
  222.             // Clear effects for other pawns
  223.             for(int i = 0; i < pawnsGUI.size(); i++)
  224.             {
  225.                 pawnsGUI.get(i).setEffect(null);
  226.             }
  227.             currentX = x;
  228.             currentY = y;
  229.             // Set effect for this pawn
  230.             Lighting lighting = new Lighting();
  231.             circle.setEffect(lighting);
  232.         }
  233.     }
  234.  
  235.     public void startTurn()
  236.     {
  237.         mainPane.setDisable(false);
  238.     }
  239.  
  240.     public void setPlayerNum(int playerNum)
  241.     {
  242.         this.playerNum = playerNum;
  243.     }
  244.  
  245.     public void movePawn(int x1, int y1, int x2, int y2)
  246.     {
  247.         Pawn pawnTemp = game.getBoard().getField(x1, y1).getPawn();
  248.         game.getBoard().getField(x1, y1).setPawn(null);
  249.         game.getBoard().getField(x2, y2).setPawn(pawnTemp);
  250.  
  251.         refresh();
  252.     }
  253.  
  254.     @FXML
  255.     public void newGame(ActionEvent e)
  256.     {
  257.         GameDirector director = new GameDirector();
  258.         GameBuilder builder;
  259.  
  260.         if(e.getSource().equals(twoPlayers))
  261.         {
  262.             builder = new CCBoard2P();
  263.             client.sendMessage("I 2");
  264.         }
  265.         else if(e.getSource().equals(threePlayers))
  266.         {
  267.             builder = new CCBoard3P();
  268.             client.sendMessage("I 3");
  269.         }
  270.         else if(e.getSource().equals(fourPlayers))
  271.         {
  272.             builder = new CCBoard4P();
  273.             client.sendMessage("I 4");
  274.         }
  275.         else
  276.         {
  277.             builder = new CCBoard6P();
  278.             client.sendMessage("I 6");
  279.         }
  280.  
  281.         director.setBuilder(builder);
  282.         director.createGame();
  283.         game = builder.setupGame();
  284.  
  285.         startGame.setDisable(true);
  286.         mainPane.setDisable(true);
  287.  
  288.         refresh();
  289.     }
  290.  
  291.     @FXML // EXIT menu item handler (exits game)
  292.     public void exitHandler()
  293.     {
  294.         if(client.isAlive())
  295.         {
  296.             client.sendMessage("END");
  297.         }
  298.         System.exit(0);
  299.     }
  300.  
  301.     @FXML // END TURN button handler (increments all score values)
  302.     public void endTurn()
  303.     {
  304.         System.out.println("Turn passed... \n");
  305.  
  306.         if(!boardGrid.getChildren().isEmpty())
  307.         {
  308.             refresh();
  309.             mainPane.setDisable(true);
  310.         }
  311.     }
  312.  
  313.     @FXML // RULES manu item handler (creates dialog window with rules)
  314.     public void rulesHandler()
  315.     {
  316.         FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/FXML/Rules.fxml"));
  317.         Parent root = null;
  318.         try
  319.         {
  320.             root = fxmlLoader.load();
  321.         }
  322.         catch (IOException e)
  323.         {
  324.             e.printStackTrace();
  325.         }
  326.  
  327.         Stage rulesDialog = new Stage();
  328.         rulesDialog.setTitle("Rules");
  329.         rulesDialog.getIcons().add(new Image(getClass().getResourceAsStream("icon.jpg")));
  330.         rulesDialog.initModality(Modality.APPLICATION_MODAL);
  331.         rulesDialog.setScene(new Scene(root));
  332.         rulesDialog.setResizable(false);
  333.         rulesDialog.show();
  334.     }
  335.  
  336.     @FXML // AUTHOR menu item handler (creates dialog window with authors)
  337.     public void authorsHandler()
  338.     {
  339.         FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/FXML/Authors.fxml"));
  340.         Parent root = null;
  341.         try
  342.         {
  343.             root = fxmlLoader.load();
  344.         }
  345.         catch (IOException e)
  346.         {
  347.             e.printStackTrace();
  348.         }
  349.  
  350.         Stage authorsDialog = new Stage();
  351.         authorsDialog.setTitle("Authors");
  352.         authorsDialog.getIcons().add(new Image(getClass().getResourceAsStream("icon.jpg")));
  353.         authorsDialog.initModality(Modality.APPLICATION_MODAL);
  354.         authorsDialog.setScene(new Scene(root));
  355.         authorsDialog.setResizable(false);
  356.         authorsDialog.show();
  357.     }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement