diff --git a/Herd.java b/Herd.java index 804caab..ae3ecdf 100755 --- a/Herd.java +++ b/Herd.java @@ -12,11 +12,14 @@ */ import java.util.ArrayList; +import java.util.Random; public class Herd { public static int initialSize = 4; private static Herd singleton; + //allow other classes to access the RNG + public static Random random; private ArrayList herd; private Cow[] parents; @@ -37,6 +40,7 @@ public static Herd getInstance() { if (singleton == null) { + random = new Random(); singleton = new Herd(); } return singleton; @@ -114,4 +118,9 @@ { //TODO load from binary file } + + public void resetHerd() + { + singleton = null; + } } diff --git a/Herd.java b/Herd.java index 804caab..ae3ecdf 100755 --- a/Herd.java +++ b/Herd.java @@ -12,11 +12,14 @@ */ import java.util.ArrayList; +import java.util.Random; public class Herd { public static int initialSize = 4; private static Herd singleton; + //allow other classes to access the RNG + public static Random random; private ArrayList herd; private Cow[] parents; @@ -37,6 +40,7 @@ public static Herd getInstance() { if (singleton == null) { + random = new Random(); singleton = new Herd(); } return singleton; @@ -114,4 +118,9 @@ { //TODO load from binary file } + + public void resetHerd() + { + singleton = null; + } } diff --git a/MooBreeder.java b/MooBreeder.java index cbeab2a..fb3e8e2 100755 --- a/MooBreeder.java +++ b/MooBreeder.java @@ -14,7 +14,6 @@ import java.awt.*; import java.awt.event.*; import javax.swing.*; -import java.util.ArrayList; public class MooBreeder extends JFrame { @@ -25,12 +24,9 @@ private Box motherBox, fatherBox, child1Box, child2Box; private JButton add, breed, keep, remove; - private ArrayList herd; - private Cow[] parents; - private Cow[] offspring; - public static void main(String[] args) { + Herd.getInstance(); MooBreeder mb = new MooBreeder(); } @@ -55,9 +51,21 @@ help = new JMenuItem("Help"); about = new JMenuItem("About"); //TODO + reset.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) { + int restart = JOptionPane.showConfirmDialog(null, "Reset the game?", "Reset?", + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE); + if (restart == JOptionPane.OK_OPTION) resetGame(); + } + }); quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - System.exit(0); + int confirm = JOptionPane.showConfirmDialog(null, "Quit MooBreeder?", "Quit?", + JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE); + if(confirm == JOptionPane.YES_OPTION) System.exit(0); } }); quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK)); @@ -90,6 +98,11 @@ this.add(breedBox, BorderLayout.EAST); breedBox.add(parentBox); breedBox.add(offspringBox); + updateGUI(); + } + + private void updateGUI() + { updateHerdBox(); updateParentBox(); updateOffspringBox(); @@ -117,4 +130,10 @@ offspringBox.add(label); } + private void resetGame() + { + Herd.getInstance().resetHerd(); + updateGUI(); + } + } diff --git a/Herd.java b/Herd.java index 804caab..ae3ecdf 100755 --- a/Herd.java +++ b/Herd.java @@ -12,11 +12,14 @@ */ import java.util.ArrayList; +import java.util.Random; public class Herd { public static int initialSize = 4; private static Herd singleton; + //allow other classes to access the RNG + public static Random random; private ArrayList herd; private Cow[] parents; @@ -37,6 +40,7 @@ public static Herd getInstance() { if (singleton == null) { + random = new Random(); singleton = new Herd(); } return singleton; @@ -114,4 +118,9 @@ { //TODO load from binary file } + + public void resetHerd() + { + singleton = null; + } } diff --git a/MooBreeder.java b/MooBreeder.java index cbeab2a..fb3e8e2 100755 --- a/MooBreeder.java +++ b/MooBreeder.java @@ -14,7 +14,6 @@ import java.awt.*; import java.awt.event.*; import javax.swing.*; -import java.util.ArrayList; public class MooBreeder extends JFrame { @@ -25,12 +24,9 @@ private Box motherBox, fatherBox, child1Box, child2Box; private JButton add, breed, keep, remove; - private ArrayList herd; - private Cow[] parents; - private Cow[] offspring; - public static void main(String[] args) { + Herd.getInstance(); MooBreeder mb = new MooBreeder(); } @@ -55,9 +51,21 @@ help = new JMenuItem("Help"); about = new JMenuItem("About"); //TODO + reset.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) { + int restart = JOptionPane.showConfirmDialog(null, "Reset the game?", "Reset?", + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE); + if (restart == JOptionPane.OK_OPTION) resetGame(); + } + }); quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - System.exit(0); + int confirm = JOptionPane.showConfirmDialog(null, "Quit MooBreeder?", "Quit?", + JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE); + if(confirm == JOptionPane.YES_OPTION) System.exit(0); } }); quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK)); @@ -90,6 +98,11 @@ this.add(breedBox, BorderLayout.EAST); breedBox.add(parentBox); breedBox.add(offspringBox); + updateGUI(); + } + + private void updateGUI() + { updateHerdBox(); updateParentBox(); updateOffspringBox(); @@ -117,4 +130,10 @@ offspringBox.add(label); } + private void resetGame() + { + Herd.getInstance().resetHerd(); + updateGUI(); + } + } diff --git a/README.md b/README.md index e2597b0..ddf2f06 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # MooBreeder *(c) 2018 Daniel Vedder, Amano Christian School* + *Licensed under the terms of the MIT license.* MooBreeder is a simple cow breeding game, designed to illustrate the