0

I'm trying to make a Connect 4 game and I'm getting it to work, but I think I'm doing it in a very long and hard way. I think that array were created to simplify code like this, but I'm not sure how to use an array for this occasion. Any help is appreciated. Thanks.

Here is the html file for my project and the source code is in the attachment.

<HTML>
<BODY>
<APPLET CODE=Connect4.class WIDTH=1000 HEIGHT=600>
</APPLET>
</BODY>
</HTML>

Attachments
/**
 * @(#)Connect4.java
 *
 * Connect4 Applet application
 *
 * @author 
 * @version 1.00 2008/6/4
 */
 
 //package events;
 
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import java.awt.event.MouseAdapter;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.applet.*;


public class Connect4 extends Applet implements MouseListener, MouseMotionListener
{
		//Creates two variables that are used to store the x and y coordinates of where the mouse was click
	int xpoint;
	int ypoint;	
		//Creates a different variable for the color of each circle
	Color color = Color.white;
	Color color2 = Color.white;
	Color color3 = Color.white;
	Color color4 = Color.white;
	Color color5 = Color.white;
	Color color6 = Color.white;
	Color color7 = Color.white;
	Color color8 = Color.white;
	Color color9 = Color.white;
	Color color10 = Color.white;
	Color color11 = Color.white;
	Color color12 = Color.white;
	Color color13 = Color.white;
	Color color14 = Color.white;
	Color color15 = Color.white;
	Color color16 = Color.white;
	Color color17 = Color.white;
	Color color18 = Color.white;
	Color color19 = Color.white;
	Color color20 = Color.white;
	Color color21 = Color.white;
	Color color22 = Color.white;
	Color color23 = Color.white;
	Color color24 = Color.white;
	Color color25 = Color.white;
	Color color26 = Color.white;
	Color color27 = Color.white;
	Color color28 = Color.white;
	Color color29 = Color.white;
	Color color30 = Color.white;
	Color color31 = Color.white;
	Color color32 = Color.white;
	Color color33 = Color.white;
	Color color34 = Color.white;
	Color color35 = Color.white;
	Color color36 = Color.white;
	Color color37 = Color.white;
	Color color38 = Color.white;
	Color color39 = Color.white;
	Color color40 = Color.white;
	Color color41 = Color.white;
	Color color42 = Color.white;
	
	int clicks = 0;
//	Shape circleShape = new Ellipse2D.Double(405,70,70,70);
	Shape circle1 = new Ellipse2D.Double(325,70,70,70);
	Shape circle2 = new Ellipse2D.Double(325,150,70,70);
	Shape circle3 = new Ellipse2D.Double(325,230,70,70);
	Shape circle4 = new Ellipse2D.Double(325,310,70,70);
	Shape circle5 = new Ellipse2D.Double(325,390,70,70);
	Shape circle6 = new Ellipse2D.Double(325,470,70,70);
	Shape circle7 = new Ellipse2D.Double(405,70,70,70);
	Shape circle8 = new Ellipse2D.Double(405,150,70,70);
	Shape circle9 = new Ellipse2D.Double(405,230,70,70);
	Shape circle10 = new Ellipse2D.Double(405,310,70,70);
	Shape circle11 = new Ellipse2D.Double(405,390,70,70);
	Shape circle12 = new Ellipse2D.Double(405,470,70,70);
	Shape circle13 = new Ellipse2D.Double(485,70,70,70);
	Shape circle14 = new Ellipse2D.Double(485,150,70,70);	
	Shape circle15 = new Ellipse2D.Double(485,230,70,70);
	Shape circle16 = new Ellipse2D.Double(485,310,70,70);
	Shape circle17 = new Ellipse2D.Double(485,390,70,70);
	Shape circle18 = new Ellipse2D.Double(485,470,70,70);
	Shape circle19 = new Ellipse2D.Double(565,70,70,70);
	Shape circle20 = new Ellipse2D.Double(565,150,70,70);
	Shape circle21 = new Ellipse2D.Double(565,230,70,70);
	Shape circle22 = new Ellipse2D.Double(565,310,70,70);
	Shape circle23 = new Ellipse2D.Double(565,390,70,70);
	Shape circle24 = new Ellipse2D.Double(565,470,70,70);
	Shape circle25 = new Ellipse2D.Double(645,70,70,70);
	Shape circle26 = new Ellipse2D.Double(645,150,70,70);
	Shape circle27 = new Ellipse2D.Double(645,230,70,70);
	Shape circle28 = new Ellipse2D.Double(645,310,70,70);
	Shape circle29 = new Ellipse2D.Double(645,390,70,70);
	Shape circle30 = new Ellipse2D.Double(645,470,70,70);
	Shape circle31 = new Ellipse2D.Double(725,70,70,70);
	Shape circle32 = new Ellipse2D.Double(725,150,70,70);
	Shape circle33 = new Ellipse2D.Double(725,230,70,70);
	Shape circle34 = new Ellipse2D.Double(725,310,70,70);
	Shape circle35 = new Ellipse2D.Double(725,390,70,70);
	Shape circle36 = new Ellipse2D.Double(725,470,70,70);
	Shape circle37 = new Ellipse2D.Double(805,70,70,70);
	Shape circle38 = new Ellipse2D.Double(805,150,70,70);
	Shape circle39 = new Ellipse2D.Double(805,230,70,70);
	Shape circle40 = new Ellipse2D.Double(805,310,70,70);
	Shape circle41 = new Ellipse2D.Double(805,390,70,70);
	Shape circle42 = new Ellipse2D.Double(805,470,70,70);
	
	Label connect = new Label("Connect 4");

	
		
				Shape [] arShape;
													
	public void init() {
		
		addMouseListener(this);
		addMouseMotionListener(this);
		/*for(int i=0;i<42;i++)
					{
						arShape[i] = new Ellipse2D;		
					}*/
		add(connect);
		
	}

            
    
    public void ClickListener(Shape circleShape)
    {
//        this.circleShape = circleShape;
    }
    
    
	public void paint(Graphics g) 
		{
			
			Graphics2D ga = (Graphics2D)g;
			
			g.setColor(Color.yellow);
			g.fillRect(300,60,600,500);
		
		
		
		
			/*g.setColor(Color.white);
			g.fillOval(325,70,70,70);
			g.fillOval(325,150,70,70);
			g.fillOval(325,230,70,70);
			g.fillOval(325,310,70,70);
			g.fillOval(325,390,70,70);
			g.fillOval(325,470,70,70);
			

			g.fillOval(405,70,70,70);
			g.fillOval(405,150,70,70);
			g.fillOval(405,230,70,70);
			g.fillOval(405,310,70,70);
			g.fillOval(405,390,70,70);
			g.fillOval(405,470,70,70);
			
			g.fillOval(485,70,70,70);
			g.fillOval(485,150,70,70);
			g.fillOval(485,230,70,70);
			g.fillOval(485,310,70,70);
			g.fillOval(485,390,70,70);
			g.fillOval(485,470,70,70);
			
			g.fillOval(565,70,70,70);
			g.fillOval(565,150,70,70);
			g.fillOval(565,230,70,70);
			g.fillOval(565,310,70,70);
			g.fillOval(565,390,70,70);
			g.fillOval(565,470,70,70);
			
			g.fillOval(645,70,70,70);
			g.fillOval(645,150,70,70);
			g.fillOval(645,230,70,70);
			g.fillOval(645,310,70,70);
			g.fillOval(645,390,70,70);
			g.fillOval(645,470,70,70);
			
			g.fillOval(725,70,70,70);
			g.fillOval(725,150,70,70);
			g.fillOval(725,230,70,70);
			g.fillOval(725,310,70,70);
			g.fillOval(725,390,70,70);
			g.fillOval(725,470,70,70);
			
			g.fillOval(805,70,70,70);
			g.fillOval(805,150,70,70);
			g.fillOval(805,230,70,70);
			g.fillOval(805,310,70,70);
			g.fillOval(805,390,70,70);
			g.fillOval(805,470,70,70);*/
		
		    ga.setColor(color);		
			ga.fill(circle1);
			ga.draw(circle1);
			
			ga.setColor(color2);
			ga.fill(circle2);
			ga.draw(circle2);
			
			ga.setColor(color3);
			ga.fill(circle3); 
			ga.draw(circle3);	
				
			ga.setColor(color4);	
			ga.fill(circle4);	
			ga.draw(circle4);
			
			ga.setColor(color5);	
			ga.fill(circle5);	
			ga.draw(circle5);
			
			ga.setColor(color6);	
			ga.fill(circle6);	
			ga.draw(circle6);
			
			ga.setColor(color7);	
			ga.fill(circle7);	
			ga.draw(circle7);
			
			ga.setColor(color8);	
			ga.fill(circle8);	
			ga.draw(circle8);
			
			ga.setColor(color9);	
			ga.fill(circle9);	
			ga.draw(circle9);
			
			ga.setColor(color10);	
			ga.fill(circle10);	
			ga.draw(circle10);
			
			ga.setColor(color11);	
			ga.fill(circle11);	
			ga.draw(circle11);
			
			ga.setColor(color12);
			ga.fill(circle12);	
			ga.draw(circle12);
			
			ga.setColor(color13);
			ga.fill(circle13);	
			ga.draw(circle13);
			
			ga.setColor(color14);
			ga.fill(circle14);	
			ga.draw(circle14);
			
			ga.setColor(color15);
			ga.fill(circle15);	
			ga.draw(circle15);
			
			ga.setColor(color16);
			ga.fill(circle16);	
			ga.draw(circle16);
			
			ga.setColor(color17);
			ga.fill(circle17);	
			ga.draw(circle17);
			
			ga.setColor(color18);
			ga.fill(circle18);	
			ga.draw(circle18);
			
			ga.setColor(color19);
			ga.fill(circle19);	
			ga.draw(circle19);
			
			ga.setColor(color20);
			ga.fill(circle20);	
			ga.draw(circle20);
			
			ga.setColor(color21);
			ga.fill(circle21);	
			ga.draw(circle21);
			
			ga.setColor(color22);
			ga.fill(circle22);	
			ga.draw(circle22);
			
			ga.setColor(color23);
			ga.fill(circle23);	
			ga.draw(circle23);
			
			ga.setColor(color24);
			ga.fill(circle24);	
			ga.draw(circle24);
			
			ga.setColor(color25);
			ga.fill(circle25);	
			ga.draw(circle25);
			
			ga.setColor(color26);
			ga.fill(circle26);	
			ga.draw(circle26);
			
			ga.setColor(color27);
			ga.fill(circle27);	
			ga.draw(circle27);
			
			ga.setColor(color28);
			ga.fill(circle28);	
			ga.draw(circle28);
			
			ga.setColor(color29);
			ga.fill(circle29);	
			ga.draw(circle29);
			
			ga.setColor(color30);
			ga.fill(circle30);	
			ga.draw(circle30);
			
			ga.setColor(color31);
			ga.fill(circle31);	
			ga.draw(circle31);
			
			ga.setColor(color32);
			ga.fill(circle32);	
			ga.draw(circle32);
			
			ga.setColor(color33);
			ga.fill(circle33);	
			ga.draw(circle33);
			
			ga.setColor(color34);
			ga.fill(circle34);	
			ga.draw(circle34);
			
			ga.setColor(color35);
			ga.fill(circle35);	
			ga.draw(circle35);
			
			ga.setColor(color36);
			ga.fill(circle36);	
			ga.draw(circle36);
			
			ga.setColor(color37);
			ga.fill(circle37);	
			ga.draw(circle37);
			
			ga.setColor(color38);
			ga.fill(circle38);	
			ga.draw(circle38);
			
			ga.setColor(color39);
			ga.fill(circle39);	
			ga.draw(circle39);
			
			ga.setColor(color40);
			ga.fill(circle40);	
			ga.draw(circle40);
			
			ga.setColor(color41);
			ga.fill(circle41);	
			ga.draw(circle41);
			
			ga.setColor(color42);
			ga.fill(circle42);	
			ga.draw(circle42);
			              
	}

	public void mouseClicked(MouseEvent e){}
	public void mouseReleased(MouseEvent e)	{}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e)
		{
			xpoint = e.getX();
			ypoint = e.getY();
  
			if(xpoint < 395 & xpoint > 325 & ypoint < 140 & ypoint >70){
   
    		switch(clicks % 2){
				case 1:
					color = Color.red;
					break;
				case 0:
					color = Color.green;
					break;	
			}
				repaint();	
				clicks++;
			}
		 // Color	1color =  g.getColor(color);
			i
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Alex Edwards
0

Syntax for an array is simple--

ClassName_[] variableName = new ClassName_[SIZE_OF_ARRAY];

//or

ClassName_ variableName[] = new ClassName_[SIZE_OF_ARRAY];

Where ClassName_ is a valid class name, variableName is the name you give the array and SIZE_OF_ARRAY is the amount of objects you want in the array.

an example...

String[] names = new String[9];//an array of Strings, can hold up to 9 strings from indices
                                              //0-to-8

Arrays begin at indice zero, so calling your first element is in the syntax--

String myName = names[0];//storing the value in names[0] to myName;

Now myName has whatever value that was in names[0].

But in order for there to be a valid value in names[0] the array has to be populated (initialized) first--

for(int i = 0; i < names.length; i++)//arrays have a public variable named length that
{                                                  //is the value of their capacity
        names[i] = new String("" + i);//giving each name the name of the int value
}

or when you create the array you can initialize it--

String[] names = {"Mark", "Jason", "Valerie"};//an array of 3 strings from indice 0-to-2

Two words of warning!

First off, you can't initialize an array after the line of declaration.
Secondly you cannot change the size of the array once it has been created.

Once you get adjusted with using Arrays, you'll most likely want to use a more lenient class like ArrayList or Vector... but for simplicity stick to arrays!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.