Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Random;
- public class main {
- static int bestMaleBull = 10;
- public static void main(String[]args){
- System.out.println("Breeding cows..");
- ArrayList<Cow> cows = new ArrayList<Cow>();
- Random r = new Random();
- //add initial male cow
- cows.add(new Cow(10,true));
- //add initial female cows
- for (int i = 0; i < 5; i++) {
- cows.add(new Cow(10,false));
- }
- //variables
- int x = 9; //Number of generations
- int y = 20; //Size of cow herd
- for (int i = 0; i < x; i++) {
- //find best male bull
- for (int j = 0; j < cows.size(); j++) {
- if (cows.get(j).male && cows.get(j).quality > bestMaleBull) {
- bestMaleBull = cows.get(j).quality;
- }
- }
- //breed all cows
- for (int j = 0; j < cows.size(); j++) {
- if (!cows.get(j).male) {
- float quality = (bestMaleBull+cows.get(j).quality)/2 + r.nextInt(11)-5;
- cows.add(new Cow((int) quality,r.nextBoolean()));
- }
- }
- //kill bad bulls
- for (int j = 0; j < cows.size(); j++) {
- if (cows.get(j).male && cows.get(j).quality < bestMaleBull) {
- cows.remove(j);
- }
- }
- Collections.sort(cows, new Comparator<Cow>() {
- @Override
- public int compare(Cow o1, Cow o2) {
- return o1.quality.compareTo(o2.quality);
- }
- });
- //kill cows if herd is above y
- while (cows.size() > y) {
- cows.remove(0);
- }
- }
- System.out.println("Cow herd:");
- System.out.println("Best Bull = " + bestMaleBull);
- for (int i = 0; i < cows.size(); i++) {
- System.out.println("Cow #" + i + ": quality = " + cows.get(i).quality);
- }
- }
- }
- public class Cow {
- public Integer quality;
- public boolean male;
- public Cow(int quality, boolean male) {
- this.quality = quality;
- this.male = male;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement