diff --git a/Cow.java b/Cow.java index 2737e4f..db55ca3 100755 --- a/Cow.java +++ b/Cow.java @@ -150,6 +150,7 @@ private int combineTrait(int fathertrait, int mothertrait) { + //FIXME This should have a higher variance int newtrait = (int) Math.round((fathertrait+mothertrait)/2); int quartertrait = (int) Math.round(newtrait*0.25); int randomfactor = Herd.randomInt(quartertrait*2) - quartertrait; diff --git a/Cow.java b/Cow.java index 2737e4f..db55ca3 100755 --- a/Cow.java +++ b/Cow.java @@ -150,6 +150,7 @@ private int combineTrait(int fathertrait, int mothertrait) { + //FIXME This should have a higher variance int newtrait = (int) Math.round((fathertrait+mothertrait)/2); int quartertrait = (int) Math.round(newtrait*0.25); int randomfactor = Herd.randomInt(quartertrait*2) - quartertrait; diff --git a/Herd.java b/Herd.java index 4fdb867..7234325 100755 --- a/Herd.java +++ b/Herd.java @@ -46,6 +46,7 @@ public static int randomInt(int max) { if (random == null) random = new Random(); + if (max <= 0) max = 1; return random.nextInt(max); } @@ -119,7 +120,8 @@ return "The age difference between the parents is too great!"; //check for siblings if (parents[0].getParents()[0] == parents[1].getParents()[0] && - parents[0].getParents()[1] == parents[1].getParents()[1]) + parents[0].getParents()[1] == parents[1].getParents()[1] && + !(parents[0].getParents()[0] == 0 || parents[0].getParents()[1] == 0)) return "You cannot let siblings mate!"; return "OK"; } diff --git a/Cow.java b/Cow.java index 2737e4f..db55ca3 100755 --- a/Cow.java +++ b/Cow.java @@ -150,6 +150,7 @@ private int combineTrait(int fathertrait, int mothertrait) { + //FIXME This should have a higher variance int newtrait = (int) Math.round((fathertrait+mothertrait)/2); int quartertrait = (int) Math.round(newtrait*0.25); int randomfactor = Herd.randomInt(quartertrait*2) - quartertrait; diff --git a/Herd.java b/Herd.java index 4fdb867..7234325 100755 --- a/Herd.java +++ b/Herd.java @@ -46,6 +46,7 @@ public static int randomInt(int max) { if (random == null) random = new Random(); + if (max <= 0) max = 1; return random.nextInt(max); } @@ -119,7 +120,8 @@ return "The age difference between the parents is too great!"; //check for siblings if (parents[0].getParents()[0] == parents[1].getParents()[0] && - parents[0].getParents()[1] == parents[1].getParents()[1]) + parents[0].getParents()[1] == parents[1].getParents()[1] && + !(parents[0].getParents()[0] == 0 || parents[0].getParents()[1] == 0)) return "You cannot let siblings mate!"; return "OK"; } diff --git a/MooBreeder.java b/MooBreeder.java index 3efab35..c823a5f 100755 --- a/MooBreeder.java +++ b/MooBreeder.java @@ -31,7 +31,7 @@ private JPanel breedPanel; private JScrollPane herdScroller; private Box herdBox, motherBox, fatherBox, calf1Box, calf2Box; - private JButton breed; + private JButton breedButton; private JFileChooser fileChooser; //TODO Insert the "breed" button! @@ -155,14 +155,15 @@ ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); herdScroller.setWheelScrollingEnabled(true); herdBorderPanel.add(herdScroller, BorderLayout.CENTER); + //Take care of the layout herdBorderPanel.add(Box.createVerticalStrut(25), BorderLayout.NORTH); herdBorderPanel.add(Box.createVerticalStrut(25), BorderLayout.SOUTH); herdBorderPanel.add(Box.createHorizontalStrut(25), BorderLayout.EAST); herdBorderPanel.add(Box.createHorizontalStrut(25), BorderLayout.WEST); this.add(herdBorderPanel); //Create the right half of the window with the breeding interface - //TODO - breed button JPanel breedBorderPanel = new JPanel(new BorderLayout()); + Box breedBox = new Box(BoxLayout.Y_AXIS); breedPanel = new JPanel(new GridLayout(2,2)); fatherBox = new Box(BoxLayout.Y_AXIS); motherBox = new Box(BoxLayout.Y_AXIS); @@ -172,11 +173,21 @@ breedPanel.add(motherBox); breedPanel.add(calf1Box); breedPanel.add(calf2Box); - breedBorderPanel.add(breedPanel, BorderLayout.CENTER); + breedBox.add(breedPanel); + //Add the breed button + breedButton = new JButton("Breed"); + breedButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + breedAction(); + } + }); + breedBox.add(Box.createVerticalStrut(10)); + breedBox.add(breedButton); + //Take care of the layout + breedBorderPanel.add(breedBox, BorderLayout.CENTER); breedBorderPanel.add(Box.createVerticalStrut(25), BorderLayout.NORTH); - breedBorderPanel.add(Box.createVerticalStrut(25), BorderLayout.SOUTH); breedBorderPanel.add(Box.createHorizontalStrut(25), BorderLayout.EAST); - breedBorderPanel.add(breedPanel); + breedBorderPanel.add(Box.createVerticalStrut(25), BorderLayout.SOUTH); this.add(breedBorderPanel); updateGUI(); } @@ -242,12 +253,6 @@ calf2Box.validate(); } - private void resetGame() - { - Herd.getInstance().resetHerd(); - updateGUI(); //FIXME ScrollPane disappears? - only until window is resized - } - private Box getCowBox(Cow c) { Box cowBox = new Box(BoxLayout.Y_AXIS); @@ -287,6 +292,25 @@ return cowBox; } + private void breedAction() + { + String possible = Herd.getInstance().checkBreeding(); + if (possible == "OK") { + Herd.getInstance().breed(); + updateGUI(); + } + else { + JOptionPane.showMessageDialog(null, possible, "Breeding failed", + JOptionPane.WARNING_MESSAGE); + } + } + + private void resetGame() + { + Herd.getInstance().resetHerd(); + updateGUI(); //FIXME ScrollPane disappears? - only until window is resized + } + /* * MouseListener methods *