From 4bedf84071bf9a98c79092667007e78c9d3f767f Mon Sep 17 00:00:00 2001 From: jsween5723 Date: Tue, 27 May 2025 13:14:03 +0900 Subject: [PATCH 1/4] refactor: extract method --- .../minesweeper/tobe/MinesweeperGame.java | 272 +++++++++++------- 1 file changed, 169 insertions(+), 103 deletions(-) diff --git a/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java b/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java index dd85c3ce0..4cc89348b 100644 --- a/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java +++ b/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java @@ -9,12 +9,25 @@ public class MinesweeperGame { private static Integer[][] landMineCounts = new Integer[8][10]; private static boolean[][] landMines = new boolean[8][10]; private static int gameStatus = 0; // 0: 게임 중, 1: 승리, -1: 패배 + private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { - System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - System.out.println("지뢰찾기 게임 시작!"); - System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - Scanner scanner = new Scanner(System.in); + initializeBoard(); + gameStart(); + } + + private static void gameStart() { + showStartMessage(); + while (true) { + showBoard(); + if (isGameOver()) break; + System.out.println(); + Coordinates coordinates = getCoordinates(); + getUserActionFor(coordinates); + } + } + + private static void initializeBoard() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 10; j++) { board[i][j] = "□"; @@ -59,106 +72,159 @@ public static void main(String[] args) { landMineCounts[i][j] = 0; } } - while (true) { - System.out.println(" a b c d e f g h i j"); - for (int i = 0; i < 8; i++) { - System.out.printf("%d ", i + 1); - for (int j = 0; j < 10; j++) { - System.out.print(board[i][j] + " "); + } + + + private static void getUserActionFor(Coordinates coordinates) { + String input2 = getUserActionInput(); + if (input2.equals("2")) { + flagOn(coordinates); + } else if (input2.equals("1")) { + openAt(coordinates); + } else { + showInvalidInputError(); + } + } + + private static void showInvalidInputError() { + System.out.println("잘못된 번호를 선택하셨습니다."); + } + + private static String getUserActionInput() { + System.out.println("선택한 셀에 대한 행위를 선택하세요. (1: 오픈, 2: 깃발 꽂기)"); + String input2 = scanner.nextLine(); + return input2; + } + + + private static void openAt(Coordinates coordinates) { + if (landMines[coordinates.row()][coordinates.col()]) { + board[coordinates.row()][coordinates.col()] = "☼"; + gameStatus = -1; + return; + } else { + openAt(coordinates.row(), coordinates.col()); + } + boolean open = true; + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 10; j++) { + if (board[i][j].equals("□")) { + open = false; } - System.out.println(); } - if (gameStatus == 1) { - System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!"); - break; + } + if (open) { + gameStatus = 1; + } + } + + private static void flagOn(Coordinates coordinates) { + board[coordinates.row()][coordinates.col()] = "⚑"; + boolean open = true; + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 10; j++) { + if (board[i][j].equals("□")) { + open = false; + } } - if (gameStatus == -1) { - System.out.println("지뢰를 밟았습니다. GAME OVER!"); + } + if (open) { + gameStatus = 1; + } + } + + private static Coordinates getCoordinates() { + String input = getCoordinatesInput(); + char c = input.charAt(0); + int col; + switch (c) { + case 'a': + col = 0; + break; + case 'b': + col = 1; break; + case 'c': + col = 2; + break; + case 'd': + col = 3; + break; + case 'e': + col = 4; + break; + case 'f': + col = 5; + break; + case 'g': + col = 6; + break; + case 'h': + col = 7; + break; + case 'i': + col = 8; + break; + case 'j': + col = 9; + break; + default: + col = -1; + break; + } + char r = input.charAt(1); + int row = Character.getNumericValue(r) - 1; + Coordinates result = new Coordinates(col, row); + return result; + } + + private static String getCoordinatesInput() { + System.out.println("선택할 좌표를 입력하세요. (예: a1)"); + String input = scanner.nextLine(); + return input; + } + + private record Coordinates(int col, int row) { + } + + private static boolean isGameOver() { + if (gameStatus == 1) { + showGameClearMessage(); + return true; + } + if (gameStatus == -1) { + showGameOverMessage(); + return true; + } + return false; + } + + private static void showGameOverMessage() { + System.out.println("지뢰를 밟았습니다. GAME OVER!"); + } + + private static void showGameClearMessage() { + System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!"); + } + + private static void showBoard() { + System.out.println(" a b c d e f g h i j"); + for (int i = 0; i < 8; i++) { + System.out.printf("%d ", i + 1); + for (int j = 0; j < 10; j++) { + System.out.print(board[i][j] + " "); } System.out.println(); - System.out.println("선택할 좌표를 입력하세요. (예: a1)"); - String input = scanner.nextLine(); - System.out.println("선택한 셀에 대한 행위를 선택하세요. (1: 오픈, 2: 깃발 꽂기)"); - String input2 = scanner.nextLine(); - char c = input.charAt(0); - char r = input.charAt(1); - int col; - switch (c) { - case 'a': - col = 0; - break; - case 'b': - col = 1; - break; - case 'c': - col = 2; - break; - case 'd': - col = 3; - break; - case 'e': - col = 4; - break; - case 'f': - col = 5; - break; - case 'g': - col = 6; - break; - case 'h': - col = 7; - break; - case 'i': - col = 8; - break; - case 'j': - col = 9; - break; - default: - col = -1; - break; - } - int row = Character.getNumericValue(r) - 1; - if (input2.equals("2")) { - board[row][col] = "⚑"; - boolean open = true; - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 10; j++) { - if (board[i][j].equals("□")) { - open = false; - } - } - } - if (open) { - gameStatus = 1; - } - } else if (input2.equals("1")) { - if (landMines[row][col]) { - board[row][col] = "☼"; - gameStatus = -1; - continue; - } else { - open(row, col); - } - boolean open = true; - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 10; j++) { - if (board[i][j].equals("□")) { - open = false; - } - } - } - if (open) { - gameStatus = 1; - } - } else { - System.out.println("잘못된 번호를 선택하셨습니다."); - } } } - private static void open(int row, int col) { + private static void showStartMessage() { + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + System.out.println("지뢰찾기 게임 시작!"); + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + } + + private static void openAt(int row, int col) { if (row < 0 || row >= 8 || col < 0 || col >= 10) { return; } @@ -174,14 +240,14 @@ private static void open(int row, int col) { } else { board[row][col] = "■"; } - open(row - 1, col - 1); - open(row - 1, col); - open(row - 1, col + 1); - open(row, col - 1); - open(row, col + 1); - open(row + 1, col - 1); - open(row + 1, col); - open(row + 1, col + 1); + openAt(row - 1, col - 1); + openAt(row - 1, col); + openAt(row - 1, col + 1); + openAt(row, col - 1); + openAt(row, col + 1); + openAt(row + 1, col - 1); + openAt(row + 1, col); + openAt(row + 1, col + 1); } } From 4794f32f9a3d9a2ada4f9f60beeb2da4ae5389e0 Mon Sep 17 00:00:00 2001 From: jsween5723 Date: Tue, 27 May 2025 14:20:30 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20Cell=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cleancode/minesweeper/tobe/Cell.java | 50 ++++++ .../minesweeper/tobe/MinesweeperGame.java | 149 ++++++------------ 2 files changed, 95 insertions(+), 104 deletions(-) create mode 100644 src/main/java/cleancode/minesweeper/tobe/Cell.java diff --git a/src/main/java/cleancode/minesweeper/tobe/Cell.java b/src/main/java/cleancode/minesweeper/tobe/Cell.java new file mode 100644 index 000000000..f17a33148 --- /dev/null +++ b/src/main/java/cleancode/minesweeper/tobe/Cell.java @@ -0,0 +1,50 @@ +package cleancode.minesweeper.tobe; + +public class Cell { + public static final String CLOSED_CELL_MARK = "□"; + public static final String OPENED_CELL_MARK = "■"; + public static final String LAND_MINE_MARK = "☼"; + public static final String FLAG_MARK = "⚑"; + private boolean isLandMine = false; + private boolean isFlag = false; + private boolean isOpen = false; + private Integer landMineCount = 0; + public void open() { + isOpen = true; + } + public void flag() { + if (isOpen) return; + isFlag = true; + } + + public boolean isLandMine() { + return isLandMine; + } + + public void applyLandMineCount(int count) { + landMineCount = count; + } + + public boolean isChecked() { + return isOpen || isFlag; + } + + @Override + public String toString() { + if (isOpen) { + if (landMineCount == 0) return OPENED_CELL_MARK; + return landMineCount.toString(); + } + if (isFlag) return FLAG_MARK; + if (isLandMine) return LAND_MINE_MARK; + return CLOSED_CELL_MARK; + } + + private Cell(boolean isLandMine) { + this.isLandMine = isLandMine; + } + + public static Cell of(boolean isLandMine) { + return new Cell(isLandMine); + } +} diff --git a/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java b/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java index 4cc89348b..b4c482e6d 100644 --- a/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java +++ b/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java @@ -5,9 +5,11 @@ public class MinesweeperGame { - private static String[][] board = new String[8][10]; - private static Integer[][] landMineCounts = new Integer[8][10]; - private static boolean[][] landMines = new boolean[8][10]; + + public static final int COLUMN_COUNT = 8; + public static final int ROW_COUNT = 10; + public static final int LAND_MINE_COUNT = 10; + private static Cell[][] board = new Cell[COLUMN_COUNT][ROW_COUNT]; private static int gameStatus = 0; // 0: 게임 중, 1: 승리, -1: 패배 private static Scanner scanner = new Scanner(System.in); @@ -28,48 +30,42 @@ private static void gameStart() { } private static void initializeBoard() { - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 10; j++) { - board[i][j] = "□"; - } - } - for (int i = 0; i < 10; i++) { - int col = new Random().nextInt(10); - int row = new Random().nextInt(8); - landMines[row][col] = true; + for (int i = 0; i < LAND_MINE_COUNT; i++) { + int col = new Random().nextInt(ROW_COUNT); + int row = new Random().nextInt(COLUMN_COUNT); + board[row][col] = Cell.of(true); } - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 10; j++) { + for (int col = 0; col < COLUMN_COUNT; col++) { + for (int row = 0; row < ROW_COUNT; row++) { int count = 0; - if (!landMines[i][j]) { - if (i - 1 >= 0 && j - 1 >= 0 && landMines[i - 1][j - 1]) { + Cell cell = board[col][row]; + if (!cell.isLandMine()) { + if (col - 1 >= 0 && row - 1 >= 0 && board[col - 1][row - 1].isLandMine()) { count++; } - if (i - 1 >= 0 && landMines[i - 1][j]) { + if (col - 1 >= 0 && board[col - 1][row].isLandMine()) { count++; } - if (i - 1 >= 0 && j + 1 < 10 && landMines[i - 1][j + 1]) { + if (col - 1 >= 0 && row + 1 < ROW_COUNT && board[col - 1][row + 1].isLandMine()) { count++; } - if (j - 1 >= 0 && landMines[i][j - 1]) { + if (row - 1 >= 0 && board[col][row - 1].isLandMine()) { count++; } - if (j + 1 < 10 && landMines[i][j + 1]) { + if (row + 1 < ROW_COUNT && board[col][row + 1].isLandMine()) { count++; } - if (i + 1 < 8 && j - 1 >= 0 && landMines[i + 1][j - 1]) { + if (col + 1 < COLUMN_COUNT && row - 1 >= 0 && board[col + 1][row - 1].isLandMine()) { count++; } - if (i + 1 < 8 && landMines[i + 1][j]) { + if (col + 1 < COLUMN_COUNT && board[col + 1][row].isLandMine()) { count++; } - if (i + 1 < 8 && j + 1 < 10 && landMines[i + 1][j + 1]) { + if (col + 1 < COLUMN_COUNT && row + 1 < ROW_COUNT && board[col + 1][row + 1].isLandMine()) { count++; } - landMineCounts[i][j] = count; - continue; + cell.applyLandMineCount(count); } - landMineCounts[i][j] = 0; } } } @@ -98,90 +94,46 @@ private static String getUserActionInput() { private static void openAt(Coordinates coordinates) { - if (landMines[coordinates.row()][coordinates.col()]) { - board[coordinates.row()][coordinates.col()] = "☼"; + Cell cell = board[coordinates.row()][coordinates.col()]; + cell.open(); + if (cell.isLandMine()) { gameStatus = -1; return; } else { openAt(coordinates.row(), coordinates.col()); } - boolean open = true; - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 10; j++) { - if (board[i][j].equals("□")) { - open = false; - } - } - } - if (open) { - gameStatus = 1; - } + gameStatus = getWinningStatus(); } private static void flagOn(Coordinates coordinates) { - board[coordinates.row()][coordinates.col()] = "⚑"; - boolean open = true; - for (int i = 0; i < 8; i++) { - for (int j = 0; j < 10; j++) { - if (board[i][j].equals("□")) { - open = false; + Cell cell = board[coordinates.row()][coordinates.col()]; + cell.flag(); + gameStatus = getWinningStatus(); + } + + private static int getWinningStatus() { + for (int col = 0; col < COLUMN_COUNT; col++) { + for (int row = 0; row < ROW_COUNT; row++) { + if (board[col][row].isChecked()) { + return 0; } } } - if (open) { - gameStatus = 1; - } + return 1; } + private static Coordinates getCoordinates() { String input = getCoordinatesInput(); - char c = input.charAt(0); - int col; - switch (c) { - case 'a': - col = 0; - break; - case 'b': - col = 1; - break; - case 'c': - col = 2; - break; - case 'd': - col = 3; - break; - case 'e': - col = 4; - break; - case 'f': - col = 5; - break; - case 'g': - col = 6; - break; - case 'h': - col = 7; - break; - case 'i': - col = 8; - break; - case 'j': - col = 9; - break; - default: - col = -1; - break; - } + int c = input.charAt(0) - 'a'; char r = input.charAt(1); int row = Character.getNumericValue(r) - 1; - Coordinates result = new Coordinates(col, row); - return result; + return new Coordinates(c, row); } private static String getCoordinatesInput() { System.out.println("선택할 좌표를 입력하세요. (예: a1)"); - String input = scanner.nextLine(); - return input; + return scanner.nextLine(); } private record Coordinates(int col, int row) { @@ -209,9 +161,9 @@ private static void showGameClearMessage() { private static void showBoard() { System.out.println(" a b c d e f g h i j"); - for (int i = 0; i < 8; i++) { + for (int i = 0; i < COLUMN_COUNT; i++) { System.out.printf("%d ", i + 1); - for (int j = 0; j < 10; j++) { + for (int j = 0; j < ROW_COUNT; j++) { System.out.print(board[i][j] + " "); } System.out.println(); @@ -225,21 +177,10 @@ private static void showStartMessage() { } private static void openAt(int row, int col) { - if (row < 0 || row >= 8 || col < 0 || col >= 10) { + if (row < 0 || row >= 8 || col < 0 || col >= ROW_COUNT) { return; } - if (!board[row][col].equals("□")) { - return; - } - if (landMines[row][col]) { - return; - } - if (landMineCounts[row][col] != 0) { - board[row][col] = String.valueOf(landMineCounts[row][col]); - return; - } else { - board[row][col] = "■"; - } + board[row][col].open(); openAt(row - 1, col - 1); openAt(row - 1, col); openAt(row - 1, col + 1); From 50fb537d93009daab9efaf712d41c125ee5c9f37 Mon Sep 17 00:00:00 2001 From: jsween5723 Date: Tue, 27 May 2025 14:44:37 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20row=20col=20=ED=98=BC=EC=9A=A9=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cleancode/minesweeper/tobe/Cell.java | 2 +- .../minesweeper/tobe/MinesweeperGame.java | 79 ++++++------------- 2 files changed, 27 insertions(+), 54 deletions(-) diff --git a/src/main/java/cleancode/minesweeper/tobe/Cell.java b/src/main/java/cleancode/minesweeper/tobe/Cell.java index f17a33148..f862631e6 100644 --- a/src/main/java/cleancode/minesweeper/tobe/Cell.java +++ b/src/main/java/cleancode/minesweeper/tobe/Cell.java @@ -32,11 +32,11 @@ public boolean isChecked() { @Override public String toString() { if (isOpen) { + if (isLandMine) return LAND_MINE_MARK; if (landMineCount == 0) return OPENED_CELL_MARK; return landMineCount.toString(); } if (isFlag) return FLAG_MARK; - if (isLandMine) return LAND_MINE_MARK; return CLOSED_CELL_MARK; } diff --git a/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java b/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java index b4c482e6d..74e915929 100644 --- a/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java +++ b/src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java @@ -1,5 +1,8 @@ package cleancode.minesweeper.tobe; +import cleancode.minesweeper.tobe.io.InputHandler; +import cleancode.minesweeper.tobe.io.OutputHandler; + import java.util.Random; import java.util.Scanner; @@ -9,9 +12,11 @@ public class MinesweeperGame { public static final int COLUMN_COUNT = 8; public static final int ROW_COUNT = 10; public static final int LAND_MINE_COUNT = 10; - private static Cell[][] board = new Cell[COLUMN_COUNT][ROW_COUNT]; + private static final InputHandler inputHandler = new InputHandler(); + private static final OutputHandler outputHandler = new OutputHandler(); + private static final Cell[][] board = new Cell[COLUMN_COUNT][ROW_COUNT]; private static int gameStatus = 0; // 0: 게임 중, 1: 승리, -1: 패배 - private static Scanner scanner = new Scanner(System.in); + public static void main(String[] args) { initializeBoard(); @@ -19,9 +24,9 @@ public static void main(String[] args) { } private static void gameStart() { - showStartMessage(); + outputHandler.showStartMessage(); while (true) { - showBoard(); + outputHandler.showBoard(board); if (isGameOver()) break; System.out.println(); Coordinates coordinates = getCoordinates(); @@ -30,10 +35,15 @@ private static void gameStart() { } private static void initializeBoard() { + for (int col = 0; col < COLUMN_COUNT; col++) { + for (int row = 0; row < ROW_COUNT; row++) { + board[col][row] = Cell.of(false); + } + } for (int i = 0; i < LAND_MINE_COUNT; i++) { - int col = new Random().nextInt(ROW_COUNT); - int row = new Random().nextInt(COLUMN_COUNT); - board[row][col] = Cell.of(true); + int col = new Random().nextInt(COLUMN_COUNT); + int row = new Random().nextInt(ROW_COUNT); + board[col][row] = Cell.of(true); } for (int col = 0; col < COLUMN_COUNT; col++) { for (int row = 0; row < ROW_COUNT; row++) { @@ -72,26 +82,16 @@ private static void initializeBoard() { private static void getUserActionFor(Coordinates coordinates) { - String input2 = getUserActionInput(); + String input2 = inputHandler.getUserActionInput(); if (input2.equals("2")) { flagOn(coordinates); } else if (input2.equals("1")) { openAt(coordinates); } else { - showInvalidInputError(); + outputHandler.showInvalidInputError(); } } - private static void showInvalidInputError() { - System.out.println("잘못된 번호를 선택하셨습니다."); - } - - private static String getUserActionInput() { - System.out.println("선택한 셀에 대한 행위를 선택하세요. (1: 오픈, 2: 깃발 꽂기)"); - String input2 = scanner.nextLine(); - return input2; - } - private static void openAt(Coordinates coordinates) { Cell cell = board[coordinates.row()][coordinates.col()]; @@ -124,63 +124,36 @@ private static int getWinningStatus() { private static Coordinates getCoordinates() { - String input = getCoordinatesInput(); + String input = inputHandler.getCoordinatesInput(); int c = input.charAt(0) - 'a'; char r = input.charAt(1); int row = Character.getNumericValue(r) - 1; return new Coordinates(c, row); } - private static String getCoordinatesInput() { - System.out.println("선택할 좌표를 입력하세요. (예: a1)"); - return scanner.nextLine(); - } private record Coordinates(int col, int row) { } private static boolean isGameOver() { if (gameStatus == 1) { - showGameClearMessage(); + outputHandler.showGameClearMessage(); return true; } if (gameStatus == -1) { - showGameOverMessage(); + outputHandler.showGameOverMessage(); return true; } return false; } - private static void showGameOverMessage() { - System.out.println("지뢰를 밟았습니다. GAME OVER!"); - } - - private static void showGameClearMessage() { - System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!"); - } - - private static void showBoard() { - System.out.println(" a b c d e f g h i j"); - for (int i = 0; i < COLUMN_COUNT; i++) { - System.out.printf("%d ", i + 1); - for (int j = 0; j < ROW_COUNT; j++) { - System.out.print(board[i][j] + " "); - } - System.out.println(); - } - } - - private static void showStartMessage() { - System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - System.out.println("지뢰찾기 게임 시작!"); - System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - } - private static void openAt(int row, int col) { - if (row < 0 || row >= 8 || col < 0 || col >= ROW_COUNT) { + if (row < 0 || row >= ROW_COUNT || col < 0 || col >= COLUMN_COUNT) { return; } - board[row][col].open(); + Cell cell = board[col][row]; + if (cell.isLandMine() || cell.isChecked()) return; + cell.open(); openAt(row - 1, col - 1); openAt(row - 1, col); openAt(row - 1, col + 1); From 0e3498dc4b7068de91f9ad2ad0591af9d189ed9f Mon Sep 17 00:00:00 2001 From: jsween5723 Date: Tue, 27 May 2025 14:45:08 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20io=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../minesweeper/tobe/io/InputHandler.java | 16 ++++++++++ .../minesweeper/tobe/io/OutputHandler.java | 30 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/main/java/cleancode/minesweeper/tobe/io/InputHandler.java create mode 100644 src/main/java/cleancode/minesweeper/tobe/io/OutputHandler.java diff --git a/src/main/java/cleancode/minesweeper/tobe/io/InputHandler.java b/src/main/java/cleancode/minesweeper/tobe/io/InputHandler.java new file mode 100644 index 000000000..01a99b7f1 --- /dev/null +++ b/src/main/java/cleancode/minesweeper/tobe/io/InputHandler.java @@ -0,0 +1,16 @@ +package cleancode.minesweeper.tobe.io; + +import java.util.Scanner; + +public class InputHandler { + private Scanner scanner = new Scanner(System.in); + public String getUserActionInput() { + System.out.println("선택한 셀에 대한 행위를 선택하세요. (1: 오픈, 2: 깃발 꽂기)"); + return scanner.nextLine(); + } + + public String getCoordinatesInput() { + System.out.println("선택할 좌표를 입력하세요. (예: a1)"); + return scanner.nextLine(); + } +} diff --git a/src/main/java/cleancode/minesweeper/tobe/io/OutputHandler.java b/src/main/java/cleancode/minesweeper/tobe/io/OutputHandler.java new file mode 100644 index 000000000..2961dd3bd --- /dev/null +++ b/src/main/java/cleancode/minesweeper/tobe/io/OutputHandler.java @@ -0,0 +1,30 @@ +package cleancode.minesweeper.tobe.io; + +import cleancode.minesweeper.tobe.Cell; + +public class OutputHandler { + public void showInvalidInputError() { + System.out.println("잘못된 번호를 선택하셨습니다."); + } + public void showGameOverMessage() { + System.out.println("지뢰를 밟았습니다. GAME OVER!"); + } + public void showGameClearMessage() { + System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!"); + } + public void showStartMessage() { + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + System.out.println("지뢰찾기 게임 시작!"); + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + } + public void showBoard(Cell[][] board) { + System.out.println(" a b c d e f g h i j"); + for (int col = 0; col < board.length; col++) { + System.out.printf("%d ", col + 1); + for (int row = 0; row < board[0].length; row++) { + System.out.print(board[col][row] + " "); + } + System.out.println(); + } + } +}