Advertisement
brilliant_moves

Connect4.java

Nov 29th, 2012
6,859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.26 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Connect4 {
  6.  
  7.     /**
  8.     *   Program:    Connect4.java
  9.     *   Purpose:    Stacking disk game for 2 players
  10.     *   Creator:    Chris Clarke
  11.     *   Created:    19.08.2007
  12.     *   Modified:   29.11.2012 (JFrame)
  13.     */ 
  14.  
  15.     public static void main(String[] args) {
  16.         Connect4JFrame frame = new Connect4JFrame();
  17.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  18.         frame.setVisible(true);
  19.     }
  20. }
  21.  
  22. class Connect4JFrame extends JFrame implements ActionListener {
  23.  
  24.     private Button      btn1, btn2, btn3, btn4, btn5, btn6, btn7;
  25.     private Label       lblSpacer;
  26.     MenuItem        newMI, exitMI, redMI, yellowMI;
  27.     int[][]         theArray;
  28.     boolean         end=false;
  29.     boolean         gameStart;
  30.     public static final int BLANK = 0;
  31.     public static final int RED = 1;
  32.     public static final int YELLOW = 2;
  33.  
  34.     public static final int MAXROW = 6; // 6 rows
  35.     public static final int MAXCOL = 7; // 7 columns
  36.  
  37.     public static final String SPACE = "                  "; // 18 spaces
  38.  
  39.     int activeColour = RED;
  40.    
  41.     public Connect4JFrame() {
  42.         setTitle("Connect4 by Chris Clarke");
  43.         MenuBar mbar = new MenuBar();
  44.         Menu fileMenu = new Menu("File");
  45.         newMI = new MenuItem("New");
  46.         newMI.addActionListener(this);
  47.         fileMenu.add(newMI);
  48.         exitMI = new MenuItem("Exit");
  49.         exitMI.addActionListener(this);
  50.         fileMenu.add(exitMI);
  51.         mbar.add(fileMenu);
  52.         Menu optMenu = new Menu("Options");
  53.         redMI = new MenuItem("Red starts");
  54.         redMI.addActionListener(this);
  55.         optMenu.add(redMI);
  56.         yellowMI = new MenuItem("Yellow starts");
  57.         yellowMI.addActionListener(this);
  58.         optMenu.add(yellowMI);
  59.         mbar.add(optMenu);
  60.         setMenuBar(mbar);
  61.  
  62.         // Build control panel.
  63.         Panel panel = new Panel();
  64.  
  65.         btn1 = new Button("1");
  66.         btn1.addActionListener(this);
  67.         panel.add(btn1);
  68.         lblSpacer = new Label(SPACE);
  69.         panel.add(lblSpacer);
  70.  
  71.         btn2 = new Button("2");
  72.         btn2.addActionListener(this);
  73.         panel.add(btn2);
  74.         lblSpacer = new Label(SPACE);
  75.         panel.add(lblSpacer);
  76.  
  77.         btn3 = new Button("3");
  78.         btn3.addActionListener(this);
  79.         panel.add(btn3);
  80.         lblSpacer = new Label(SPACE);
  81.         panel.add(lblSpacer);
  82.  
  83.         btn4 = new Button("4");
  84.         btn4.addActionListener(this);
  85.         panel.add(btn4);
  86.         lblSpacer = new Label(SPACE);
  87.         panel.add(lblSpacer);
  88.  
  89.         btn5 = new Button("5");
  90.         btn5.addActionListener(this);
  91.         panel.add(btn5);
  92.         lblSpacer = new Label(SPACE);
  93.         panel.add(lblSpacer);
  94.  
  95.         btn6 = new Button("6");
  96.         btn6.addActionListener(this);
  97.         panel.add(btn6);
  98.         lblSpacer = new Label(SPACE);
  99.         panel.add(lblSpacer);
  100.  
  101.         btn7 = new Button("7");
  102.         btn7.addActionListener(this);
  103.         panel.add(btn7);
  104.  
  105.         add(panel, BorderLayout.NORTH);
  106.         initialize();
  107.         // Set to a reasonable size.
  108.         setSize(1024, 768);
  109.     } // Connect4
  110.  
  111.     public void initialize() {
  112.         theArray=new int[MAXROW][MAXCOL];
  113.         for (int row=0; row<MAXROW; row++)
  114.             for (int col=0; col<MAXCOL; col++)
  115.                 theArray[row][col]=BLANK;
  116.         gameStart=false;
  117.     } // initialize
  118.  
  119.     public void paint(Graphics g) {
  120.         g.setColor(Color.BLUE);
  121.         g.fillRect(110, 50, 100+100*MAXCOL, 100+100*MAXROW);
  122.         for (int row=0; row<MAXROW; row++)
  123.             for (int col=0; col<MAXCOL; col++) {
  124.                 if (theArray[row][col]==BLANK) g.setColor(Color.WHITE);
  125.                 if (theArray[row][col]==RED) g.setColor(Color.RED);
  126.                 if (theArray[row][col]==YELLOW) g.setColor(Color.YELLOW);
  127.                 g.fillOval(160+100*col, 100+100*row, 100, 100);
  128.             }
  129.         check4(g);
  130.     } // paint
  131.  
  132.     public void putDisk(int n) {
  133.     // put a disk on top of column n
  134.         // if game is won, do nothing
  135.         if (end) return;
  136.         gameStart=true;
  137.         int row;
  138.         n--;
  139.         for (row=0; row<MAXROW; row++)
  140.             if (theArray[row][n]>0) break;
  141.         if (row>0) {
  142.             theArray[--row][n]=activeColour;
  143.             if (activeColour==RED)
  144.                 activeColour=YELLOW;
  145.             else
  146.                 activeColour=RED;
  147.             repaint();
  148.         }
  149.     }
  150.  
  151.     public void displayWinner(Graphics g, int n) {
  152.         g.setColor(Color.BLACK);
  153.         g.setFont(new Font("Courier", Font.BOLD, 100));
  154.         if (n==RED)
  155.             g.drawString("Red wins!", 100, 400);
  156.         else
  157.             g.drawString("Yellow wins!", 100, 400);
  158.         end=true;
  159.     }
  160.  
  161.     public void check4(Graphics g) {
  162.     // see if there are 4 disks in a row: horizontal, vertical or diagonal
  163.         // horizontal rows
  164.         for (int row=0; row<MAXROW; row++) {
  165.             for (int col=0; col<MAXCOL-3; col++) {
  166.                 int curr = theArray[row][col];
  167.                 if (curr>0
  168.                  && curr == theArray[row][col+1]
  169.                  && curr == theArray[row][col+2]
  170.                  && curr == theArray[row][col+3]) {
  171.                     displayWinner(g, theArray[row][col]);
  172.                 }
  173.             }
  174.         }
  175.         // vertical columns
  176.         for (int col=0; col<MAXCOL; col++) {
  177.             for (int row=0; row<MAXROW-3; row++) {
  178.                 int curr = theArray[row][col];
  179.                 if (curr>0
  180.                  && curr == theArray[row+1][col]
  181.                  && curr == theArray[row+2][col]
  182.                  && curr == theArray[row+3][col])
  183.                     displayWinner(g, theArray[row][col]);
  184.             }
  185.         }
  186.         // diagonal lower left to upper right
  187.         for (int row=0; row<MAXROW-3; row++) {
  188.             for (int col=0; col<MAXCOL-3; col++) {
  189.                 int curr = theArray[row][col];
  190.                 if (curr>0
  191.                  && curr == theArray[row+1][col+1]
  192.                  && curr == theArray[row+2][col+2]
  193.                  && curr == theArray[row+3][col+3])
  194.                     displayWinner(g, theArray[row][col]);
  195.             }
  196.         }
  197.         // diagonal upper left to lower right
  198.         for (int row=MAXROW-1; row>=3; row--) {
  199.             for (int col=0; col<MAXCOL-3; col++) {
  200.                 int curr = theArray[row][col];
  201.                 if (curr>0
  202.                  && curr == theArray[row-1][col+1]
  203.                  && curr == theArray[row-2][col+2]
  204.                  && curr == theArray[row-3][col+3])
  205.                     displayWinner(g, theArray[row][col]);
  206.             }
  207.         }
  208.     } // end check4
  209.  
  210.     public void actionPerformed(ActionEvent e) {
  211.         if (e.getSource() == btn1)
  212.             putDisk(1);
  213.         else if (e.getSource() == btn2)
  214.             putDisk(2);
  215.         else if (e.getSource() == btn3)
  216.             putDisk(3);
  217.         else if (e.getSource() == btn4)
  218.             putDisk(4);
  219.         else if (e.getSource() == btn5)
  220.             putDisk(5);
  221.         else if (e.getSource() == btn6)
  222.             putDisk(6);
  223.         else if (e.getSource() == btn7)
  224.             putDisk(7);
  225.         else if (e.getSource() == newMI) {
  226.             end=false;
  227.             initialize();
  228.             repaint();
  229.         } else if (e.getSource() == exitMI) {
  230.             System.exit(0);
  231.         } else if (e.getSource() == redMI) {
  232.             // don't change colour to play in middle of game
  233.             if (!gameStart) activeColour=RED;
  234.         } else if (e.getSource() == yellowMI) {
  235.             if (!gameStart) activeColour=YELLOW;
  236.         }
  237.     } // end ActionPerformed
  238.  
  239. } // class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement