Results 1 to 2 of 2
  1. #1
    AJ07 is offline Member
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Post Need Help With Connect 4!!!

    Hi everyone,

    I'm trying to test Java Eclipse to see if I can make a connect 4 game and so far I cant seem to get most my methods done. If I can get some help that would be appreciated.



    package connect4;


    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    import java.util.Random;

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;


    public class Board {

    /**
    * The background window of the game board
    */
    private JFrame windowFrame;

    /**
    * The part of the game board that holds the grid
    */
    private JPanel gridPanel;

    /**
    * The playing area of the game board
    */
    private JLabel grid[][];

    /**
    * Black image that represents an empty spot
    */
    private static final ImageIcon BLACK = new ImageIcon("images\\black.jpg");

    /**
    * Red image that represents the first player (or human)
    */
    private static final ImageIcon RED = new ImageIcon("images\\red.jpg");

    /**
    * Blue image that represents the second player (or computer)
    */
    private static final ImageIcon BLUE = new ImageIcon("images\\blue.jpg");

    /**
    * Image that represents the current player (starts at RED)
    */
    private ImageIcon turn = RED;

    /**
    * Use to generate random numbers for computer AI
    */
    private Random random = new Random();

    /**
    * Tracks the current game level;0 - Two Player (default); 1, 2, 3 levels of computer AI
    */
    private int gameLevel;

    /**
    * Used to listen for mouse click events on the game panel
    * and determine which column was clicked.
    * Each column (label) has a width of 100 pixels.
    */
    private MouseListener columnListener = new MouseListener(){
    @Override
    public void mouseReleased(MouseEvent e){}

    @Override
    public void mousePressed(MouseEvent e){}

    @Override
    public void mouseExited(MouseEvent e){}

    @Override
    public void mouseEntered(MouseEvent e){}

    @Override
    public void mouseClicked(MouseEvent e){playPiece(e.getX()/100);}
    };

    /**
    * Used to listen for menu item selections
    */
    private ActionListener menuListener = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent action){

    if(action.getActionCommand().equals("Contents"))
    showHelpContents();
    else
    gameLevel = Integer.parseInt(action.getActionCommand());
    }
    };

    /**
    * Creates the Connect Four Board including a frame, panel,
    * menu (menu bar and menu items). The board is made up from a
    * grid of labels - each with 'black' picture.
    *
    * @param rowSize number of rows in grid
    * @param columnSize number of columns in grid
    *
    * @author Ajith
    */
    public Board(int rowSize, int columnSize){

    windowFrame = new JFrame();
    windowFrame.setTitle("Connect Four Template");

    gridPanel = new JPanel();
    gridPanel.setLayout(new GridLayout(rowSize, columnSize));
    gridPanel.addMouseListener(columnListener);

    windowFrame.setJMenuBar(createMenu());

    grid = new JLabel[rowSize][columnSize];

    for(int r=0; r<grid.length;r++)
    {
    for(int c=0; c<grid[0].length;c++)
    {
    grid[r][c] = new JLabel(BLACK);
    gridPanel.add(grid[r][c]);
    }
    }

    windowFrame.add(gridPanel);

    windowFrame.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);
    windowFrame.setResizable(false);
    windowFrame.pack();
    windowFrame.setVisible(true);
    }


    /*
    * @author: Katherine
    */

    private void showHelpContents(){

    JOptionPane.showMessageDialog(gridPanel,"Provide instructions here...");
    }

    private JMenuBar createMenu(){

    JMenuBar menuBar = newJMenuBar();

    JMenu gameMenu = new JMenu("Game");
    JMenu helpMenu = new JMenu("Help");

    JMenuItem[]gameType = new JMenuItem[4];

    gameType[0] = new JMenuItem("Two Player");
    gameType[1] = new JMenuItem("Level 1");
    gameType[2] = new JMenuItem("Level 2");
    gameType[3] = new JMenuItem("Level 3");

    for (int x=0;x<4;x++)
    {
    gameType[x].setActionCommand(Integer.toString(x));
    gameType[x].addActionListener(menuListener);
    gameMenu.add(gameType[x]);
    }

    JMenuItem contents = new JMenuItem("Contents");

    contents.setActionCommand("Contents");
    contents.addActionListener(menuListener);
    helpMenu.add(contents);

    menuBar.add(gameMenu);
    menuBar.add(helpMenu);

    return menuBar;

    }


    public void playPiece(int column){

    int r = findNextAvailableRow(column);

    if(r==-1)
    JOptionPane.showMessageDialog(gridPanel,"Select another column");
    else
    {
    grid[r][column].setIcon(turn);

    if(checkWin())
    endGame();
    else
    {
    changeTurn();

    if (gameLevel>0)
    {
    computerPlay();

    if (checkWin())
    endGame();
    else
    changeTurn();
    }
    }
    }
    }

    public void endGame(){

    int answer = JOptionPane.showConfirmDialog(windowFrame,"Would you like to play again?","Game Over",
    JOptionPane.YES_NO_OPTION);

    if(answer==JOptionPane.YES_OPTION)
    reset();
    else
    windowFrame.dispose();
    }


    /**Complete the Design Phase for the following methods:

    /**
    *For each row in the column check to see if grid is empty (BLACK)
    *
    *@param column the current column being used
    *@return the row number found or -1 if the column is full
    */

    public int findNextAvailableRow(int column){}

    /**
    * For each row in the column check to see if grid is empty(Black)
    *
    * @param column the current column being used
    * @return the row number found or -1 if the column is full
    */

    private boolean checkWin(){}

    /**
    * Scans the grid for a winning pattern (all 4 directions)
    *
    * @return true if a winning pattern is found;false if no winning pattern was found
    */

    private void changeTurn(){}

    /**
    * Checks the current turn and switches the turn to the other color
    */
    public void reset(){}

    /**
    * Runs the appropriate sequence of AI levels based on the algorithm for the current
    */

    private void computerPlay(){}

    /**
    * Plays based on selecting a random column number
    */
    private void level1(){}

    /**
    * Attempts to play based on finding a column where the play results in a win
    *
    * @return returns true if a play is made; false if no play is made
    */
    private boolean level2(){}

    /**
    * Attempts to play based on finding a column where the play results in blocking a loss
    *
    * @return returns true if a play is made; false if no play is made
    */
    private boolean level3(){}
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Need Help With Connect 4!!!

    Format your code: Guide For New Members
    Ask specific questions. What doesn't work? What errors do you get?
    Don't expect us to finish your program for you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Connect 4 AI help!
    By jenxin in forum New To Java
    Replies: 2
    Last Post: 10-01-2012, 01:35 AM
  2. Connect to wap
    By HoangNgoc in forum CLDC and MIDP
    Replies: 2
    Last Post: 12-23-2010, 09:13 AM
  3. connect through usb
    By taufiqbzz in forum Networking
    Replies: 0
    Last Post: 11-01-2010, 11:04 PM
  4. Connect 4
    By pansylea in forum New To Java
    Replies: 0
    Last Post: 12-11-2009, 03:36 PM
  5. how connect
    By herfnai in forum JDBC
    Replies: 1
    Last Post: 08-18-2008, 06:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •