Advertisement
Guest User

Untitled

a guest
Jun 8th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.12 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.util.ArrayList;
  3.  
  4.  
  5. public class Forest {
  6.  
  7.     private static Tree[][] trees;
  8.     private static Lumberjack[][] lumberjacks;
  9.     private static Bear[][] bears;
  10.     private static char[][] forest;
  11.     private static int lumberCollected;
  12.     private static int monthCount;
  13.     private static int lumberMod = -10; //The higher the number, the more of a disadvantage the lumberjacks have
  14.  
  15.     private static int N;
  16.     private static double treePercentage = 50;
  17.     private static double lumberjackPercentage = 10;
  18.     private static double bearPercentage = 2;
  19.  
  20.  
  21.     //S = Sapling
  22.     //T = Tree
  23.     //E = Elder Tree
  24.     //L = Lumberjack
  25.     //B = Bear
  26.     public Forest(int size) {
  27.         this.N = size;
  28.         trees = new Tree[size][size];
  29.         lumberjacks = new Lumberjack[size][size];
  30.         bears = new Bear[size][size];
  31.         forest = spawnForest();
  32.         lumberCollected = 0;
  33.     }
  34.  
  35.     public static void nextMonth(){
  36.         //Spawn a new sapling, 10-20% chance
  37.         spawnSaplings();
  38.         //Check for plants maturing
  39.         maturePlants();
  40.         //Lumberjacks are to cut trees each month
  41.         chopTrees();
  42.         //Check if lumberjacks have enough lumber
  43.         lumberCheck();
  44.         //Bears need to hunt lumberjacks
  45.         huntLumberjacks();
  46.         //Check if bears need killing or spawning
  47.         bearCheck();
  48.     }
  49.  
  50.     private static void spawnSaplings(){
  51.         for(int i = 0; i < N; i++){
  52.             for(int j = 0; j < N; j++){
  53.                 if(trees[i][j] != null){
  54.                     if((int)(Math.random()*100) <= trees[i][j].getSpawnPower()){
  55.                         Point p = getTreeNeighbour(i, j);
  56.                         if(p != null){
  57.                             trees[(int)p.getX()][(int)p.getY()] = new Tree();
  58.                             forest[(int)p.getX()][(int)p.getY()] = trees[(int)p.getX()][(int)p.getY()].getChar();
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65.  
  66.     private static void maturePlants(){
  67.         for(int i = 0; i < N; i++){
  68.             for(int j = 0; j < N; j++){
  69.                 if(trees[i][j] != null){
  70.                     trees[i][j].increaseAge();
  71.                     forest[i][j] = trees[i][j].getChar();
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     private static void chopTrees(){
  78.         int w;
  79.         Point p;
  80.         for(int i = 0; i < N; i++){
  81.             for(int j = 0; j < N; j++){
  82.                 if(lumberjacks[i][j] != null){
  83.                     w = (int)(Math.random()*3)+1;
  84.                     //Lumberjack will wander UP TO 3 times. That means 1, 2 or 3 times.
  85.                     //0 wanders would put the lumberjacks at too much of a disadvantage.
  86.                     //Also assumes that lumberjacks will not even go to where a sapling is. They will just ignore the spot.
  87.                     for(int k = 0; k < w; k++){
  88.                         p = getLumberNeighbour(i, j);
  89.                         if(p != null){
  90.                             if(forest[(int)p.getX()][(int)p.getY()] == 'T' || forest[(int)p.getX()][(int)p.getY()] == 'E'){
  91.                                 lumberjacks[i][j].addLumber(trees[(int)p.getX()][(int)p.getY()].getTreeType());
  92.                                 trees[(int)p.getX()][(int)p.getY()] = null;
  93.                                 lumberjacks[(int)p.getX()][(int)p.getY()] = lumberjacks[i][j];
  94.                                 forest[(int)p.getX()][(int)p.getY()] = 'L';
  95.                                 lumberjacks[i][j] = null;
  96.                                 forest[i][j] = '#';
  97.                                 lumberCollected++;
  98.                                 break;
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     private static void lumberCheck(){
  108.         if(monthCount == 12){
  109.             int lumberjackCount = 0;
  110.             for(int i = 0; i < N; i++){
  111.                 for(int j = 0; j < N; j++){
  112.                     if(lumberjacks[i][j] != null){
  113.                         lumberjackCount++;
  114.                     }
  115.                 }
  116.             }
  117.             if(lumberjackCount != 0){
  118.                 int hireCount = lumberCollected/lumberjackCount;
  119.                 if(lumberCollected-lumberMod > lumberjackCount){
  120.                     ArrayList<Point> lumJacks = new ArrayList<>();
  121.                     //Hire a new lumberjack
  122.                     for(int i = 0; i < N; i++){
  123.                         for(int j = 0; j < N; j++){
  124.                             if(forest[i][j] == '#'){
  125.                                 lumJacks.add(new Point(i, j));
  126.                             }
  127.                         }
  128.                     }
  129.                     for(int i = 0; i < hireCount; i++){
  130.                         Point p = lumJacks.get((int)(Math.random()*lumJacks.size()));
  131.                         forest[(int)(p.getX())][(int)(p.getY())] = 'L';
  132.                         lumberjacks[(int)(p.getX())][(int)(p.getY())] = new Lumberjack();
  133.                     }
  134.  
  135.                 } else{
  136.                     ArrayList<Point> lumJacks = new ArrayList<>();
  137.                     for(int i = 0; i < N; i++){
  138.                         for(int j = 0; j < N; j++){
  139.                             if(forest[i][j] == 'L'){
  140.                                 lumJacks.add(new Point(i, j));
  141.                             }
  142.                         }
  143.                     }
  144.                     try{
  145.                         int randPoint = (int)(Math.random()*lumJacks.size());
  146.                         forest[(int)lumJacks.get(randPoint).getX()][(int)lumJacks.get(randPoint).getY()] = '#';
  147.                         lumberjacks[(int)lumJacks.get(randPoint).getX()][(int)lumJacks.get(randPoint).getY()] = null;
  148.                     } catch(Exception e){
  149.                        
  150.                     }
  151.                    
  152.                 }
  153.             }
  154.  
  155.             monthCount = 0;
  156.             lumberCollected = 0;
  157.         }
  158.         monthCount++;
  159.     }
  160.  
  161.     private static void huntLumberjacks(){
  162.         for(int i = 0; i < N; i++){
  163.             for(int j = 0; j < N; j++){
  164.                 if(forest[i][j] == 'B' && bears[i][j] != null){
  165.                     Point p = getBearNeighbour(i, j);
  166.                     if(getBearNeighbour(i, j) != null){ //Found a lumberjack!
  167.                         System.out.println("Lumberjack found \n");
  168.                         bears[i][j].addMaw();
  169.                         lumberjacks[(int)p.getX()][(int)p.getY()] = null;
  170.                         forest[(int)p.getX()][(int)p.getY()] = 'B';
  171.                         bears[(int)p.getX()][(int)p.getY()] = bears[i][j];
  172.                         bears[i][j] = null;
  173.                     } else{
  174.                         Point pN = getTreeNeighbour(i, j);
  175.                         if(pN != null){
  176.                             //Go here
  177.                             forest[(int)pN.getX()][(int)pN.getY()] = 'B';
  178.                             forest[i][j] = '#';
  179.                             bears[(int)pN.getX()][(int)pN.getY()] = bears[i][j];
  180.                             bears[i][j] = null;
  181.                         }
  182.                     }
  183.                 }
  184.             }
  185.         }
  186.     }
  187.    
  188.     private static void bearCheck(){
  189.         if(monthCount == 12){
  190.             int bearCount = 0;
  191.             for(int i = 0; i < N; i++){
  192.                 for(int j = 0; j < N; j++){
  193.                     if(bears[i][j] != null){
  194.                         bearCount++;
  195.                     }
  196.                 }
  197.             }
  198.             if(bearCount != 0){
  199.                 int mawCount = lumberCollected/bearCount;
  200.                 if(lumberCollected-lumberMod > bearCount){
  201.                     ArrayList<Point> brs = new ArrayList<>();
  202.                     //spawn a new bear
  203.                     for(int i = 0; i < N; i++){
  204.                         for(int j = 0; j < N; j++){
  205.                             if(forest[i][j] == '#'){
  206.                                 brs.add(new Point(i, j));
  207.                             }
  208.                         }
  209.                     }
  210.                     for(int i = 0; i < mawCount; i++){
  211.                         Point p = brs.get((int)(Math.random()*brs.size()));
  212.                         forest[(int)(p.getX())][(int)(p.getY())] = 'L';
  213.                         bears[(int)(p.getX())][(int)(p.getY())] = new Bear();
  214.                     }
  215.  
  216.                 } else{
  217.                     ArrayList<Point> brs = new ArrayList<>();
  218.                     for(int i = 0; i < N; i++){
  219.                         for(int j = 0; j < N; j++){
  220.                             if(forest[i][j] == 'L'){
  221.                                 brs.add(new Point(i, j));
  222.                             }
  223.                         }
  224.                     }
  225.                     int randPoint = (int)(Math.random()*brs.size());
  226.                     forest[(int)brs.get(randPoint).getX()][(int)brs.get(randPoint).getY()] = '#';
  227.                     bears[(int)brs.get(randPoint).getX()][(int)brs.get(randPoint).getY()] = null;
  228.                 }
  229.             }
  230.  
  231.             monthCount = 0;
  232.             lumberCollected = 0;
  233.         }
  234.         monthCount++;
  235.     }
  236.    
  237.     private static Point getBearNeighbour(int row, int col){
  238.         ArrayList<Point> list = new ArrayList<>();
  239.         try {
  240.             for (int rowMod = -1; rowMod <= 1; rowMod++ ) {
  241.                 for (int colMod = -1; colMod <= 1; colMod++) {
  242.                     if (forest[row+rowMod][col+colMod] == 'L') {
  243.                         list.add(new Point(row+rowMod, col+colMod));
  244.                     }
  245.                 }
  246.             }
  247.         } catch ( ArrayIndexOutOfBoundsException e ) {
  248.  
  249.         }
  250.         if(list.size() == 0)    return null;
  251.         return list.get((int)(Math.random()*list.size()));
  252.     }
  253.  
  254.     private static Point getTreeNeighbour(int row, int col){
  255.         ArrayList<Point> list = new ArrayList<>();
  256.         try {
  257.             for (int rowMod = -1; rowMod <= 1; rowMod++ ) {
  258.                 for (int colMod = -1; colMod <= 1; colMod++) {
  259.                     if (forest[row+rowMod][col+colMod] == '#') {
  260.                         list.add(new Point(row+rowMod, col+colMod));
  261.                     }
  262.                 }
  263.             }
  264.         } catch ( ArrayIndexOutOfBoundsException e ) {
  265.  
  266.         }
  267.         if(list.size() == 0)    return null;
  268.         return list.get((int)(Math.random()*list.size()));
  269.     }
  270.  
  271.     private static Point getLumberNeighbour(int row, int col){
  272.         ArrayList<Point> list = new ArrayList<>();
  273.         try {
  274.             for (int rowMod = -1; rowMod <= 1; rowMod++ ) {
  275.                 for (int colMod = -1; colMod <= 1; colMod++) {
  276.                     if (forest[row+rowMod][col+colMod] == 'T' || forest[row+rowMod][col+colMod] == 'E') {
  277.                         list.add(new Point(row+rowMod, col+colMod));
  278.                     }
  279.                 }
  280.             }
  281.         } catch ( ArrayIndexOutOfBoundsException e ) {
  282.  
  283.         }
  284.         if(list.size() == 0)    return null;
  285.         return list.get((int)(Math.random()*list.size()));
  286.     }
  287.  
  288.     private static char[][] spawnForest(){
  289.         char[][] tempForest = new char[N][N];
  290.         int t = (int)(((N*N)/100)*treePercentage), l = (int)(((N*N)/100)*lumberjackPercentage), b = (int)(((N*N)/100)*bearPercentage);
  291.  
  292.         for(int i = 0; i < N; i++){
  293.             for(int j = 0; j < N; j++){
  294.                 if(t > 0){
  295.                     tempForest[i][j] = 'S';
  296.                     trees[i][j] = new Tree();
  297.                     t--;
  298.                 } else if(l > 0){
  299.                     tempForest[i][j] = 'L';
  300.                     lumberjacks[i][j] = new Lumberjack();
  301.                     l--;
  302.                 } else if(b > 0){
  303.                     tempForest[i][j] = 'B';
  304.                     bears[i][j] = new Bear();
  305.                     b--;
  306.                 } else{
  307.                     tempForest[i][j] = '#';
  308.                 }
  309.             }
  310.         }
  311.  
  312.         //Shuffle the contents of the forest
  313.         char temp;
  314.         for(int i = 0; i < N; i++){
  315.             for(int j = 0; j < N; j++){
  316.                 int m = (int)(Math.random()*N), n = (int)(Math.random()*N);
  317.                 temp = tempForest[i][j];
  318.                 tempForest[i][j] = tempForest[m][n];
  319.                 tempForest[m][n] = temp;
  320.             }
  321.         }
  322.         return tempForest;
  323.     }
  324.  
  325.     public char getTileAt(int i, int j){
  326.         return forest[i][j];
  327.     }
  328.  
  329.     public int getTreeCount(){
  330.         return 1;
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement