Advertisement
Guest User

fuck this shit

a guest
May 28th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. import java.awt.Container;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JMenu;
  9. import javax.swing.JMenuBar;
  10. import javax.swing.JMenuItem;
  11. import javax.swing.JPanel;
  12. import java.util.Random;
  13.  
  14. public class Minesweeper extends JFrame implements Runnable
  15. {
  16. final int rows = 9;
  17. final int columns = 9;
  18. final JButton [][] mineArray;
  19. final int [][] placeArray;
  20.  
  21. public Minesweeper()
  22. {
  23. super("Minesweeper");
  24. Container cp = getContentPane();
  25. JPanel minePanel = new JPanel();
  26. mineArray = null;
  27. placeArray = null;
  28. minePanel.setLayout(new GridLayout(9,9));
  29. cp.add(minePanel);
  30. for(int x = 0; x < rows; x++)
  31. {
  32. for(int y = 0; y < columns; y++)
  33. {
  34. MineButton button = new MineButton(x, y, false);
  35. mineArray[x][y] = button;
  36. minePanel.add(button);
  37. }
  38. }
  39. // System.out.println(mineArray[1][1]);
  40. }
  41.  
  42. public static void main(String[] args)
  43. {
  44. Minesweeper ms = new Minesweeper();
  45. javax.swing.SwingUtilities.invokeLater(ms);
  46. }
  47.  
  48. // makes menu bar - a file option that opens to a quit option
  49. private void makeMenu()
  50. {
  51. JMenu fileMenu = new JMenu("File");
  52. JMenuItem quitItem = new JMenuItem("Quit");
  53. quitItem.addActionListener(e -> System.exit(0));
  54. fileMenu.add(quitItem);
  55. JMenuItem resetItem = new JMenuItem("Reset");
  56. fileMenu.add(resetItem);
  57. // RESET here
  58.  
  59. JMenuBar menuBar = new JMenuBar();
  60. menuBar.add(fileMenu);
  61. setJMenuBar(menuBar);
  62. }
  63.  
  64.  
  65. public void end()
  66. {
  67. // change all buttons to fire engine red
  68. System.out.println("GAME OVER");
  69. }
  70.  
  71. public class MineButton extends JButton
  72. {
  73. private int x;
  74. private int y;
  75. private boolean isMine;
  76. private boolean isFlagged;
  77. private int numMines;
  78.  
  79. public MineButton(int _x, int _y, boolean _isMine)
  80. {
  81. isMine = _isMine;
  82. isFlagged = false;
  83. }
  84.  
  85. public void setFlag()
  86. {
  87. isFlagged = !(isFlagged);
  88. }
  89.  
  90. public boolean hasMine()
  91. {
  92. return isMine;
  93. }
  94.  
  95. public boolean getFlag()
  96. {
  97. return isFlagged;
  98. }
  99.  
  100. public void setAdjacentMineCount()
  101. {
  102. int count = 0;
  103. // if(x > 0 && (mineArray[x-1][y]).hasMine()) { count++; }
  104. // if(x < 8 && (mineArray[x+1][y]).hasMine()) { count++; }
  105. // if(y > 0 && (mineArray[x][y - 1]).hasMine()) { count++; }
  106. // if(y < 8 && (mineArray[x][y + 1]).hasMine()) { count++; }
  107. // diagonals
  108. System.out.println(mineArray[x-1][y]);
  109. setText(count > 0 ? "" + count : "");
  110. numMines = count;
  111. }
  112.  
  113. public void reveal()
  114. {
  115. if(isMine)
  116. {
  117. //End the game idk
  118. }
  119. setLabel(Integer.toString(numMines));
  120. System.out.println(numMines);
  121. }
  122.  
  123.  
  124. }
  125.  
  126. public void run()
  127. {
  128. setSize(500,500);
  129. setTitle("Minesweeper");
  130. setDefaultCloseOperation(EXIT_ON_CLOSE);
  131. makeMenu();
  132.  
  133. setVisible(true);
  134. }
  135.  
  136. class ClickListener implements ActionListener
  137. {
  138. // MineButton click = new MineButton("5", 0);
  139. public void actionPerformed(ActionEvent e)
  140. {
  141. //if(isRightMouseButton(e))
  142. if(4 == 5)
  143. {
  144. //getFlag();
  145. // this is the flag function - we want to implement something similar to grenredyellowstopthing
  146. }
  147.  
  148. else
  149. {
  150. //if(a.getFlag())
  151. {
  152. return;
  153. }
  154. //else
  155. //{
  156. //a.reveal();
  157. //}
  158. }
  159. }
  160. }
  161.  
  162. public void generateMines()
  163. //Maybe we should make sure that the first mine clicked doesn't become a bomb.
  164. {
  165. Random r = new Random();
  166. for(int a = 0; a < 10; a = a)
  167. {
  168. MineButton change = mineArray[r.nextInt(10)] [r.nextInt(10)];
  169. System.out.println(change);
  170. if(change.hasMine())
  171. {
  172. return;
  173. }
  174. else
  175. {
  176. change.setMine();
  177. a++;
  178. }
  179. }
  180.  
  181. return;
  182. }
  183.  
  184.  
  185. /*
  186. * Game Structure:
  187. * begin with all unlabeled buttons
  188. * when the first button is clicked, make sure that button isn't a mine. Maybe make sure that several buttons reveal themselves?
  189. * randomly assign the positions of the (10?) bombs
  190. * fill the number labels of each so that they represent how many mines each is touching
  191. * when the user clicks on a non-revealed block, have the game end if it's a mine. Otherwise, have it reveal the block that is
  192. * clicked and any surrounding blocks with a value of 0. We need to set off a chain reaction of some kind here?
  193. * When the user right clicks on a non-revealed block, mark the block with a flag.
  194. * Turn off the (left)clickability for any flagged block. Right clicking again unflags.
  195. * Have a counter for the number of flags used in the game and the number of flags left.
  196. * The game ends when all non-mine blocks are revealed or a mine is clicked.
  197. * If the user loses, display where all the mines were that hadn't been clicked. Have the clicked mine turn red.
  198. */
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement