944,020 Members | Top Members by Rank

Ad:
  • Java Code Snippet
  • Views: 13364
  • Java RSS
0

MineSweeper

by on Aug 24th, 2008
I made this minesweeper game earlier this summer. It shows many things, including:
array use,
1D array to 2D array,
gridlayout,
changing the way a JButton works depending on mouse button used,
floodfill,
loops,
GUI.

slightly long, 382 lines, can probably be condensed.

There is one known bug that i cannot seem to fix: when a floodfill causes a win, and the OK button is clicked on the win dialog, it will automatically click a button on the new game, not a problem usually, but in games that are set to be large with a small amount of bombs, it can cause an infinite loop , if you see a fix please comment or PM me, thanks.

have fun.
Java Code Snippet (Toggle Plain Text)
  1. import java.awt.* ;
  2. import java.awt.event.* ;
  3. import java.awt.geom.* ;
  4. import javax.swing.* ;
  5. import javax.swing.event.* ;
  6. /**MineSweeper by SciWizEH*/
  7. public class MineSweeper extends JFrame implements ActionListener ,
  8. MouseListener {
  9. int rows = 10 ;
  10. int cols = 10 ;
  11. int numMines = 10 ;
  12. GridLayout layout = new GridLayout ( rows , cols ) ;
  13. /*type[][] name = new type[rows][cols];
  14. * type[x][y];
  15. * is 1d
  16. * type[] name = new type[rows*cols];
  17. * type[(rows*y)+x];*/
  18. boolean [ ] mines = new boolean [ rows * cols ] ;
  19. boolean [ ] clickable = new boolean [ rows * cols ] ;
  20. boolean lost = false ;
  21. boolean won = false ;
  22. int [ ] numbers = new int [ rows * cols ] ;
  23. JButton [ ] buttons = new JButton [ rows * cols ] ;
  24. boolean [ ] clickdone = new boolean [ rows * cols ] ;
  25. JMenuItem newGameButton = new JMenuItem ( "new game" ) ;
  26. JMenuItem difficulty = new JMenuItem ( "options" ) ;
  27. JLabel mineLabel = new JLabel ( "mines: " + numMines + " marked: 0" ) ;
  28. JPanel p = new JPanel ( ) ;
  29.  
  30. public MineSweeper ( ) {
  31. p.setLayout ( layout ) ;
  32. setupI ( ) ;
  33. for ( int i = 0 ; i < ( rows * cols ) ; i ++ ) {
  34. p.add ( buttons [ i ] ) ;
  35. }
  36. JMenuBar mb = new JMenuBar ( ) ;
  37. JMenu m = new JMenu ( "file" ) ;
  38. newGameButton.addActionListener ( this ) ;
  39. m.add ( newGameButton ) ;
  40. difficulty.addActionListener ( this ) ;
  41. m.add ( difficulty ) ;
  42. mb.add ( m ) ;
  43. this.setJMenuBar ( mb ) ;
  44. this.add ( p ) ;
  45. this.add ( mineLabel , BorderLayout.SOUTH ) ;
  46. this.pack ( ) ;
  47. this.setVisible ( true ) ;
  48. }
  49.  
  50. public void fillMines ( ) {
  51. int needed = numMines ;
  52. while ( needed > 0 ) {
  53. int x = ( int ) Math.floor ( Math.random ( ) * rows ) ;
  54. int y = ( int ) Math.floor ( Math.random ( ) * cols ) ;
  55. if ( ! mines [ ( rows * y ) + x ] ) {
  56. mines [ ( rows * y ) + x ] = true ;
  57. needed -- ;
  58. }
  59. }
  60. }
  61.  
  62. public void fillNumbers ( ) {
  63. for ( int x = 0 ; x < rows ; x ++ ) {
  64. for ( int y = 0 ; y < cols ; y ++ ) {
  65. int cur = ( rows * y ) + x ;
  66. if ( mines [ cur ] ) {
  67. numbers [ cur ] = 0 ;
  68. continue ;
  69. }
  70. int temp = 0 ;
  71. boolean l = ( x - 1 ) >= 0 ;
  72. boolean r = ( x + 1 ) < rows ;
  73. boolean u = ( y - 1 ) >= 0 ;
  74. boolean d = ( y + 1 ) < cols ;
  75. int left = ( rows * ( y ) ) + ( x - 1 ) ;
  76. int right = ( rows * ( y ) ) + ( x + 1 ) ;
  77. int up = ( rows * ( y - 1 ) ) + ( x ) ;
  78. int upleft = ( rows * ( y - 1 ) ) + ( x - 1 ) ;
  79. int upright = ( rows * ( y - 1 ) ) + ( x + 1 ) ;
  80. int down = ( rows * ( y + 1 ) ) + ( x ) ;
  81. int downleft = ( rows * ( y + 1 ) ) + ( x - 1 ) ;
  82. int downright = ( rows * ( y + 1 ) ) + ( x + 1 ) ;
  83. if ( u ) {
  84. if ( mines [ up ] ) {
  85. temp ++ ;
  86. }
  87. if ( l ) {
  88. if ( mines [ upleft ] ) {
  89. temp ++ ;
  90. }
  91. }
  92. if ( r ) {
  93. if ( mines [ upright ] ) {
  94. temp ++ ;
  95. }
  96. }
  97. }
  98. if ( d ) {
  99. if ( mines [ down ] ) {
  100. temp ++ ;
  101. }
  102. if ( l ) {
  103. if ( mines [ downleft ] ) {
  104. temp ++ ;
  105. }
  106. }
  107. if ( r ) {
  108. if ( mines [ downright ] ) {
  109. temp ++ ;
  110. }
  111. }
  112. }
  113. if ( l ) {
  114. if ( mines [ left ] ) {
  115. temp ++ ;
  116. }
  117. }
  118. if ( r ) {
  119. if ( mines [ right ] ) {
  120. temp ++ ;
  121. }
  122. }
  123. numbers [ cur ] = temp ;
  124. }
  125. }
  126. }
  127.  
  128. public void setupI ( ) {
  129. for ( int x = 0 ; x < rows ; x ++ ) {
  130. for ( int y = 0 ; y < cols ; y ++ ) {
  131. mines [ ( rows * y ) + x ] = false ;
  132. clickdone [ ( rows * y ) + x ] = false ;
  133. clickable [ ( rows * y ) + x ] = true ;
  134. buttons [ ( rows * y ) + x ] = new JButton ( /*"" + ( x * y )*/) ;
  135. buttons [ ( rows * y ) + x ].setPreferredSize ( new Dimension (
  136. 45 , 45 ) ) ;
  137. buttons [ ( rows * y ) + x ].addActionListener ( this ) ;
  138. buttons [ ( rows * y ) + x ].addMouseListener ( this ) ;
  139. }
  140. }
  141. fillMines ( ) ;
  142. fillNumbers ( ) ;
  143. }
  144.  
  145. public void setupI2 ( ) {
  146. this.remove ( p ) ;
  147. p = new JPanel ( ) ;
  148. layout = new GridLayout ( rows , cols ) ;
  149. p.setLayout ( layout ) ;
  150. buttons = new JButton [ rows * cols ] ;
  151. mines = new boolean [ rows * cols ] ;
  152. clickdone = new boolean [ rows * cols ] ;
  153. clickable = new boolean [ rows * cols ] ;
  154. numbers = new int [ rows * cols ] ;
  155. setupI ( ) ;
  156. for ( int i = 0 ; i < ( rows * cols ) ; i ++ ) {
  157. p.add ( buttons [ i ] ) ;
  158. }
  159. this.add ( p ) ;
  160. this.pack ( ) ;
  161. fillMines ( ) ;
  162. fillNumbers ( ) ;
  163. }
  164.  
  165. public void setup ( ) {
  166. for ( int x = 0 ; x < rows ; x ++ ) {
  167. for ( int y = 0 ; y < cols ; y ++ ) {
  168. mines [ ( rows * y ) + x ] = false ;
  169. clickdone [ ( rows * y ) + x ] = false ;
  170. clickable [ ( rows * y ) + x ] = true ;
  171. buttons [ ( rows * y ) + x ].setEnabled ( true ) ;
  172. buttons [ ( rows * y ) + x ].setText ( "" ) ;
  173. }
  174. }
  175. fillMines ( ) ;
  176. fillNumbers ( ) ;
  177. lost = false ;
  178. mineLabel.setText ( "mines: " + numMines + " marked: 0" ) ;
  179. }
  180.  
  181. public static void main ( String [ ] args ) {
  182. new MineSweeper ( ) ;
  183. }
  184.  
  185. public void actionPerformed ( ActionEvent e ) {
  186. if ( e.getSource ( ) == difficulty ) {
  187. rows = Integer.parseInt ( ( String ) JOptionPane.showInputDialog (
  188. this , "Rows" , "Rows" , JOptionPane.PLAIN_MESSAGE , null ,
  189. null , 10 ) ) ;
  190. cols = Integer.parseInt ( ( String ) JOptionPane.showInputDialog (
  191. this , "Columns" , "Columns" , JOptionPane.PLAIN_MESSAGE ,
  192. null , null , 10 ) ) ;
  193. numMines = Integer.parseInt ( ( String ) JOptionPane
  194. .showInputDialog ( this , "Mines" , "Mines" ,
  195. JOptionPane.PLAIN_MESSAGE , null , null , 10 ) ) ;
  196. setupI2 ( ) ;
  197. }
  198. if ( ! won ) {
  199. for ( int x = 0 ; x < rows ; x ++ ) {
  200. for ( int y = 0 ; y < cols ; y ++ ) {
  201. if ( e.getSource ( ) == buttons [ ( rows * y ) + x ]
  202. && ! won && clickable [ ( rows * y ) + x ] ) {
  203. doCheck ( x , y ) ;
  204. break ;
  205. }
  206. }
  207. }
  208. }
  209. if ( e.getSource ( ) == newGameButton ) {
  210. setup ( ) ;
  211. won = false ;
  212. return ;
  213.  
  214. }
  215. checkWin ( ) ;
  216. }
  217.  
  218. public void mouseEntered ( MouseEvent e ) {
  219.  
  220. }
  221.  
  222. public void mouseExited ( MouseEvent e ) {
  223.  
  224. }
  225.  
  226. public void mousePressed ( MouseEvent e ) {
  227. if ( e.getButton ( ) == 3 ) {
  228. int n = 0 ;
  229. for ( int x = 0 ; x < rows ; x ++ ) {
  230. for ( int y = 0 ; y < cols ; y ++ ) {
  231. if ( e.getSource ( ) == buttons [ ( rows * y ) + x ] ) {
  232. clickable [ ( rows * y ) + x ] = ! clickable [ ( rows * y )
  233. + x ] ;
  234. }
  235. if ( ! clickdone [ ( rows * y ) + x ] ) {
  236. if ( ! clickable [ ( rows * y ) + x ] ) {
  237. buttons [ ( rows * y ) + x ].setText ( "X" ) ;
  238. n ++ ;
  239. } else {
  240. buttons [ ( rows * y ) + x ].setText ( "" ) ;
  241. }
  242. mineLabel.setText ( "mines: " + numMines + " marked: "
  243. + n ) ;
  244. }
  245. }
  246. }
  247. }
  248. }
  249.  
  250. public void mouseReleased ( MouseEvent e ) {
  251.  
  252. }
  253.  
  254. public void mouseClicked ( MouseEvent e ) {
  255.  
  256. }
  257.  
  258. public void doCheck ( int x , int y ) {
  259. int cur = ( rows * y ) + x ;
  260. boolean l = ( x - 1 ) >= 0 ;
  261. boolean r = ( x + 1 ) < rows ;
  262. boolean u = ( y - 1 ) >= 0 ;
  263. boolean d = ( y + 1 ) < cols ;
  264. int left = ( rows * ( y ) ) + ( x - 1 ) ;
  265. int right = ( rows * ( y ) ) + ( x + 1 ) ;
  266. int up = ( rows * ( y - 1 ) ) + ( x ) ;
  267. int upleft = ( rows * ( y - 1 ) ) + ( x - 1 ) ;
  268. int upright = ( rows * ( y - 1 ) ) + ( x + 1 ) ;
  269. int down = ( rows * ( y + 1 ) ) + ( x ) ;
  270. int downleft = ( rows * ( y + 1 ) ) + ( x - 1 ) ;
  271. int downright = ( rows * ( y + 1 ) ) + ( x + 1 ) ;
  272.  
  273. clickdone [ cur ] = true ;
  274. buttons [ cur ].setEnabled ( false ) ;
  275. if ( numbers [ cur ] == 0 && ! mines [ cur ] && ! lost && ! won ) {
  276. if ( u && ! won ) {
  277. if ( ! clickdone [ up ] && ! mines [ up ] ) {
  278. clickdone [ up ] = true ;
  279. buttons [ up ].doClick ( ) ;
  280. }
  281. if ( l && ! won ) {
  282. if ( ! clickdone [ upleft ] && numbers [ upleft ] != 0
  283. && ! mines [ upleft ] ) {
  284. clickdone [ upleft ] = true ;
  285. buttons [ upleft ].doClick ( ) ;
  286. }
  287. }
  288. if ( r && ! won ) {
  289. if ( ! clickdone [ upright ] && numbers [ upright ] != 0
  290. && ! mines [ upright ] ) {
  291. clickdone [ upright ] = true ;
  292. buttons [ upright ].doClick ( ) ;
  293. }
  294. }
  295. }
  296. if ( d && ! won ) {
  297. if ( ! clickdone [ down ] && ! mines [ down ] ) {
  298. clickdone [ down ] = true ;
  299. buttons [ down ].doClick ( ) ;
  300. }
  301. if ( l && ! won ) {
  302. if ( ! clickdone [ downleft ] && numbers [ downleft ] != 0
  303. && ! mines [ downleft ] ) {
  304. clickdone [ downleft ] = true ;
  305. buttons [ downleft ].doClick ( ) ;
  306. }
  307. }
  308. if ( r && ! won ) {
  309. if ( ! clickdone [ downright ]
  310. && numbers [ downright ] != 0
  311. && ! mines [ downright ] ) {
  312. clickdone [ downright ] = true ;
  313. buttons [ downright ].doClick ( ) ;
  314. }
  315. }
  316. }
  317. if ( l && ! won ) {
  318. if ( ! clickdone [ left ] && ! mines [ left ] ) {
  319. clickdone [ left ] = true ;
  320. buttons [ left ].doClick ( ) ;
  321. }
  322. }
  323. if ( r && ! won ) {
  324. if ( ! clickdone [ right ] && ! mines [ right ] ) {
  325. clickdone [ right ] = true ;
  326. buttons [ right ].doClick ( ) ;
  327. }
  328. }
  329. } else {
  330. buttons [ cur ].setText ( "" + numbers [ cur ] ) ;
  331. if ( ! mines [ cur ] && numbers [ cur ] == 0 ) {
  332. buttons [ cur ].setText ( "" ) ;
  333. }
  334. }
  335. if ( mines [ cur ] && ! won ) {
  336. buttons [ cur ].setText ( "0" ) ;
  337. doLose ( ) ;
  338. }
  339. }
  340.  
  341. public void checkWin ( ) {
  342. for ( int x = 0 ; x < rows ; x ++ ) {
  343. for ( int y = 0 ; y < cols ; y ++ ) {
  344. int cur = ( rows * y ) + x ;
  345. if ( ! clickdone [ cur ] ) {
  346. if ( mines [ cur ] ) {
  347. continue ;
  348. } else {
  349. return ;
  350. }
  351. }
  352. }
  353. }
  354.  
  355. doWin ( ) ;
  356. }
  357.  
  358. public void doWin ( ) {
  359. if ( ! lost && ! won ) {
  360. won = true ;
  361. JOptionPane.showMessageDialog ( null ,
  362. "you win!\nstarting a new game" , "you lose" ,
  363. JOptionPane.INFORMATION_MESSAGE ) ;
  364. newGameButton.doClick ( ) ;
  365. }
  366. }
  367.  
  368. public void doLose ( ) {
  369. if ( ! lost && ! won ) {
  370. lost = true ;
  371. for ( int i = 0 ; i < rows * cols ; i ++ ) {
  372. if ( ! clickdone [ i ] ) {
  373. buttons [ i ].doClick ( 0 ) ;
  374. }
  375. }
  376. JOptionPane.showMessageDialog ( null ,
  377. "you lose!\nstarting a new game" , "you lose" ,
  378. JOptionPane.ERROR_MESSAGE ) ;
  379. setup ( ) ;
  380. }
  381. }
  382. }
Comments on this Code Snippet
Nov 3rd, 2008
-1

Re: MineSweeper

Whoo O_O! Pretty fun XD

I can't seem to win! Right when I think I will, I end up landing on a mine XD

=)
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Mar 16th, 2010
-3

Re: MineSweeper

is this code running? I can't find a right code for ordinary minesweeper code in Joption!!! plz help me!!! this is my e-mail <EMAIL SNIPPED>, pls email me!!!
Last edited by peter_budo; Mar 16th, 2010 at 1:01 pm. Reason: Keep It On The Site - Do not post asking for an answer to be sent to you via email or PM.
Newbie Poster
crow91 is offline Offline
1 posts
since Mar 2010
Oct 6th, 2010
0

Re: MineSweeper

how can I fix the errors of your minesweeper in the Jcreator 4.50 ??
Newbie Poster
EhLy is offline Offline
1 posts
since Oct 2010
Oct 6th, 2010
0

Re: MineSweeper

Click to Expand / Collapse  Quote originally posted by EhLy ...
how can I fix the errors of your minesweeper in the Jcreator 4.50 ??
I never used JCreater, I wrote this without code completion in BlueJ, it should compile. what errors are you getting?
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Oct 7th, 2010
0

Re: MineSweeper

There is no error on compile, it is running, so far didn't seen bug reported by you
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Message:
Previous Thread in Java Forum Timeline: Hashmap, ArrayList and Comparator
Next Thread in Java Forum Timeline: A software for finance management in a bank





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC