Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.MouseInputListener;
- public class Project {
- public static void main(String args[]){
- boolean continuePlay = true;
- int player1Score = 0;
- int player2Score = 0;
- JFrame game = new Connect4(); //call the gameboard
- }
- }
- class Connect4 extends JFrame
- {
- final static int NUMROWS = 6;
- final static int NUMCOLUMNS = 7;
- final int EMPTY = 0;
- static int currentPlayer = 0;
- int playerNumber = 1;
- static int[][] gameboard = new int[NUMCOLUMNS][NUMROWS];
- int x;
- //create the game board GUI
- private JFrame board;
- //create a place in the game board for the pieces
- private JLabel[][] grid;
- //Constructor (Make the Game Board)
- public Connect4()
- {
- //set the game board to blank
- for ( int y = 0 ; y < NUMROWS ; y++ )
- {
- for ( int x = 0 ; x < NUMCOLUMNS ; x++)
- {
- gameboard[x][y] = EMPTY ;
- }
- }
- //create a GUI
- board = new JFrame("Connect 4");
- JPanel panel = (JPanel) board.getContentPane();
- board.setContentPane(panel);
- // 10 x 10 for each disk and then 10 x 10 for the buttons
- board.setSize(750 , 750);
- board.setVisible(true);
- //panel.setLayout(new GridLayout( (NUMROWS + 1),NUMCOLUMNS));
- grid = new JLabel[NUMCOLUMNS][NUMROWS];
- //create the individual Jlabels
- for (int y = 0 ; y < NUMROWS ; y++)
- {
- for ( int x = 0 ; x < NUMCOLUMNS ; x++)
- {
- grid[x][y] = new JLabel();
- // panel.add(grid[x][y]);
- }
- }
- //create buttons above the columns
- JButton jbtcol0 = new JButton("Column 1");
- JButton jbtcol1 = new JButton("Column 2");
- JButton jbtcol2 = new JButton("Column 3");
- JButton jbtcol3 = new JButton("Column 4");
- JButton jbtcol4 = new JButton("Column 5");
- JButton jbtcol5 = new JButton("Column 6");
- JButton jbtcol6 = new JButton("Column 7");
- //add buttons to the board
- JPanel buttonPanel = new JPanel();
- buttonPanel.add(jbtcol0);
- buttonPanel.add(jbtcol1);
- buttonPanel.add(jbtcol2);
- buttonPanel.add(jbtcol3);
- buttonPanel.add(jbtcol4);
- buttonPanel.add(jbtcol5);
- buttonPanel.add(jbtcol6);
- //add panel to the game board
- board.add(buttonPanel);
- //Register Listeners for the buttons
- Column_1ListenerClass listener0 = new Column_1ListenerClass();
- Column_2ListenerClass listener1 = new Column_2ListenerClass();
- Column_3ListenerClass listener2 = new Column_3ListenerClass();
- Column_4ListenerClass listener3 = new Column_4ListenerClass();
- Column_5ListenerClass listener4 = new Column_5ListenerClass();
- Column_6ListenerClass listener5 = new Column_6ListenerClass();
- Column_7ListenerClass listener6 = new Column_7ListenerClass();
- jbtcol0.addActionListener(listener0);
- jbtcol1.addActionListener(listener1);
- jbtcol2.addActionListener(listener2);
- jbtcol3.addActionListener(listener3);
- jbtcol4.addActionListener(listener4);
- jbtcol5.addActionListener(listener5);
- jbtcol6.addActionListener(listener6);
- }
- //reset the game board
- public void reset(){
- for( int y = 0 ; y < NUMROWS ; y++)
- {
- for ( int x = 0 ; x < NUMCOLUMNS ; x++)
- {
- //Game logic to blank
- gameboard[x][y] = EMPTY;
- //GUI update to blank
- }
- }
- }
- public static void changePlayer()
- {
- if (currentPlayer == 0)
- currentPlayer = 1;
- else if (currentPlayer == 1)
- currentPlayer = 2;
- else currentPlayer = 1;
- }
- public static boolean canDropDisk ( int x )
- {
- if ( gameboard[x][0] == 0)
- return true;
- else
- return false;
- }
- public int diskLands(int x)
- {
- for ( int y = NUMROWS ; y <= 0 ; y-- )
- {
- if (gameboard[y][x] == 0)
- return y;
- }
- return 0;
- }
- public static void dropDisk(int x)
- {
- //update array
- for ( int y = NUMROWS ; y <=0 ; y--)
- {
- if(gameboard[y][x] == 0)
- {
- gameboard[y][x] = currentPlayer;
- }
- }
- changePlayer();
- }
- public boolean didWin()
- {
- int numMatches;
- // check horizontally
- for ( int y = 0 ; y < NUMROWS ; y++)
- {
- numMatches = 0;
- for ( int x = 0 ; x + 1 < NUMCOLUMNS ; x++)
- {
- if ( ( gameboard[y][x] == gameboard[y][x+1]) && (gameboard[y][x] > 0) ) numMatches++;
- if (numMatches == 4)
- return true;
- }
- }
- //check vertically
- for ( int x = 0 ; x < NUMCOLUMNS ; x++)
- {
- numMatches = 0;
- for ( int y = 0 ; y + 1 < NUMROWS ; y++)
- {
- if ( ( gameboard[y][x] == gameboard[y+1][x]) && (gameboard[y][x] > 0 )) numMatches++;
- if ( numMatches == 4)
- return true;
- }
- }
- //check right slanted diagonal
- for ( int y = 3 ; y < NUMROWS ; y++)
- {
- numMatches = 0;
- for ( int x = 0 ; x < 4 ; x++)
- {
- if ((gameboard[y][x] == gameboard[y+1][x+1]) && (gameboard [y][x] == gameboard[y+2][x+2]) && (gameboard[y][x] == gameboard[y+3][x+3]) && (gameboard[y][x] > 0))
- return true;
- }
- }
- //check left slanted diagonal
- for ( int y = 3 ; y < NUMROWS ; y ++)
- {
- numMatches = 0;
- for ( int x = 3 ; x < NUMCOLUMNS ; x++)
- {
- if ((gameboard[y][x] == gameboard[y-1][x-1]) && (gameboard [y][x] == gameboard[y-2][x-2]) && (gameboard[y][x] == gameboard[y-3][x-3]) && (gameboard[y][x] > 0))
- return true;
- }
- }
- return false;
- }
- public int whoWon()
- {
- int numMatches;
- int matchingNumber = 0;
- // check horizontally
- for ( int y = 0 ; y < NUMROWS ; y++)
- {
- numMatches = 0;
- for ( int x = 0 ; x + 1 < NUMCOLUMNS ; x++){
- if ( ( gameboard[y][x] == gameboard[y][x+1]) && (gameboard[y][x] > 0) )
- {
- numMatches++;
- matchingNumber = gameboard[y][x];
- }
- if (numMatches == 4) return matchingNumber;
- }
- }
- //check vertically
- for ( int x = 0 ; x < NUMCOLUMNS ; x++)
- {
- numMatches = 0;
- for ( int y = 0 ; y + 1 < NUMROWS ; y++)
- {
- if ( ( gameboard[y][x] == gameboard[y+1][x]) && (gameboard[y][x] > 0 ))
- {
- numMatches++;
- matchingNumber = gameboard[y][x];
- }
- if ( numMatches == 4) return matchingNumber;
- }
- }
- //check right slanted diagonal
- for ( int y = 3 ; y < NUMROWS ; y++)
- {
- numMatches = 0;
- for ( int x = 0 ; x < 4 ; x++){
- if ((gameboard[y][x] == gameboard[y+1][x+1]) && (gameboard [y][x] == gameboard[y+2][x+2]) && (gameboard[y][x] == gameboard[y+3][x+3]) && (gameboard[y][x] > 0))
- return (gameboard[y][x]);
- }
- }
- //check left slanted diagonal
- for ( int y = 3 ; y < NUMROWS ; y ++)
- {
- numMatches = 0;
- for ( int x = 3 ; x < NUMCOLUMNS ; x++)
- {
- if ((gameboard[y][x] == gameboard[y-1][x-1]) && (gameboard [y][x] == gameboard[y-2][x-2]) && (gameboard[y][x] == gameboard[y-3][x-3]) && (gameboard[y][x] > 0))
- return (gameboard[y][x]);
- }
- }
- return matchingNumber;
- }
- }
- class Disk extends JLabel{
- public void animateFall(int column , int row, int player){
- //create a disk that starts above the top row in that column and falls to the desired row
- repaint();
- }
- protected void paintComponent(Graphics G){
- }
- }
- class Winner extends JLabel{
- }
- class Column_1ListenerClass implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if (Connect4.canDropDisk(0) ){
- Connect4.dropDisk(0);
- }
- }
- }
- class Column_2ListenerClass implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if (Connect4.canDropDisk(1) ){
- Connect4.dropDisk(1);
- }
- }
- }
- class Column_3ListenerClass implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if (Connect4.canDropDisk(2) ){
- Connect4.dropDisk(2);
- }
- }
- }
- class Column_4ListenerClass implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if (Connect4.canDropDisk(3) ){
- Connect4.dropDisk(3);
- }
- }
- }
- class Column_5ListenerClass implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if (Connect4.canDropDisk(4) ){
- Connect4.dropDisk(4);
- }
- }
- }
- class Column_6ListenerClass implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if (Connect4.canDropDisk(5) ){
- Connect4.dropDisk(5);
- }
- }
- }
- class Column_7ListenerClass implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if (Connect4.canDropDisk(6) ){
- Connect4.dropDisk(6);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement