Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: I have two partialy made classes below (both work), but would like Connect4 to be the super and Grid to be sub, however since

JAVA: I have two partialy made classes below (both work), but would like Connect4 to be the super and Grid to be sub, however since Grid extends Application, due to the GUI, how does one go about doing this? I want Connect4 to hold all the logic code and Grid to reference the logic/methods for its GUI, so I can have a matrix in the background tracking the GUI button clicks to determine a winner. Right now I just need to know how to link them, so could someone show me how I can link the add method which is currently present in both classes? I can take it from there. Just need a little help. Thanks!

**********************************************class Connect4*************************8

import java.util.Scanner;

public class Connect4 { // instance for each 7 columns in array. starts at bottom

private static int row1 = 6; private static int row2 = 6; private static int row3 = 6; private static int row4 = 6; private static int row5 = 6; private static int row6 = 6; private static int row7 = 6;

// determines total number of possible moves private static int count = 42;

// instance to determine who's move it is private static int determinePlayer = 0;

// row/col/diagonal counters for both pieces for 4 in a row matches private static int xRowCounter; private static int yRowCounter; private static int xColCounter; private static int yColCounter; private static int xDiagonalCounter; private static int yDiagonalCounter; //create array private static char[][] objArray; private static int rows; private static int columns;

// boolean values for different searches private static boolean winnerRow; private static boolean winnerCol; private static boolean winnerDiagonal; private static boolean winnerDiagonal2; private static boolean winnerDiagonal3; private static boolean winnerDiagonal4;

public static void main(String[] args) {

Scanner scan = new Scanner(System.in); Connect4 matrix = new Connect4(); //Connect4 test = new Connect4TextConsole();

//test.printArray(); // while loop to start game. 42 possible moves so any more game stops while (count != 1) { if (determinePlayer % 2 == 0) { System.out.print("Player 1 enter a column (1-7): "); int temp = scan.nextInt(); while (temp > 7 || temp < 1) { System.out.print("Incorrect Choice - Player 1 enter a column (1-7): "); temp = scan.nextInt(); } matrix.add(temp);

for (int i = 1; i < 7; i++) { for (int i2 = 0; i2 < 7; i2++) { if (matrix.getElement(i, i2) == '\0') { System.out.print("| |"); } // end of if else { System.out.print("|" + matrix.getElement(i, i2) + "|"); } // end of else } // end of 1st for loop System.out.println(""); } // end of 2nd for loop if (winnerRow == true || winnerCol == true || winnerDiagonal == true || winnerDiagonal2 == true || winnerDiagonal3 == true || winnerDiagonal4 == true) { System.out.println("Player 1 is the winner!"); break; } // end of if } // end of if else { System.out.print("Player 2 enter a column (1-7): "); int temp = scan.nextInt(); while (temp > 7 || temp < 1) { System.out.print("Incorrect Choice - Player 2 enter a column (1-7): "); temp = scan.nextInt(); } matrix.add(temp);

for (int i = 1; i < 7; i++) { for (int i2 = 0; i2 < 7; i2++) { if (matrix.getElement(i, i2) == '\0') { System.out.print("| |"); } // end of if else { System.out.print("|" + matrix.getElement(i, i2) + "|"); } // end of else } // end of 1st for loop System.out.println(""); } // end of 2nd for loop

if (winnerRow == true || winnerCol == true || winnerDiagonal == true || winnerDiagonal2 == true || winnerDiagonal3 == true || winnerDiagonal4 == true) { System.out.println("Player 2 is the winner!"); break; } // end of if } // end of else } // end of while loop

// if all false then no winner if (winnerRow == false && winnerCol == false && winnerDiagonal == false && winnerDiagonal2 == false && winnerDiagonal3 == false && winnerDiagonal4 == false) { System.out.println("Sorry there is no winner. Please play again!"); } // end of if } // end of main

public Connect4() { objArray = new char[7][7]; }

public Connect4(int x, int y) { rows = x; columns = y; objArray = new char[x][y]; } // end of constructor 2

public char getElement(int a, int b) { return objArray[a][b]; }

public void setElement(int a, int b, char value) { objArray[a][b] = value; }

/** * Method adds pieces based on determinePlayer value. Each case is * equivalent to the column chosen. * * @return value to 2d Array * */ // public Connect4 add(int x) { public void add(int x) { // Connect4 connect = new Connect4(getRows(), getColumns()); switch (x) { case 1: if (determinePlayer % 2 == 0) { // connect.setElement(row1, 0, 'X'); setElement(row1, 0, 'X'); } else { // connect.setElement(row1, 0, 'O'); setElement(row1, 0, 'O'); } row1--; count--; determinePlayer++; break; // return connect; case 2: if (determinePlayer % 2 == 0) { // connect.setElement(row2, 1, 'X'); setElement(row2, 1, 'X'); } else { setElement(row2, 1, 'O'); } row2--; count--; determinePlayer++; break; // return connect; case 3: if (determinePlayer % 2 == 0) { setElement(row3, 2, 'X'); } else { setElement(row3, 2, 'O'); } row3--; count--; determinePlayer++; break; // return connect; case 4: if (determinePlayer % 2 == 0) { setElement(row4, 3, 'X'); } else { setElement(row4, 3, 'O'); } row4--; count--; determinePlayer++; break; // return connect; case 5: if (determinePlayer % 2 == 0) { setElement(row5, 4, 'X'); } else { setElement(row5, 4, 'O'); } row5--; count--; determinePlayer++; break; // return connect; case 6: if (determinePlayer % 2 == 0) { setElement(row6, 5, 'X'); } else { setElement(row6, 5, 'O'); } row6--; count--; determinePlayer++; break; // return connect; case 7: if (determinePlayer % 2 == 0) { setElement(row7, 6, 'X'); } else { setElement(row7, 6, 'O'); } row7--; count--; determinePlayer++; break; } // end of switch } // end of }

***********************class Grid***************

import javafx.application.Application; import static javafx.application.Application.launch; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.layout.GridPane; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Shape; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.event.EventHandler; import javafx.scene.paint.Color; import javafx.scene.shape.Circle;

public class Grid extends Application {

private static final int TILE_SIZE = 80; private static final int COLUMNS = 7; private static int ROWS = 7;

private static Circle circles[][];

// instance for each 7 columns in array. starts at bottom private static int row1 = 6; private static int row2 = 6; private static int row3 = 6; private static int row4 = 6; private static int row5 = 6; private static int row6 = 6; private static int row7 = 6;

// determines total number of possible moves protected static int count = 42;

// instance to determine who's move it is protected static int determinePlayer = 0;

// row/col/diagonal counters for both pieces for 4 in a row matches private static int xRowCounter; private static int yRowCounter; private static int xColCounter; private static int yColCounter; private static int xDiagonalCounter; private static int yDiagonalCounter;

// boolean values for different searches protected static boolean winnerRow; protected static boolean winnerCol; protected static boolean winnerDiagonal; protected static boolean winnerDiagonal2; protected static boolean winnerDiagonal3; protected static boolean winnerDiagonal4; protected static boolean winnerOfGame;

//create array private static char[][] objArray;

public static void main(String[] args) {

launch(args); }

public Grid() { objArray = new char[7][7]; } // end of constructor

/** * Accessor for elements of object Connect 4 * * @param row row value * @param col column value * @return element from objects row/column */ public char getElement(int row, int col) { return objArray[row][col]; } // end of accessor getElement

/** * Mutator that adds game pieces to Connect4 2D array * * @param row row value * @param col column value * @param value element to be added to object */ public void setElement(int row, int col, char value) { objArray[row][col] = value; } // end of mutator setElement

public void fillColumn(int col) {

for (int i = ROWS - 2; i >= 0; i--) {

Circle c = circles[i][col]; if ((Color) c.getFill() == Color.WHITE) {

if (determinePlayer % 2 == 0) {

c.setFill(Color.BLUE); } else {

c.setFill(Color.RED); }

return; } } }

@Override public void start(Stage primaryStage) {

Grid matrix = new Grid();

Button bt[] = new Button[COLUMNS];

for (int i = 0; i < bt.length; i++) { bt[i] = new Button("Col " + (i + 1)); bt[i].setMaxSize(TILE_SIZE, TILE_SIZE); bt[i].setShape(new Circle(1.5));

// set button position bt[i].setTranslateX(TILE_SIZE / 4 + i * (TILE_SIZE + 5)); bt[i].setTranslateY(-260); }

// Action Events bt[0].setOnAction(new EventHandler() { @Override // Override the handle method public void handle(ActionEvent e) { String label = ((Button) e.getSource()).getText(); System.out.println(label);

// column are from 1 to COLUMN, while index is from 0 // hence subtracting 1 fillColumn(Integer.parseInt(label.split(" ")[1]) - 1); if (!columnSpace(1)) { System.out.println("No more column Space"); } else { matrix.add(1); }

for (int i = 1; i < 7; i++) { for (int i2 = 0; i2 < 7; i2++) { if (matrix.getElement(i, i2) == '\0') { System.out.print("| |"); } // end of if else { System.out.print("|" + matrix.getElement(i, i2) + "|"); } // end of else } // end of 1st for loop System.out.println(""); } // end of 2nd for loop if (winnerOfGame == true) { System.out.println("Player 1 is the winner!"); System.exit(0); } } });

bt[1].setOnAction(new EventHandler() { @Override // Override the handle method public void handle(ActionEvent e) { String label = ((Button) e.getSource()).getText(); System.out.println(label);

// column are from 1 to COLUMN, while index is from 0 // hence subtracting 1 fillColumn(Integer.parseInt(label.split(" ")[1]) - 1); if (!columnSpace(2)) { System.out.println("No more column Space"); } else { matrix.add(2); } } });

bt[2].setOnAction(new EventHandler() { @Override // Override the handle method public void handle(ActionEvent e) { String label = ((Button) e.getSource()).getText(); System.out.println(label);

// column are from 1 to COLUMN, while index is from 0 // hence subtracting 1 fillColumn(Integer.parseInt(label.split(" ")[1]) - 1); if (!columnSpace(3)) { System.out.println("No more column Space"); } else { matrix.add(3); } } });

bt[3].setOnAction(new EventHandler() { @Override // Override the handle method public void handle(ActionEvent e) { String label = ((Button) e.getSource()).getText(); System.out.println(label);

// column are from 1 to COLUMN, while index is from 0 // hence subtracting 1 fillColumn(Integer.parseInt(label.split(" ")[1]) - 1); if (!columnSpace(4)) { System.out.println("No more column Space"); } else { matrix.add(4); } } });

bt[4].setOnAction(new EventHandler() { @Override // Override the handle method public void handle(ActionEvent e) { String label = ((Button) e.getSource()).getText(); System.out.println(label);

// column are from 1 to COLUMN, while index is from 0 // hence subtracting 1 fillColumn(Integer.parseInt(label.split(" ")[1]) - 1); if (!columnSpace(5)) { System.out.println("No more column Space"); } else { matrix.add(5); } } });

bt[5].setOnAction(new EventHandler() { @Override // Override the handle method public void handle(ActionEvent e) { String label = ((Button) e.getSource()).getText(); System.out.println(label);

// column are from 1 to COLUMN, while index is from 0 // hence subtracting 1 fillColumn(Integer.parseInt(label.split(" ")[1]) - 1); if (!columnSpace(6)) { System.out.println("No more column Space"); } else { matrix.add(6); } } });

bt[6].setOnAction(new EventHandler() { @Override // Override the handle method public void handle(ActionEvent e) { String label = ((Button) e.getSource()).getText(); System.out.println(label);

// column are from 1 to COLUMN, while index is from 0 // hence subtracting 1 fillColumn(Integer.parseInt(label.split(" ")[1]) - 1); if (!columnSpace(7)) { System.out.println("No more column Space"); } else { matrix.add(7); } } });

// Create rectangle grid Shape shape = new Rectangle((COLUMNS + 1) * TILE_SIZE, (ROWS + 1) * TILE_SIZE);

// create GridPane GridPane pane = new GridPane();

pane.setAlignment(Pos.CENTER);

pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5)); // pixel buffer layout pane.setHgap(5.5); // pixel per blocks pane.setVgap(5.5);

// add everything to gridpane pane.getChildren().addAll(shape); pane.getChildren().addAll(bt);

circles = new Circle[ROWS - 1][COLUMNS];

for (int i = 0; i < ROWS - 1; i++) { for (int i2 = 0; i2 < COLUMNS; i2++) { Circle circle = new Circle(TILE_SIZE / 2); circle.setCenterX(TILE_SIZE / 2); circle.setCenterY(TILE_SIZE / 2); circle.setTranslateX(i2 * (TILE_SIZE + 5) + TILE_SIZE / 4); circle.setTranslateY(-20 + (i - 2) * (TILE_SIZE + 5) + TILE_SIZE / 4);

circle.setFill(Color.WHITE); pane.getChildren().add(circle);

circles[i][i2] = circle; } }

Scene scene = new Scene(pane);

primaryStage.setTitle("GridPane"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage

primaryStage.show(); // Display the stage }

public void add(int x) {

switch (x) { case 1: if (determinePlayer % 2 == 0) {

setElement(row1, 0, 'X'); } else {

setElement(row1, 0, 'O'); } row1--; count--; determinePlayer++; break;

case 2: if (determinePlayer % 2 == 0) {

setElement(row2, 1, 'X'); } else { setElement(row2, 1, 'O'); } row2--; count--; determinePlayer++; break;

case 3: if (determinePlayer % 2 == 0) { setElement(row3, 2, 'X'); } else { setElement(row3, 2, 'O'); } row3--; count--; determinePlayer++; break;

case 4: if (determinePlayer % 2 == 0) { setElement(row4, 3, 'X'); } else { setElement(row4, 3, 'O'); } row4--; count--; determinePlayer++; break;

case 5: if (determinePlayer % 2 == 0) { setElement(row5, 4, 'X'); } else { setElement(row5, 4, 'O'); } row5--; count--; determinePlayer++; break;

case 6: if (determinePlayer % 2 == 0) { setElement(row6, 5, 'X'); } else { setElement(row6, 5, 'O'); } row6--; count--; determinePlayer++; break;

case 7: if (determinePlayer % 2 == 0) { setElement(row7, 6, 'X'); } else { setElement(row7, 6, 'O'); } row7--; count--; determinePlayer++; break; } // end of switch } // end of

public static boolean columnSpace(int choice) {

if (choice == 1 && row1 < 1) { return false; } else if (choice == 2 && row2 < 1) { return false; } else if (choice == 3 && row3 < 1) { return false; } else if (choice == 4 && row4 < 1) { return false; } else if (choice == 5 && row5 < 1) { return false; } else if (choice == 6 && row6 < 1) { return false; } else if (choice == 7 && row7 < 1) { return false; } else { return true; } // if not false then true there is spcae }

}

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

Step: 3

blur-text-image

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

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions