Im trying to link this gui menu that i have done with connect 4 game that I have created, im fairly new to coding and all the help would be appreciated. Mainly when i press on the start button the connect 4 game begins
What I have tried:
//Game class
import java.util.Scanner;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// we are going to create a simple 2-players Connect Four implementation in Java 8
public class Connect4 {
private static final char[] PLAYERS = {'R', 'B'}; // we define characters for players (R for Red, B for Blue)
private final int width, height; // dimensions for our board
private final char[][] grid; // grid for the board
private int lastCol = -1, lastTop = -1; // we store last move made by a player
public Connect4(int w, int h) {
width = w;
height = h;
grid = new char[h][];
for (int i = 0; i < h; i++) { // . to resemble black cell in grid
Arrays.fill(grid[i] = new char[w], '.');
}
}
// use of Streams
// Displaying the board/grid of connect 4 game
public String toString() {
return IntStream.range(0, width).
mapToObj(Integer::toString).
collect(Collectors.joining()) +
"\n" +
Arrays.stream(grid).
map(String::new).
collect(Collectors.joining("\n"));
}
// geting a string representation of the:
//Row
//Rolumn
//Diagonally(/)
//Diagonally(\)
//containing the last play of the user
public String horizontal() {
return new String(grid[lastTop]);
}
public String vertical() {
StringBuilder sb = new StringBuilder(height);
for (int h = 0; h < height; h++) {
sb.append(grid[h][lastCol]);
}
return sb.toString();
}
public String slashDiagonal() {
StringBuilder sb = new StringBuilder(height);
for (int h = 0; h < height; h++) {
int w = lastCol + lastTop - h;
if (0 <= w && w < width) {
sb.append(grid[h][w]);
}
}
return sb.toString();
}
public String backslashDiagonal() {
StringBuilder sb = new StringBuilder(height);
for (int h = 0; h < height; h++) {
int w = lastCol - lastTop + h;
if (0 <= w && w < width) {
sb.append(grid[h][w]);
}
}
return sb.toString();
}
// static method checking if a substring is in str
public static boolean contains(String str, String substring) {
return str.indexOf(substring) >= 0;
}
// method to check if last play is the winning play
public boolean WinPlay() {
if (lastCol == -1) {
System.err.println("No move made yet");
return false;
}
char sym = grid[lastTop][lastCol];
String streak = String.format("%c%c%c%c", sym, sym, sym, sym);
// check if streak is in row, col, slashDiagonal or backslashDiagonal
return contains(horizontal(), streak) ||
contains(vertical(), streak) ||
contains(slashDiagonal(), streak) ||
contains(backslashDiagonal(), streak);
}
// scanner api
// asking user ro enter an integer between 0 and width-1
// prompts the user for a column, repeating until a valid choice is made
public void Switch(char symbol, Scanner input) {
do {
System.out.println("\nPlayer " + symbol + " turn: ");
int col = input.nextInt();
// checking column is ok
if (!(0 <= col && col < width)) {
System.out.println("Column must be between 0 and " + (width - 1));
continue;
}
// now we can place the symbol to the first
// available row in the asked column
for (int h = height - 1; h >= 0; h--) {
if (grid[h][col] == '.') {
grid[lastTop = h][lastCol = col] = symbol;
return;
}
}
// if column is full ask for new unput from user
System.out.println("Column " + col + " is full.");
} while (true);
}
public static void main(String[] args) {
// merging pieces
try (Scanner input = new Scanner(System.in)) {
int height = 6; int width = 8; int moves = height * width; // defining variables for dimensions and number of max of moves
Connect4 board = new Connect4(width, height); // ConnectFour instance
System.out.println("Use 0-" + (width - 1) + " to choose a column"); // display to guide user when enterning number
System.out.println(board); // display of board
// repeating until number of max of moves reached
for (int player = 0; moves-- > 0; player = 1 - player) {
// symbol for current player
char symbol = PLAYERS[player];
// ask user to choose a column
board.Switch(symbol, input);
// display board
System.out.println(board);
// Checking if a player won: If no, continue. If yes, display a message
if (board.WinPlay()) {
System.out.println("\nPlayer " + symbol + " wins!");
return;
}
}
//If no one managed to win display this message
System.out.println("Game over. DRAW!, Good Luck Next time!");
}
}
}
//gui menu class
/**
* Write a description of class Menu here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
public class Menu implements ActionListener
{
Scanner scan = new Scanner(System.in);
public Menu(){
JButton NewGameButton,ContinueButton,ExitButton;
JFrame frame = new JFrame(); //CREATING JFrame object
NewGameButton = new JButton ("New Game");
NewGameButton.addActionListener(this);
NewGameButton.setActionCommand("command1");
ContinueButton = new JButton ("Continue Game");
ContinueButton.addActionListener(this);
ContinueButton.setActionCommand("command2");
ExitButton= new JButton ("Exit");
ExitButton.addActionListener(this);
ExitButton.setActionCommand("command3");
JPanel panel= new JPanel(); //creating jpanel object
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30)); // set up panel
panel.setLayout(new GridLayout(0, 1));
panel.add(NewGameButton);
panel.add(ContinueButton);
panel.add(ExitButton);
frame.add(panel, BorderLayout.CENTER); // add panel to frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set default close operation
frame.setTitle("Connect-4"); // title
frame.pack(); // window options
frame.setVisible(true);
}
public static void main(String[] args) {
new Menu();
Connect4 ConnectObject= new Connect4();
ConnectObject.GameScreen();
}
@Override
public void actionPerformed(ActionEvent e) {
String command = ((JButton) e.getSource()).getActionCommand();
switch (command) {
case "command1":
GameScreen();
break;
case "command2":
break;
case "command3":
System.exit(0);
break;
}
}
}