Advertisement
Guest User

Untitled

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