Difference between revisions of "Minesweeper in Java"

From Gumstix User Wiki
Jump to: navigation, search
(Undo revision 4594 by Shescort1 (Talk))
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://yxiwisewava.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
 +
----
 +
=[http://yxiwisewava.co.cc CLICK HERE]=
 +
----
 +
</div>
 
This tutorial gives a brief overview of what to do to get a small, graphical Minesweeper game running on your Overo. If you have not done so, please go through the tutorials [[Eclipse on Gumstix for new users]] and [[HelloWorld in Java]].
 
This tutorial gives a brief overview of what to do to get a small, graphical Minesweeper game running on your Overo. If you have not done so, please go through the tutorials [[Eclipse on Gumstix for new users]] and [[HelloWorld in Java]].
  
Line 4: Line 12:
  
 
*Create a new Project in Eclipse  
 
*Create a new Project in Eclipse  
:*'''File -> New Project -> Java Project'''
+
:*'''File -&gt; New Project -&gt; Java Project'''
 
*Create a class '''Main Class'''
 
*Create a class '''Main Class'''
 
:*Copy and paste the following code to that class:
 
:*Copy and paste the following code to that class:
<pre>  
+
&lt;pre&gt;  
 
import java.awt.BorderLayout;
 
import java.awt.BorderLayout;
 
import java.awt.Color;
 
import java.awt.Color;
Line 20: Line 28:
 
public class MainClass {
 
public class MainClass {
 
   public static void main(String args[]) {
 
   public static void main(String args[]) {
     JFrame f = new JFrame("JColorChooser Sample");
+
     JFrame f = new JFrame(&quot;JColorChooser Sample&quot;);
 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     final JButton button = new JButton("Pick to Change Background");
+
     final JButton button = new JButton(&quot;Pick to Change Background&quot;);
  
 
     ActionListener actionListener = new ActionListener() {
 
     ActionListener actionListener = new ActionListener() {
Line 28: Line 36:
 
         Color initialBackground = button.getBackground();
 
         Color initialBackground = button.getBackground();
 
         Color background = JColorChooser.showDialog(null,
 
         Color background = JColorChooser.showDialog(null,
             "JColorChooser Sample", initialBackground);
+
             &quot;JColorChooser Sample&quot;, initialBackground);
 
         if (background != null) {
 
         if (background != null) {
 
           button.setBackground(background);
 
           button.setBackground(background);
Line 40: Line 48:
 
   }
 
   }
  
}</pre>
+
}&lt;/pre&gt;
 
*Create a class '''MineSweeper''' (Capital S!)
 
*Create a class '''MineSweeper''' (Capital S!)
 
:*Copy and paste the following code to that class:
 
:*Copy and paste the following code to that class:
<pre>import java.awt.AWTEvent;
+
&lt;pre&gt;import java.awt.AWTEvent;
 
import java.awt.BorderLayout;
 
import java.awt.BorderLayout;
 
import java.awt.Color;
 
import java.awt.Color;
Line 81: Line 89:
 
  private int          ROWS      = 9, COLUMNS = 9, TOTAL = ROWS * COLUMNS;
 
  private int          ROWS      = 9, COLUMNS = 9, TOTAL = ROWS * COLUMNS;
 
  private JPanel        pnlMain      = new JPanel(new GridLayout(ROWS, COLUMNS));
 
  private JPanel        pnlMain      = new JPanel(new GridLayout(ROWS, COLUMNS));
  private JLabel        lblBombCount  = new JLabel(MAX_BOMB_COUNT + "");
+
  private JLabel        lblBombCount  = new JLabel(MAX_BOMB_COUNT + &quot;&quot;);
  private JLabel        lblTimer    = new JLabel("0");
+
  private JLabel        lblTimer    = new JLabel(&quot;0&quot;);
 
  private boolean        isColorCheatOn  = false;
 
  private boolean        isColorCheatOn  = false;
  private JButton        btnReset    = new JButton("Reset");
+
  private JButton        btnReset    = new JButton(&quot;Reset&quot;);
  
 
  private void startThread() {
 
  private void startThread() {
Line 90: Line 98:
 
      public void run() {
 
      public void run() {
 
        while (state == GameState.Playing) {
 
        while (state == GameState.Playing) {
          lblTimer.setText((Long.parseLong(lblTimer.getText()) + 1) + "");
+
          lblTimer.setText((Long.parseLong(lblTimer.getText()) + 1) + &quot;&quot;);
 
          lblTimer.updateUI();
 
          lblTimer.updateUI();
 
          try {
 
          try {
Line 114: Line 122:
  
 
  private void showAbout() {
 
  private void showAbout() {
    JOptionPane.showMessageDialog(this, "<html>Author : Justin Cowles Cowperthwaite <br>Version : 1.0</html>", "About", JOptionPane.INFORMATION_MESSAGE);
+
    JOptionPane.showMessageDialog(this, &quot;&lt;html&gt;Author : Justin Cowles Cowperthwaite &lt;br&gt;Version : 1.0&lt;/html&gt;&quot;, &quot;About&quot;, JOptionPane.INFORMATION_MESSAGE);
 
  }
 
  }
  
 
  private void restartGame() {
 
  private void restartGame() {
 
    state = GameState.NotStarted;
 
    state = GameState.NotStarted;
    lblTimer.setText("0");
+
    lblTimer.setText(&quot;0&quot;);
 
    pnlMain.removeAll();
 
    pnlMain.removeAll();
 
    createButtons();
 
    createButtons();
 
    pnlMain.updateUI();
 
    pnlMain.updateUI();
    lblBombCount.setText("" + MAX_BOMB_COUNT);
+
    lblBombCount.setText(&quot;&quot; + MAX_BOMB_COUNT);
 
    lblBombCount.updateUI();
 
    lblBombCount.updateUI();
 
  }
 
  }
Line 134: Line 142:
 
    JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
 
    JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
  
    btnReset.setToolTipText("<HTML>Press <B>F2</B> to reset the current game</HTML>");
+
    btnReset.setToolTipText(&quot;&lt;HTML&gt;Press &lt;B&gt;F2&lt;/B&gt; to reset the current game&lt;/HTML&gt;&quot;);
 
    pnl.add(lblBombCount);
 
    pnl.add(lblBombCount);
 
    pnl.add(btnReset);
 
    pnl.add(btnReset);
Line 147: Line 155:
  
 
  private void createButtons() {
 
  private void createButtons() {
    List<Point> lstBombsLocation = new ArrayList<Point>();
+
    List&lt;Point&gt; lstBombsLocation = new ArrayList&lt;Point&gt;();
  
    for (int row = 0; row < ROWS; row++) {
+
    for (int row = 0; row &lt; ROWS; row++) {
      for (int col = 0; col < COLUMNS; col++) {
+
      for (int col = 0; col &lt; COLUMNS; col++) {
 
        JButton btn = getButton(lstBombsLocation, TOTAL, new Point(row, col) {
 
        JButton btn = getButton(lstBombsLocation, TOTAL, new Point(row, col) {
 
          @Override
 
          @Override
 
          public String toString() {
 
          public String toString() {
            return (int) getX() + ", " + (int) getY();
+
            return (int) getX() + &quot;, &quot; + (int) getY();
 
          }
 
          }
  
 
          @Override
 
          @Override
 
          public boolean equals(Object obj) {
 
          public boolean equals(Object obj) {
            return ((Point) obj).getX() == getX() && ((Point) obj).getY() == getY();
+
            return ((Point) obj).getX() == getX() &amp;&amp; ((Point) obj).getY() == getY();
 
          }
 
          }
 
        });
 
        });
Line 165: Line 173:
 
      }
 
      }
 
    }
 
    }
    while (lstBombsLocation.size() < MAX_BOMB_COUNT) {
+
    while (lstBombsLocation.size() &lt; MAX_BOMB_COUNT) {
 
      updateBomds(lstBombsLocation, pnlMain.getComponents());
 
      updateBomds(lstBombsLocation, pnlMain.getComponents());
 
    }
 
    }
Line 171: Line 179:
 
      updateBombCount((GameButton) c, pnlMain.getComponents());
 
      updateBombCount((GameButton) c, pnlMain.getComponents());
 
    }
 
    }
    // System.out.println("Total Bomb Count: " + lstBombsLocation.size());
+
    // System.out.println(&quot;Total Bomb Count: &quot; + lstBombsLocation.size());
 
  }
 
  }
  
  private void updateBomds(List<Point> lstBombsLocation, Component[] components) {
+
  private void updateBomds(List&lt;Point&gt; lstBombsLocation, Component[] components) {
 
    // int currentPosition = new Double(((location.x) * COLUMNS ) +
 
    // int currentPosition = new Double(((location.x) * COLUMNS ) +
 
    // location.getY()).intValue();
 
    // location.getY()).intValue();
Line 190: Line 198:
 
  }
 
  }
  
  private GameButton getButton(List<Point> lstBombsLocation, int totalLocations, Point location) {
+
  private GameButton getButton(List&lt;Point&gt; lstBombsLocation, int totalLocations, Point location) {
 
    GameButton btn = new GameButton(location);
 
    GameButton btn = new GameButton(location);
 
    btn.setMargin(new Insets(0, 0, 0, 0));
 
    btn.setMargin(new Insets(0, 0, 0, 0));
 
    btn.setFocusable(false);
 
    btn.setFocusable(false);
    if (lstBombsLocation.size() < MAX_BOMB_COUNT) {
+
    if (lstBombsLocation.size() &lt; MAX_BOMB_COUNT) {
 
      if (isBomb()) {
 
      if (isBomb()) {
 
        btn.setBomb(true);
 
        btn.setBomb(true);
Line 213: Line 221:
 
          if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) {
 
          if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) {
 
            ((GameButton) mouseEvent.getSource()).setState(State.Initial);
 
            ((GameButton) mouseEvent.getSource()).setState(State.Initial);
            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + "");
+
            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + &quot;&quot;);
 
            ((GameButton) mouseEvent.getSource()).updateUI();
 
            ((GameButton) mouseEvent.getSource()).updateUI();
 
            return;
 
            return;
Line 232: Line 240:
 
          if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) {
 
          if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) {
 
            ((GameButton) mouseEvent.getSource()).setState(State.Initial);
 
            ((GameButton) mouseEvent.getSource()).setState(State.Initial);
            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + "");
+
            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + &quot;&quot;);
 
          } else {
 
          } else {
 
            ((GameButton) mouseEvent.getSource()).setState(State.Marked);
 
            ((GameButton) mouseEvent.getSource()).setState(State.Marked);
            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) - 1) + "");
+
            lblBombCount.setText((Long.parseLong(lblBombCount.getText()) - 1) + &quot;&quot;);
 
          }
 
          }
 
        }
 
        }
Line 266: Line 274:
  
 
      }
 
      }
      JOptionPane.showMessageDialog(this, "You win the game :D", "Congrats", JOptionPane.INFORMATION_MESSAGE, null);
+
      JOptionPane.showMessageDialog(this, &quot;You win the game :D&quot;, &quot;Congrats&quot;, JOptionPane.INFORMATION_MESSAGE, null);
 
    }
 
    }
 
    return isWin;
 
    return isWin;
Line 276: Line 284:
 
    for (Point p : points) {
 
    for (Point p : points) {
 
      GameButton b = getButtonAt(pnlMain.getComponents(), p);
 
      GameButton b = getButtonAt(pnlMain.getComponents(), p);
      if (b != null && b.getBombCount() == 0 && b.getState() != State.Clicked && b.getState() != State.Marked && b.isBomb() == false) {
+
      if (b != null &amp;&amp; b.getBombCount() == 0 &amp;&amp; b.getState() != State.Clicked &amp;&amp; b.getState() != State.Marked &amp;&amp; b.isBomb() == false) {
 
        b.setState(State.Clicked);
 
        b.setState(State.Clicked);
 
        updateSurroundingZeros(b.getPosition());
 
        updateSurroundingZeros(b.getPosition());
 
        b.updateUI();
 
        b.updateUI();
 
      }
 
      }
      if (b != null && b.getBombCount() > 0 && b.getState() != State.Clicked && b.getState() != State.Marked && b.isBomb() == false) {
+
      if (b != null &amp;&amp; b.getBombCount() &gt; 0 &amp;&amp; b.getState() != State.Clicked &amp;&amp; b.getState() != State.Marked &amp;&amp; b.isBomb() == false) {
 
        b.setEnabled(false);
 
        b.setEnabled(false);
 
        b.setState(State.Clicked);
 
        b.setState(State.Clicked);
Line 294: Line 302:
 
      ((GameButton) c).setEnabled(false);
 
      ((GameButton) c).setEnabled(false);
 
      ((GameButton) c).transferFocus();
 
      ((GameButton) c).transferFocus();
      if (((GameButton) c).isBomb() && ((GameButton) c).getState() != State.Marked) {
+
      if (((GameButton) c).isBomb() &amp;&amp; ((GameButton) c).getState() != State.Marked) {
 
        ((GameButton) c).setState(State.Clicked);
 
        ((GameButton) c).setState(State.Clicked);
 
        ((GameButton) c).updateUI();
 
        ((GameButton) c).updateUI();
 
        blastCount++;
 
        blastCount++;
 
      }
 
      }
      if (((GameButton) c).isBomb() == false && ((GameButton) c).getState() == State.Marked) {
+
      if (((GameButton) c).isBomb() == false &amp;&amp; ((GameButton) c).getState() == State.Marked) {
 
        ((GameButton) c).setState(State.WrongMarked);
 
        ((GameButton) c).setState(State.WrongMarked);
 
      }
 
      }
 
    }
 
    }
    lblBombCount.setText("" + blastCount);
+
    lblBombCount.setText(&quot;&quot; + blastCount);
 
    lblBombCount.updateUI();
 
    lblBombCount.updateUI();
 
    state = GameState.Finished;
 
    state = GameState.Finished;
    JOptionPane.showMessageDialog(this, "You loose the game :(", "Game Over", JOptionPane.ERROR_MESSAGE, null);
+
    JOptionPane.showMessageDialog(this, &quot;You loose the game :(&quot;, &quot;Game Over&quot;, JOptionPane.ERROR_MESSAGE, null);
 
    for (Component c : pnlMain.getComponents()) {
 
    for (Component c : pnlMain.getComponents()) {
 
      GameButton b = (GameButton) c;
 
      GameButton b = (GameButton) c;
Line 319: Line 327:
  
 
  public static void main(String[] args) {
 
  public static void main(String[] args) {
    JFrame fr = new JFrame("MineSweeper");
+
    JFrame fr = new JFrame(&quot;MineSweeper&quot;);
 
    fr.setLayout(new BorderLayout());
 
    fr.setLayout(new BorderLayout());
 
    fr.add(new MineSweeper());
 
    fr.add(new MineSweeper());
Line 337: Line 345:
 
    public void setState(State state) {
 
    public void setState(State state) {
 
      this.state = state;
 
      this.state = state;
      if (getBombCount() == 0 && !isBomb) {
+
      if (getBombCount() == 0 &amp;&amp; !isBomb) {
 
        setEnabled(false);
 
        setEnabled(false);
 
      }
 
      }
Line 378: Line 386:
 
    public String getText() {
 
    public String getText() {
 
      if (state == State.Initial) {
 
      if (state == State.Initial) {
        return "";
+
        return &quot;&quot;;
 
      }
 
      }
 
      if (state == State.Marked) {
 
      if (state == State.Marked) {
        return "\u00B6";
+
        return &quot;\u00B6&quot;;
 
      }
 
      }
 
      if (state == State.Clicked) {
 
      if (state == State.Clicked) {
 
        if (isBomb) {
 
        if (isBomb) {
          return "<html><font size='16'><b>*</b></font></html>";
+
          return &quot;&lt;html&gt;&lt;font size='16'&gt;&lt;b&gt;*&lt;/b&gt;&lt;/font&gt;&lt;/html&gt;&quot;;
 
        } else {
 
        } else {
          if (getBombCount() > 0)
+
          if (getBombCount() &gt; 0)
            return getBombCount() + "";
+
            return getBombCount() + &quot;&quot;;
 
          else
 
          else
            return "";
+
            return &quot;&quot;;
 
        }
 
        }
 
      }
 
      }
 
      if (state == State.WrongMarked) {
 
      if (state == State.WrongMarked) {
        return "X";
+
        return &quot;X&quot;;
 
      }
 
      }
 
      return super.getText();
 
      return super.getText();
Line 401: Line 409:
 
    @Override
 
    @Override
 
    public Color getBackground() {
 
    public Color getBackground() {
      if (isColorCheatOn && isBomb) {
+
      if (isColorCheatOn &amp;&amp; isBomb) {
 
        return Color.MAGENTA;
 
        return Color.MAGENTA;
 
      }
 
      }
Line 408: Line 416:
 
          return Color.RED;
 
          return Color.RED;
 
        }
 
        }
        if (getBombCount() > 0) {
+
        if (getBombCount() &gt; 0) {
 
          return Color.GREEN;
 
          return Color.GREEN;
 
        }
 
        }
Line 433: Line 441:
 
    for (Point p : points) {
 
    for (Point p : points) {
 
      GameButton b = getButtonAt(components, p);
 
      GameButton b = getButtonAt(components, p);
      if (b != null && b.isBomb()) {
+
      if (b != null &amp;&amp; b.isBomb()) {
 
        btn.setBombCount(btn.getBombCount() + 1);
 
        btn.setBombCount(btn.getBombCount() + 1);
 
      }
 
      }
 
    }
 
    }
    btn.setText(btn.getBombCount() + "");
+
    btn.setText(btn.getBombCount() + &quot;&quot;);
 
  }
 
  }
  
Line 450: Line 458:
  
 
  public void eventDispatched(AWTEvent event) {
 
  public void eventDispatched(AWTEvent event) {
    if (KeyEvent.class.isInstance(event) && ((KeyEvent) (event)).getID() == KeyEvent.KEY_RELEASED) {
+
    if (KeyEvent.class.isInstance(event) &amp;&amp; ((KeyEvent) (event)).getID() == KeyEvent.KEY_RELEASED) {
 
      if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F1) {
 
      if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F1) {
 
        showAbout();
 
        showAbout();
Line 484: Line 492:
 
    }
 
    }
 
  }
 
  }
}</pre>
+
}&lt;/pre&gt;
  
 
Go back to [[HelloWorld in Java]] and follow the instructions for Exporting and Running a Java Program on the Over COM. When you are done, you should be able to play a 9x10 colorful game of MineSweeper on the monitor connected to your Overo COM expansion board.
 
Go back to [[HelloWorld in Java]] and follow the instructions for Exporting and Running a Java Program on the Over COM. When you are done, you should be able to play a 9x10 colorful game of MineSweeper on the monitor connected to your Overo COM expansion board.

Revision as of 15:42, 23 November 2010


This tutorial gives a brief overview of what to do to get a small, graphical Minesweeper game running on your Overo. If you have not done so, please go through the tutorials Eclipse on Gumstix for new users and HelloWorld in Java.

This does not explain the game of MineSweeper, rather just lets use utilize the code to see an example of the graphical possibilities using an Overo COM.

  • Create a new Project in Eclipse
  • File -> New Project -> Java Project
  • Create a class Main Class
  • Copy and paste the following code to that class:

<pre> import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JOptionPane;

public class MainClass {

 public static void main(String args[]) {
   JFrame f = new JFrame("JColorChooser Sample");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   final JButton button = new JButton("Pick to Change Background");
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       Color initialBackground = button.getBackground();
       Color background = JColorChooser.showDialog(null,
           "JColorChooser Sample", initialBackground);
       if (background != null) {
         button.setBackground(background);
       }
     }
   };
   button.addActionListener(actionListener);
   f.add(button, BorderLayout.CENTER);
   f.setSize(300, 200);
   f.setVisible(true);
 }

}</pre>

  • Create a class MineSweeper (Capital S!)
  • Copy and paste the following code to that class:

<pre>import java.awt.AWTEvent; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.AWTEventListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import java.util.Random;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel;

public class MineSweeper extends JPanel implements AWTEventListener, ActionListener {

public static enum State { Clicked, Marked, Initial, WrongMarked }

public static enum GameState { NotStarted, Playing, Finished }

private static final int MAX_BOMB_COUNT = 10; private int ROWS = 9, COLUMNS = 9, TOTAL = ROWS * COLUMNS; private JPanel pnlMain = new JPanel(new GridLayout(ROWS, COLUMNS)); private JLabel lblBombCount = new JLabel(MAX_BOMB_COUNT + ""); private JLabel lblTimer = new JLabel("0"); private boolean isColorCheatOn = false; private JButton btnReset = new JButton("Reset");

private void startThread() { Thread th = new Thread(new Runnable() { public void run() { while (state == GameState.Playing) { lblTimer.setText((Long.parseLong(lblTimer.getText()) + 1) + ""); lblTimer.updateUI(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); th.start(); }

private GameState state = GameState.NotStarted;

public MineSweeper() { setLayout(new BorderLayout()); add(pnlMain, BorderLayout.CENTER); createButtons(); addControlPanel(); Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK); }

private void showAbout() { JOptionPane.showMessageDialog(this, "<html>Author : Justin Cowles Cowperthwaite <br>Version : 1.0</html>", "About", JOptionPane.INFORMATION_MESSAGE); }

private void restartGame() { state = GameState.NotStarted; lblTimer.setText("0"); pnlMain.removeAll(); createButtons(); pnlMain.updateUI(); lblBombCount.setText("" + MAX_BOMB_COUNT); lblBombCount.updateUI(); }

private void addControlPanel() { JPanel pnlTimer = new JPanel(new FlowLayout(FlowLayout.RIGHT));

pnlTimer.add(lblTimer);

JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));

btnReset.setToolTipText("<HTML>Press <B>F2</B> to reset the current game</HTML>"); pnl.add(lblBombCount); pnl.add(btnReset); JPanel pnlN = new JPanel(new GridLayout(1, 3));

pnlN.add(lblBombCount); pnlN.add(pnl); pnlN.add(pnlTimer); add(pnlN, BorderLayout.NORTH); btnReset.addActionListener(this); }

private void createButtons() { List<Point> lstBombsLocation = new ArrayList<Point>();

for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLUMNS; col++) { JButton btn = getButton(lstBombsLocation, TOTAL, new Point(row, col) { @Override public String toString() { return (int) getX() + ", " + (int) getY(); }

@Override public boolean equals(Object obj) { return ((Point) obj).getX() == getX() && ((Point) obj).getY() == getY(); } }); pnlMain.add(btn); } } while (lstBombsLocation.size() < MAX_BOMB_COUNT) { updateBomds(lstBombsLocation, pnlMain.getComponents()); } for (Component c : pnlMain.getComponents()) { updateBombCount((GameButton) c, pnlMain.getComponents()); } // System.out.println("Total Bomb Count: " + lstBombsLocation.size()); }

private void updateBomds(List<Point> lstBombsLocation, Component[] components) { // int currentPosition = new Double(((location.x) * COLUMNS ) + // location.getY()).intValue(); Random r = new Random(); for (Component c : components) { Point location = ((GameButton) c).getPosition(); int currentPosition = new Double(((location.x) * COLUMNS) + location.getY()).intValue(); int bombLocation = r.nextInt(TOTAL); if (bombLocation == currentPosition) { ((GameButton) c).setBomb(true); lstBombsLocation.add(((GameButton) c).getPosition()); return; } } }

private GameButton getButton(List<Point> lstBombsLocation, int totalLocations, Point location) { GameButton btn = new GameButton(location); btn.setMargin(new Insets(0, 0, 0, 0)); btn.setFocusable(false); if (lstBombsLocation.size() < MAX_BOMB_COUNT) { if (isBomb()) { btn.setBomb(true); lstBombsLocation.add(location); } } btn.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent mouseEvent) { if (state != GameState.Playing) { state = GameState.Playing; startThread(); } if (((GameButton) mouseEvent.getSource()).isEnabled() == false) { return; } if (mouseEvent.getButton() == MouseEvent.BUTTON1) { if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) { ((GameButton) mouseEvent.getSource()).setState(State.Initial); lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + ""); ((GameButton) mouseEvent.getSource()).updateUI(); return; } ((GameButton) mouseEvent.getSource()).setState(State.Clicked); if (((GameButton) mouseEvent.getSource()).isBomb()) { blastBombs(); return; } else { if (((GameButton) mouseEvent.getSource()).getBombCount() == 0) { updateSurroundingZeros(((GameButton) mouseEvent.getSource()).getPosition()); } } if (!checkGameState()) { ((GameButton) mouseEvent.getSource()).setEnabled(false); } } else if (mouseEvent.getButton() == MouseEvent.BUTTON3) { if (((GameButton) mouseEvent.getSource()).getState() == State.Marked) { ((GameButton) mouseEvent.getSource()).setState(State.Initial); lblBombCount.setText((Long.parseLong(lblBombCount.getText()) + 1) + ""); } else { ((GameButton) mouseEvent.getSource()).setState(State.Marked); lblBombCount.setText((Long.parseLong(lblBombCount.getText()) - 1) + ""); } } ((GameButton) mouseEvent.getSource()).updateUI(); } }); return btn; }

private boolean checkGameState() { boolean isWin = false; for (Component c : pnlMain.getComponents()) { GameButton b = (GameButton) c; if (b.getState() != State.Clicked) { if (b.isBomb()) { isWin = true; } else { return false; } } } if (isWin) { state = GameState.Finished; for (Component c : pnlMain.getComponents()) { GameButton b = (GameButton) c; if (b.isBomb()) { b.setState(State.Marked); } b.setEnabled(false);

} JOptionPane.showMessageDialog(this, "You win the game :D", "Congrats", JOptionPane.INFORMATION_MESSAGE, null); } return isWin; }

private void updateSurroundingZeros(Point currentPoint) { Point[] points = getSurroundings(currentPoint);

for (Point p : points) { GameButton b = getButtonAt(pnlMain.getComponents(), p); if (b != null && b.getBombCount() == 0 && b.getState() != State.Clicked && b.getState() != State.Marked && b.isBomb() == false) { b.setState(State.Clicked); updateSurroundingZeros(b.getPosition()); b.updateUI(); } if (b != null && b.getBombCount() > 0 && b.getState() != State.Clicked && b.getState() != State.Marked && b.isBomb() == false) { b.setEnabled(false); b.setState(State.Clicked); b.updateUI(); } } }

private void blastBombs() { int blastCount = 0; for (Component c : pnlMain.getComponents()) { ((GameButton) c).setEnabled(false); ((GameButton) c).transferFocus(); if (((GameButton) c).isBomb() && ((GameButton) c).getState() != State.Marked) { ((GameButton) c).setState(State.Clicked); ((GameButton) c).updateUI(); blastCount++; } if (((GameButton) c).isBomb() == false && ((GameButton) c).getState() == State.Marked) { ((GameButton) c).setState(State.WrongMarked); } } lblBombCount.setText("" + blastCount); lblBombCount.updateUI(); state = GameState.Finished; JOptionPane.showMessageDialog(this, "You loose the game :(", "Game Over", JOptionPane.ERROR_MESSAGE, null); for (Component c : pnlMain.getComponents()) { GameButton b = (GameButton) c; b.setEnabled(false); } }

private boolean isBomb() { Random r = new Random(); return r.nextInt(ROWS) == 1; }

public static void main(String[] args) { JFrame fr = new JFrame("MineSweeper"); fr.setLayout(new BorderLayout()); fr.add(new MineSweeper()); fr.setResizable(false); fr.setSize(250, 350); fr.setLocationRelativeTo(null); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fr.setVisible(true); }

class GameButton extends JButton { private boolean isBomb = false; private Point position = null; private int bombCount = 0; private State state = State.Initial;

public void setState(State state) { this.state = state; if (getBombCount() == 0 && !isBomb) { setEnabled(false); } }

public State getState() { return state; }

public int getBombCount() { return bombCount; }

public void setBombCount(int bombCount) { this.bombCount = bombCount; }

public GameButton(Point position) { setPosition(position); setText(position.toString()); }

public Point getPosition() { return position; }

public void setPosition(Point position) { this.position = position; }

public boolean isBomb() { return isBomb; }

public void setBomb(boolean isBomb) { this.isBomb = isBomb; }

@Override public String getText() { if (state == State.Initial) { return ""; } if (state == State.Marked) { return "\u00B6"; } if (state == State.Clicked) { if (isBomb) { return "<html><font size='16'><b>*</b></font></html>"; } else { if (getBombCount() > 0) return getBombCount() + ""; else return ""; } } if (state == State.WrongMarked) { return "X"; } return super.getText(); }

@Override public Color getBackground() { if (isColorCheatOn && isBomb) { return Color.MAGENTA; } if (state == State.Clicked) { if (isBomb) { return Color.RED; } if (getBombCount() > 0) { return Color.GREEN; } } if (isEnabled()) { return Color.YELLOW.brighter(); } else { return super.getBackground(); } } }

private Point[] getSurroundings(Point cPoint) { int cX = (int) cPoint.getX(); int cY = (int) cPoint.getY(); Point[] points = { new Point(cX - 1, cY - 1), new Point(cX - 1, cY), new Point(cX - 1, cY + 1), new Point(cX, cY - 1), new Point(cX, cY + 1),

                                        new Point(cX + 1, cY - 1), new Point(cX + 1, cY), new Point(cX + 1, cY + 1) };

return points; }

private void updateBombCount(GameButton btn, Component[] components) { Point[] points = getSurroundings(btn.getPosition());

for (Point p : points) { GameButton b = getButtonAt(components, p); if (b != null && b.isBomb()) { btn.setBombCount(btn.getBombCount() + 1); } } btn.setText(btn.getBombCount() + ""); }

private GameButton getButtonAt(Component[] components, Point position) { for (Component btn : components) { if ((((GameButton) btn).getPosition().equals(position))) { return (GameButton) btn; } } return null; }

public void eventDispatched(AWTEvent event) { if (KeyEvent.class.isInstance(event) && ((KeyEvent) (event)).getID() == KeyEvent.KEY_RELEASED) { if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F1) { showAbout(); } if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F2) { restartGame(); } if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F3) { isColorCheatOn = !isColorCheatOn; if (state == GameState.Playing) { pnlMain.updateUI(); } }

if (((KeyEvent) (event)).getKeyCode() == KeyEvent.VK_F12) { for (Component c : pnlMain.getComponents()) { GameButton b = (GameButton) c; if (b.isBomb() == false) { b.setState(State.Clicked); } else { b.setState(State.Marked); } b.setEnabled(false); } checkGameState(); } } }

public void actionPerformed(ActionEvent actionEvent) { if (actionEvent.getSource() == btnReset) { restartGame(); } } }</pre>

Go back to HelloWorld in Java and follow the instructions for Exporting and Running a Java Program on the Over COM. When you are done, you should be able to play a 9x10 colorful game of MineSweeper on the monitor connected to your Overo COM expansion board.