/ assign6 / Grid.java
Grid.java
  1  import java.io.IOException;
  2  import java.lang.System.Logger.Level;
  3  import picocli.CommandLine;
  4  import picocli.CommandLine.Command;
  5  import picocli.CommandLine.Option;
  6  
  7  /// Grid measures - picocli executable
  8  ///
  9  /// SPDX-License-Identifier: GPL-3.0-or-later
 10  /// SPDX-FileCopyrightText: 2024 Jonas Smedegaard <dr@jones.dk>
 11  ///
 12  /// Compute distance from center within a 9x9 grid
 13  /// of the centermost vertically or horisontally aligned objects
 14  /// among 10 randomly placed objects.
 15  ///
 16  /// * v0.0.2
 17  ///   * fix distance calculation
 18  /// * v0.0.1
 19  ///   * initial release, as delivery "assignment 6"
 20  ///
 21  /// @version 0.0.2
 22  /// @since JDK 15
 23  /// @see <https://app.radicle.xyz/nodes/ash.radicle.garden/rad:z3YzABgyz2D36LiKe3YcdJ6PcDCXM/tree/assign6/README.md>
 24  /// @see <https://moodle.ruc.dk/mod/assign/view.php?id=508549>
 25  @Command(name = "Grid",
 26  	version = "Grid 0.0.2-draft",
 27  	mixinStandardHelpOptions = true,
 28  	// TODO: enable when picocli v4.7 is in Debian
 29  	// @see <https://bugs.debian.org/1086095>
 30  	//sortSynopsis = false,
 31  	description = "Compute visibility in a grid.",
 32  	// use heredoc
 33  	// @since JDK 15
 34  	// @see <https://openjdk.org/jeps/378>
 35  	footer = """
 36  
 37  EXAMPLES:
 38    java Grid
 39  """)
 40  public final class Grid implements Runnable {
 41  
 42  	/// Grid constructor
 43  	///
 44  	/// no-op to silence javadoc 23+
 45  	public Grid() {
 46  	}
 47  
 48  	/// JVM entry point
 49  	///
 50  	/// @param args  command-line arguments
 51  	public static void main(final String[] args) {
 52  		int exitCode = new CommandLine(new Grid()).execute(args);
 53  		System.exit(exitCode);
 54  	}
 55  
 56  	/// logger object
 57  	///
 58  	/// @see <https://renato.athaydes.com/posts/java-system-logging>
 59  	private System.Logger logger;
 60  
 61  	/// flag whether to clear screen initially
 62  	@Option(names = { "--clear" },
 63  		description = "try to initially clear the console")
 64  	private boolean clear;
 65  
 66  	public void run() {
 67  		if (clear) {
 68  			clearConsole();
 69  		}
 70  
 71  		// simplify logging format
 72  		// @since JDK 7
 73  		// @see <https://stackoverflow.com/a/34229629/18619283>
 74  		System.setProperty("java.util.logging.SimpleFormatter.format",
 75  			"%5$s%6$s%n");
 76  		logger = System.getLogger("");
 77  
 78  		// Instantiation as dictated by assignment
 79  		final int population = 10;
 80  		final int[] observationpoint = new int[] {5, 5};
 81  		GridCore grid = new GridCore(observationpoint, population);
 82  
 83  		// minimal viable product
 84  		System.out.println(grid.map()
 85  			.replace("\\n", System.lineSeparator()));
 86  		for (GridCore.Bearing bearing: GridCore.Bearing.values()) {
 87  			System.out.printf("Distance %s = %s%n",
 88  				bearing, grid.view(bearing));
 89  		}
 90  	}
 91  
 92  	/// Try to clear the console
 93  	///
 94  	/// @see <https://stackoverflow.com/a/64038023/18619283>
 95  	public static void clearConsole() {
 96  		try {
 97  			if (System.getProperty("os.name").contains("Windows")) {
 98  				new ProcessBuilder("cmd", "/c", "cls")
 99  					.inheritIO().start().waitFor();
100  			} else {
101  				System.out.print("\033\143");
102  				System.out.flush();
103  			}
104  		} catch (IOException | InterruptedException ex) { }
105  	}
106  
107  	/// Environment variable lookup
108  	///
109  	/// @param variable  the environment variable to look up
110  	/// @param fallback  a fallback string if variable is undefined
111  	/// @return          contents of environment variable,
112  	///                  or fallback string
113  	public static String envString(
114  		final String variable,
115  		final String fallback
116  	) {
117  		String s = System.getenv(variable);
118  		if (s != null && s.length() > 0) {
119  			return s;
120  		}
121  		return fallback;
122  	}
123  }