+ Reply to Thread
Results 1 to 3 of 3

Thread: JAVA help

  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Points
    9,102.23
    Rep Power
    161

    Default JAVA help

    Ok, first off, this code is very messy and uncommented sooo if you know a decent amount on Java you should be able to navigate it for the most part.

    I am making a connect4 game, and please don't refer me to the post below this one, as I can't follow it entirely. I am still very new to Java.

    My issues right now revolve around three things.
    1. Whenever I choose an odd number for the board size, it only displays an even amount of holes for the columns (if you test it you will see what I mean).
    2. As you can see in my code, I am trying to somehow link up my JButtons to an actionlistener...basically be entitled buttons[0] to buttons[(board size)]. However, it only seems to loop and actually assign a listener to half of the buttons, not sure why it is doing that.
    3. Once I do get the buttons working, how will I be able to get the JLabel images updated for a player's piece and at the same time only go down to the next available spot, rather than overlapping.

    Those are my current issues right now and this code is ultra messy, so sorry about that.
    (I've attached the images as well)

    Also, I use Eclipse.
    Code's in the spoiler



    I attached the player piece images, however the arrow image didn't want to upload so I hosted that one offshore, here: http://i421.photobucket.com/albums/p...skom/arrow.png


    Thank you guys so much!
    -Austin
    Attached Images
    Last edited by aanders5; 02-02-2011 at 05:41 PM.

  2. #2
    Join Date
    Jan 2008
    Location
    PA
    Posts
    1,164
    Points
    1,899,029.25
    Rep Power
    206

    Default Re: JAVA help

    Don't really have time to help but heres my version i made my 1st year with java.
    Code:
    /**
     * ConnectFour [Version 1.50]
     * @author Brian SonniE
     * @Source: FluidCoding.com
     * @version 1.50 2009/9/12
     */
    
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.event.*;
    import java.net.*;
    import java.awt.image.*;
    import javax.swing.ImageIcon;
    
    public class ConnectFour extends JFrame{
        private JPanel mainPane;
        private JButton[] slots;
        private JButton[] selector;
        // Set Images
        ImageIcon emptySpace = new ImageIcon(ConnectFour.class.getResource("images/whitecon.png"));
      	ImageIcon blackSpace = new ImageIcon(ConnectFour.class.getResource("images/blackcon.png"));
      	ImageIcon redSpace = new ImageIcon(ConnectFour.class.getResource("images/redcon.png"));
      	ImageIcon selectIcon = new ImageIcon(ConnectFour.class.getResource("images/selectcon.png"));
        int[] board;
        int turn = 1;	
        int tieCounter = 0;
    	boolean gameRunning = true;
    
        /**
         *Constructor
         */
        public ConnectFour()
        {
        	setTitle("SonniE's ConnectFour");
        	setVisible(true);
        	setSize(600,550);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	buildMainPane();
        }
        /**
         *	Setup Gui
         */
        public void buildMainPane()
        {
        	mainPane = new JPanel(new GridLayout(7,6));
        	mainPane.setBackground(Color.black);
        
        	board = new int[42];
        	slots = new JButton[42];
        	selector = new JButton[7];
        	
        	// Init Buttons/Board
        	for(int ct=0; ct< 7; ct++)
        	{
        		selector[ct] = new JButton(selectIcon);
        		selector[ct].addActionListener(new SelectListener());
        		mainPane.add(selector[ct]);
        	}
        	for(int ct=0; ct<42; ct++)
        	{
        		board[ct] = 0; 
        		slots[ct] = new JButton(emptySpace);
        		mainPane.add(slots[ct]);
        	}
        	
        	add(mainPane);
        }
        
        private class SelectListener implements ActionListener
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		boolean validMove = true;
        		int activeColumn = 0;
        		int activePiece = 0;
        		ImageIcon activeIcon = redSpace;
        		if(turn == 1)
        		{
        			activeIcon = blackSpace;
        		}
        		Object obj = e.getSource();
        		// Get Column
    			for(int i=0; i<7; i++)
    			{
    				if(obj == selector[i])
    					activeColumn = i;
    			}
    			// Get Row
    			activePiece = 5*7+activeColumn;
    			for(int i = 0; i<6; i++)
    			{
    				if(board[activePiece] == 0)
    				{
    					board[activePiece] = turn;
    					break;
    				}
    				else 
    				{
    					activePiece-=7;
    				}
    			}
    			// Check if Valid
    			if(activePiece<0)
    				validMove=false;
    			else{
    			
    			slots[activePiece].setIcon(activeIcon);
    			
        		// Check 4 Win
        		
        		// Check Horizontal Win
        		for(int ct=0; ct<39; ct++)
        		{
        			if(ct%7<5)
        			{
        				if(board[ct] != 0 && board[ct] == board[ct+1] && board[ct] == board[ct+2] && board[ct] == board[ct+3])
        				{
        					gameRunning = false;
        				}
        			}
        		}
        		
        		// Check Vertical Win
        		
        		for(int ct=0; ct<22; ct++)
        		{
        			if(board[ct] != 0 && board[ct] == board[ct+7] && board[ct] == board[ct+14] && board[ct] == board[ct+21])
        			{
        				gameRunning = false;
        			}
        		}
        		
        		// Check Diagnol Win
        		// L to R
        		for (int ct = 0; ct < 18; ct++)
                {
                	if (ct % 7 < 4)
                    {
                    	if(board[ct] != 0 && board[ct] == board[ct+8] && board[ct] == board[ct+16] && board[ct] == board[ct+24])
                    	{
                    		gameRunning = false;
                    	}
                    }
                }
                
                for(int ct=0; ct<24; ct++)
                {
                	if(ct % 7 > 2)
                	{
                		if(board[ct] != 0 && board[ct] == board[ct+6] && board[ct] == board[ct+12] && board[ct] == board[ct+18])
                		{
                			gameRunning = false;
        				
                		}
                	}
                }
        		
        	
        		// Display Winner Message
        		if(!gameRunning)
        		{
        			if(turn==1)
        				JOptionPane.showMessageDialog(null, "Player 1 Wins!!!");
        			else
        				JOptionPane.showMessageDialog(null, "Player 2 Wins!!!");
        				
        			for(int i = 0; i<7; i++)
    	    			selector[i].setEnabled(false);
        		}		
        		
        		tieCounter++;
        		if(tieCounter==42)
        		{
        			JOptionPane.showMessageDialog(null, "Tie You Both Lose!!!");
        			gameRunning = false;
        			for(int i = 0; i<7; i++)
    	    			selector[i].setEnabled(false);
        		}
        		// Update Player Turn
        			if(turn==1)
        				turn = 2;
        			else
        				turn = 1;
        		if(!gameRunning)
        		{
        			int n = JOptionPane.showConfirmDialog(null,
    			    "Would You Like a Rematch",
        			"New Game?",
        			JOptionPane.YES_NO_OPTION);
        			
        			if(n == 0)
        				restartGame();
        			
        			else
        				System.exit(0);
        		}
        		}
        	}
        }  
        public void restartGame()
        {
        	for(int i = 0; i<42; i++)
        	{
        		slots[i].setIcon(emptySpace);
        		board[i] = 0;
        		turn = 1;
        		tieCounter = 0;
        		gameRunning = true;
        	}
        	for(int i =0; i<7; i++)
        		selector[i].setEnabled(true);
        }
        
        public static void main(String[] args) {
        	ConnectFour window = new ConnectFour();
        }
    }


    Get Vip: »Here«
    Donate: »Here«
    >>List of Compilers<<
    >>SFDM Name Generator<<
    [Owner Of FluidCoding]

  3. #3
    Join Date
    Apr 2008
    Posts
    135
    Points
    115,975.39
    Rep Power
    194

    Default Re: JAVA help

    Bravo.

    Ay you know about the new coder Ruffian? well he isnt new but he makes good codes now for SF



+ Reply to 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