Hi All,
I need a bit of help with a java Dots and Lines game i am developing, Similar to the Dots and Box game. I have laid out the Dots with the two colours blue and yellow which change accordingly.

I now need to draw a horizontal or a vertical line when 2 Dots are the same colour, Can anyone help me out with this.

I'm doing something similar to this:
[IMG]http://i2.photobucket.com/albums/y6/wasim1924/DotsGame.jpg[/IMG]

Here is the code i Have now:

import java.awt.*;
import java.awt.event.*;
import java.awt.Rectangle.*;

public class DotsGame
{
   public static void main( String[ ] args)
   {
      new Dots4Win();
   }
}

class Dots4Win extends Frame
{
   private int counter = 0 ;

   private Rectangle[][] dots = new Rectangle[7][7] ;

   /* 0 denotes black; 1 denotes red;  2 denotes blue */
   private int[][] status = new int[7][7] ;

   Dots4Win()
   {
      setTitle( "Dots Game" );
      setLocation( new Point( 200, 100 ) ) ;
      setSize( 500, 500 );
      setResizable( true ) ;  // this prevents re-sizing of window

      for ( int i = 0; i < 7; i++ )
      {
         for ( int j = 0; j < 7; j++ )
         {
            dots[i][j] = new Rectangle( 40 + 70*i, 70 + 40*j, 11, 11 ) ;

         }
      }

      for ( int i = 0; i < 7; i++ )
      {
         for ( int j = 0; j < 7; j++ )
         {
            status[i][j] = 0 ;
         }
      }

      addMouseListener( new MouseCatcher() ) ;        // register a mouse listener
      addWindowListener( new WindowCatcher( ) );      // register a window listener

      setVisible( true );
   }

   public void paint( Graphics gc )
   {
      for ( int i = 0; i < 7; i++ )
      {
         for ( int j = 0; j < 7; j++ )
         {
            if ( status[i][j] == 0 )
            {
               gc.setColor( Color.black ) ;
            }
            else if ( status[i][j] == 1 )
            {
               gc.setColor(Color.red ) ;
            }
            else
            {
               gc.setColor( Color.yellow ) ;
            }
            gc.fillOval( dots[i][j].x, dots[i][j].y, dots[i][j].width, dots[i][j].height ) ;

         }
      }
   }

   class MouseCatcher extends MouseAdapter
   {
      public void mousePressed(MouseEvent evt )
      {
         Point ptMouse = evt.getPoint() ;

         for ( int i = 0; i < 7; i++ )
         {
            for ( int j = 0; j < 7; j++ )
            {
               if ( dots[i][j].contains( ptMouse ) && (status[i][j] == 0) )   // check you're on a black dot
               {
                  if ( counter % 2 == 0 )
                  {
                     status[i][j] = 1 ;
                  }
                  else
                  {
                     status[i][j] = 2 ;
                  }
                  counter++ ;
                  repaint();
               }
            }
         }
      }
   }

   class WindowCatcher extends WindowAdapter
   {
      public void windowClosing( WindowEvent evt )
      {
         evt.getWindow( ).dispose( );                // releases this window's resources
         System.exit( 0 );
      }
   }
}

To draw line you need two points. Inside double loop you have access to one of them, current P(i,j). You need additional varibles to remember in the current loops these values, so you can use them on the next loop.

void paintVertically(Graphics g) {
        int i0 = 0;
        int j0 = 0;
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {
                if (status[i][j] == status[i0][j0]) { //the same status
                    if (status[i][j] == 1) {
                        g.setColor(Color.red);
                    } else {
                        g.setColor(Color.yellow);
                    }
                    if (status[i][j] != 0) {
                        g.drawLine(dots[i0][j0].x, dots[i0][j0].y, dots[i][j].x, dots[i][j].y);
                    }
                }
                //i0 = i;
                j0 = j;
            }
            i0 = i;
        }
    }

Invoke this inside paint function.
Make same with horizontal lines ( swap places of loops i-j --> j-i ).
Apply additional conditions ( do not draw line for this same point,do not draw diagonals ).

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.