Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.util.Observable;
  2. import java.util.Observer;
  3. import javafx.application.Application;
  4. import javafx.event.EventHandler;
  5. import javafx.geometry.Insets;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Alert;
  8. import javafx.scene.control.Alert.AlertType;
  9. import javafx.scene.input.MouseEvent;
  10. import javafx.scene.layout.TilePane;
  11. import javafx.scene.paint.Color;
  12. import javafx.scene.shape.Circle;
  13. import javafx.stage.Stage;
  14.  
  15. public class Connect4View extends Application implements Observer
  16. {
  17.     Circle[][] slots = new Circle[6][7];
  18.     Scene scene;
  19.    
  20.     public static void main(String[] args)
  21.     {
  22.         launch(args);
  23.     }
  24.  
  25.     @Override
  26.     public void start(Stage stage)
  27.     {
  28.         Connect4View view = new Connect4View();
  29.         Connect4Controller controller = new Connect4Controller(view);
  30.         TilePane tilePane = new TilePane(8, 8);
  31.         tilePane.setPrefRows(6);
  32.         tilePane.setPrefColumns(7);
  33.         tilePane.setPadding(new Insets(8));
  34.         for(int i = 0;i < view.slots.length;i++)
  35.         {
  36.             for(int j = 0;j < view.slots[i].length;j++)
  37.             {
  38.                 view.slots[i][j] = new Circle(20, Color.WHITE);
  39.                 tilePane.getChildren().add(view.slots[i][j]);
  40.             }
  41.         }
  42.         tilePane.setOnMouseClicked(new EventHandler<MouseEvent>()
  43.         {
  44.             @Override
  45.             public void handle(MouseEvent e)
  46.             {
  47.                 int x = (int) e.getX();
  48.                 int col;
  49.                
  50.                 if(x <= 52)
  51.                     col = 0;
  52.                 else if(x > 52 && x <= 100)
  53.                     col = 1;
  54.                 else if(x > 100 && x <= 148)
  55.                     col = 2;
  56.                 else if(x > 148 && x <= 196)
  57.                     col = 3;
  58.                 else if(x > 196 && x <= 244)
  59.                     col = 4;
  60.                 else if(x > 244 && x <= 292)
  61.                     col = 5;
  62.                 else
  63.                     col = 6;   
  64.                 try
  65.                 {
  66.                     controller.humanTurn(col);
  67.                     if(controller.isGameEnded())
  68.                     {
  69.                         new Alert(AlertType.INFORMATION, "You won!").showAndWait();
  70.                         tilePane.setDisable(true);
  71.                     }
  72.                     else
  73.                     {
  74.                         controller.computerTurn();
  75.                         if(controller.isGameEnded())
  76.                         {
  77.                             new Alert(AlertType.INFORMATION, "You lost!").showAndWait();
  78.                             tilePane.setDisable(true);
  79.                         }
  80.                     }
  81.                 }
  82.                 catch(InvalidColumnException exception)
  83.                 {
  84.                     new Alert(AlertType.ERROR, "Column full, pick somewhere else!").showAndWait();
  85.                 }
  86.             }
  87.         });
  88.         scene = new Scene(tilePane, Color.BLUE);
  89.         stage.setTitle("Connect 4");
  90.         stage.setScene(scene);
  91.         stage.show();
  92.     }
  93.    
  94.     public void update(Observable o, Object arg)
  95.     {
  96.         slots[((Connect4MoveMessage) arg).row][((Connect4MoveMessage) arg).col].setFill(((Connect4MoveMessage) arg).color);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement