Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //===============================================================================================================================================================================
- //Assignment 1
- //Nathan Leung
- //March 18, 2019
- //Java, jre1.8.0_152
- //===============================================================================================================================================================================
- //The code reads blobs
- //I - The x and y coordinate of the location of the asterisk
- //O - The original grid with the blobs and the grid after the blob is earased
- //===============================================================================================================================================================================
- /*List of variables
- * LENGTH - a class variable for the constant value of the number of rows in the 2D array (byte)
- * WIDTH - a class variable for the constant value of the the number of columns in the 2D array (byte)
- //===============================================================================================================================================================================
- */
- import java.io.*;
- public class Assignment1 {
- public static final byte LENGTH = 20;
- public static final byte WIDTH = 25;
- /**
- * main method- Procedural method that calls all the static methods and determines whether the user want to enter the program
- * 1. Call method commandCenter and to define anyString to see if they want to enter the program and provide a description of the program
- * 2. Call method blobs to read the file and store into the 2D array
- * 3. Call method column to read the column number of the asterisk that user chooess
- * 4. Call method row
- * @param args
- * @throws IOException
- * @throws InterruptedException
- */
- public static void main(String args[]) throws IOException, InterruptedException{
- char grid[][]= new char[LENGTH][WIDTH];
- byte pointX, pointY;
- boolean flag;
- String anyString;
- anyString= commandCenter();
- while(!anyString.equals("stop")) {
- grid = blobs();
- pointX=column();
- pointY=row();
- flag = eraseBlob(grid, pointY-1,pointX-1);
- anyString = checkFlag(flag,grid);
- }
- System.out.println("Thanks for using Nathan's Program");
- }
- public static String input() throws IOException{
- BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
- String input;
- input=br.readLine();
- return input;
- }
- public static String commandCenter() throws InterruptedException, IOException {
- String word;
- System.out.println("============================================================================================");
- System.out.println("This program will first print out 3 blobs in a 20x25 rectangle.");
- System.out.println("Then the user will be able to input a coordinate on the rectangle to delete a blob");
- System.out.println("If you wish to proceed type [yes]");
- System.out.println("If you do not wish to run the program type [stop]");
- System.out.println("============================================================================================");
- while (true) {
- word=input();
- if (word.equalsIgnoreCase("stop")) {
- return word;
- }
- if (word.equalsIgnoreCase("yes")) {
- System.out.println("Lets begin:");
- System.out.println("Loadng your blob...");
- Thread.sleep(1000);
- return word;
- }
- else {
- System.out.println("Invalid, please try again");
- }
- }
- }
- public static char[][] blobs() throws IOException {
- BufferedReader input=new BufferedReader (new FileReader("D:\\Java\\Blob.txt"));
- String line;
- char grid[][]=new char[LENGTH][WIDTH];
- for(byte row=0;row<LENGTH;row++){
- line=input.readLine();
- for(byte col=0;col<WIDTH;col++){
- grid[row][col]=line.charAt(col);
- }
- }
- gridFinal(grid);
- return grid;
- }
- public static byte column() throws IOException{
- byte x;
- System.out.println("Please input the column number of the point you wish to delete");
- while (true) {
- x = selectPoint();
- if (x<=WIDTH)
- return x;
- else
- System.out.println("The column number is invalid, please try again");
- }
- }
- public static byte row() throws IOException{
- byte y;
- System.out.println("Please input the row number of the point you wish to delete");
- while (true) {
- y = selectPoint();
- if (y<=LENGTH)
- return y;
- else
- System.out.println("The row number is invalid, please try again");
- }
- }
- public static String checkFlag(boolean flag, char[][]grid) throws IOException, InterruptedException{
- String input;
- if(flag==true) {
- System.out.println("One Second, Deleting blob...");
- Thread.sleep(1000);
- gridFinal(grid);
- System.out.println("Your blob has been deleted, enter [yes] to run again, [stop] to end");
- input= input();
- return input;
- }
- else {
- System.out.println("Your point was not an asterick, enter [yes] to continue, [stop] to end");
- input = input();
- return input;
- }
- }
- public static boolean eraseBlob(char grid[][], int y, int x) {
- boolean flag=false;
- if (x>=0&&x<WIDTH&&y>=0&&y<LENGTH) {
- if(grid[y][x]== '*'){
- flag=true;
- grid[y][x]= ' ';
- eraseBlob(grid,y,x+1);
- eraseBlob(grid,y,x-1);
- eraseBlob(grid,y+1,x);
- eraseBlob(grid,y-1,x);
- }
- }
- return flag;
- }
- public static byte selectPoint() throws IOException{
- String temp;
- byte num;
- while (true) {
- try {
- temp = input();
- num = Byte.parseByte(temp);
- break;
- }
- catch (Exception e){
- System.out.println("Input not accpeted, please try again");
- }
- }
- return num;
- }
- public static void gridFinal(char[][]grid){
- System.out.println(" 1234567891111111111222222");
- System.out.println(" 0123456789012345");
- byte rows=1;
- for(byte row=0;row<LENGTH;row++){
- if(rows<10)
- System.out.print(rows+" ");
- else
- System.out.print(rows);
- rows++;
- for(byte col=0;col<WIDTH;col++){
- System.out.print(grid[row][col]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement