Advertisement
Guest User

Untitled

a guest
Apr 11th, 2012
7,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5. public class Mine extends JFrame implements ActionListener, MouseListener{
  6. int nomines = 80;
  7. int perm[][];
  8. String tmp;
  9. boolean found = false;
  10. int row;
  11. int column;
  12. int guesses[][];
  13. JButton b[][];
  14. int[][] mines;
  15. boolean allmines;
  16. int n = 30;
  17. int m = 30;
  18. int deltax[] = {-1, 0, 1, -1, 1, -1, 0, 1};
  19. int deltay[] = {-1, -1, -1, 0, 0, 1, 1, 1};
  20. double starttime;
  21. double endtime;
  22. public Mine(){
  23. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  24. perm = new int[n][m];
  25. boolean allmines = false;
  26. guesses = new int [n+2][m+2];
  27. mines = new int[n+2][m+2];
  28. b = new JButton [n][m];
  29. setLayout(new GridLayout(n,m));
  30. for (int y = 0;y<m+2;y++){
  31. mines[0][y] = 3;
  32. mines[n+1][y] = 3;
  33. guesses[0][y] = 3;
  34. guesses[n+1][y] = 3;
  35. }
  36. for (int x = 0;x<n+2;x++){
  37. mines[x][0] = 3;
  38. mines[x][m+1] = 3;
  39. guesses[x][0] = 3;
  40. guesses[x][m+1] = 3;
  41. }
  42. do {
  43. int check = 0;
  44. for (int y = 1;y<m+1;y++){
  45. for (int x = 1;x<n+1;x++){
  46. mines[x][y] = 0;
  47. guesses[x][y] = 0;
  48. }
  49. }
  50. for (int x = 0;x<nomines;x++){
  51. mines [(int) (Math.random()*(n)+1)][(int) (Math.random()*(m)+1)] = 1;
  52. }
  53. for (int x = 0;x<n;x++){
  54. for (int y = 0;y<m;y++){
  55. if (mines[x+1][y+1] == 1){
  56. check++;
  57. }
  58. }}
  59. if (check == nomines){
  60. allmines = true;
  61. }}while (allmines == false);
  62. for (int y = 0;y<m;y++){
  63. for (int x = 0;x<n;x++){
  64. if ((mines[x+1][y+1] == 0) || (mines[x+1][y+1] == 1)){
  65. perm[x][y] = perimcheck(x,y);
  66. }
  67. b[x][y] = new JButton("?");
  68. b[x][y].addActionListener(this);
  69. b[x][y].addMouseListener(this);
  70. add(b[x][y]);
  71. b[x][y].setEnabled(true);
  72. }//end inner for
  73. }//end for
  74. pack();
  75. setVisible(true);
  76. for (int y = 0;y<m+2;y++){
  77. for (int x = 0;x<n+2;x++){
  78. System.out.print(mines[x][y]);
  79. }
  80. System.out.println("");}
  81. starttime = System.nanoTime();
  82. }//end constructor Mine()
  83.  
  84. public void actionPerformed(ActionEvent e){
  85. found = false;
  86. JButton current = (JButton)e.getSource();
  87. for (int y = 0;y<m;y++){
  88. for (int x = 0;x<n;x++){
  89. JButton t = b[x][y];
  90. if(t == current){
  91. row=x;column=y; found =true;
  92. }
  93. }//end inner for
  94. }//end for
  95. if(!found) {
  96. System.out.println("didn't find the button, there was an error "); System.exit(-1);
  97. }
  98. Component temporaryLostComponent = null;
  99. if (b[row][column].getBackground() == Color.orange){
  100. return;
  101. }else if (mines[row+1][column+1] == 1){
  102. JOptionPane.showMessageDialog(temporaryLostComponent, "You set off a Mine!!!!.");
  103. System.exit(0);
  104. } else {
  105. tmp = Integer.toString(perm[row][column]);
  106. if (perm[row][column] == 0){
  107. tmp = " ";
  108. }
  109. b[row][column].setText(tmp);
  110. b[row][column].setEnabled(false);
  111. checkifend();
  112. if (perm[row][column] == 0){
  113. scan(row, column);
  114. checkifend();
  115. }}}
  116.  
  117. public void checkifend(){
  118. int check= 0;
  119. for (int y = 0; y<m;y++){
  120. for (int x = 0;x<n;x++){
  121. if (b[x][y].isEnabled()){
  122. check++;
  123. }
  124. }}
  125. if (check == nomines){
  126. endtime = System.nanoTime();
  127. Component temporaryLostComponent = null;
  128. JOptionPane.showMessageDialog(temporaryLostComponent, "Congratulations you won!!! It took you "+(int)((endtime-starttime)/1000000000)+" seconds!");
  129. }
  130. }
  131.  
  132. public void scan(int x, int y){
  133. for (int a = 0;a<8;a++){
  134. if (mines[x+1+deltax[a]][y+1+deltay[a]] == 3){
  135.  
  136. } else if ((perm[x+deltax[a]][y+deltay[a]] == 0) && (mines[x+1+deltax[a]][y+1+deltay[a]] == 0) && (guesses[x+deltax[a]+1][y+deltay[a]+1] == 0)){
  137. if (b[x+deltax[a]][y+deltay[a]].isEnabled()){
  138. b[x+deltax[a]][y+deltay[a]].setText(" ");
  139. b[x+deltax[a]][y+deltay[a]].setEnabled(false);
  140. scan(x+deltax[a], y+deltay[a]);
  141. }
  142. } else if ((perm[x+deltax[a]][y+deltay[a]] != 0) && (mines[x+1+deltax[a]][y+1+deltay[a]] == 0) && (guesses[x+deltax[a]+1][y+deltay[a]+1] == 0)){
  143. tmp = new Integer(perm[x+deltax[a]][y+deltay[a]]).toString();
  144. b[x+deltax[a]][y+deltay[a]].setText(Integer.toString(perm[x+deltax[a]][y+deltay[a]]));
  145. b[x+deltax[a]][y+deltay[a]].setEnabled(false);
  146. }
  147. }}
  148.  
  149. public int perimcheck(int a, int y){
  150. int minecount = 0;
  151. for (int x = 0;x<8;x++){
  152. if (mines[a+deltax[x]+1][y+deltay[x]+1] == 1){
  153. minecount++;
  154. }
  155. }
  156. return minecount;
  157. }
  158.  
  159. public void windowIconified(WindowEvent e){
  160.  
  161. }
  162.  
  163. public static void main(String[] args){
  164. new Mine();
  165. }
  166.  
  167.  
  168. public void mouseClicked(MouseEvent e) {
  169.  
  170. }
  171.  
  172.  
  173.  
  174. @Override
  175. public void mouseEntered(MouseEvent arg0) {
  176. // TODO Auto-generated method stub
  177.  
  178. }
  179.  
  180. @Override
  181. public void mouseExited(MouseEvent arg0) {
  182. // TODO Auto-generated method stub
  183.  
  184. }
  185.  
  186. @Override
  187. public void mousePressed(MouseEvent e) {
  188. if (e.getButton() == MouseEvent.BUTTON3) {
  189. found = false;
  190. Object current = e.getSource();
  191. for (int y = 0;y<m;y++){
  192. for (int x = 0;x<n;x++){
  193. JButton t = b[x][y];
  194. if(t == current){
  195. row=x;column=y; found =true;
  196. }
  197. }//end inner for
  198. }//end for
  199. if(!found) {
  200. System.out.println("didn't find the button, there was an error "); System.exit(-1);
  201. }
  202. if ((guesses[row+1][column+1] == 0) && (b[row][column].isEnabled())){
  203. b[row][column].setText("x");
  204. guesses[row+1][column+1] = 1;
  205. b[row][column].setBackground(Color.orange);
  206. } else if (guesses[row+1][column+1] == 1){
  207. b[row][column].setText("?");
  208. guesses[row+1][column+1] = 0;
  209. b[row][column].setBackground(null);
  210. }}
  211. }
  212.  
  213. @Override
  214. public void mouseReleased(MouseEvent arg0) {
  215. // TODO Auto-generated method stub
  216.  
  217. }
  218.  
  219.  
  220. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement