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

Java Swing Goal: Implement Connect Four using the Model-View-Controller architec

ID: 3834019 • Letter: J

Question

Java Swing

Goal: Implement Connect Four using the Model-View-Controller architecture. Connect Four is a game similar to Tic-Tac-Toe. Two players alternate dropping tokens into a grid (7 columns times 6 rows), where tokens fall as far down into the grid as gravity allows. If there is no space available in a particular column, then a player cannot place a token there. The game ends (a) when a player places four tokens in a row horizontally, vertically, or diagonally, or (b) when the entire grid is filled so that no player can make a move. Use the Model-View-Controller software architecture to implement a version of the Connect Four game where players switch off using the same keyboard/mouse. The GUI must show the following information at all times: The current state of the game board Whose turn it is In the event that the game ends, it must show which player won, or that the game was a draw. Additionally, the GUI must allow users to perform the following actions: Pick which column to place a token in, while also disallowing placement of tokens in columns that are full Reset the game to an empty game board. Quit the game and exit the program. In the event that the game ends, the GUI must notify the players which player won or that the game was a draw.

Explanation / Answer


//Working on it

//Updating soon

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

public class TicTacToe {

   private static int[][] winCombinations = new int[][] {
           {0, 1, 2, 3, 4, 5, 6}, //horizontal wins
           {7, 8, 9, 10, 11, 12, 13}, //horizontal wins
           {14, 15, 16, 17, 18, 19, 20}, //horizontal wins
           {21, 22, 23, 24, 25, 26, 27}, //horizontal wins
           {28, 29, 30, 31, 32, 33, 34}, //horizontal wins
           {35, 36, 37, 38, 39, 40, 41}, //horizontal wins
          
           {0, 7, 14, 21, 28, 35}, //vertical wins
           {1, 8, 15, 22, 29, 36}, //vertical wins
           {2, 9, 16, 23, 30, 37}, //vertical wins
           {3, 10, 17, 24, 31, 38}, //vertical wins
           {4, 11, 18, 25, 32, 39}, //vertical wins
           {5, 12, 19, 26, 33, 40}, //vertical wins
           {6, 13, 20, 27, 34, 41}, //vertical wins
           {0, 4, 8}, {2, 4, 6}           //diagonal wins
   };
  
   private static JButton buttons[] = new JButton[42]; //create 9 buttons

   public static void main (String[] args)
   {
       gamePanel(); //launch game
   }
  
   private static void gamePanel(){
       JFrame frame = new JFrame ("Tic Tac Toe");
       frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);


       JPanel panel = new JPanel(); //creating a panel with a box like a tic tac toe board
       panel.setLayout (new GridLayout (6, 7));
       panel.setBorder (BorderFactory.createLineBorder (Color.gray, 7));
       panel.setBackground (Color.white);

       for(int i=0; i<=41; i++){ //placing the button onto the board
           buttons[i] = new MyButton();
           panel.add(buttons[i]);          
       }

       frame.getContentPane().add (panel);
       frame.pack();
       frame.setVisible(true);
       frame.setSize(800, 800);// set frame size and let teh game begin
   }

   public static int xOrO=0; // used for counting

   private static class MyButton extends JButton
   implements ActionListener {//creating own button class because JButton sucks:)
      
       int again=1000;//set again at 1000 so we don't make the mistake we can play again
       boolean win=false; // there is not a win
       String letter; // x or o
       public MyButton() {   // creating blank board
           super();
           letter=" ";
           setFont(new Font("Dialog", 1, 60));
           setText(letter);
           addActionListener(this);
       }
       public void actionPerformed(ActionEvent e) { // placing x or o's
           if((xOrO%2)==0 && getText().equals(" ") && win==false){
               letter="X";
               xOrO=xOrO+1;
               System.out.println(letter + " "+xOrO);
           } else if((xOrO%2)==1 && getText().equals(" ") && win==false) {
               letter="O";
               xOrO=xOrO+1;
               System.out.println(letter + " "+xOrO);
           } // if user does click on a button that is already played, nothing will happen
          
           setText(letter); // place the x or the o on the actual board
           for(int i=0; i<=15; i++){ // check for the winning combinations
               if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
                   buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
                   buttons[winCombinations[i][3]].getText().equals(buttons[winCombinations[i][4]].getText()) &&
                   buttons[winCombinations[i][0]].getText() != " "){//the winning is true
                   win = true;
               }
           }
           if(win == true){ // if the game ends let the user know who wins and give option to play again
               again=JOptionPane.showConfirmDialog(null, letter + " wins the game! Do you want to play again?",letter + "won!",JOptionPane.YES_NO_OPTION);
              
           } else if(xOrO == 41 && win == false){//tie game, announce and ask if the user want to play again
               again=JOptionPane.showConfirmDialog(null, "The game was tie! Do you want to play again?","Tie game!",JOptionPane.YES_NO_OPTION);
               win=true;
           }  
          
           if(again==JOptionPane.YES_OPTION && win==true){ // if the user want to play again clear all the button and start over
                   clearButtons();          
                   win=false;
           }
           else if(again==JOptionPane.NO_OPTION){
               System.exit(0); // exit game if the user do not want to play again
           }
       }
   }
  
   public static void clearButtons(){
      
       for(int i=0; i<=41; i++){// clear all 8 buttons
           buttons[i].setText(" ");                      
       }
       xOrO=0; // reset the count
      
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote