Results 1 to 8 of 8
  1. #1
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default Checkers Game problems

    okay guys so I'm trying to program a checkers game and I'm having some problems. I have created an SSCCE to show you guys. The graphics arent working right because I had to use internet images and i didnt have time to fit them all together but you should be able to get a general idea of the problems.
    some of the problems are:
    1 The checks dont turn green when you click them
    2 Also when you click the checks, 2 gray circles are supposed to show up where the checker can move but arent
    3 the Move() methods in the checker classes arent working correctly
    any solutions to these problems are appreciated. thanks.

    CheckersGameSSCCE class:
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    public class CheckersGameSSCCE extends Applet implements MouseMotionListener, MouseListener{
    	public BlackChecker b [] = new BlackChecker [12];
    	public RedChecker r [] = new RedChecker [12];
    	public int board[][] = new int [][] {{0,1,0,1,0,1,0,1,},{1,0,1,0,1,0,1,0},
    			{0,1,0,1,0,1,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{2,0,2,0,2,0,2,0},{0,2,0,2,0,2,0,2},{2,0,2,0,2,0,2,0}};
    	
    	int xpos;
    	int ypos;
    	public int xposc;
    	public int yposc;
    	boolean rCheckerPSelect [] = new boolean [12];
    	boolean bCheckerPSelect [] = new boolean [12];
    	boolean rCheckerSelect [] = new boolean [12];
    	boolean bCheckerSelect [] = new boolean [12];
            
            Color bgColor = Color.black;
            
            Image checkerboard;
            Image redChecker;
            Image redCheckerKing;
            Image blackChecker;
            Image blackCheckerKing;
            Image pselect;
            Image select;
            Image sDot;
            URL base;
            MediaTracker mt;
            
            Graphics bufferGraphics;
            Image offscreen;
            
            public CheckersGameSSCCE(){}
            
            public void init(){
                try {
         	               base = getDocumentBase();
        	         }
                	         catch (Exception e) {} 
                	         
                mt = new MediaTracker(this);
                checkerboard = getImage(base,"http://www.world-wide-net.com/library/graphics/8x8.gif");
                redChecker = getImage(base,"http://www.ozark.k12.mo.us/jrhi/cooks/redcircle.gif");
                blackChecker = getImage(base,"http://s2.webstarts.com/famousmarketstrategies/uploads/Black_circle.jpg");
                pselect = getImage(base,"http://www.clipartguide.com/_small/0808-0710-2914-4646.jpg");
                select = getImage(base,"http://www.clipartguide.com/_small/0808-0710-2914-4641.jpg");
                sDot = getImage(base,"http://www.visasint.com/images/GreyCircleGif.gif");
                
                mt.addImage(checkerboard,1);
                mt.addImage(redChecker,2);
                mt.addImage(blackChecker,3);
                mt.addImage(pselect,4);
                mt.addImage(select,5);
                mt.addImage(sDot,6);
                
                try {
                    mt.waitForAll();
                	}
               catch (InterruptedException  e) {} 
    
                addMouseMotionListener(this);
                addMouseListener(this);
                
                setBackground(bgColor);
                
                offscreen = createImage(600,600);
                bufferGraphics = offscreen.getGraphics();
                
                BlackChecker b1 = new BlackChecker(1,0,75,0);
                BlackChecker b2 = new BlackChecker(3,0,225,0);
                BlackChecker b3 = new BlackChecker(5,0,375,0);
                BlackChecker b4 = new BlackChecker(7,0,525,0);
                BlackChecker b5 = new BlackChecker(0,1,0,75);
                BlackChecker b6 = new BlackChecker(2,1,150,75);
                BlackChecker b7 = new BlackChecker(4,1,300,75);
                BlackChecker b8 = new BlackChecker(6,1,450,75);
                BlackChecker b9 = new BlackChecker(1,2,75,150);
                BlackChecker b10 = new BlackChecker(3,2,225,150);
                BlackChecker b11 = new BlackChecker(5,2,375,150);
                BlackChecker b12 = new BlackChecker(7,2,525,150);
                b[0] = b1;
                b[1] = b2;
                b[2] = b3;
                b[3] = b4;
                b[4] = b5;
                b[5] = b6;
                b[6] = b7;
                b[7] = b8;
                b[8] = b9;
                b[9] = b10;
                b[10] = b11;
                b[11] = b12;
                
                
                RedChecker r1 = new RedChecker(0,5,0,375);
                RedChecker r2 = new RedChecker(2,5,150,375);
                RedChecker r3 = new RedChecker(4,5,300,375);
                RedChecker r4 = new RedChecker(6,5,450,375);
                RedChecker r5 = new RedChecker(1,6,75,450);
                RedChecker r6 = new RedChecker(3,6,225,450);
                RedChecker r7 = new RedChecker(5,6,375,450);
                RedChecker r8 = new RedChecker(7,6,525,450);
                RedChecker r9 = new RedChecker(0,7,0,525);
                RedChecker r10 = new RedChecker(2,7,150,525);
                RedChecker r11 = new RedChecker(4,7,300,525);
                RedChecker r12 = new RedChecker(6,7,450,525);
                r[0] = r1;
                r[1] = r2;
                r[2] = r3;
                r[3] = r4;
                r[4] = r5;
                r[5] = r6;
                r[6] = r7;
                r[7] = r8;
                r[8] = r9;
                r[9] = r10;
                r[10] = r11;
                r[11] = r12;
                
    
                
            }
            
            public void stop(){
            }
            
            public void paint(Graphics g){
            resize(600,600);
            bufferGraphics.clearRect(0,0,600,600);
            
            bufferGraphics.drawImage(checkerboard, 0, 0, 600, 600, this);
            
            for(int i = 0; i < 12; i++){
                bufferGraphics.drawImage(blackChecker, b[i].blackCheckerBoardX, b[i].blackCheckerBoardY, 75, 75, this);
                bufferGraphics.drawImage(redChecker, r[i].redCheckerBoardX, r[i].redCheckerBoardY, 75, 75, this);
                if(rCheckerPSelect[i]){
                	bufferGraphics.drawImage(pselect, r[i].redCheckerBoardX, r[i].redCheckerBoardY, 75, 75, this);
                }
                if(bCheckerPSelect[i]){
                	bufferGraphics.drawImage(pselect, b[i].blackCheckerBoardX, b[i].blackCheckerBoardY, 75, 75, this);
                }
                if(rCheckerSelect[i]){
                	bufferGraphics.drawImage(select, r[i].redCheckerBoardX, r[i].redCheckerBoardY, 75, 75, this);
                	if(board[r[i].redCheckerX-1][r[i].redCheckerY-1] == 0){
                		bufferGraphics.drawImage(sDot, r[i].redCheckerBoardX-75, r[i].redCheckerBoardY-75, 75, 75, this);
                	}
                	if(board[r[i].redCheckerX-1][r[i].redCheckerY-1] == 1 && board[r[i].redCheckerX-2][r[i].redCheckerY-2] == 0){
                		bufferGraphics.drawImage(sDot, r[i].redCheckerBoardX-150, r[i].redCheckerBoardY-150, 75, 75, this);
                	}
                	if(board[r[i].redCheckerX+1][r[i].redCheckerY-1] == 0){
                		bufferGraphics.drawImage(sDot, r[i].redCheckerBoardX+75, r[i].redCheckerBoardY-75, 75, 75, this);
                	}
                	if(board[r[i].redCheckerX+1][r[i].redCheckerY-1] == 1 && board[r[i].redCheckerX+2][r[i].redCheckerY-2] == 0){
                		bufferGraphics.drawImage(sDot, r[i].redCheckerBoardX+150, r[i].redCheckerBoardY-150, 75, 75, this);
                	}
                	RedChecker.Move(i);
                }
                if(bCheckerSelect[i]){
                	bufferGraphics.drawImage(select, b[i].blackCheckerBoardX, b[i].blackCheckerBoardY, 75, 75, this);
                	if(board[b[i].blackCheckerX-1][b[i].blackCheckerY+1] == 0){
                		bufferGraphics.drawImage(sDot, b[i].blackCheckerBoardX-75, b[i].blackCheckerBoardY+75, 75, 75, this);
                	}
                	if(board[b[i].blackCheckerX-1][b[i].blackCheckerY+1] == 2 && board[b[i].blackCheckerX-2][b[i].blackCheckerY+2] == 0){
                		bufferGraphics.drawImage(sDot, b[i].blackCheckerBoardX-150, b[i].blackCheckerBoardY+150, 75, 75, this);
                	}
                	if(board[b[i].blackCheckerX+1][b[i].blackCheckerY+1] == 0){
                		bufferGraphics.drawImage(sDot, b[i].blackCheckerBoardX+75, b[i].blackCheckerBoardY+75, 75, 75, this);
                	}
                	if(board[b[i].blackCheckerX+1][b[i].blackCheckerY+1] == 2 && board[b[i].blackCheckerX+2][b[i].blackCheckerY+2] == 0){
                		bufferGraphics.drawImage(sDot, b[i].blackCheckerBoardX+150, b[i].blackCheckerBoardY+150, 75, 75, this);
                	}
                	BlackChecker.Move(i);
                }
            }        
            /*bufferGraphics.setColor(Color.red);
            bufferGraphics.drawString("("+xpos+","+ypos+")",xpos,ypos);*/
            g.drawImage(offscreen, 0, 0, this);
        }
            
            public void update(Graphics g){
            	paint(g);
            }
        
        public void mouseMoved(MouseEvent me){
            xpos = me.getX();
            ypos = me.getY();
            repaint();
            for(int i = 0; i < 12; i++){
                if((xpos >= r[i].redCheckerBoardX && xpos <= (r[i].redCheckerBoardX+75))&&(ypos >= r[i].redCheckerBoardY && ypos <= (r[i].redCheckerBoardY+75))){
                    rCheckerPSelect[i] = true;
                    repaint();
                }
                else{
                	rCheckerPSelect[i] = false;
                }
                if((xpos >= b[i].blackCheckerBoardX && xpos <= (b[i].blackCheckerBoardX+75))&&(ypos >= b[i].blackCheckerBoardY && ypos <= (b[i].blackCheckerBoardY+75))){
                    bCheckerPSelect[i] = true;
                    repaint();
                }
                else{
                	bCheckerPSelect[i] = false;
                }
            }
        }
            public void mouseDragged(MouseEvent me){}
            public void mouseClicked(MouseEvent me){
                xposc = me.getX();
                yposc = me.getY();
                repaint();
                for(int i = 0; i < 12; i++){
                    if((xposc >= r[i].redCheckerBoardX && xposc <= (r[i].redCheckerBoardX+75))&&(yposc >= r[i].redCheckerBoardY && yposc <= (r[i].redCheckerBoardY+75))){
                        rCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	rCheckerSelect[i] = false;
                    }
                    if((xposc >= b[i].blackCheckerBoardX && xposc <= (b[i].blackCheckerBoardX+75))&&(yposc >= b[i].blackCheckerBoardY && yposc <= (b[i].blackCheckerBoardY+75))){
                        bCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	bCheckerSelect[i] = false;
                    }
                }
            }
            public void mousePressed(MouseEvent me){}
            public void mouseReleased(MouseEvent me){}
            public void mouseEntered(MouseEvent me){}
            public void mouseExited(MouseEvent me){}
        }
    RedChecker class:
    Java Code:
    public class RedChecker {
    	
    	public int redCheckerX;
    	public int redCheckerY;
    	public int redCheckerBoardX;
    	public int redCheckerBoardY;
    	
    	public RedChecker(int x, int y, int bx, int by){
    		redCheckerX = x;
    		redCheckerY = y;
    		redCheckerBoardX = bx;
    		redCheckerBoardY = by;
    	}
    	
    	static CheckersGameSSCCE c = new CheckersGameSSCCE();
    	
    	public static void Move(int i){
    		if((c.xposc >= c.r[i].redCheckerBoardX+75 && c.xposc <= c.r[i].redCheckerBoardX+150) && (c.yposc >= c.r[i].redCheckerBoardY-75 && c.yposc <= c.r[i].redCheckerBoardY) && c.board[c.r[i].redCheckerX+1][c.r[i].redCheckerY-1] == 0){
    			c.r[i].redCheckerBoardX += 75;
    			c.r[i].redCheckerBoardY -= 75;
    			c.r[i].redCheckerX++;
    			c.r[i].redCheckerY--;
    			c.repaint();
    		}
    		if((c.xposc >= c.r[i].redCheckerBoardX-75 && c.xposc <= c.r[i].redCheckerBoardX) && (c.yposc >= c.r[i].redCheckerBoardY-75 && c.yposc <= c.r[i].redCheckerBoardY) && c.board[c.r[i].redCheckerX+1][c.r[i].redCheckerY-1] == 0){
    			c.r[i].redCheckerBoardX -= 75;
    			c.r[i].redCheckerBoardY -= 75;
    			c.r[i].redCheckerX--;
    			c.r[i].redCheckerY--;
    			c.repaint();
    		}
    		if((c.xposc >= c.r[i].redCheckerBoardX+75 && c.xposc <= c.r[i].redCheckerBoardX+150) && (c.yposc >= c.r[i].redCheckerBoardY-75 && c.yposc <= c.r[i].redCheckerBoardY) && c.board[c.r[i].redCheckerX+1][c.r[i].redCheckerY-1] == 2 && c.board[c.r[i].redCheckerX+2][c.r[i].redCheckerY-2] == 0){
    			c.r[i].redCheckerBoardX += 150;
    			c.r[i].redCheckerBoardY -= 150;
    			c.r[i].redCheckerX += 2;
    			c.r[i].redCheckerY -= 2;
    			c.repaint();
    		}
    		if((c.xposc >= c.r[i].redCheckerBoardX-75 && c.xposc <= c.r[i].redCheckerBoardX) && (c.yposc >= c.r[i].redCheckerBoardY-75 && c.yposc <= c.r[i].redCheckerBoardY) && c.board[c.r[i].redCheckerX-1][c.r[i].redCheckerY-1] == 2 && c.board[c.r[i].redCheckerX-2][c.r[i].redCheckerY-2] == 0){
    			c.r[i].redCheckerBoardX -= 150;
    			c.r[i].redCheckerBoardY -= 150;
    			c.r[i].redCheckerX -= 2;
    			c.r[i].redCheckerY -= 2;
    			c.repaint();
    		}
    	}
    }
    BlackChecker class
    Java Code:
    public class BlackChecker{
    
    	public int blackCheckerX;
    	public int blackCheckerY;
    	public int blackCheckerBoardX;
    	public int blackCheckerBoardY;
    	
    	public BlackChecker(int x, int y, int bx, int by){
    		blackCheckerX = x;
    		blackCheckerY = y;
    		blackCheckerBoardX = bx;
    		blackCheckerBoardY = by;
    	}
    	
    	static CheckersGameSSCCE c = new CheckersGameSSCCE();
    	
    	public static void Move(int i){
    		if((c.xposc >= c.b[i].blackCheckerBoardX+75 && c.xposc >= c.b[i].blackCheckerBoardX+150) && (c.yposc >= c.b[i].blackCheckerBoardY+75 && c.yposc <= c.b[i].blackCheckerBoardY+150) && c.board[c.b[i].blackCheckerX+1][c.b[i].blackCheckerY+1] == 0){
    			c.b[i].blackCheckerBoardX += 75;
    			c.b[i].blackCheckerBoardY += 75;
    			c.b[i].blackCheckerX++;
    			c.b[i].blackCheckerY++;
    			c.repaint();
    		}
    		if((c.xposc >= c.b[i].blackCheckerBoardX-75 && c.xposc >= c.b[i].blackCheckerBoardX) && (c.yposc >= c.b[i].blackCheckerBoardY+75 && c.yposc <= c.b[i].blackCheckerBoardY+150) && c.board[c.b[i].blackCheckerX-1][c.b[i].blackCheckerY+1] == 0){
    			c.b[i].blackCheckerBoardX -= 75;
    			c.b[i].blackCheckerBoardY += 75;
    			c.b[i].blackCheckerX--;
    			c.b[i].blackCheckerY++;
    			c.repaint();
    		}
    		if((c.xposc >= c.b[i].blackCheckerBoardX+150 && c.xposc >= c.b[i].blackCheckerBoardX+225) && (c.yposc >= c.b[i].blackCheckerBoardY+150 && c.yposc <= c.b[i].blackCheckerBoardY+225) && c.board[c.b[i].blackCheckerX+1][c.b[i].blackCheckerY+1] == 1 && c.board[c.b[i].blackCheckerX+2][c.b[i].blackCheckerY+2] == 0){
    			c.b[i].blackCheckerBoardX += 150;
    			c.b[i].blackCheckerBoardY += 150;
    			c.b[i].blackCheckerX += 2;
    			c.b[i].blackCheckerY += 2;
    			c.repaint();
    		}
    		if((c.xposc >= c.b[i].blackCheckerBoardX-75 && c.xposc >= c.b[i].blackCheckerBoardX) && (c.yposc >= c.b[i].blackCheckerBoardY+75 && c.yposc <= c.b[i].blackCheckerBoardY+150) && c.board[c.b[i].blackCheckerX-1][c.b[i].blackCheckerY+1] == 1 && c.board[c.b[i].blackCheckerX-2][c.b[i].blackCheckerY+2] == 0){
    			c.b[i].blackCheckerBoardX -= 150;
    			c.b[i].blackCheckerBoardY += 150;
    			c.b[i].blackCheckerX -= 2;
    			c.b[i].blackCheckerY += 2;
    			c.repaint();
    		}
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    For your first question, can you show me the specific code segment, the event you invoke. It's a long text, and difficult to read.

  3. #3
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default

    for the first problem, that code uses
    Java Code:
     public void mouseClicked(MouseEvent me){
                xposc = me.getX();
                yposc = me.getY();
                repaint();
                for(int i = 0; i < 12; i++){
                    if((xposc >= r[i].redCheckerBoardX && xposc <= (r[i].redCheckerBoardX+75))&&(yposc >= r[i].redCheckerBoardY && yposc <= (r[i].redCheckerBoardY+75))){
                        rCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	rCheckerSelect[i] = false;
                    }
                    if((xposc >= b[i].blackCheckerBoardX && xposc <= (b[i].blackCheckerBoardX+75))&&(yposc >= b[i].blackCheckerBoardY && yposc <= (b[i].blackCheckerBoardY+75))){
                        bCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	bCheckerSelect[i] = false;
                    }

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by dyelax View Post
    for the first problem, that code uses
    Java Code:
     public void mouseClicked(MouseEvent me){
                xposc = me.getX();
                yposc = me.getY();
                repaint();
                for(int i = 0; i < 12; i++){
                    if((xposc >= r[i].redCheckerBoardX && xposc <= (r[i].redCheckerBoardX+75))&&(yposc >= r[i].redCheckerBoardY && yposc <= (r[i].redCheckerBoardY+75))){
                        rCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	rCheckerSelect[i] = false;
                    }
                    if((xposc >= b[i].blackCheckerBoardX && xposc <= (b[i].blackCheckerBoardX+75))&&(yposc >= b[i].blackCheckerBoardY && yposc <= (b[i].blackCheckerBoardY+75))){
                        bCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	bCheckerSelect[i] = false;
                    }

    Dude; long conditions. I've rewritten them for you:

    Java Code:
    public void mouseClicked(MouseEvent me){
                xposc = me.getX();
                yposc = me.getY();
                repaint();
                for (int i = 0; i < 12; i++) {
    		int rx = r[i].redCheckerBoardX;
    		int ry = r[i].redCheckerBoardY;
    
                    if ((xposc >= rc && xposc <= rx + 75) && (yposc >= ry && yposc <= ry + 75)){
                        rCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	rCheckerSelect[i] = false;
                    }
    
    		int bx = b[i].blackCheckerBoardX;
    		int by = b[i].blackCheckerBoardY;
    
                    if ((xposc >= bx && xposc <= bx + 75) && (yposc >= by && yposc <= by + 75)){
                        bCheckerSelect[i] = true;
                        repaint();
                    }
                    else{
                    	bCheckerSelect[i] = false;
                    }
    
    		// [...]

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I reduced and styled up your CheckersGameSSCCE class a bit,
    hopefully it is easier to read (and modify) now.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    
    public class CheckersGameSSCCE extends Applet implements MouseMotionListener, MouseListener
    {
        public BlackChecker[] b = new BlackChecker[12];
        public RedChecker[] r = new RedChecker[12];
        public int[][] board = new int[][] {
                {0, 1, 0, 1, 0, 1, 0, 1},
                {1, 0, 1, 0, 1, 0, 1, 0},
                {0, 1, 0, 1, 0, 1, 0, 1},
                {0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0},
                {2, 0, 2, 0, 2, 0, 2, 0},
                {0, 2, 0, 2, 0, 2, 0, 2},
                {2, 0, 2, 0, 2, 0, 2, 0}};
        
        int xpos;
        int ypos;
        public int xposc;
        public int yposc;
        boolean[] rCheckerPSelect = new boolean[12];
        boolean[] bCheckerPSelect = new boolean[12];
        boolean[] rCheckerSelect = new boolean[12];
        boolean[] bCheckerSelect = new boolean[12];
            
        Color bgColor = Color.black;
            
        Image checkerboard;
        Image redChecker;
        Image redCheckerKing;
        Image blackChecker;
        Image blackCheckerKing;
        Image pselect;
        Image select;
        Image sDot;
        URL base;
        MediaTracker mt;
            
        Graphics bufferGraphics;
        Image offscreen;
            
        public CheckersGameSSCCE(){}
            
        public void init()
        {
            try {
                base = getDocumentBase();
            }
            catch (Exception e) {} 
    
            mt = new MediaTracker(this);
            checkerboard = getImage(base,"http://www.world-wide-net.com/library/graphics/8x8.gif");
            redChecker   = getImage(base,"http://www.ozark.k12.mo.us/jrhi/cooks/redcircle.gif");
            blackChecker = getImage(base,"http://s2.webstarts.com/famousmarketstrategies/uploads/Black_circle.jpg");
            pselect      = getImage(base,"http://www.clipartguide.com/_small/0808-0710-2914-4646.jpg");
            select       = getImage(base,"http://www.clipartguide.com/_small/0808-0710-2914-4641.jpg");
            sDot         = getImage(base,"http://www.visasint.com/images/GreyCircleGif.gif");
    
            mt.addImage(checkerboard, 1);
            mt.addImage(redChecker, 2);
            mt.addImage(blackChecker, 3);
            mt.addImage(pselect, 4);
            mt.addImage(select, 5);
            mt.addImage(sDot, 6);
    
            try {
                mt.waitForAll();
            }
            catch (InterruptedException e) {} 
    
            addMouseMotionListener(this);
            addMouseListener(this);
    
            setBackground(bgColor);
                
            offscreen = createImage(600, 600);
            bufferGraphics = offscreen.getGraphics();
    
            for (int bi = 0, ri = 0, y = 0; y < 8; y++)
                for (int x = 0; x < 8; x++)
                    if      (board[y][x] == 1)  b[bi++] = new BlackChecker(x, y, x * 75, y * 75);
                    else if (board[y][x] == 2)  r[ri++] = new   RedChecker(x, y, x * 75, y * 75);
    
            /* (Replaced by double for-loop above.)
            b[0] = new BlackChecker(1,0,75,0);
            b[1] = new BlackChecker(3,0,225,0);
            b[2] = new BlackChecker(5,0,375,0);
            b[3] = new BlackChecker(7,0,525,0);
            b[4] = new BlackChecker(0,1,0,75);
            b[5] = new BlackChecker(2,1,150,75);
            b[6] = new BlackChecker(4,1,300,75);
            b[7] = new BlackChecker(6,1,450,75);
            b[8] = new BlackChecker(1,2,75,150);
            b[9] = new BlackChecker(3,2,225,150);
            b[10] = new BlackChecker(5,2,375,150);
            b[11] = new BlackChecker(7,2,525,150);
    
            r[0] = new RedChecker(0,5,0,375);
            r[1] = new RedChecker(2,5,150,375);
            r[2] = new RedChecker(4,5,300,375);
            r[3] = new RedChecker(6,5,450,375);
            r[4] = new RedChecker(1,6,75,450);
            r[5] = new RedChecker(3,6,225,450);
            r[6] = new RedChecker(5,6,375,450);
            r[7] = new RedChecker(7,6,525,450);
            r[8] = new RedChecker(0,7,0,525);
            r[9] = new RedChecker(2,7,150,525);
            r[10] = new RedChecker(4,7,300,525);
            r[11] = new RedChecker(6,7,450,525);
            */
        }
    
        public void stop() {}
    
        public void paint(Graphics g)
        {
            resize(600, 600); // ####### This feels like a bad idea, you should only resize once #######
            bufferGraphics.clearRect(0, 0, 600, 600);
    
            bufferGraphics.drawImage(checkerboard, 0, 0, 600, 600, this);
    
            for(int i = 0; i < 12; i++)
            {
                bufferGraphics.drawImage(blackChecker, b[i].blackCheckerBoardX, b[i].blackCheckerBoardY, 75, 75, this);
                bufferGraphics.drawImage(  redChecker, r[i].  redCheckerBoardX, r[i].  redCheckerBoardY, 75, 75, this);
    
                if (rCheckerPSelect[i])     bufferGraphics.drawImage(pselect, r[i].  redCheckerBoardX, r[i].  redCheckerBoardY, 75, 75, this);
                if (bCheckerPSelect[i])     bufferGraphics.drawImage(pselect, b[i].blackCheckerBoardX, b[i].blackCheckerBoardY, 75, 75, this);
    
                if (rCheckerSelect[i])
                {
                    int x = r[i].redCheckerBoardX;
                    int y = r[i].redCheckerBoardY;
    
                    bufferGraphics.drawImage(select, x, y, 75, 75, this);
    
                    if (board[x - 1][y - 1] == 0)                              bufferGraphics.drawImage(sDot, x - 75,  y - 75,  75, 75, this);
                    if (board[x - 1][y - 1] == 1 && board[x - 2][y - 2] == 0)  bufferGraphics.drawImage(sDot, x - 150, y - 150, 75, 75, this);
                    if (board[x + 1][y - 1] == 0)                              bufferGraphics.drawImage(sDot, x + 75,  y - 75,  75, 75, this);
                    if (board[x + 1][y - 1] == 1 && board[x + 2][y - 2] == 0)  bufferGraphics.drawImage(sDot, x + 150, y - 150, 75, 75, this);
    
                    RedChecker.Move(i);
                }
                if (bCheckerSelect[i])
                {
                    int x = b[i].blackCheckerBoardX;
                    int y = b[i].blackCheckerBoardY;
    
                    bufferGraphics.drawImage(select, x, y, 75, 75, this);
    
                    if (board[x - 1][y + 1] == 0)                              bufferGraphics.drawImage(sDot, x - 75,  y + 75,  75, 75, this);
                    if (board[x - 1][y + 1] == 2 && board[x - 2][y + 2] == 0)  bufferGraphics.drawImage(sDot, x - 150, y + 150, 75, 75, this);
                    if (board[x + 1][y + 1] == 0)                              bufferGraphics.drawImage(sDot, x + 75,  y + 75,  75, 75, this);
                    if (board[x + 1][y + 1] == 2 && board[x + 2][y + 2] == 0)  bufferGraphics.drawImage(sDot, x + 150, y + 150, 75, 75, this);
    
                    BlackChecker.Move(i);
                }
            }
    
            /*
            bufferGraphics.setColor(Color.red);
            bufferGraphics.drawString("(" + xpos + "," + ypos + ")", xpos, ypos);
            */
            g.drawImage(offscreen, 0, 0, this);
        }
    
        public void update(Graphics g)
        {
            paint(g);
        }
        
        public void mouseMoved  (MouseEvent me)  {  mouseAction(me, rCheckerPSelect, bCheckerPSelect);  }
        public void mouseClicked(MouseEvent me)  {  mouseAction(me, rCheckerSelect,  bCheckerSelect);   }
        public void mouseAction(MouseEvent me, boolean[] rCheckers, boolean[] bCheckers)
        {
            xposc = me.getX();
            yposc = me.getY();
            repaint();
    
            for (int i = 0; i < 12; i++)
            {
                int rx = r[i].redCheckerBoardX;
                int ry = r[i].redCheckerBoardY;
                int bx = b[i].blackCheckerBoardX;
                int by = b[i].blackCheckerBoardY;
    
                rCheckers[i] = (xposc >= x && xposc <= rx + 75) && (yposc >= ry && yposc <= ry + 75);
                bCheckers[i] = (xposc >= bx && xposc <= bx + 75) && (yposc >= by && yposc <= by + 75);
    
                if (rCheckers[i] || bCheckers[i])
                    repaint();
            }
        }
    
        public void mouseDragged(MouseEvent me){}
        public void mousePressed(MouseEvent me){}
        public void mouseReleased(MouseEvent me){}
        public void mouseEntered(MouseEvent me){}
        public void mouseExited(MouseEvent me){}
    }
    Last edited by Hibernate; 12-28-2010 at 04:18 AM. Reason: typo

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I rewrote your RedChecker class so it would be easier to read:

    Java Code:
    public class RedChecker
    {
    	public int redCheckerX;
    	public int redCheckerY;
    	public int redCheckerBoardX;
    	public int redCheckerBoardY;
    	
    	public RedChecker(int x, int y, int bx, int by)
    	{
    		redCheckerX = x;
    		redCheckerY = y;
    		redCheckerBoardX = bx;
    		redCheckerBoardY = by;
    	}
    	
    	static CheckersGameSSCCE c = new CheckersGameSSCCE();
    	
    	public static void Move(int i)
    	{
    		int x = c.r[i].redCheckerBoardX;
    		int y = c.r[i].redCheckerBoardY;
    		int xi = c.r[i].redCheckerX;
    		int yi = c.r[i].redCheckerY;
    		int cx = c.xposc;
    		int cy = c.yposc;
    
    		if (cy >= y - 75 && cy <= y)
    		{
    			if      ((cx >= x + 75 && cx <= x + 150) && c.board[xi + 1][yi - 1] == 0) {
    				c.r[i].redCheckerBoardX += 75;
    				c.r[i].redCheckerBoardY -= 75;
    				c.r[i].redCheckerX++;
    				c.r[i].redCheckerY--;
    				c.repaint();
    			}
    			else if ((cx >= x - 75 && cx <= x)       && c.board[xi + 1][yi - 1] == 0) {
    				c.r[i].redCheckerBoardX -= 75;
    				c.r[i].redCheckerBoardY -= 75;
    				c.r[i].redCheckerX--;
    				c.r[i].redCheckerY--;
    				c.repaint();
    			}
    			else if ((cx >= x + 75 && cx <= x + 150) && c.board[xi + 1][yi - 1] == 2 && c.board[xi + 2][yi - 2] == 0) {
    				c.r[i].redCheckerBoardX += 150;
    				c.r[i].redCheckerBoardY -= 150;
    				c.r[i].redCheckerX += 2;
    				c.r[i].redCheckerY -= 2;
    				c.repaint();
    			}
    			else if ((cx >= x - 75 && cx <= x)       && c.board[xi - 1][yi - 1] == 2 && c.board[xi - 2][yi - 2] == 0) {
    				c.r[i].redCheckerBoardX -= 150;
    				c.r[i].redCheckerBoardY -= 150;
    				c.r[i].redCheckerX -= 2;
    				c.r[i].redCheckerY -= 2;
    				c.repaint();
    			}
    	}
    
    }
    BlackChecker can be modified analogously.

    However it is possible to merge the two classes:

    Java Code:
    public class Checker
    {
    	public int checkerX;
    	public int checkerY;
    	public int checkerBoardX;
    	public int checkerBoardY;
    	public boolean red;
    	
    	/**
    	 * @param  red  <code>true</code> if red, <code>false</code> if black
    	 */
    	public Checker(int x, int y, int bx, int by, boolean red)
    	{
    		checkerX = x;
    		checkerY = y;
    		checkerBoardX = bx;
    		checkerBoardY = by;
    		this.red = red;
    	}
    	
    	static CheckersGameSSCCE c = new CheckersGameSSCCE();
    	
    	public static void Move(int i)
    	{
    		Checker checker = (red ? c.r : c.b)[i];
    		int colour = red ? 2 : 1;	//2 if red is true, otherwise 1
    
    		int x = checker.checkerBoardX;
    		int y = checker.checkerBoardY;
    		int xi = checker.checkerX;
    		int yi = checker.checkerY;
    		int cx = c.xposc;
    		int cy = c.yposc;
    
    		if (cy >= y - 75 && cy <= y)
    		{
    			if      ((cx >= x + 75 && cx <= x + 150) && c.board[xi + 1][yi - 1] == 0) {
    				checker.checkerBoardX += 75;
    				checker.checkerBoardY -= 75;
    				checker.checkerX++;
    				checker.checkerY--;
    				c.repaint();
    			}
    			else if ((cx >= x - 75 && cx <= x)       && c.board[xi + 1][yi - 1] == 0) {
    				checker.checkerBoardX -= 75;
    				checker.checkerBoardY -= 75;
    				checker.checkerX--;
    				checker.checkerY--;
    				c.repaint();
    			}
    			else if ((cx >= x + 75 && cx <= x + 150) && c.board[xi + 1][yi - 1] == colour && c.board[xi + 2][yi - 2] == 0) {
    				checker.checkerBoardX += 150;
    				checker.checkerBoardY -= 150;
    				checker.checkerX += 2;
    				checker.checkerY -= 2;
    				c.repaint();
    			}
    			else if ((cx >= x - 75 && cx <= x)       && c.board[xi - 1][yi - 1] == colour && c.board[xi - 2][yi - 2] == 0) {
    				checker.checkerBoardX -= 150;
    				checker.checkerBoardY -= 150;
    				checker.checkerX -= 2;
    				checker.checkerY -= 2;
    				c.repaint();
    			}
    	}
    
    }
    Now I think your problems will be solved, or atleast one, if you edit it a bit:

    Java Code:
    public class Checker
    {
    	public int checkerX;
    	public int checkerY;
    	public int checkerBoardX;
    	public int checkerBoardY;
    	public boolean red;
    	private CheckersGameSSCCE c;
    	
    	/**
    	 * @param  red  <code>true</code> if red, <code>false</code> if black
    	 * @param  c    the CheckersGameSSCCE instance, <code>this<code> when constructed from your CheckersGameSSCCE instance 
    	 */
    	public Checker(int x, int y, int bx, int by, boolean red, CheckersGameSSCCE c)
    	{
    		checkerX = x;
    		checkerY = y;
    		checkerBoardX = bx;
    		checkerBoardY = by;
    
    		this.red = red;
    		this.c = c;
    	}
    	
    	public static void Move(int i)
    	{
    		Checker checker = (red ? c.r : c.b)[i];
    		int colour = red ? 2 : 1;	//2 if red is true, otherwise 1
    
    		int x = checker.checkerBoardX;
    		int y = checker.checkerBoardY;
    		int xi = checker.checkerX;
    		int yi = checker.checkerY;
    		int cx = c.xposc;
    		int cy = c.yposc;
    
    		if (cy >= y - 75 && cy <= y)
    		{
    			if      ((cx >= x + 75 && cx <= x + 150) && c.board[xi + 1][yi - 1] == 0) {
    				checker.checkerBoardX += 75;
    				checker.checkerBoardY -= 75;
    				checker.checkerX++;
    				checker.checkerY--;
    				c.repaint();
    			}
    			else if ((cx >= x - 75 && cx <= x)       && c.board[xi + 1][yi - 1] == 0) {
    				checker.checkerBoardX -= 75;
    				checker.checkerBoardY -= 75;
    				checker.checkerX--;
    				checker.checkerY--;
    				c.repaint();
    			}
    			else if ((cx >= x + 75 && cx <= x + 150) && c.board[xi + 1][yi - 1] == colour && c.board[xi + 2][yi - 2] == 0) {
    				checker.checkerBoardX += 150;
    				checker.checkerBoardY -= 150;
    				checker.checkerX += 2;
    				checker.checkerY -= 2;
    				c.repaint();
    			}
    			else if ((cx >= x - 75 && cx <= x)       && c.board[xi - 1][yi - 1] == colour && c.board[xi - 2][yi - 2] == 0) {
    				checker.checkerBoardX -= 150;
    				checker.checkerBoardY -= 150;
    				checker.checkerX -= 2;
    				checker.checkerY -= 2;
    				c.repaint();
    			}
    	}
    
    }


    Don't forget if anything helped. ;)
    Last edited by Hibernate; 12-28-2010 at 04:34 AM.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Hibernate, the way you helping is not good. Guide OP to tryout something first by themselves. Otherwise they never learn stuff.

  8. #8
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I know, I'm only one my second year in my Master of Enginnering in Science and Master of Teaching education and I've fail all teaching classes. (Although people always get an A, when I help them privately.)

    Most of that was just clean up so we don't have such a hard time to understand the code.
    But, yes the last code was a partial direct solution,

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  3. Othello/Reversi flip checkers
    By chielt in forum Java Applets
    Replies: 10
    Last Post: 01-02-2010, 11:25 PM
  4. need advice for checkers game
    By javanoob73 in forum New To Java
    Replies: 13
    Last Post: 12-02-2009, 01:10 AM
  5. Checkers
    By evan42781 in forum New To Java
    Replies: 8
    Last Post: 05-08-2009, 04:07 AM

Posting Permissions

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