Advertisement
Dasomeone

Chapter1

Feb 12th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.concurrent.SynchronousQueue;
  3.  
  4. /**
  5.  * Created by nrager on 02/02/16.
  6.  */
  7. public class Chapter1 {
  8.  
  9.   public static void main(String[] args) {
  10.  
  11.     // Declarations
  12.     InputHandler input = new InputHandler();
  13.     TiledMap1 map = new TiledMap1(input);
  14.  
  15.   }
  16.  
  17. }
  18.  
  19. class TiledMap1 {
  20.  
  21.   // Declarations
  22.   InputHandler input;
  23.   MapRender mapRender;
  24.   private boolean[][] map;
  25.   private int WIDTH, HEIGHT, GAPSIZE, HEDGEWIDTH;
  26.   private double passableAreaPercentage;
  27.   private int defaultWIDTH      = 20;
  28.   private int defaultHEIGHT     = 15;
  29.   private int defaultGAPSIZE    = 5;
  30.   private int defaultHEDGEWIDTH = 1;
  31.  
  32.   public TiledMap1(InputHandler input) {
  33.     this.input = input;
  34.  
  35.     // Prompt for sizes
  36.     this.promptSize();
  37.     this.map = new boolean[this.WIDTH][this.HEIGHT];
  38.     this.generateMapContent();
  39.     this.calculatePassableArea();
  40.  
  41.     mapRender = new MapRender(this.map, this.WIDTH, this.HEIGHT, this.passableAreaPercentage);
  42.     mapRender.render();
  43.   }
  44.  
  45.   private void calculatePassableArea() {
  46.     double countPassableArea = 0;
  47.     double totalArea = this.WIDTH * this.HEIGHT;
  48.  
  49.     for (int i = 0; i < this.WIDTH; i++) {
  50.       for (int j = 0; j < this.HEIGHT; j++) {
  51.         if(this.map[i][j] == true) {
  52.           countPassableArea++; // Only count if a tile is passable.
  53.         }
  54.       }
  55.     }
  56.  
  57.     this.passableAreaPercentage = (countPassableArea / totalArea) * 100;
  58.  
  59.   }
  60.  
  61.   private void generateMapContent() {
  62.     int centre = (this.HEIGHT/2);
  63.     for (int i = 0; i < this.WIDTH; i++) {
  64.       for (int j = 0; j < this.HEIGHT; j++) {
  65.         if((j >= centre - (this.GAPSIZE/2) && j < (centre - (this.GAPSIZE/2))+this.GAPSIZE && i >= this.WIDTH-this.HEDGEWIDTH)) {
  66.           // Within hedge gap boundary. Set to true.
  67.           this.map[i][j] = true;
  68.         } else if (j < this.HEDGEWIDTH || ((this.HEIGHT - 1) - j) < this.HEDGEWIDTH) {
  69.           // Currently on either top or bottom on the y-axis, within range of hedge boundary. Set to false.
  70.           this.map[i][j] = false;
  71.         } else if (i < this.HEDGEWIDTH || ((this.WIDTH - 1) - i) < this.HEDGEWIDTH) {
  72.           // Currently on either side on the x-axis, within the hedge boundary. Set to false.
  73.           this.map[i][j] = false;
  74.         } else {
  75.           // Not currently in a hedge area. Set to true.
  76.           this.map[i][j] = true;
  77.         }
  78.       }
  79.     }
  80.   }
  81.  
  82.   private void promptSize() {
  83.  
  84.     // Prompt and get sizes, or set to default.
  85.     // Width
  86.     int temp = this.input.getInput("Please input width of map: ");
  87.     if(temp > 0) {
  88.       // Set width to input.
  89.       this.WIDTH = temp;
  90.     } else {
  91.       // Set to default.
  92.       this.WIDTH = this.defaultWIDTH;
  93.     }
  94.     // Height
  95.     temp = this.input.getInput("Please input height of map: ");
  96.     if(temp > 0) {
  97.       // Set height to input.
  98.       this.HEIGHT = temp;
  99.     } else {
  100.       // Set to default.
  101.       this.HEIGHT = this.defaultHEIGHT;
  102.     }
  103.     // Gapsize
  104.     temp = this.input.getInput("Please input width of gap: ");
  105.     if(temp > 0) {
  106.       // Set GAPSIZE to input.
  107.       this.GAPSIZE = temp;
  108.     } else {
  109.       // Set to default.
  110.       this.GAPSIZE = this.defaultGAPSIZE;
  111.     }
  112.     // HedgeWidth
  113.     temp = this.input.getInput("Please input width of Hedge: ");
  114.     if(temp > 0) {
  115.       // Set hedgewidth to input.
  116.       this.HEDGEWIDTH = temp;
  117.     } else {
  118.       // Set to default.
  119.       this.HEDGEWIDTH = this.defaultHEDGEWIDTH;
  120.     }
  121.  
  122.   }
  123.  
  124. }
  125.  
  126. class InputHandler {
  127.  
  128.   // Declarations of variables and objects
  129.   Scanner sc = new Scanner(System.in);
  130.  
  131.   public InputHandler() {
  132.     // Empty for now
  133.   }
  134.  
  135.   /**
  136.    * getInput method returns the input in response to the provided prompt.
  137.    * @param message Message to prompt the user.
  138.    * @return Returns int value of input.
  139.    */
  140.   public int getInput(String message) {
  141.     int INPUT; // Declare variable to hold input
  142.     System.out.print(message); // Print the message taken
  143.     INPUT = sc.nextInt(); // Store the input
  144.     System.out.println(); // Set console to new line
  145.     return INPUT; // Return the input to calling method.
  146.   }
  147. }
  148.  
  149. class MapRender {
  150.  
  151.   // Declarations
  152.   boolean[][] map;
  153.   int width, height;
  154.   double passableArea;
  155.  
  156.   public MapRender(boolean[][] inputMap, int width, int height, double passableArea) {
  157.     this.map = inputMap;
  158.     this.width = width;
  159.     this.height = height;
  160.     this.passableArea = passableArea;
  161.   }
  162.  
  163.   public void render() {
  164.     System.out.println("Passable Area: " + this.passableArea + "%"); // Print passable area percentage
  165.  
  166.     // Print map
  167.     for (int i = 0; i < this.width; i++) {
  168.       for (int j = 0; j < this.height; j++) {
  169.         // If passable, print . for grass and H for hedge.
  170.         if(this.map[i][j] == true) {
  171.          System.out.print('.');
  172.         } else {
  173.           System.out.print('H');
  174.         }
  175.  
  176.       }
  177.       // New line
  178.       System.out.println();
  179.     }
  180.  
  181.  
  182.   }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement