Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SIMPLE GUI BUILD FOR MY CONNECT FOUR JAVA CODE i need help building a simple GUI

ID: 3681494 • Letter: S

Question

SIMPLE GUI BUILD FOR MY CONNECT FOUR JAVA CODE

i need help building a simple GUI to run the gamne i have put below. doesnt have to be fancy just working and running .

import java.util.Scanner;

class GameBoard{
byte[][] GameBoard = new byte[6][7];
  
public GameBoard(){
GameBoard = new byte[][]{
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
};
}
  
public boolean checkMove(int column){
return GameBoard[0][column]==0;
}
  
public boolean placeMove(int column, int player){
if(!checkMove(column)) {System.out.println("Illegal move!"); return false;}
for(int i=5;i>=0;--i){
if(GameBoard[i][column] == 0) {
GameBoard[i][column] = (byte)player;
return true;
}
}
return false;
}
  
public void undoMove(int column){
for(int i=0;i<=5;++i){
if(GameBoard[i][column] != 0) {
GameBoard[i][column] = 0;
break;
}
}
}
public void displayGameBoard(){
System.out.println();
for(int i=0;i<=5;++i){
for(int j=0;j<=6;++j){
System.out.print(GameBoard[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}

public class ConnectFour {
private GameBoard b;
private Scanner scan;
private int nextMoveLocation=-1;
private int maxDepth = 8;
  
public ConnectFour(GameBoard b){
this.b = b;
scan = new Scanner(System.in);
}
  
public void Computer_Move(){
System.out.println("Your move (1-7): ");
int move = scan.nextInt();
while(move<1 || move > 7 || !b.checkMove(move-1)){
System.out.println("Invalid move. Your move (1-7): ");
move = scan.nextInt();
}
  
b.placeMove(move-1, (byte)2);
}
  
  
  
public int gameResult(GameBoard b){
int computerScore = 0, humanScore = 0;
for(int i=5;i>=0;--i){
for(int j=0;j<=6;++j){
if(b.GameBoard[i][j]==0) continue;
  
if(j<=3){
for(int k=0;k<4;++k){
if(b.GameBoard[i][j+k]==1) computerScore++;
else if(b.GameBoard[i][j+k]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
  
if(i>=3){
for(int k=0;k<4;++k){
if(b.GameBoard[i-k][j]==1) computerScore++;
else if(b.GameBoard[i-k][j]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
  
if(j<=3 && i>= 3){
for(int k=0;k<4;++k){
if(b.GameBoard[i-k][j+k]==1) computerScore++;
else if(b.GameBoard[i-k][j+k]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
  
if(j>=3 && i>=3){
for(int k=0;k<4;++k){
if(b.GameBoard[i-k][j-k]==1) computerScore++;
else if(b.GameBoard[i-k][j-k]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
}
}
  
for(int j=0;j<7;++j){
if(b.GameBoard[0][j]==0)return -1;
}
return 0;
}
  
int calculateScore(int computerScore, int moreMoves){   
int moveScore = 4 - moreMoves;
if(computerScore==0)return 0;
else if(computerScore==1)return 1*moveScore;
else if(computerScore==2)return 10*moveScore;
else if(computerScore==3)return 100*moveScore;
else return 1000;
}
  
public int eval_Board(GameBoard b){
  
int computerScore=1;
int score=0;
int blanks = 0;
int k=0, moreMoves=0;
for(int i=5;i>=0;--i){
for(int j=0;j<=6;++j){
  
if(b.GameBoard[i][j]==0 || b.GameBoard[i][j]==2) continue;
  
if(j<=3){
for(k=1;k<4;++k){
if(b.GameBoard[i][j+k]==1)computerScore++;
else if(b.GameBoard[i][j+k]==2){computerScore=0;blanks = 0;break;}
else blanks++;
}

moreMoves = 0;
if(blanks>0)
for(int c=1;c<4;++c){
int column = j+c;
for(int m=i; m<= 5;m++){
if(b.GameBoard[m][column]==0)moreMoves++;
else break;
}
}
  
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;   
blanks = 0;
}
  
if(i>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i-k][j]==1)computerScore++;
else if(b.GameBoard[i-k][j]==2){computerScore=0;break;}
}
moreMoves = 0;
  
if(computerScore>0){
int column = j;
for(int m=i-k+1; m<=i-1;m++){
if(b.GameBoard[m][column]==0)moreMoves++;
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}

if(j>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i][j-k]==1)computerScore++;
else if(b.GameBoard[i][j-k]==2){computerScore=0; blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0)
for(int c=1;c<4;++c){
int column = j- c;
for(int m=i; m<= 5;m++){
if(b.GameBoard[m][column]==0)moreMoves++;
else break;
}
}
  
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}

if(j<=3 && i>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i-k][j+k]==1)computerScore++;
else if(b.GameBoard[i-k][j+k]==2){computerScore=0;blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0){
for(int c=1;c<4;++c){
int column = j+c, row = i-c;
for(int m=row;m<=5;++m){
if(b.GameBoard[m][column]==0)moreMoves++;
else if(b.GameBoard[m][column]==1);
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}
}

if(i>=3 && j>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i-k][j-k]==1)computerScore++;
else if(b.GameBoard[i-k][j-k]==2){computerScore=0;blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0){
for(int c=1;c<4;++c){
int column = j-c, row = i-c;
for(int m=row;m<=5;++m){
if(b.GameBoard[m][column]==0)moreMoves++;
else if(b.GameBoard[m][column]==1);
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}
}
}
}
return score;
}
  
public int minimax(int depth, int turn){
int gameResult = gameResult(b);
if(gameResult==1)return Integer.MAX_VALUE;
else if(gameResult==2)return Integer.MIN_VALUE;
else if(gameResult==0)return 0;
  
if(depth==maxDepth)return eval_Board(b);
  
int maxScore=Integer.MIN_VALUE, minScore = Integer.MAX_VALUE;
for(int j=0;j<=6;++j){
if(!b.checkMove(j)) continue;
  
if(turn==1){
b.placeMove(j, 1);
int currentScore = minimax(depth+1, 2);
maxScore = Math.max(currentScore, maxScore);
if(depth==0){
System.out.println("Score for location "+j+" = "+currentScore);
if(maxScore==currentScore) nextMoveLocation = j;
}
}else if(turn==2){
b.placeMove(j, 2);
int currentScore = minimax(depth+1, 1);
minScore = Math.min(currentScore, minScore);
}
b.undoMove(j);
}
return turn==1?maxScore:minScore;
}
  
public int getAIMove(){
nextMoveLocation = -1;
minimax(0, 1);
return nextMoveLocation;
}
  
public void computer_Move(){
int humanMove=-1;
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to play first? (yes/no) ");
String answer = scan.next().trim();
  
if(answer.equalsIgnoreCase("yes")) Computer_Move();
b.displayGameBoard();
b.placeMove(3, 1);
b.displayGameBoard();
  
while(true){
Computer_Move();
b.displayGameBoard();
  
int gameResult = gameResult(b);
if(gameResult==1){System.out.println("computer Wins!");break;}
else if(gameResult==2){System.out.println("You Win!");break;}
else if(gameResult==0){System.out.println("Draw!");break;}
  
b.placeMove(getAIMove(), 1);
b.displayGameBoard();
gameResult = gameResult(b);
if(gameResult==1){System.out.println("computer Wins!");break;}
else if(gameResult==2){System.out.println("You Win!");break;}
else if(gameResult==0){System.out.println("Draw!");break;}
}
  
}
  
public static void main(String[] args) {
GameBoard b = new GameBoard();
ConnectFour computer = new ConnectFour(b);
computer.computer_Move();
}
}

Explanation / Answer

I have created the gui game. The code is divided into 4 files.

Code is:


connect4.java

public class Connect4 {

public static void main(String[] args) {
int state = 0;
boolean useGui = true;

if (useGui) {
gui Gui = new gui();
while (state != -1) {//checks if program is in quitting stage
switch (state) {
case 0://runtime
Gui.updateBoard();
if (Gui.getHasWon()) {
state = 1;
} else if (Gui.getHasDraw()) {
state = 2;
} else if (Gui.getNewGame()) {
Gui = new gui();
state = 0;
}
break;
case 1://endgame with winner
Gui.showWon();
if (Gui.getQuit()) {
state = -1;
} else if (Gui.getNewGame()) {
Gui = new gui();
state = 0;
}
break;
case 2://endgame with drawgame
Gui.showDraw();
if (Gui.getQuit()) {
state = -1;
} else if (Gui.getNewGame()) {
Gui = new gui();
state = 0;
}
break;
}
}
}
}
}


gui.java

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class gui {
private JFrame frame;
private JLabel[][] slots;
private JButton[] buttons;
private int xsize = 7;
private int ysize = 6;
private int currentPlayer = 1;
private boolean hasWon = false;
private boolean hasDraw = false;
private boolean quit = false;
private boolean newGame = false;
//making of grid and logic
Grid my_grid = new Grid();
logic my_logic = new logic(my_grid); //create game logic

public gui() {

frame = new JFrame("connect four");

JPanel panel = (JPanel) frame.getContentPane();
panel.setLayout(new GridLayout(xsize, ysize + 1));

slots = new JLabel[xsize][ysize];
buttons = new JButton[xsize];

for (int i = 0; i < xsize; i++) {
buttons[i] = new JButton("" + (i + 1));
buttons[i].setActionCommand("" + i);
buttons[i].addActionListener(
new ActionListener() {

public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(e.getActionCommand());
int y = my_grid.find_y(a);//check for space in collumn
if (y != -1) {
if (my_logic.set_and_check(a, y, currentPlayer)) {
hasWon = true;
} else if (my_logic.draw_game()) {//checks for drawgame
hasDraw = true;
} else {
currentPlayer = my_grid.changeplayer(currentPlayer, 2);
frame.setTitle("connect four - player " + currentPlayer + "'s turn");
}
} else {
JOptionPane.showMessageDialog(null, "choose another one", "column is filled", JOptionPane.INFORMATION_MESSAGE);
}
}
});
panel.add(buttons[i]);
}
for (int column = 0; column < ysize; column++) {
for (int row = 0; row < xsize; row++) {
slots[row][column] = new JLabel();
slots[row][column].setHorizontalAlignment(SwingConstants.CENTER);
slots[row][column].setBorder(new LineBorder(Color.black));
panel.add(slots[row][column]);
}
}

//jframe stuff
frame.setContentPane(panel);
frame.setSize(
700, 600);
frame.setVisible(
true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void updateBoard() {//keeps the gui in sync with the logggggtjiic and grid
for (int row = 0; row < xsize; row++) {
for (int column = 0; column < ysize; column++) {
if (my_grid.matrix_equals(row, column, 1)) {
slots[row][column].setOpaque(true);
slots[row][column].setBackground(Color.red);
}
if (my_grid.matrix_equals(row, column, 2)) {
slots[row][column].setOpaque(true);
slots[row][column].setBackground(Color.blue);
}
}
}
}

public void showWon() {
String winner = "player " + currentPlayer + " wins";
int n = JOptionPane.showConfirmDialog(
frame,
"new game?",
winner,
JOptionPane.YES_NO_OPTION);
if (n < 1) {
frame.dispose();
newGame = true;
} else {
frame.dispose();
quit = true;
}
}

public void showDraw() {
String winner = "draw game";
int n = JOptionPane.showConfirmDialog(
frame,
"new game?",
winner,
JOptionPane.YES_NO_OPTION);
if (n < 1) {
frame.dispose();
newGame = true;
} else {
frame.dispose();
quit = true;
}
}

public boolean getHasWon() {
return hasWon;
}

public boolean getHasDraw() {
return hasDraw;
}

public boolean getQuit() {
return quit;
}

public boolean getNewGame() {
return newGame;
}

public static void main(String[] args) {
gui Gui = new gui();
}
}


logic.java:

public class logic {

private int cells_left = 0;
private int max;
private int xsize;
private int ysize;
Grid my_grid;

public logic(Grid tempGrid) {
max = 4;//connect 4 or n
my_grid = tempGrid;
cells_left = my_grid.get_cells_left();
xsize = my_grid.get_xsize();
ysize = my_grid.get_ysize();
}

public boolean set_and_check(int x, int y, int player) {
my_grid.set_matrix(x, y, player);
cells_left--;
return check_one(x, y, 0, 1, player)
|| check_one(x, y, -1, 1, player)
|| check_one(x, y, -1, 0, player)
|| check_one(x, y, 1, 1, player);
}

public boolean draw_game() {//checks for draw game
return cells_left == 0;
}

private boolean check_one(int x, int y, int dx, int dy, int player) {
int count = 0;
int tempx = x;
int tempy = y;

while (count < max && valid(tempx, tempy)) {
if (!my_grid.matrix_equals(tempx, tempy, player)) {
break;

}
tempx += dx;
tempy += dy;
count++;
}
tempx = x - dx;
tempy = y - dy;
while (count < max && valid(tempx, tempy)) {
if (!my_grid.matrix_equals(tempx, tempy, player)) {
break;
}
tempx -= dx;
tempy -= dy;
count++;
}
return count == max;
}

private boolean valid(int x, int y) {
return x >= 0 && x < xsize && y >= 0 && y < ysize;
}
}


grid.java

public class Grid {

private int xsize;
private int ysize;
private int[][] matrix;
private int cells_left = 0;

public Grid() {
xsize = 7;
ysize = 6;

matrix = new int[xsize][ysize];
for (int i = 0; i < xsize; i++) {
for (int j = 0; j < ysize; j++) {
matrix[i][j] = 0;
cells_left++;
}
}
}

public int get_cells_left() {
return cells_left;
}

public int[][] get_matrix() {
return matrix;
}

public boolean matrix_equals(int a, int b, int c) {
return matrix [a][b] == c;
}

public void set_matrix(int a, int b, int temp_player) {
matrix[a][b] = temp_player;
}

public int get_xsize() {//returns the xsize
return xsize;
}

public int get_ysize() {//returns the xsize
return ysize;
}

public int find_y(int x) {//checks for room in collumn and returns free spot.
int y = -1;
for (int i = 0; i < ysize; i++) {
if (matrix[x][i] == 0) {
y = i;
}
}
return y;
}

public int changeplayer(int player, int max_players) {
player++;
if (player > max_players) {
return 1;
}
return player;
}
}