Jump to content
  • Advertisement
Sign in to follow this  
BlackReign

[java] Connect 4 applet

This topic is 4967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I have to make connect 4 in java for a school project, and I've hit a roadblock. I have figured out how to make my selector move across the the columns and I have the code to place a chip(don't know if it works yet). Anyways, I don't know how I should approach it because although calling repaint()worked for the "selector", it seems dumb to repaint the grid plus all the circles that have been filled. I've looked at other code for connect 4 but it's way too complicated. For now I don't even want it to see if a player has won or not. I'm new to applets, give me a break. Code:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Culminating extends Applet implements KeyListener {
	

	int playerTurn = 1;	
    int selectorX=65;
    final int selectorY=0;
    int selectorPos = 1;
    int gameEnd =0;
    int endPressed = 0;
    int beenPressed = 0;
    int positionofLastChip = 0;
    int i = 0;
    int dropLevel = 0;
    int x = 0;
    int count = 0;
    
    int [] col1 = {0,0,0,0,0,0,0};
    int [] col2 = {0,0,0,0,0,0,0};
    int [] col3 = {0,0,0,0,0,0,0};
    int [] col4 = {0,0,0,0,0,0,0};
    int [] col5 = {0,0,0,0,0,0,0};
    int [] col6 = {0,0,0,0,0,0,0};
    int [] col7 = {0,0,0,0,0,0,0};
    
    
    public void keyPressed(KeyEvent ke) {
       
      switch (ke.getKeyCode()) {
         
        case KeyEvent.VK_LEFT:  // move x coordinate left
          
          if (selectorPos != 1) {
             
             selectorX -= 60;
             selectorPos -= 1;
          }
                      
             
          break;
          
        case KeyEvent.VK_RIGHT: // move x coordinate right
            
          if (selectorPos != 7) {
             
             selectorX += 60;
             selectorPos += 1;
          }
                  
         
          break;
          
        case KeyEvent.VK_ENTER:  // place piece when pressed
         
            
            break;
      }
      
      repaint(0,0,540,50);

      
    }   		
    
    public void keyTyped(KeyEvent event) {}
    public void keyReleased(KeyEvent event){}
    
    public void dropPiece() {
    
      
    	
    }	

    
    public void init() {
    	
      addKeyListener(this);
      requestFocus();

    }
    
    public void start() {
    
        
    } 

    public void paint (Graphics gr) {
    	
      setBackground(Color.white);
    	
      //left/right stand
    	
      gr.setColor(Color.blue);
      gr.fillRect(40,60,10,420);
      gr.fillRect(490,60,10,420);
      gr.fillRect(50,450,440,2);
    	
      //middle
    	
      gr.setColor(Color.yellow); 	
      gr.fillRect(50,70,440,380);
    	
   	  //circles
    	
      gr.setColor(Color.white); 
    	
      gr.fillOval(65,85,50,50); // column #1
      gr.fillOval(65,145,50,50); 
      gr.fillOval(65,205,50,50); 
      gr.fillOval(65,265,50,50); 
      gr.fillOval(65,325,50,50); 
      gr.fillOval(65,385,50,50); 
    	
      gr.fillOval(125,85,50,50); // column #2
      gr.fillOval(125,145,50,50); 
      gr.fillOval(125,205,50,50); 
      gr.fillOval(125,265,50,50); 
      gr.fillOval(125,325,50,50); 
      gr.fillOval(125,385,50,50); 
    	
      gr.fillOval(185,85,50,50); // column #3
      gr.fillOval(185,145,50,50); 
      gr.fillOval(185,205,50,50); 
      gr.fillOval(185,265,50,50); 
      gr.fillOval(185,325,50,50); 
      gr.fillOval(185,385,50,50); 
    	
      gr.fillOval(245,85,50,50); // column #4
      gr.fillOval(245,145,50,50); 
      gr.fillOval(245,205,50,50); 
      gr.fillOval(245,265,50,50); 
      gr.fillOval(245,325,50,50); 
      gr.fillOval(245,385,50,50); 
    	
      gr.fillOval(305,85,50,50); // column #5
      gr.fillOval(305,145,50,50); 
   	  gr.fillOval(305,205,50,50); 
      gr.fillOval(305,265,50,50); 
      gr.fillOval(305,325,50,50); 
      gr.fillOval(305,385,50,50); 
    	
      gr.fillOval(365,85,50,50); // column #6
      gr.fillOval(365,145,50,50); 
      gr.fillOval(365,205,50,50); 
      gr.fillOval(365,265,50,50); 
      gr.fillOval(365,325,50,50); 
      gr.fillOval(365,385,50,50); 
    	
   	  gr.fillOval(425,85,50,50); // column #7
      gr.fillOval(425,145,50,50); 
      gr.fillOval(425,205,50,50); 
      gr.fillOval(425,265,50,50); 
      gr.fillOval(425,325,50,50); 
      gr.fillOval(425,385,50,50); 
    	
    	
    	
    	
   	// gridlines
   	    	
    //	gr.setColor(Color.black); 
    //	gr.drawLine(50,100,480,100); 
    //	gr.drawLine(50,160,480,160);
    //	gr.drawLine(50,220,480,220); 
    //	gr.drawLine(50,280,480,280);
    //	gr.drawLine(50,340,480,340);
    //	
    //	gr.drawLine(120,40,120,400); 
    //	gr.drawLine(180,40,180,400); 
    //  gr.drawLine(240,40,240,400); 
    //	gr.drawLine(300,40,300,400); 
    //	gr.drawLine(360,40,360,400); 
    //	gr.drawLine(420,40,420,400); 
    
     
       if (playerTurn == 1)                    //select colour to paint circlebased on which players turn it is   
         gr.setColor(Color.red);
       else
         gr.setColor(Color.black); 
        
       gr.fillOval(selectorX,selectorY,50,50); // paint circle selector
       
    
     
     

    
            
    	
    
    }
    
    public void stop() {
    	
    }

    public void destroy () {
    	
    
    }

     
  }
  

[Edited by - BlackReign on January 12, 2005 6:46:11 AM]

Share this post


Link to post
Share on other sites
Advertisement
You can draw everything to a BufferedImage:
// init
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//when you want to draw something new
Graphics2D g = image.getGraphics();
g.draw... --> drawing the new things

//in your paint method you just draw the entire image to the screen
g.drawImage(image,0,0,width,height,null);

I'm not completley sure about all the details of this code so you'd better take a look at the class description for BufferedImage on Sun's site.

Share this post


Link to post
Share on other sites
Dude!

Think/learn about loops!

This monstrum:


gr.fillOval(65,85,50,50); // column #1
gr.fillOval(65,145,50,50);
gr.fillOval(65,205,50,50);
gr.fillOval(65,265,50,50);
gr.fillOval(65,325,50,50);
gr.fillOval(65,385,50,50);

gr.fillOval(125,85,50,50); // column #2
gr.fillOval(125,145,50,50);
gr.fillOval(125,205,50,50);
gr.fillOval(125,265,50,50);
gr.fillOval(125,325,50,50);
gr.fillOval(125,385,50,50);

gr.fillOval(185,85,50,50); // column #3
gr.fillOval(185,145,50,50);
gr.fillOval(185,205,50,50);
gr.fillOval(185,265,50,50);
gr.fillOval(185,325,50,50);
gr.fillOval(185,385,50,50);

gr.fillOval(245,85,50,50); // column #4
gr.fillOval(245,145,50,50);
gr.fillOval(245,205,50,50);
gr.fillOval(245,265,50,50);
gr.fillOval(245,325,50,50);
gr.fillOval(245,385,50,50);

gr.fillOval(305,85,50,50); // column #5
gr.fillOval(305,145,50,50);
gr.fillOval(305,205,50,50);
gr.fillOval(305,265,50,50);
gr.fillOval(305,325,50,50);
gr.fillOval(305,385,50,50);

gr.fillOval(365,85,50,50); // column #6
gr.fillOval(365,145,50,50);
gr.fillOval(365,205,50,50);
gr.fillOval(365,265,50,50);
gr.fillOval(365,325,50,50);
gr.fillOval(365,385,50,50);

gr.fillOval(425,85,50,50); // column #7
gr.fillOval(425,145,50,50);
gr.fillOval(425,205,50,50);
gr.fillOval(425,265,50,50);
gr.fillOval(425,325,50,50);
gr.fillOval(425,385,50,50);




may be vastly simplified into this (among other things):

for (int x = 0; x < 7; x++) //seven columns
for (int y = 0; y < 6; y++) //six rows
gr.fillOval(65 + x * 60, 85 + y * 60, 50, 50);



It'll save you a lot of typing, and is also less prone to errors. As for the original question - redrawing the whole grid isn't too bad; don't prematurely optimize here. But you can just redraw the area in question, by painting a fillOval() over it, or a fillRect(), and updating it accordingly. I think you can also use gr.setClip() for that purpose, which is even a bit more elegant. Nonetheless, I think it's not necessary to do any of this. Figure out your game code first, then begin optimizing it if it's really too slow.

Hope to help!

Share this post


Link to post
Share on other sites
Thanks for the code simplification, but if you don't mind can you or someone elaborate on your other idea? I'm not quite sure what you mean.

I've already set up my code for a BufferedImage but it "cannot resolve symbol variable image". I've looked for the answer to no avail.

Thanks for all your help!

EDIT: Solved

[Edited by - BlackReign on January 12, 2005 6:22:25 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!