Noam_15

Connect4

May 15th, 2018
1,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.94 KB | None | 0 0
  1.  
  2. // FROM: https://www.youtube.com/watch?v=B5H_t0A_C14
  3.  
  4.  
  5. package fourinaline;
  6.  
  7. import com.almasb.fxgl.app.GameApplication;
  8. import EnumsFolder.EntityType;
  9. import com.almasb.fxgl.app.GameApplication;
  10. import com.almasb.fxgl.entity.*;
  11. import com.almasb.fxgl.input.Input;
  12. import com.almasb.fxgl.input.UserAction;
  13. import com.almasb.fxgl.physics.CollisionHandler;
  14. import com.almasb.fxgl.settings.GameSettings;
  15. import com.almasb.fxgl.texture.Texture;
  16. import static com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation.ANONYMOUS.optional;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Optional;
  21. import java.util.stream.Collectors;
  22. import java.util.stream.IntStream;
  23. import javafx.scene.input.KeyCode;
  24. import javafx.scene.paint.Color;
  25. import javafx.scene.shape.Rectangle;
  26. import javafx.scene.text.Text;
  27. import javafx.util.Duration;
  28. import javafx.animation.TranslateTransition;
  29. import javafx.application.Application;
  30. import javafx.geometry.Point2D;
  31. import javafx.scene.Parent;
  32. import javafx.scene.Scene;
  33. import javafx.scene.effect.Light;
  34. import javafx.scene.effect.Lighting;
  35. import javafx.scene.layout.Pane;
  36. import javafx.scene.shape.Circle;
  37. import javafx.scene.shape.Shape;
  38. import javafx.stage.Stage;
  39.  
  40.  
  41.  
  42.  
  43. public class Connect4Try extends Application{
  44.    
  45.     private static final int TILE_SIZE = 80;
  46.     private static final int COLUMNS = 7;
  47.     private static final int ROWS = 6;
  48.    
  49.     private boolean redMove = true;
  50.     private Disc[][] grid = new Disc[COLUMNS][ROWS];
  51.    
  52.     private Pane discRoot = new Pane();
  53.  
  54.    
  55.     private Parent createContent(){
  56.         Pane root = new Pane();
  57.         root.getChildren().add(discRoot);
  58.        
  59.         Shape gridsShape = makeGrid();
  60.         root.getChildren().add(gridsShape);
  61.         root.getChildren().addAll(makeColumns());
  62.        
  63.         return root;
  64.     }
  65.    
  66.     private Shape makeGrid() {
  67.         Shape shape = new Rectangle((COLUMNS + 1) * TILE_SIZE, (ROWS + 1) * TILE_SIZE);
  68.        
  69.         for(int y = 0; y < ROWS; y++){
  70.             for(int x = 0; x < COLUMNS; x++){
  71.                 Circle circle = new Circle(TILE_SIZE / 2);
  72.                 circle.setCenterX(TILE_SIZE / 2);
  73.                 circle.setCenterY(TILE_SIZE / 2);
  74.                 circle.setTranslateX(x * (TILE_SIZE + 5) + TILE_SIZE / 4);
  75.                 circle.setTranslateY(y * (TILE_SIZE + 5) + TILE_SIZE / 4);
  76.                
  77.                 shape = Shape.subtract(shape, circle);
  78.             }
  79.         }
  80.        
  81.         Light.Distant light = new Light.Distant();
  82.         light.setAzimuth(45.0);
  83.         light.setElevation(30.0);
  84.        
  85.         Lighting lighting = new Lighting();
  86.         lighting.setLight(light);
  87.         lighting.setSurfaceScale(5.0);
  88.        
  89.         shape.setEffect(lighting);
  90.         shape.setFill(Color.BLUE);
  91.        
  92.         return shape;
  93.     }
  94.    
  95.     private List<Rectangle> makeColumns(){
  96.         List<Rectangle> list = new ArrayList<>();
  97.        
  98.         for(int x = 0; x < COLUMNS; x++) {
  99.             Rectangle rect = new Rectangle(TILE_SIZE, (ROWS + 1) * TILE_SIZE);
  100.             rect.setTranslateX(x * (TILE_SIZE + 5) + TILE_SIZE / 4);
  101.             rect.setFill(Color.TRANSPARENT);
  102.            
  103.             rect.setOnMouseEntered(e -> rect.setFill(Color.rgb(200, 200, 50, 0.3)));
  104.             rect.setOnMouseExited(e -> rect.setFill(Color.TRANSPARENT));
  105.            
  106.             final int column = x;
  107.             rect.setOnMouseClicked(e -> placeDisc(new Disc(redMove), column));
  108.            
  109.             list.add(rect);
  110.         }
  111.         return list;
  112.     }
  113.    
  114.     private void placeDisc(Disc disc, int column){
  115.         int row = ROWS - 1;
  116.         do{
  117.             if(!getDisc(column, row).isPresent())
  118.                 break;
  119.             row--;
  120.         }while(row >= 0);
  121.         if(row < 0)
  122.             return;
  123.        
  124.         grid[column][row] = disc;
  125.         discRoot.getChildren().add(disc);
  126.         disc.setTranslateX(column * (TILE_SIZE + 5) + TILE_SIZE / 4);
  127.        
  128.         final int currentRow = row;
  129.        
  130.         TranslateTransition animation = new TranslateTransition(Duration.seconds(0.5), disc);
  131.         animation.setToY(row * (TILE_SIZE + 5) + TILE_SIZE / 4);
  132.         animation.setOnFinished(e -> {
  133.             if(gameEnded(column, currentRow)){
  134.                 gameOver();
  135.             }
  136.            
  137.             redMove = !redMove;
  138.         });
  139.         animation.play();
  140.     }
  141.    
  142.     private boolean gameEnded(int column, int row){
  143.         List<Point2D> vertical = IntStream.rangeClosed(row - 3, row + 3)
  144.                 .mapToObj(r -> new Point2D(column, r))
  145.                 .collect(Collectors.toList());
  146.        
  147.         List<Point2D> horizontal = IntStream.rangeClosed(column - 3, column + 3)
  148.                 .mapToObj(c -> new Point2D(c, row))
  149.                 .collect(Collectors.toList());
  150.        
  151.         Point2D topLeft = new Point2D(column - 3, row - 3);
  152.         List<Point2D> diagonal1 = IntStream.rangeClosed(0, 6)
  153.                 .mapToObj(i -> topLeft.add(i, i))
  154.                 .collect(Collectors.toList());
  155.        
  156.         Point2D botLeft = new Point2D(column - 3, row + 3);
  157.         List<Point2D> diagonal2 = IntStream.rangeClosed(0, 6)
  158.                 .mapToObj(i -> botLeft.add(i, -i))
  159.                 .collect(Collectors.toList());
  160.        
  161.         return checkRange(vertical) || checkRange(horizontal)
  162.                 || checkRange(diagonal1) || checkRange(diagonal2);
  163.     }
  164.    
  165.     private boolean checkRange(List<Point2D> points){
  166.         int chain = 0;
  167.         for(Point2D p : points){
  168.             int column = (int) p.getX();
  169.             int row = (int) p.getY();
  170.            
  171.             Disc disc = getDisc(column, row).orElse(new Disc(!redMove));
  172.             if(disc.red == redMove){
  173.                 chain++;
  174.                 if(chain == 4){
  175.                     return true;
  176.                 }
  177.             } else {
  178.                 chain = 0;
  179.             }
  180.         }
  181.         return false;
  182.     }
  183.    
  184.     private void gameOver(){
  185.         System.out.println("Winner: " + (redMove ? "RED:" : "YELLOW"));
  186.     }
  187.    
  188.     private Optional<Disc> getDisc(int column, int row){
  189.         if(column < 0 || column >= COLUMNS
  190.                 || row < 0 || row >= ROWS)
  191.             return Optional.empty();
  192.        
  193.         return Optional.ofNullable(grid[column][row]);
  194.     }
  195.    
  196.     private static class Disc extends Circle {
  197.         private final boolean red;
  198.         public Disc(boolean red){
  199.             super(TILE_SIZE / 2, red? Color.RED : Color.YELLOW);
  200.             this.red = red;
  201.            
  202.             setCenterX(TILE_SIZE / 2);
  203.             setCenterY(TILE_SIZE / 2);
  204.         }
  205.     }
  206.    
  207.     @Override
  208.     public void start(Stage stage) throws Exception{
  209.         stage.setScene(new Scene(createContent()));
  210.         stage.show();
  211.     }
  212.    
  213.     public static void main(String[] args){
  214.        
  215.         launch(args);
  216.        
  217.     }
  218.    
  219. }
Advertisement
Add Comment
Please, Sign In to add comment