Hi, supposed to implement a game where the queen(counter) is supposed to travel over all 64 squares in 16 moves and it must highlight or mark squares allready passed over. Been at this for last two nights and begining to lose patience any help would be great, thanks. Code is below:

// Description:    
//
// Directory: 
//
// Date: 11/04/2006
// Uses:   
//
//
// Comments:

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

public class queenMoves extends Applet implements ActionListener{
private int top=0, left1=0, BWidth=400, Width,
Height,N=8;  



    private Queen myQueen;
    private Button left, right,up, down, diagonaldl,diagonaldr,diagonalur,diagonalul;

    private int countLeft = 0;
    private int countRight = 0;
    private int countUp = 0;
    private int countDown = 0;
    private int countDiagonaldl = 0;
    private int countDiagonaldr = 0;
    private int countDiagonalur = 0;
    private int countDiagonalul = 0;
    private int countTotal = 0;
   // private Font font;
    private Image stopFlick;
    private Graphics flick;

   public void init() 
      {


       setBackground(Color.white);
       setLayout(new FlowLayout (FlowLayout.RIGHT,10,10));

      left =  new Button("Left"); add(left);
      left.addActionListener(this);
      right=  new Button("Right"); add(right);
      right.addActionListener(this);
      up = new Button("Up"); add(up);
      up.addActionListener(this);
      down = new Button("Down"); add(down);
      down.addActionListener(this);

      diagonaldl = new Button("Diag Dwn Left"); add(diagonaldl);
      diagonaldl.addActionListener(this);
      diagonaldr = new Button("Diag Dwn Right"); add(diagonaldr);
      diagonaldr.addActionListener(this);
      diagonalur = new Button("Diag Up Right"); add(diagonalur);
      diagonalur.addActionListener(this);
      diagonalul = new Button("Diag Up Left"); add(diagonalul);
      diagonalul.addActionListener(this);

      add (left); left.addActionListener(this);
      add (right); right.addActionListener(this);
      add (up); up.addActionListener(this);
      add (down); down.addActionListener(this);
      add (diagonaldl); diagonaldl.addActionListener(this);
      add (diagonaldr); diagonaldr.addActionListener(this);
      add (diagonalul); diagonalul.addActionListener(this);
      add (diagonalur); diagonalur.addActionListener(this);

      myQueen = new Queen ();
      }

   private void Draw_Board(Graphics g) {
        int SqWidth = BWidth/N;   // Rounds down

        for(int Row=0; Row<8; Row++)
          for(int Col=0; Col<8; Col++) {
        g.setColor(Pos_Color(Row, Col));
        g.fillRect(left1+Col*SqWidth, top+Row*SqWidth,
               SqWidth, SqWidth);
          }

}
   private boolean Is_Even(int X) {
        return(X == 2*(X/2));
      }
   private Color Pos_Color(int Row, int Col) {
        if ( (Is_Even(Row) && Is_Even(Col)) ||
         (!Is_Even(Row) && !Is_Even(Col)))
          return(Color.GRAY);
        else
          return(Color.lightGray);

}

   public void update (Graphics g) {


        if (stopFlick==null) {
            stopFlick = createImage (this.getSize().width,
            this.getSize().height);
            flick=stopFlick.getGraphics();

        }

        flick.setColor (getBackground());
        flick.fillRect (0, 0, this.getSize().width,
        this.getSize().height);

        flick.setColor (getForeground());
        paint (flick);

        //draw image on the screen
        g.drawImage (stopFlick, 0, 0, this);
    } 


   public void paint(Graphics g) 
      {

       Draw_Board(g);
       myQueen .display(g);


       g.drawString("Number of moves is: " + countTotal,100,500);
       if (countTotal > 16)
          {
              g.drawString("You have made too many moves, Maximum moves is " + 16, 100,600); 

          }



      }

    public void actionPerformed(ActionEvent e)
      {

      if (e.getSource() ==left )
         {
          myQueen .left();


         }
      if (e.getSource() == right)
         {
         myQueen .right();
         }
      if (e.getSource()== up)
      {
          myQueen.up();
      }
      if (e.getSource() == down)
      {
          myQueen.down();
      }
      if (e.getSource() == diagonaldl)
      {
          myQueen.diagonaldl();
      }


      if (e.getSource() == diagonaldr)
      {
          myQueen.diagonaldr();
      }
      if (e.getSource() == diagonalur)
      {
          myQueen.diagonalur();
      }
      if (e.getSource() == diagonalul)
      {
          myQueen.diagonalul();
      }


      if (e.getSource()==up)countUp++;
      if (e.getSource()==down)countDown++;
      if (e.getSource()==left)countLeft++;
      if (e.getSource()==right)countRight++;
      if (e.getSource()==diagonaldl)countDiagonaldl++;
      if (e.getSource()==diagonaldr)countDiagonaldr++;
      if (e.getSource()==diagonalur)countDiagonalur++;
      if (e.getSource()==diagonalul)countDiagonalul++;

      countTotal = countUp / 2 + countDown / 2 + countLeft /2 + countRight /2 + 
      countDiagonaldl /2 + countDiagonaldr /2 + countDiagonalur /2 + countDiagonalul / 2;
      if (e.getSource()==up)
      {
        ;
      }
      repaint();
        }
   }

   class Queen 
   {

   private int diameter = 20;
   private int xCoord = 165;
   private int yCoord = 165;


   public void display(Graphics g)
      {
       g.setColor(Color.black);   
       g.fillOval(xCoord,yCoord,diameter,diameter);

      }

   public void left()
      {
      xCoord = xCoord - 25;

    // int select = 1;

      }



public void right()
      {
      xCoord = xCoord + 25;   
      }
   public void up()
      {
          yCoord = yCoord - 25;
      }
   public void down()
      {
          yCoord = yCoord + 25;

      }
   public void diagonaldl()
      {
          yCoord = yCoord + 25;
          xCoord = xCoord - 25;
      }
   public void diagonaldr()
      {
         xCoord = xCoord + 25;
         yCoord = yCoord + 25;
      }
   public void diagonalul()
      {
          yCoord = yCoord - 25;
          xCoord = xCoord - 25;
     }
   public void diagonalur()
      {
          yCoord = yCoord - 25;
          xCoord = xCoord + 25;
      }

}

Recommended Answers

All 4 Replies

Instead of filling the rectangle just draw it in a different color....

highlighting the square

A simple 2D array can make this easy.

boolean[][] grid = new boolean[8][8];

When the queen comes a square, update the array.

grid[x][y] = true;

Then when you draw your squares:

for(int Row=0; Row<8; Row++)
	for(int Col=0; Col<8; Col++) 
	{
		 if (grid[col][row])
			 //g.setColor(highlight);
		 else
			 g.setColor(Pos_Color(Row, Col));

		 g.fillRect(left1+Col*SqWidth, top+Row*SqWidth,
		 SqWidth, SqWidth);
}

Of course, I'm assuming you already know how to track the queen's movement and just want a simple way to keep track what to highlight.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.