Dot and Lines Game

Reply

Join Date: Oct 2007
Posts: 1
Reputation: wizzywas is an unknown quantity at this point 
Solved Threads: 0
wizzywas wizzywas is offline Offline
Newbie Poster

Dot and Lines Game

 
0
  #1
Feb 26th, 2009
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:
http://i2.photobucket.com/albums/y6/...4/DotsGame.jpg

Here is the code i Have now:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.Rectangle.*;
  4.  
  5. public class DotsGame
  6. {
  7. public static void main( String[ ] args)
  8. {
  9. new Dots4Win();
  10. }
  11. }
  12.  
  13. class Dots4Win extends Frame
  14. {
  15. private int counter = 0 ;
  16.  
  17. private Rectangle[][] dots = new Rectangle[7][7] ;
  18.  
  19. /* 0 denotes black; 1 denotes red; 2 denotes blue */
  20. private int[][] status = new int[7][7] ;
  21.  
  22. Dots4Win()
  23. {
  24. setTitle( "Dots Game" );
  25. setLocation( new Point( 200, 100 ) ) ;
  26. setSize( 500, 500 );
  27. setResizable( true ) ; // this prevents re-sizing of window
  28.  
  29. for ( int i = 0; i < 7; i++ )
  30. {
  31. for ( int j = 0; j < 7; j++ )
  32. {
  33. dots[i][j] = new Rectangle( 40 + 70*i, 70 + 40*j, 11, 11 ) ;
  34.  
  35. }
  36. }
  37.  
  38. for ( int i = 0; i < 7; i++ )
  39. {
  40. for ( int j = 0; j < 7; j++ )
  41. {
  42. status[i][j] = 0 ;
  43. }
  44. }
  45.  
  46. addMouseListener( new MouseCatcher() ) ; // register a mouse listener
  47. addWindowListener( new WindowCatcher( ) ); // register a window listener
  48.  
  49. setVisible( true );
  50. }
  51.  
  52. public void paint( Graphics gc )
  53. {
  54. for ( int i = 0; i < 7; i++ )
  55. {
  56. for ( int j = 0; j < 7; j++ )
  57. {
  58. if ( status[i][j] == 0 )
  59. {
  60. gc.setColor( Color.black ) ;
  61. }
  62. else if ( status[i][j] == 1 )
  63. {
  64. gc.setColor(Color.red ) ;
  65. }
  66. else
  67. {
  68. gc.setColor( Color.yellow ) ;
  69. }
  70. gc.fillOval( dots[i][j].x, dots[i][j].y, dots[i][j].width, dots[i][j].height ) ;
  71.  
  72. }
  73. }
  74. }
  75.  
  76. class MouseCatcher extends MouseAdapter
  77. {
  78. public void mousePressed(MouseEvent evt )
  79. {
  80. Point ptMouse = evt.getPoint() ;
  81.  
  82. for ( int i = 0; i < 7; i++ )
  83. {
  84. for ( int j = 0; j < 7; j++ )
  85. {
  86. if ( dots[i][j].contains( ptMouse ) && (status[i][j] == 0) ) // check you're on a black dot
  87. {
  88. if ( counter % 2 == 0 )
  89. {
  90. status[i][j] = 1 ;
  91. }
  92. else
  93. {
  94. status[i][j] = 2 ;
  95. }
  96. counter++ ;
  97. repaint();
  98. }
  99. }
  100. }
  101. }
  102. }
  103.  
  104. class WindowCatcher extends WindowAdapter
  105. {
  106. public void windowClosing( WindowEvent evt )
  107. {
  108. evt.getWindow( ).dispose( ); // releases this window's resources
  109. System.exit( 0 );
  110. }
  111. }
  112. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Dot and Lines Game

 
0
  #2
Feb 28th, 2009
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.
  1. void paintVertically(Graphics g) {
  2. int i0 = 0;
  3. int j0 = 0;
  4. for (int i = 0; i < 7; i++) {
  5. for (int j = 0; j < 7; j++) {
  6. if (status[i][j] == status[i0][j0]) { //the same status
  7. if (status[i][j] == 1) {
  8. g.setColor(Color.red);
  9. } else {
  10. g.setColor(Color.yellow);
  11. }
  12. if (status[i][j] != 0) {
  13. g.drawLine(dots[i0][j0].x, dots[i0][j0].y, dots[i][j].x, dots[i][j].y);
  14. }
  15. }
  16. //i0 = i;
  17. j0 = j;
  18. }
  19. i0 = i;
  20. }
  21. }
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 ).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC