// ================================================================================================================================= import java.util.Scanner; import java.util.Random; // ================================================================================================================================= // ================================================================================================================================= public class Minesweeper { // ================================================================================================================================= // ============================================================================================================================= public static Scanner keyboard = new Scanner(System.in); public static final int REVEAL = 1; public static final int FLAG = 2; // ============================================================================================================================= // ============================================================================================================================= public static void main (String[] args) { if (args.length != 2) { showUsageAndExit(); } int numMines = 0; int sideSize = 0; try { numMines = Integer.parseInt(args[0]); sideSize = Integer.parseInt(args[1]); } catch (NumberFormatException e) { showUsageAndExit(); } if ((numMines <= 0) || (sideSize <= 0)) { showUsageAndExit(); } Cell[][] mines = populate(sideSize, numMines); play(mines); } // main () // ============================================================================================================================= // ============================================================================================================================= public static void showUsageAndExit () { System.err.println("USAGE: java Minesweeper "); System.exit(1); } // showUsageAndExit // ============================================================================================================================= // ============================================================================================================================= public static Cell[][] populate (int sideSize, int numMines) { // SET UP THE GRID. } // populate () // ============================================================================================================================= // ============================================================================================================================= public static void play (Cell[][] mines) { // PLAY THE GAME. } // play () // ============================================================================================================================= // ============================================================================================================================= public static void showGrid (Cell[][] mines) { System.out.println(); // Show the column numbers System.out.print("\t"); for (int col = 0; col < mines[0].length; col = col + 1) { System.out.print("\t" + col); } System.out.println(); System.out.print("\t"); for (int col = 0; col < mines[0].length; col = col + 1) { System.out.print("--------"); } System.out.println(); // Show the grid... for (int row = 0; row < mines.length; row = row + 1) { // ...with leading row numbers. System.out.print(row + "\t|\t"); for (int col = 0; col < mines[row].length; col = col + 1) { System.out.print(mines[row][col].toString() + '\t'); } System.out.println(); } System.out.println(); } // showGrid () // ============================================================================================================================= // ============================================================================================================================= public static int[] getCommand (int gridSize) { System.out.print("Enter a move (H for help): "); String[] entry = keyboard.nextLine().toUpperCase().split("\\s+"); if (entry.length != 3 || entry[0].length() != 1) { printMenu(); return getCommand(gridSize); } int op = -1; if (entry[0].charAt(0) == 'R') { op = REVEAL; } else if (entry[0].charAt(0) == 'F') { op = FLAG; } else { printMenu(); return getCommand(gridSize); } int row = -1; int col = -1; try { row = Integer.parseInt(entry[1]); col = Integer.parseInt(entry[2]); } catch (NumberFormatException e) { printMenu(); return getCommand(gridSize); } if (row < 0 || gridSize <= row || col < 0 || gridSize <= col) { printMenu(); return getCommand(gridSize); } return new int[] { op, row, col }; } // ============================================================================================================================= // ============================================================================================================================= public static void printMenu () { System.out.println("Command format: [R|F|H] [row #] [col #]\n" + "\tR = Reveal a cell\n" + "\tF = Flag a cell\n" + "\tH = Help (this info)\n" + "\tExample: R 3 6 (to reveal the cell in row 3, column 6"); } // ============================================================================================================================= // ================================================================================================================================= } // class Minesweeper // =================================================================================================================================