Results 1 to 11 of 11
  1. #1
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default Othello/Reversi flip checkers

    Hi,

    I'm making a two human players version of othello for my university Java course. I am stuck tho, and I can't really figure out what is going wrong.

    When a person places a checker all enclosed checkers should change to the active player's colour. This doesn't always happen.

    The code beneath can be copy pasted straight into eclipse.
    On a 6x6 playing board (default) with A - F for horizontal and 1 - 6 for vertical, try clicking E4, E3, E2, F3, B2, B1. It sometimes doesn't flip the checkers it had to flip.

    The method draaien() is the method that is supposed to turn all checkers that have been enclosed. I just can't figure out why it isn't always working.

    Can someone help me fix draaien(); or any other method that needs fixing to make this part of my program work?


    Bord.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    
    @SuppressWarnings("serial")
    
    public class Bord extends Applet implements ActionListener, MouseListener
    {
    	Label lAppName, lSize, lX, lBeurt, lPunten1, lPunten2, lStatus;
    	Button bNewGame, bHelp;
    	TextField tSizeX, tSizeY;
    	Panel pMenu, pSpel, pScore, pHeader;
    	MijnCanvas c;
    	int iBeurt, iABord[][], iBreedte, iHoogte, iCanvBreedte, iCanvHoogte;
    	
    	public void init()
    	{
    		iBreedte = 6;
    		iHoogte = 6;
    		iCanvBreedte = 400;
    		iCanvHoogte = 400;
    		iBeurt = 1;
    		
    		lAppName = new Label("Reversi - Othello");
    		lSize = new Label("Grootte:");
    		lX = new Label("x");
    		lBeurt = new Label("Speler 1 is aan zet");
    		lPunten1 = new Label("Speler 1: 2 punten");
    		lPunten2 = new Label("Speler 2: 2 punten");
    		lStatus = new Label("Nieuw spel!");
    		
    		bHelp = new Button("Help");
    		bNewGame = new Button("Nieuw Spel");
    		
    		tSizeX = new TextField(""+iBreedte,3);
    		tSizeY = new TextField(""+iHoogte,3);
    		
    		pMenu = new Panel();
    		pSpel = new Panel();
    		pScore = new Panel();
    		pHeader = new Panel();
    		
    		c = new MijnCanvas(iBreedte, iHoogte, iCanvBreedte,iCanvHoogte);
    		
    		this.setSize(iCanvBreedte+200, iCanvHoogte+100);
    		c.setBackground(new Color(100,100,100));
    		
    		this.setLayout(new BorderLayout());
    
    		this.add(pSpel,BorderLayout.WEST);
    		this.add(pMenu, BorderLayout.SOUTH);
    		this.add(pScore, BorderLayout.CENTER);
    		this.add(pHeader, BorderLayout.NORTH);
    		
    		
    		pHeader.add(lAppName);
    		pSpel.add(c);
    		pMenu.add(bHelp);
    		pMenu.add(bNewGame);
    		pMenu.add(lSize);
    		pMenu.add(tSizeX);
    		pMenu.add(lX);
    		pMenu.add(tSizeY);
    		pScore.add(lBeurt);
    		pScore.add(lPunten1);
    		pScore.add(lPunten2);
    		pScore.add(lStatus);
    		
    		tSizeX.addActionListener(this);	
    		tSizeY.addActionListener(this);
    		bNewGame.addActionListener(this);
    		bHelp.addActionListener(this);
    		c.addMouseListener(this);
    		
    		lAppName.setFont(new Font("Times New Roman", Font.BOLD, 24));		
    		lBeurt.setFont(new Font("Times New Roman", Font.BOLD, 14));	
    		c.newGame();
    	}
    	
    	
    	public void actionPerformed(ActionEvent ap) 
    	{
    		if(ap.getSource()==bNewGame)
    		{
    			lStatus.setText("Nieuw Spel!");
    			
    			iBeurt = 1;
    			lBeurt.setText("Speler 1 is aan zet");
    			c.newGame();
    			setScoreBoard();
    		}
    		
    
    		
    		iBreedte = Integer.parseInt(tSizeX.getText());
    		iHoogte = Integer.parseInt(tSizeY.getText());
    		
    		c.schermGrootte(iBreedte, iHoogte, iCanvBreedte, iCanvHoogte);
    		c.repaint();
    	}
    
    	public void zetBeurt()
    	{
    		iBeurt = 3-iBeurt;
    		if(iBeurt == 1)
    			lBeurt.setText("Speler 1 is aan zet");
    		else if(iBeurt == 2)
    			lBeurt.setText("Speler 2 is aan zet");
    		else
    			lStatus.setText("Foutje, Start nieuw spel!");
    		
    		setScoreBoard();
    	}
    
    	public void mouseClicked(MouseEvent e) 
    	{
    		int iXCor = e.getX()/c.iRuimte+1;
    		int iYCor = e.getY()/c.iRuimte+1;
    		
    		if (controle(iXCor, iYCor))
    		{
    			c.iABord[iXCor][iYCor]=iBeurt;
    			lStatus.setText("Goedzo!");
    			this.draaien(iXCor, iYCor);
    			this.zetBeurt();
    			c.repaint();
    		}
    		else
    			lStatus.setText(iXCor+", "+iYCor+": Ongeldige zet!");
    	}
    
    	private void setScoreBoard()
    	{
    		lPunten1.setText("Speler 1: "+ c.getScore(1)+" punten.");
    		lPunten2.setText("Speler 2: "+ c.getScore(2)+" punten.");
    	}
    	
    	private boolean controle(int iX, int iY)
    	{
    		int iDx, iDy;
    		
    		if(c.iABord[iX][iY]!= 0)
    			return false;
    		else
    			for(iDx = -1; iDx<2;iDx++)
    				for(iDy = -1; iDy < 2; iDy++)
    					if(controle2(iX, iY, iDx, iDy))
    						return true;
    		return false;
    	}
    	
    	private boolean controle2(int iX, int iY, int iDx, int iDy)
    	{
    		int iAfstand = 2;
    		boolean bLoop = true;
    		
    		if(c.iABord[iX+iDx][iY+iDy]==3-iBeurt)
    		{
    			while(bLoop)
    				if(c.iABord[iX+iAfstand*iDx][iY+iAfstand*iDy]==3-iBeurt)
    					iAfstand++;
    				else bLoop = false;
    		
    			if(c.iABord[iX+iAfstand*iDx][iY+iAfstand*iDy]==iBeurt)
    				return true;
    		}
    		return false;
    	}
    	
    	private void draaien(int iX, int iY)
    	{
    		int iAfstand = 2;
    		boolean bLoop = true;
    		int iDx, iDy;
    		
    		for(iDx = -1; iDx<2;iDx++)
    			for(iDy = -1; iDy < 2; iDy++)
    				if(c.iABord[iX+iDx][iY+iDy]==3-iBeurt)
    				{
    					while(bLoop)
    						if(c.iABord[iX+iAfstand*iDx][iY+iAfstand*iDy]==3-iBeurt)
    							iAfstand++;
    						else bLoop = false;
    				
    				if(c.iABord[iX+iAfstand*iDx][iY+iAfstand*iDy]==iBeurt)
    					for(int a = 1; a<= iAfstand; a++)
    						c.iABord[iX+iDx*a][iY+iDy*a] = iBeurt;
    				}
    	}
    	
    
    		
    
    	
    	@Override
    	public void mouseEntered(MouseEvent arg0) {}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {}
    		
    	@Override
    	public void mousePressed(MouseEvent arg0) {}
    	
    	@Override
    	public void mouseReleased(MouseEvent arg0) {}
    	
    }

    MijnCanvas.java
    Java Code:
    import java.awt.*;
    
    @SuppressWarnings("serial")
    public class MijnCanvas extends Canvas
    {
    	int iBreedte, iHoogte, iCanvBreedte, iCanvHoogte, iRuimte, iAantalWit=0, iAantalZwart=0, iABord[][];
    	boolean boolHelp = false;
    	
    	public MijnCanvas(int iBreedteV, int iHoogteV, int iBreedteC, int iHoogteC)
    	{
    		schermGrootte(iBreedteV, iHoogteV, iBreedteC, iHoogteC);
    	}
    	
    	// Deze methode bepaald 5 variabelen: iBreedte, iHoogte, iCanvHoogte, ICanvBreedte en iRuimte
    	// Ook wordt de grootte van het canvas door deze methode aangepast.
    	public void schermGrootte(int iNieuweBreedte, int iNieuweHoogte, int iMaxBreedte, int iMaxHoogte)
    	{
    		iBreedte = iNieuweBreedte; iHoogte = iNieuweHoogte;
    		
    		int iRuimteX, iRuimteY;
    		
    		iRuimteX = iMaxBreedte/iBreedte;
    		iRuimteY = iMaxHoogte/iHoogte;
    		
    		if(iRuimteX <= iRuimteY)
    			iRuimte = iRuimteX;
    		else
    			iRuimte = iRuimteY;
    		
    		iCanvBreedte = iRuimte*iBreedte;
    		iCanvHoogte = iRuimte*iHoogte;
    		
    		this.setSize(iCanvBreedte, iCanvHoogte);
    		this.setBackground(new Color(100,100,100));
    		
    		iABord = new int[iBreedte+2][iHoogte+2];
    		this.newGame();
    
    	}
    	
    	
    	public void paint(Graphics g)
    	{
    		g.drawRect(1, 1, iCanvBreedte-2, iCanvHoogte-2);
    		int iRuimte = this.getWidth()/(iBreedte);
    	
    		int iY = 0;
    		int iX = 0;
    		
    		for(int i=0;i <= iBreedte ;i++)
    		{
    			g.drawLine(iX, 0, iX, iCanvHoogte );
    			iX += iRuimte;
    		}
    		for(int i=0;i <= iHoogte; i++)
    		{
    			g.drawLine(0, iY, iCanvBreedte, iY );
    			iY += iRuimte;
    		}
    		this.tekenSteen(g);
    	}
    	
    	public void tekenSteen(Graphics g)
    	{
    		for (int iX = 1; iX <= iBreedte;iX++)
    			for(int iY = 1; iY <= iHoogte; iY++)
    				if(iABord[iX][iY] == 2)
    				{
    					g.setColor(Color.black );
    					g.fillOval(iRuimte*(iX-1)+2, iRuimte*(iY-1)+2, iRuimte-4, iRuimte-4);
    					iAantalZwart += 1;
    				}
    				else if(iABord[iX][iY] == 1)
    				{
    					g.setColor(Color.white );
    					g.fillOval(iRuimte*(iX-1)+2, iRuimte*(iY-1)+2, iRuimte-4, iRuimte-4);
    					iAantalWit += 1;
    				}
    	}
    	
    
    	public void newGame()
    	{
    		int iX, iY;
    		for(iX = 0; iX < iBreedte; iX++)
    			for(iY = 0; iY < iHoogte; iY++)
    				iABord[iX][iY] = 0;
    		
    		iABord[iBreedte/2][iHoogte/2] = 2;
    		iABord[iBreedte/2][iHoogte/2+1] = 1;
    		iABord[iBreedte/2+1][iHoogte/2] = 1;
    		iABord[iBreedte/2+1][iHoogte/2+1] = 2;
    	}
    	public int getScore(int iSpeler)
    	{
    		int iScore=0;
    		
    		for (int iX = 0;iX<iBreedte;iX++)
    			for(int iY= 0;iY<iBreedte;iY++)
    				if (iABord[iX][iY] == iSpeler)
    					iScore++;
    		
    		return iScore;
    	}
    	
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    can't figure out why it isn't always working.
    The mouseClicked method requires both a mousePressed and a mouseReleased return so it is a bit higher–level than the more basic and reliable mousePressed method.
    So try implementing mousePressed for your Bord class instead of mouseClicked.
    In java, try
    Java Code:
        public void mousePressed(MouseEvent e)
        {
            ...
        @Override
        public void mouseClicked(MouseEvent arg0) {}
    instead of
    Java Code:
        public void mouseClicked(MouseEvent e)
        {
            ...
        @Override
        public void mousePressed(MouseEvent arg0) {}

  3. #3
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    It doesn't really do that much.. It still doesn't flip all the checkers it needs to, to playing player's color.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    doesn't flip all the checkers it needs to
    According to the rules of the game opposition tokens can be captured/flipped in a horizontal, vertical or diagonal line in which the opposing tokens are bounded by the current grid location and another, distant same–color token.
    So it seems that you will need to examine, relative to the current location:
    1 — left and right
    2 — up and down
    3 — along each diagonal from extremes inward
    to get complete coverage.
    I didn't have the patience to deal with the language in your app so I started from scratch and made separate methods for each of the above items.

  5. #5
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    doesn't flip all the checkers it needs to

    I didn't have the patience to deal with the language in your app so I started from scratch and made separate methods for each of the above items.
    Would it help if I translate my variable names and method names to English logicical names instead of Dutch?

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Would it help if I translate my variable names and method names to English
    Yes.

  7. #7
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Well, here is my translated source:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    
    @SuppressWarnings("serial")
    
    public class Main extends Applet implements ActionListener, MouseListener
    {
    	Label lAppName, lSize, lX, lTurn, lPointsP1, lPointsP2, lStatus;
    	Button bNewGame, bHelp;
    	TextField tSizeX, tSizeY;
    	Panel pMenu, pGame, pScore, pHeader;
    	MyCanvas c;
    	int iTurn, iABoard[][], iWidth, iHeight, iCanvWidth, iCanvHeight;
    	
    	public void init()
    	{
    		iWidth = 6;
    		iHeight = 6;
    		iCanvWidth = 400;
    		iCanvHeight = 400;
    		iTurn = 1;
    		
    		lAppName = new Label("Reversi - Othello");
    		lSize = new Label("Grootte:");
    		lX = new Label("x");
    		lTurn = new Label("It's player 1's turn");
    		lPointsP1 = new Label("Speler 1: 2 punten");
    		lPointsP2 = new Label("Speler 2: 2 punten");
    		lStatus = new Label("New Game!");
    		
    		bHelp = new Button("Help");
    		bNewGame = new Button("New Game");
    		
    		tSizeX = new TextField(""+iWidth,3);
    		tSizeY = new TextField(""+iHeight,3);
    		
    		pMenu = new Panel();
    		pGame = new Panel();
    		pScore = new Panel();
    		pHeader = new Panel();
    		
    		c = new MyCanvas(iWidth, iHeight, iCanvWidth,iCanvHeight);
    		
    		this.setSize(iCanvWidth+200, iCanvHeight+100);
    		c.setBackground(new Color(100,100,100));
    		
    		this.setLayout(new BorderLayout());
    
    		this.add(pGame,BorderLayout.WEST);
    		this.add(pMenu, BorderLayout.SOUTH);
    		this.add(pScore, BorderLayout.CENTER);
    		this.add(pHeader, BorderLayout.NORTH);
    		
    		
    		pHeader.add(lAppName);
    		pGame.add(c);
    		pMenu.add(bHelp);
    		pMenu.add(bNewGame);
    		pMenu.add(lSize);
    		pMenu.add(tSizeX);
    		pMenu.add(lX);
    		pMenu.add(tSizeY);
    		pScore.add(lTurn);
    		pScore.add(lPointsP1);
    		pScore.add(lPointsP2);
    		pScore.add(lStatus);
    		
    		tSizeX.addActionListener(this);	
    		tSizeY.addActionListener(this);
    		bNewGame.addActionListener(this);
    		bHelp.addActionListener(this);
    		c.addMouseListener(this);
    		
    		lAppName.setFont(new Font("Times New Roman", Font.BOLD, 24));		
    		lTurn.setFont(new Font("Times New Roman", Font.BOLD, 14));	
    		c.newGame();
    	}
    	
    	
    	public void actionPerformed(ActionEvent ap) 
    	{
    		if(ap.getSource()==bNewGame)
    		{
    			lStatus.setText("New Game!");
    			
    			iTurn = 1;
    			lTurn.setText("It's player 1's turn");
    			c.newGame();
    			setScoreBoard();
    		}
    		
    
    		
    		iWidth = Integer.parseInt(tSizeX.getText());
    		iHeight = Integer.parseInt(tSizeY.getText());
    		
    		c.schermGrootte(iWidth, iHeight, iCanvWidth, iCanvHeight);
    		c.repaint();
    	}
    
    	public void setTurn()
    	{
    		iTurn = 3-iTurn;
    		if(iTurn == 1)
    			lTurn.setText("It's player 1's turn");
    		else if(iTurn == 2)
    			lTurn.setText("It's Player 2's turn");
    		else
    			lStatus.setText("Foutje, Start nieuw spel!");
    		
    		setScoreBoard();
    	}
    
    	public void mouseClicked(MouseEvent e) 
    	{
    		int iXCor = e.getX()/c.iSpace+1;
    		int iYCor = e.getY()/c.iSpace+1;
    		
    		if (controle(iXCor, iYCor))
    		{
    			c.iABoard[iXCor][iYCor]=iTurn;
    			lStatus.setText("Good!");
    			this.flipCheckers(iXCor, iYCor);
    			this.setTurn();
    			c.repaint();
    		}
    		else
    			lStatus.setText(iXCor+", "+iYCor+": Illegal move");
    	}
    
    	private void setScoreBoard()
    	{
    		lPointsP1.setText("Speler 1: "+ c.getScore(1)+" punten.");
    		lPointsP2.setText("Speler 2: "+ c.getScore(2)+" punten.");
    	}
    	
    	private boolean controle(int iX, int iY)
    	{
    		int iDx, iDy;
    		
    		if(c.iABoard[iX][iY]!= 0)
    			return false;
    		else
    			for(iDx = -1; iDx<2;iDx++)
    				for(iDy = -1; iDy < 2; iDy++)
    					if(controle2(iX, iY, iDx, iDy))
    						return true;
    		return false;
    	}
    	
    	private boolean controle2(int iX, int iY, int iDx, int iDy)
    	{
    		int iAfstand = 2;
    		boolean bLoop = true;
    		
    		if(c.iABoard[iX+iDx][iY+iDy]==3-iTurn)
    		{
    			while(bLoop)
    				if(c.iABoard[iX+iAfstand*iDx][iY+iAfstand*iDy]==3-iTurn)
    					iAfstand++;
    				else bLoop = false;
    		
    			if(c.iABoard[iX+iAfstand*iDx][iY+iAfstand*iDy]==iTurn)
    				return true;
    		}
    		return false;
    	}
    	
    	private void flipCheckers(int iX, int iY)
    	{
    		int iAfstand = 2;
    		boolean bLoop = true;
    		int iDx, iDy;
    		
    		for(iDx = -1; iDx<2;iDx++)
    			for(iDy = -1; iDy < 2; iDy++)
    				if(c.iABoard[iX+iDx][iY+iDy]==3-iTurn)
    				{
    					while(bLoop)
    						if(c.iABoard[iX+iAfstand*iDx][iY+iAfstand*iDy]==3-iTurn)
    							iAfstand++;
    						else bLoop = false;
    				
    				if(c.iABoard[iX+iAfstand*iDx][iY+iAfstand*iDy]==iTurn)
    					for(int a = 1; a<= iAfstand; a++)
    						c.iABoard[iX+iDx*a][iY+iDy*a] = iTurn;
    				}
    	}
    	
    
    		
    
    	
    	@Override
    	public void mouseEntered(MouseEvent arg0) {}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {}
    		
    	@Override
    	public void mousePressed(MouseEvent arg0) {}
    	
    	@Override
    	public void mouseReleased(MouseEvent arg0) {}
    	
    }
    Java Code:
    import java.awt.*;
    
    @SuppressWarnings("serial")
    public class MyCanvas extends Canvas
    {
    	int iWidth, iHeight, iCanvWidth, iCanvHeight, iSpace, iABoard[][];
    	boolean boolHelp = false;
    	
    	public MyCanvas(int iWidthV, int iHeightV, int iWidthC, int iHeightC)
    	{
    		schermGrootte(iWidthV, iHeightV, iWidthC, iHeightC);
    	}
    	
    	// Deze methode bepaald 5 variabelen: iWidth, iHeight, iCanvHeight, ICanvWidth en iSpace
    	// Ook wordt de grootte van het canvas door deze methode aangepast.
    	
    	// This method defines 5 variables: iWidth, iHeight, iCanvHeight, iCanvWidth and iSpace.
    	// The size of the canvas is also adjusted by this method.
    	
    	public void schermGrootte(int iNieuweWidth, int iNieuweHeight, int iMaxWidth, int iMaxHeight)
    	{
    		iWidth = iNieuweWidth; iHeight = iNieuweHeight;
    		
    		int iSpaceX, iSpaceY;
    		
    		iSpaceX = iMaxWidth/iWidth;
    		iSpaceY = iMaxHeight/iHeight;
    		
    		if(iSpaceX <= iSpaceY)
    			iSpace = iSpaceX;
    		else
    			iSpace = iSpaceY;
    		
    		iCanvWidth = iSpace*iWidth;
    		iCanvHeight = iSpace*iHeight;
    		
    		this.setSize(iCanvWidth, iCanvHeight);
    		this.setBackground(new Color(100,100,100));
    		
    		iABoard = new int[iWidth+2][iHeight+2];
    		this.newGame();
    
    	}
    	
    	//This method draws the grid.
    	public void paint(Graphics g)
    	{
    		g.drawRect(1, 1, iCanvWidth-2, iCanvHeight-2);
    		int iSpace = this.getWidth()/(iWidth);
    	
    		int iY = 0;
    		int iX = 0;
    		
    		for(int i=0;i <= iWidth ;i++)
    		{
    			g.drawLine(iX, 0, iX, iCanvHeight );
    			iX += iSpace;
    		}
    		for(int i=0;i <= iHeight; i++)
    		{
    			g.drawLine(0, iY, iCanvWidth, iY );
    			iY += iSpace;
    		}
    		this.tekenSteen(g);
    	}
    	
    	//This method draws the checkers.
    	public void tekenSteen(Graphics g)
    	{
    		for (int iX = 1; iX <= iWidth;iX++)
    			for(int iY = 1; iY <= iHeight; iY++)
    				if(iABoard[iX][iY] == 2)
    				{
    					g.setColor(Color.black );
    					g.fillOval(iSpace*(iX-1)+2, iSpace*(iY-1)+2, iSpace-4, iSpace-4);
    				}
    				else if(iABoard[iX][iY] == 1)
    				{
    					g.setColor(Color.white );
    					g.fillOval(iSpace*(iX-1)+2, iSpace*(iY-1)+2, iSpace-4, iSpace-4);
    	
    				}
    	}
    	
    	//This method clears the array containing checker positions, and places the 4 default checkers.
    	public void newGame()
    	{
    		int iX, iY;
    		for(iX = 0; iX < iWidth; iX++)
    			for(iY = 0; iY < iHeight; iY++)
    				iABoard[iX][iY] = 0;
    		
    		iABoard[iWidth/2][iHeight/2] = 2;
    		iABoard[iWidth/2][iHeight/2+1] = 1;
    		iABoard[iWidth/2+1][iHeight/2] = 1;
    		iABoard[iWidth/2+1][iHeight/2+1] = 2;
    	}
    	
    	//This method gets the score of the specified player
    	public int getScore(int iTurn)
    	{
    		int iScore=0;
    		
    		for (int iX = 0;iX<iWidth;iX++)
    			for(int iY= 0;iY<iWidth;iY++)
    				if (iABoard[iX][iY] == iTurn)
    					iScore++;
    		
    		return iScore;
    	}
    	
    }

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Since you are checking cells along radials around the central/selected point you may have more that one trail to follow. So move the boolean "bLoop" to just above, ie, before entering, the while loop.
    Java Code:
        private void flipCheckers(int iX, int iY)
        {
            int iAfstand = 1;
    //        boolean bLoop = true;
            int iDx, iDy;
    
            for(iDx = -1; iDx < 2; iDx++)
            {
                for(iDy = -1; iDy < 2; iDy++)
                {
                    if(c.iABoard[iX+iDx][iY+iDy]==3-iTurn)
                    {
                        boolean bLoop = true;
                        while(bLoop)
    Seems to be an improvement.
    From your original post the try clicking E4, E3, E2, F3, B2, B1 sequence seems to work okay. The B1 move is illegal, maybe a typo.

    In the play sequence
    Java Code:
        3,2 2,4 4,5 5,4 3,5 4,2 6,5 4,6
    the white checker at 4,3 is not captured.
    I don't know what the rules are about this. Should it be flipped?
    This highlights the difference between the two counting approaches: inside out vs. outside in.

  9. #9
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    In the play sequence
    Java Code:
        3,2 2,4 4,5 5,4 3,5 4,2 6,5 4,6
    the white checker at 4,3 is not captured.
    I don't know what the rules are about this. Should it be flipped?
    This highlights the difference between the two counting approaches: inside out vs. outside in.
    You mean on the last turn? When 5,6 is played? That is correct. Only the checkers enclosed by the played checker count. Also, newly enclosed checkers after flipping the checkers do NOT count. So it works like a charm now (I think). I'll do some more testing to be sure, but it seems to be doing the job. Thanks a lot mate :)

    -edit-

    After testing a bit more I found a LOT of situations that aren't right. I think I should program a history method that saves all the moves in an array, I can't remember how to restage the situations. I'll post a few of them later.

    -edit2-
    The following results in an Array Out of Bound exception:
    Java Code:
    [5,4] [3,5] [2,4] [1,5] [3,6] [6,4] [4,5] [5,6] [4,6] [2,6] [3,2] [2,1] [5,2] [5,3] [2,5] [5,5] [1,4] [4,2] [2,2] [4,1] [1,6] [2,3] [6,5]
    Last edited by chielt; 01-01-2010 at 03:16 PM.

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    program a history method that saves all the moves in an array
    A good idea. You'll need to find a way to monitor/track what your search logic is doing so you can see where the trouble is happening. I used print statements in both controle and controle2 to track the flow.
    -edit2-
    I followed this along on your applet and the game I made up and found
    2,1 — is an illegal move, 1,3 would do okay
    5,3 — cell was occupied
    I stopped there. I see that trouble came in the southeast corner.

    Earlier, while going through your applet, I noticed that you instantiated your iABoard with
    Java Code:
    iABoard = new int[iWidth+2][iHeight+2];
    extra rows and columns. Later I saw that this may have been to allow you to examine the grid around the edge/corner cells with the controle method.
    One of the problems with this approach is that you will have to be careful in how you navigate inside this array during the game. One example of this appeared when the newGame method did not clear all the cells in the southeast corner.

    An alternative way to design this would be to keep the iABoard grid dimensions accurate and do bounds checking in your controle and controle2 methods. Either way you will have be careful in your search patterns.

  11. #11
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Good thing you pointed out the clearing of the array. I found giving the array extra cells the best way to check on a legal move. I actually didn't think of any other possibilities, but can't think of any atm.

    I still don't get why the south-east corner is out of bound in the array tho. I'm all confused by my own programming, and can't figure out how to fix it. I am open for any suggestions.

Similar Threads

  1. Random coin flip application
    By Boomer1 in forum New To Java
    Replies: 8
    Last Post: 12-18-2009, 02:57 AM
  2. need advice for checkers game
    By javanoob73 in forum New To Java
    Replies: 13
    Last Post: 12-02-2009, 01:10 AM
  3. Checkers
    By evan42781 in forum New To Java
    Replies: 8
    Last Post: 05-08-2009, 04:07 AM
  4. Replies: 2
    Last Post: 11-11-2007, 08: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
  •