Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hi, I am making a game in Java FX but I have completed the connect 4 games. I need help with making that into Java

hi,

I am making a game in Java FX but I have completed the connect 4 games. I need help with making that into Java model view controller which I don't know hows that work. Please help me with creating MVC. Please help me, I spend so much time to figured it out.

here is my working code:

import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.layout.*; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class ConnectFour extends Application { //declaring an instance variables  int columns = 7; int rows = 6; private char playerTurn='p'; private Cell[][] cell = new Cell[rows][columns]; @Override public void start(Stage primaryStage) throws Exception { //creating an GirdPane  GridPane gridPane = new GridPane(); for (int i = 0; i < 6; i++) for (int j = 0; j < 7; j++) gridPane.add(cell[i][j] = new Cell(i,j), j, i); //creating an border pan to display the connect-four game  BorderPane borderPane = new BorderPane(); HBox bottomPane = new HBox(); bottomPane.setAlignment(Pos.CENTER); bottomPane.setPadding(new Insets(20)); borderPane.setBottom(bottomPane); borderPane.setCenter(gridPane); //setting the size for the consule  Scene scene = new Scene(borderPane, 750, 750); scene.setFill(Color.GREEN); primaryStage.setTitle("Exercise16_31"); primaryStage.setScene(scene); primaryStage.setResizable(true); primaryStage.show(); } //now all thses function going to check all circle  public boolean isWon(char occurrence) { for (int p = 0; p < 6; p++) if (cell[p][0].getOcc() == occurrence && cell[p][1].getOcc() == occurrence && cell[p][2].getOcc() == occurrence && cell[p][3].getOcc() == occurrence) { cell[p][0].setFlashCircles(occurrence); cell[p][1].setFlashCircles(occurrence); cell[p][2].setFlashCircles(occurrence); cell[p][3].setFlashCircles(occurrence); return true; } for (int i = 0; i < 6; i++) if (cell[i][1].getOcc() == occurrence && cell[i][2].getOcc() == occurrence && cell[i][3].getOcc() == occurrence && cell[i][4].getOcc() == occurrence) { cell[i][1].setFlashCircles(occurrence); cell[i][2].setFlashCircles(occurrence); cell[i][3].setFlashCircles(occurrence); cell[i][4].setFlashCircles(occurrence); return true; } for (int i = 0; i < 6; i++) if (cell[i][2].getOcc() == occurrence && cell[i][3].getOcc() == occurrence && cell[i][4].getOcc() == occurrence && cell[i][5].getOcc() == occurrence) { cell[i][2].setFlashCircles(occurrence); cell[i][3].setFlashCircles(occurrence); cell[i][4].setFlashCircles(occurrence); cell[i][5].setFlashCircles(occurrence); return true; } for (int i = 0; i < 6; i++) if (cell[i][3].getOcc() == occurrence && cell[i][4].getOcc() == occurrence && cell[i][5].getOcc() == occurrence && cell[i][6].getOcc() == occurrence) { cell[i][3].setFlashCircles(occurrence); cell[i][4].setFlashCircles(occurrence); cell[i][5].setFlashCircles(occurrence); cell[i][6].setFlashCircles(occurrence); return true; } for (int j = 0; j < 7; j++) if (cell[0][j].getOcc() == occurrence && cell[1][j].getOcc() == occurrence && cell[2][j].getOcc() == occurrence && cell[3][j].getOcc() == occurrence) { cell[0][j].setFlashCircles(occurrence); cell[1][j].setFlashCircles(occurrence); cell[2][j].setFlashCircles(occurrence); cell[3][j].setFlashCircles(occurrence); return true; } for (int j = 0; j < 7; j++) if (cell[1][j].getOcc() == occurrence && cell[2][j].getOcc() == occurrence && cell[3][j].getOcc() == occurrence && cell[4][j].getOcc() == occurrence) { cell[1][j].setFlashCircles(occurrence); cell[2][j].setFlashCircles(occurrence); cell[3][j].setFlashCircles(occurrence); cell[4][j].setFlashCircles(occurrence); return true; } for (int j = 0; j < 7; j++) if (cell[2][j].getOcc() == occurrence && cell[3][j].getOcc() == occurrence && cell[4][j].getOcc() == occurrence && cell[5][j].getOcc() == occurrence) { cell[2][j].setFlashCircles(occurrence); cell[3][j].setFlashCircles(occurrence); cell[4][j].setFlashCircles(occurrence); cell[5][j].setFlashCircles(occurrence); return true; } for (int k = 0; k < 3; k++) if (cell[k][k].getOcc() == occurrence && cell[k+1][k+1].getOcc() == occurrence && cell[k+2][k+2].getOcc() == occurrence && cell[k+3][k+3].getOcc() == occurrence) { cell[k][k].setFlashCircles(occurrence); cell[k+1][k+1].setFlashCircles(occurrence); cell[k+2][k+2].setFlashCircles(occurrence); cell[k+3][k+3].setFlashCircles(occurrence); return true; } for (int l = 0; l < 3; l++) if (cell[l][l+1].getOcc() == occurrence && cell[l+1][l+2].getOcc() == occurrence && cell[l+2][l+3].getOcc() == occurrence && cell[l+3][l+4].getOcc() == occurrence) { cell[l][l+1].setFlashCircles(occurrence); cell[l+1][l+2].setFlashCircles(occurrence); cell[l+2][l+3].setFlashCircles(occurrence); cell[l+3][l+4].setFlashCircles(occurrence); return true; } for(int m = 5; m > 2; m--) if (cell[m][0].getOcc() == occurrence && cell[m-1][1].getOcc() == occurrence && cell[m-2][2].getOcc() == occurrence && cell[m-3][3].getOcc() == occurrence) { cell[m][0].setFlashCircles(occurrence); cell[m-1][1].setFlashCircles(occurrence); cell[m-2][2].setFlashCircles(occurrence); cell[m-3][3].setFlashCircles(occurrence); return true; } for(int m = 5; m > 2; m--) if (cell[m][1].getOcc() == occurrence && cell[m-1][2].getOcc() == occurrence && cell[m-2][3].getOcc() == occurrence && cell[m-3][4].getOcc() == occurrence) { cell[m][1].setFlashCircles(occurrence); cell[m-1][2].setFlashCircles(occurrence); cell[m-2][3].setFlashCircles(occurrence); cell[m-3][4].setFlashCircles(occurrence); return true; } for(int m = 5; m > 2; m--) if (cell[m][2].getOcc() == occurrence && cell[m-1][3].getOcc() == occurrence && cell[m-2][4].getOcc() == occurrence && cell[m-3][5].getOcc() == occurrence) { cell[m][2].setFlashCircles(occurrence); cell[m-1][3].setFlashCircles(occurrence); cell[m-2][4].setFlashCircles(occurrence); cell[m-3][5].setFlashCircles(occurrence); return true; } for(int m = 5; m > 2; m--) if (cell[m][3].getOcc() == occurrence && cell[m-1][4].getOcc() == occurrence && cell[m-2][5].getOcc() == occurrence && cell[m-3][6].getOcc() == occurrence) { cell[m][3].setFlashCircles(occurrence); cell[m-1][4].setFlashCircles(occurrence); cell[m-2][5].setFlashCircles(occurrence); cell[m-3][6].setFlashCircles(occurrence); return true; } return false; } public class Cell extends Pane { char sequence = 'W'; private Circle circle; public Cell(int r, int c) { circle = new Circle(52, 52, 52); circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); this.setPrefSize(750, 750); this.setOnMouseClicked(e -> handleMouseClick(r, c)); this.getChildren().add(circle); } public void setOcc(char c) { sequence = c; //now setting the different color which would be red and yellow as shown on text book  if (sequence == 'R') { circle.setFill(Color.RED); } else if (sequence == 'p') { circle.setFill(Color.YELLOW); } } public char getOcc() { return sequence; } //this function will handel the task that user going to perform on mouse  private void handleMouseClick(int x, int y) { if(x==5 || (cell[x+1][y].sequence == 'p' || cell[x+1][y].sequence == 'R')) { if (sequence == 'W' && playerTurn != ' ') { setOcc(playerTurn); //now checking the circle if circle still empty the game is not over  if (isWon(playerTurn)) { playerTurn = ' '; } else if (isTableFull()) { playerTurn = ' '; } else  { //chaning the player turn  playerTurn = (playerTurn == 'R') ? 'p' : 'R'; } } } } public void setFlashCircles(char c) { if (c == 'p') { circle.setFill(Color.YELLOWGREEN); } else if (c == 'R') { circle.setFill(Color.INDIANRED); } } public boolean isTableFull() { for (int i = 0; i < 6; i++) for (int j = 0; j < 7; j++) if (cell[i][j].getOcc() == 'W') return false; return true; } } public static void main(String[] args) { launch(args); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions