Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <head>
  2.     <script>
  3.         trees = 0;
  4.         function treePlanter(){
  5.             n = Math.random()*100;
  6.             if (treeRatio>n){
  7.                 return true;
  8.             }else{
  9.                 return false;
  10.             }
  11.         }
  12.         function lumberjackPlanter(){  
  13.             n = Math.random()*100;
  14.             if (lumberjackRatio*100>n){
  15.                 return true;
  16.             }else{
  17.                 return false;
  18.             }
  19.         }
  20.         function mapBuilder(dimension){
  21.             map = [];
  22.             for (i=0;i<dimension;i++){
  23.                 map[i] = [];
  24.                 for(j=0;j<dimension;j++){
  25.                     map[i][j] = treePlanter() ? {type: "T", age: 0}:{type: "G", age: 0};
  26.                     if (map[i][j].type!="T"){
  27.                         map[i][j] = lumberjackPlanter() ? {type: "L", age: 0}:{type: "G", age: 0};
  28.                     }
  29.                 }
  30.             }
  31.             return map;
  32.         }
  33.         function lumberAI(xCord, yCord){
  34.             for(i=1;i<dimension;i++){
  35.                 var maxX = (xCord+i>dimension-1) ? dimension-1:xCord+i;
  36.                 var maxY = (yCord+i>dimension-1) ? dimension-1:yCord+i;
  37.                 var minX = (xCord-i<0) ? 0:xCord-i;
  38.                 var minY = (yCord-i<0) ? 0:yCord-i;
  39.                 var treeLocations = [];
  40.                
  41.                 for(;minY-1<maxY;minY++){
  42.                     for(innerX = minX;innerX-1<maxX;innerX++){
  43.                         if(map[minY][innerX].type=="T"){
  44.                             treeLocations.push({x:innerX, y:minY});
  45.                         }
  46.                     }
  47.                 }
  48.                
  49.                 if(treeLocations.length!=0){
  50.                     var location = treeLocations[Math.floor(Math.random()*(treeLocations.length))];
  51.                     if(location.x!=xCord){
  52.                         newX = (location.x>xCord) ? newX = xCord+1:newX = xCord-1;
  53.                     }else{
  54.                         newX = xCord;
  55.                     }
  56.                     if(location.y!=yCord){
  57.                         newY = (location.y>yCord) ? newY = yCord+1:newY = yCord-1;
  58.                     }else{
  59.                         newY = yCord;
  60.                     }
  61.                     return {x:newX, y:newY};
  62.                 }
  63.             }
  64.         }
  65.  
  66.         function createTable(tableData) {
  67.             var table = document.createElement('table');
  68.             var tableBody = document.createElement('tbody');
  69.             table.id = "gameBody";
  70.             tableData.forEach(function(rowData) {
  71.                 var row = document.createElement('tr');
  72.                 rowData.forEach(function(cellData) {
  73.                 var cell = document.createElement('td');
  74.                 cell.style.width = '10px';
  75.                 cell.style.height = '10px';
  76.  
  77.                 switch(cellData.type){
  78.                     case "G":
  79.                         cell.bgColor = '#00FF00';
  80.                         break;
  81.                     case "T":
  82.                         cell.bgColor = '#004d00';
  83.                         break;
  84.                     case "L":
  85.                         cell.bgColor = '#4d3900';
  86.                 }
  87.                 cell.appendChild(document.createTextNode(''));
  88.                 row.appendChild(cell);
  89.                 });
  90.  
  91.             tableBody.appendChild(row);
  92.             });
  93.             table.appendChild(tableBody);
  94.             document.body.appendChild(table);
  95.         }
  96.         function updateTable(tableData) {
  97.             currentTrees = 0;
  98.             currentLumberjacks = 0;
  99.             treeRatio = trees<treeRatio*dimension*dimension;
  100.            
  101.             console.log(treePlanter());
  102.             console.log(lumberjackPlanter());
  103.             for(var i=0;i<dimension;i++){
  104.                 var row = document.getElementById("gameBody").rows[i].cells;
  105.                 for(var j=0;j<dimension;j++){
  106.                         cell = row[j];
  107.                         cellData = tableData[i][j];
  108.  
  109.                         switch(cellData.type){
  110.                             case "G":
  111.                                 if(treeRatio && treePlanter()){
  112.                                     cell.bgColor = '#004d00';
  113.                                     cellData.type = "T";
  114.                                     cellData.age = 0;
  115.                                     currentTrees++;
  116.                                 }else{
  117.                                     cell.bgColor = '#00FF00';
  118.                                 }
  119.                                 break;
  120.                             case "T":
  121.                                 if(cellData.age>treeAge){
  122.                                     cell.bgColor = '#00FF00';
  123.                                     cellData.type = "G";
  124.                                     cellData.age = 0;
  125.                                 }else{
  126.                                     cell.bgColor = '#004d00';
  127.                                     currentTrees++;
  128.                                 }
  129.                                 break;
  130.                             case "L":
  131.                                 nextPosition = lumberAI(j,i);
  132.                                 if(tableData[nextPosition.y][nextPosition.x].type=="G"){
  133.                                     row[nextPosition.x].bgColor = '#ff0000';
  134.                                     tableData[nextPosition.y][nextPosition.x].type="L";
  135.                                     tableData[nextPosition.y][nextPosition.x].age = cellData.age;
  136.                                     cell.bgColor = '#00FF00';
  137.                                     cellData.type = "G";
  138.                                     cellData.age = 0;
  139.                                 }else if (tableData[nextPosition.y][nextPosition.x].type=="T"){
  140.                                     row[nextPosition.x].bgColor = '#ff0000';
  141.                                     tableData[nextPosition.y][nextPosition.x].type="L";
  142.                                     tableData[nextPosition.y][nextPosition.x].age = cellData.age;
  143.                                     cell.bgColor = '#00FF00';
  144.                                     cellData.type = "G";
  145.                                     cellData.age = 0;
  146.                                     currentTrees--;
  147.                                 }else{
  148.                                     cell.bgColor = '#ff0000';
  149.                                 }
  150.                                 break;
  151.                         }
  152.                         cellData.age++;
  153.                 }
  154.             }
  155.             trees = currentTrees;
  156.         }
  157.         function mainLoop(){
  158.             updateTable(board);
  159.             requestAnimationFrame(mainLoop);
  160.         }
  161.     </script>
  162. </head>
  163. <body>
  164. <script>
  165. dimension = 100;
  166. treeRatio = 0.001;
  167. lumberjackRatio = 0.001;
  168. treeAge = 100000;
  169. board = mapBuilder(dimension); 
  170. createTable(board);
  171. mainLoop();
  172. </script>
  173. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement