Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.concurrent.SynchronousQueue;
- /**
- * Created by nrager on 02/02/16.
- */
- public class Chapter1 {
- public static void main(String[] args) {
- // Declarations
- InputHandler input = new InputHandler();
- TiledMap1 map = new TiledMap1(input);
- }
- }
- class TiledMap1 {
- // Declarations
- InputHandler input;
- MapRender mapRender;
- private boolean[][] map;
- private int WIDTH, HEIGHT, GAPSIZE, HEDGEWIDTH;
- private double passableAreaPercentage;
- private int defaultWIDTH = 20;
- private int defaultHEIGHT = 15;
- private int defaultGAPSIZE = 5;
- private int defaultHEDGEWIDTH = 1;
- public TiledMap1(InputHandler input) {
- this.input = input;
- // Prompt for sizes
- this.promptSize();
- this.map = new boolean[this.WIDTH][this.HEIGHT];
- this.generateMapContent();
- this.calculatePassableArea();
- mapRender = new MapRender(this.map, this.WIDTH, this.HEIGHT, this.passableAreaPercentage);
- mapRender.render();
- }
- private void calculatePassableArea() {
- double countPassableArea = 0;
- double totalArea = this.WIDTH * this.HEIGHT;
- for (int i = 0; i < this.WIDTH; i++) {
- for (int j = 0; j < this.HEIGHT; j++) {
- if(this.map[i][j] == true) {
- countPassableArea++; // Only count if a tile is passable.
- }
- }
- }
- this.passableAreaPercentage = (countPassableArea / totalArea) * 100;
- }
- private void generateMapContent() {
- int centre = (this.HEIGHT/2);
- for (int i = 0; i < this.WIDTH; i++) {
- for (int j = 0; j < this.HEIGHT; j++) {
- if((j >= centre - (this.GAPSIZE/2) && j < (centre - (this.GAPSIZE/2))+this.GAPSIZE && i >= this.WIDTH-this.HEDGEWIDTH)) {
- // Within hedge gap boundary. Set to true.
- this.map[i][j] = true;
- } else if (j < this.HEDGEWIDTH || ((this.HEIGHT - 1) - j) < this.HEDGEWIDTH) {
- // Currently on either top or bottom on the y-axis, within range of hedge boundary. Set to false.
- this.map[i][j] = false;
- } else if (i < this.HEDGEWIDTH || ((this.WIDTH - 1) - i) < this.HEDGEWIDTH) {
- // Currently on either side on the x-axis, within the hedge boundary. Set to false.
- this.map[i][j] = false;
- } else {
- // Not currently in a hedge area. Set to true.
- this.map[i][j] = true;
- }
- }
- }
- }
- private void promptSize() {
- // Prompt and get sizes, or set to default.
- // Width
- int temp = this.input.getInput("Please input width of map: ");
- if(temp > 0) {
- // Set width to input.
- this.WIDTH = temp;
- } else {
- // Set to default.
- this.WIDTH = this.defaultWIDTH;
- }
- // Height
- temp = this.input.getInput("Please input height of map: ");
- if(temp > 0) {
- // Set height to input.
- this.HEIGHT = temp;
- } else {
- // Set to default.
- this.HEIGHT = this.defaultHEIGHT;
- }
- // Gapsize
- temp = this.input.getInput("Please input width of gap: ");
- if(temp > 0) {
- // Set GAPSIZE to input.
- this.GAPSIZE = temp;
- } else {
- // Set to default.
- this.GAPSIZE = this.defaultGAPSIZE;
- }
- // HedgeWidth
- temp = this.input.getInput("Please input width of Hedge: ");
- if(temp > 0) {
- // Set hedgewidth to input.
- this.HEDGEWIDTH = temp;
- } else {
- // Set to default.
- this.HEDGEWIDTH = this.defaultHEDGEWIDTH;
- }
- }
- }
- class InputHandler {
- // Declarations of variables and objects
- Scanner sc = new Scanner(System.in);
- public InputHandler() {
- // Empty for now
- }
- /**
- * getInput method returns the input in response to the provided prompt.
- * @param message Message to prompt the user.
- * @return Returns int value of input.
- */
- public int getInput(String message) {
- int INPUT; // Declare variable to hold input
- System.out.print(message); // Print the message taken
- INPUT = sc.nextInt(); // Store the input
- System.out.println(); // Set console to new line
- return INPUT; // Return the input to calling method.
- }
- }
- class MapRender {
- // Declarations
- boolean[][] map;
- int width, height;
- double passableArea;
- public MapRender(boolean[][] inputMap, int width, int height, double passableArea) {
- this.map = inputMap;
- this.width = width;
- this.height = height;
- this.passableArea = passableArea;
- }
- public void render() {
- System.out.println("Passable Area: " + this.passableArea + "%"); // Print passable area percentage
- // Print map
- for (int i = 0; i < this.width; i++) {
- for (int j = 0; j < this.height; j++) {
- // If passable, print . for grass and H for hedge.
- if(this.map[i][j] == true) {
- System.out.print('.');
- } else {
- System.out.print('H');
- }
- }
- // New line
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement