Skip to content

Instantly share code, notes, and snippets.

@pgoodman
Created February 13, 2009 02:47
Show Gist options
  • Select an option

  • Save pgoodman/63009 to your computer and use it in GitHub Desktop.

Select an option

Save pgoodman/63009 to your computer and use it in GitHub Desktop.
package minesweeper;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import minesweeper.functional.*;
import minesweeper.gui.*;
import minesweeper.components.*;
import static minesweeper.functional.ArrayUtils.map;
/**
* The Mine Sweeper program.
*
* @author Peter Goodman
*/
public class MineSweeper extends SimpleGUI {
protected static int width,
num_bombs;
// poor man's typedef
private interface MenuItemDelegate extends Delegate<JMenuItem> { }
/**
* Initialize the arrays needed.
* @param width
*/
public MineSweeper(int w, int nb) {
width = w;
num_bombs = nb;
}
/**
* Create the menu bar, all menus, and menu items.
*/
protected static void createMenu(final JFrame f) {
menu_bar(f,
menu("File",
menu_item("Exit", new MenuItemDelegate() {
public void call(JMenuItem item) {
System.exit(0);
}
})
),
menu("Theme",
menu_item("System Default", new MenuItemDelegate() {
public void call(JMenuItem item) {
set_look_and_feel(f,
UIManager.getSystemLookAndFeelClassName()
);
}
}),
menu_item("Motif", new MenuItemDelegate() {
public void call(JMenuItem item) {
set_look_and_feel(f,
"com.sun.java.swing.plaf.motif.MotifLookAndFeel"
);
}
}),
menu_item("OMG Ponies")
)
);
}
/**
* Create the mine field.
*/
protected static JPanel createMineField() {
final MineField field = new MineField(width, num_bombs);
return grid(map(field.getAreas(), new Function<MineArea, GridCell>() {
public GridCell call(final MineArea area) {
return grid_cell(area.getView(new Delegate<JPanel>() {
public void call(JPanel pane) {
// deal with mouse clicks
pane.addMouseListener(new MouseInputAdapter() {
public void mouseClicked(MouseEvent e) {
// left click, reveal a mine
if(e.getButton() == MouseEvent.BUTTON1) {
field.revealArea(area);
// any non-left-click click, flag this area
} else {
area.changeIsFlagged();
}
}
});
}
// set the grid bag position constraints
})).pos(area.getX(), area.getY());
}
}));
}
/**
* Create the GUI.
*/
public void create() {
frame("Mine Sweeper", new Delegate<JFrame>() {
public void call(JFrame f) {
createMenu(f);
add_content(f, createMineField());
}
});
}
/**
* Create and run the GUI.
* @param args
*/
public static void main(final String[] args) {
run(new MineSweeper(10, 15));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment