Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 9th, 2012  |  syntax: None  |  size: 8.36 KB  |  views: 117  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.MouseInputListener;
  5.  
  6. public class Project {
  7.  
  8.   public static void main(String args[]){
  9.    boolean continuePlay = true;
  10.    int player1Score = 0;
  11.    int player2Score = 0;
  12.    
  13.  
  14.    JFrame game = new Connect4(); //call the gameboard
  15.   }
  16.  }
  17.  
  18. class Connect4 extends JFrame
  19. {
  20.  final static int NUMROWS = 6;
  21.  final static int NUMCOLUMNS = 7;
  22.  final int EMPTY = 0;
  23.  static int currentPlayer = 0;
  24.  int playerNumber = 1;
  25.  static int[][] gameboard = new int[NUMCOLUMNS][NUMROWS];
  26.  int x;
  27.  
  28.  //create the game board GUI
  29.  private JFrame board;
  30.  
  31.  //create a place in the game board for the pieces
  32.  private JLabel[][] grid;
  33.  
  34.  //Constructor (Make the Game Board)
  35.  public Connect4()
  36.  {
  37.  
  38.   //set the game board to blank
  39.   for ( int y = 0 ; y < NUMROWS ; y++ )
  40.   {
  41.    for ( int x = 0 ; x < NUMCOLUMNS ; x++)
  42.    {
  43.     gameboard[x][y] = EMPTY ;
  44.    }
  45.   }
  46.  
  47.   //create a GUI
  48.   board = new JFrame("Connect 4");
  49.   JPanel panel = (JPanel) board.getContentPane();
  50.   board.setContentPane(panel);
  51.  
  52.   // 10 x 10 for each disk and then 10 x 10 for the buttons
  53.   board.setSize(750 , 750);
  54.   board.setVisible(true);
  55.   //panel.setLayout(new GridLayout( (NUMROWS + 1),NUMCOLUMNS));
  56.   grid = new JLabel[NUMCOLUMNS][NUMROWS];
  57.  
  58.   //create the individual Jlabels
  59.   for (int y = 0 ; y < NUMROWS ; y++)
  60.   {
  61.    for ( int x = 0 ; x < NUMCOLUMNS ; x++)
  62.    {
  63.     grid[x][y] = new JLabel();
  64.    // panel.add(grid[x][y]);
  65.    }
  66.   }
  67.  
  68.   //create buttons above the columns
  69.   JButton jbtcol0 = new JButton("Column 1");
  70.   JButton jbtcol1 = new JButton("Column 2");
  71.   JButton jbtcol2 = new JButton("Column 3");
  72.   JButton jbtcol3 = new JButton("Column 4");
  73.   JButton jbtcol4 = new JButton("Column 5");
  74.   JButton jbtcol5 = new JButton("Column 6");
  75.   JButton jbtcol6 = new JButton("Column 7");
  76.  
  77.   //add buttons to the board
  78.   JPanel buttonPanel = new JPanel();
  79.   buttonPanel.add(jbtcol0);
  80.   buttonPanel.add(jbtcol1);
  81.   buttonPanel.add(jbtcol2);
  82.   buttonPanel.add(jbtcol3);
  83.   buttonPanel.add(jbtcol4);
  84.   buttonPanel.add(jbtcol5);
  85.   buttonPanel.add(jbtcol6);
  86.  
  87.   //add panel to the game board
  88.   board.add(buttonPanel);
  89.  
  90.   //Register Listeners for the buttons
  91.   Column_1ListenerClass listener0 = new Column_1ListenerClass();
  92.   Column_2ListenerClass listener1 = new Column_2ListenerClass();
  93.   Column_3ListenerClass listener2 = new Column_3ListenerClass();
  94.   Column_4ListenerClass listener3 = new Column_4ListenerClass();
  95.   Column_5ListenerClass listener4 = new Column_5ListenerClass();
  96.   Column_6ListenerClass listener5 = new Column_6ListenerClass();
  97.   Column_7ListenerClass listener6 = new Column_7ListenerClass();
  98.   jbtcol0.addActionListener(listener0);
  99.   jbtcol1.addActionListener(listener1);
  100.   jbtcol2.addActionListener(listener2);
  101.   jbtcol3.addActionListener(listener3);
  102.   jbtcol4.addActionListener(listener4);
  103.   jbtcol5.addActionListener(listener5);
  104.   jbtcol6.addActionListener(listener6);
  105.  
  106.  }
  107.  
  108.  //reset the game board
  109.  public void reset(){
  110.   for( int y = 0 ; y < NUMROWS ; y++)
  111.   {
  112.    for ( int x = 0 ; x < NUMCOLUMNS ; x++)
  113.    {
  114.     //Game logic to blank
  115.     gameboard[x][y] = EMPTY;
  116.    
  117.     //GUI update to blank
  118.    }
  119.   }
  120.  }
  121.  
  122.   public static void changePlayer()
  123.   {
  124.    if (currentPlayer == 0)
  125.      currentPlayer = 1;
  126.    else if (currentPlayer == 1)
  127.      currentPlayer = 2;
  128.    else currentPlayer = 1;
  129.   }
  130.  
  131.   public static boolean canDropDisk ( int x )
  132.   {
  133.    if ( gameboard[x][0] == 0)
  134.      return true;
  135.    else
  136.      return false;
  137.   }
  138.  
  139.   public int diskLands(int x)
  140.   {
  141.    for ( int y = NUMROWS ; y <= 0 ; y-- )
  142.    {
  143.     if (gameboard[y][x] == 0)
  144.       return y;
  145.     }
  146.    
  147.    return 0;
  148.    }
  149.  
  150.   public static void dropDisk(int x)
  151.   {
  152.    //update array
  153.    for ( int y = NUMROWS ; y <=0 ; y--)
  154.    {
  155.     if(gameboard[y][x] == 0)
  156.     {
  157.      gameboard[y][x] = currentPlayer;
  158.     }
  159.    }
  160.    changePlayer();
  161.   }
  162.  
  163.   public boolean didWin()
  164.   {
  165.    int numMatches;
  166.    
  167.    // check horizontally
  168.    for ( int y = 0 ; y < NUMROWS ; y++)
  169.    {
  170.     numMatches = 0;
  171.     for ( int x = 0 ; x + 1 < NUMCOLUMNS ; x++)
  172.     {
  173.      if ( ( gameboard[y][x] == gameboard[y][x+1]) && (gameboard[y][x] > 0) ) numMatches++;
  174.      if (numMatches == 4)
  175.        return true;
  176.     }
  177.    }
  178.    
  179.    //check vertically
  180.    for ( int x = 0 ; x < NUMCOLUMNS ; x++)
  181.    {
  182.     numMatches = 0;
  183.     for ( int y = 0 ; y + 1 < NUMROWS ; y++)
  184.     {
  185.     if ( ( gameboard[y][x] == gameboard[y+1][x]) && (gameboard[y][x] > 0 )) numMatches++;
  186.     if ( numMatches == 4)
  187.       return true;
  188.     }
  189.    }
  190.    
  191.    //check right slanted diagonal
  192.    for ( int y = 3 ; y < NUMROWS ; y++)
  193.    {
  194.     numMatches = 0;
  195.     for ( int x = 0 ; x < 4 ; x++)
  196.     {
  197.      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))
  198.        return true;
  199.     }
  200.    }
  201.    
  202.    //check left slanted diagonal
  203.    for ( int y = 3 ; y < NUMROWS ; y ++)
  204.    {
  205.     numMatches = 0;
  206.     for ( int x = 3 ; x < NUMCOLUMNS ; x++)
  207.     {
  208.      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))
  209.        return true;
  210.     }
  211.    }
  212.    
  213.    return false;
  214.    
  215.   }
  216.  
  217.   public int whoWon()
  218.   {
  219.    int numMatches;
  220.    int matchingNumber = 0;
  221.    
  222.    // check horizontally
  223.    for ( int y = 0 ; y < NUMROWS ; y++)
  224.    {
  225.     numMatches = 0;
  226.     for ( int x = 0 ; x + 1 < NUMCOLUMNS ; x++){
  227.      if ( ( gameboard[y][x] == gameboard[y][x+1]) && (gameboard[y][x] > 0) )
  228.      {
  229.       numMatches++;
  230.       matchingNumber = gameboard[y][x];
  231.      }
  232.      if (numMatches == 4) return matchingNumber;
  233.     }
  234.    }
  235.    
  236.    //check vertically
  237.    for ( int x = 0 ; x < NUMCOLUMNS ; x++)
  238.    {
  239.     numMatches = 0;
  240.     for ( int y = 0 ; y + 1 < NUMROWS ; y++)
  241.     {
  242.     if ( ( gameboard[y][x] == gameboard[y+1][x]) && (gameboard[y][x] > 0 ))
  243.     {
  244.      numMatches++;
  245.      matchingNumber = gameboard[y][x];
  246.     }
  247.     if ( numMatches == 4) return matchingNumber;
  248.     }
  249.    }
  250.    
  251.    //check right slanted diagonal
  252.    for ( int y = 3 ; y < NUMROWS ; y++)
  253.    {
  254.     numMatches = 0;
  255.     for ( int x = 0 ; x < 4 ; x++){
  256.      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))
  257.        return (gameboard[y][x]);
  258.     }
  259.    }
  260.    
  261.    //check left slanted diagonal
  262.    for ( int y = 3 ; y < NUMROWS ; y ++)
  263.    {
  264.     numMatches = 0;
  265.     for ( int x = 3 ; x < NUMCOLUMNS ; x++)
  266.     {
  267.      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))
  268.        return (gameboard[y][x]);
  269.     }
  270.    }
  271.    
  272.    return matchingNumber;
  273.   }
  274.  
  275. }
  276.  
  277. class Disk extends JLabel{
  278.  
  279.  public void animateFall(int column , int row, int player){
  280.   //create a disk that starts above the top row in that column and falls to the desired row
  281.   repaint();
  282.  }
  283.  
  284.  protected void paintComponent(Graphics G){
  285.  
  286.  }
  287. }
  288.  
  289. class Winner extends JLabel{
  290.  
  291. }
  292.  
  293.  
  294. class Column_1ListenerClass implements ActionListener{
  295.  public void actionPerformed(ActionEvent e){
  296.   if (Connect4.canDropDisk(0) ){
  297.    Connect4.dropDisk(0);
  298.   }
  299.  }
  300. }
  301.  
  302. class Column_2ListenerClass implements ActionListener{
  303.  public void actionPerformed(ActionEvent e){
  304.   if (Connect4.canDropDisk(1) ){
  305.    Connect4.dropDisk(1);
  306.   }
  307.  }
  308. }
  309.  
  310. class Column_3ListenerClass implements ActionListener{
  311.  public void actionPerformed(ActionEvent e){
  312.   if (Connect4.canDropDisk(2) ){
  313.   Connect4.dropDisk(2);
  314.   }
  315.  }
  316. }
  317.  
  318. class Column_4ListenerClass implements ActionListener{
  319.  public void actionPerformed(ActionEvent e){
  320.   if (Connect4.canDropDisk(3) ){
  321.    Connect4.dropDisk(3);
  322.   }
  323.  }
  324. }
  325.  
  326. class Column_5ListenerClass implements ActionListener{
  327.  public void actionPerformed(ActionEvent e){
  328.   if (Connect4.canDropDisk(4) ){
  329.    Connect4.dropDisk(4);
  330.   }
  331.  }
  332. }
  333.  
  334. class Column_6ListenerClass implements ActionListener{
  335.  public void actionPerformed(ActionEvent e){
  336.   if (Connect4.canDropDisk(5) ){
  337.    Connect4.dropDisk(5);
  338.   }
  339.  }
  340. }
  341.  
  342. class Column_7ListenerClass implements ActionListener{
  343.  public void actionPerformed(ActionEvent e){
  344.   if (Connect4.canDropDisk(6) ){
  345.    Connect4.dropDisk(6);
  346.   }
  347.  }
  348. }
clone this paste RAW Paste Data