Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Readable Code
고정석인 경우 추가금을 내면 사물함 이용 가능.
선택한 이용권과 사물함 여부에 따른 최종 금액을 계산해준다.
선택한 이용권이 2주권 이상일 경우 10%, 12주권 이상일 경우 15% 할인을 진행한다
310 changes: 184 additions & 126 deletions src/main/java/cleancode/minesweeper/tobe/MinesweeperGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,175 +5,233 @@

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 BOARD_ROW_SIZE = 8;
public static final int BOARD_COLUMN_SIZE = 8;
public static final int LAND_MINE_COUNT = 10;
public static final String FLAG_SIGN = "⚑";
public static final String LAND_MINE_SIGN = "☼";

private static final String[][] BOARD = new String[BOARD_ROW_SIZE][BOARD_COLUMN_SIZE];
private static final Integer[][] NEARBY_LAND_MINE_COUNTS = new Integer[BOARD_ROW_SIZE][BOARD_COLUMN_SIZE];
private static final boolean[][] LAND_MINES = new boolean[BOARD_ROW_SIZE][BOARD_COLUMN_SIZE];

public static final String CLOSED_CELL_SIGN = "□";
public static final String OPENED_CELL_SIGN = "■";

// 얘는 ENUM이야
private static int gameStatus = 0; // 0: 게임 중, 1: 승리, -1: 패배

public static void main(String[] args) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("지뢰찾기 게임 시작!");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
showGameStartComments();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
board[i][j] = "□";
initializeGame();
while (true) {
showBoard();
// 게임이 이기는 건데, 나같으면 메서드로 추출을 하거나 gamestatus == WIN 뭐 이런식으로
if (doesUserWinTheGame()) {
System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!");
break;
}
if (doesUserLoseTheGame()) {
System.out.println("지뢰를 밟았습니다. GAME OVER!");
break;
}
String cellInput = getCellInputFromUser(scanner);
String userActionInput = getUserActionInputUser(scanner);

int selectedColIndex = getSelectedColIndex(cellInput);
int selectedRowIndex = getSelectedRowIndex(cellInput);

if (doesUserChooseToPlantFlag(userActionInput)) {
BOARD[selectedRowIndex][selectedColIndex] = FLAG_SIGN;
checkIfAllCellIsOpened();
} else if (doesUserChooseToOpenCell(userActionInput)) {
if (LAND_MINES[selectedRowIndex][selectedColIndex]) {
BOARD[selectedRowIndex][selectedColIndex] = LAND_MINE_SIGN;
changeGameStatusToLost();
continue; // 여기더 early return
} else {
open(selectedRowIndex, selectedColIndex);
}
checkIfAllCellIsOpened();
} else {
System.out.println("잘못된 번호를 선택하셨습니다.");
}
}
}

private static void changeGameStatusToLost() {
gameStatus = -1;
}

private static boolean doesUserChooseToOpenCell(String userActionInput) {
return userActionInput.equals("1");
}

private static boolean doesUserChooseToPlantFlag(String userActionInput) {
return userActionInput.equals("2");
}

private static int getSelectedRowIndex(String cellInput) {
char cellInputRow = cellInput.charAt(1);
return convertRowFrom(cellInputRow);
}

private static int getSelectedColIndex(String cellInput) {
char cellInputCol = cellInput.charAt(0);
return convertColFrom(cellInputCol);
}

private static String getUserActionInputUser(Scanner scanner) {
System.out.println("선택한 셀에 대한 행위를 선택하세요. (1: 오픈, 2: 깃발 꽂기)");
return scanner.nextLine();
}

private static String getCellInputFromUser(Scanner scanner) {
System.out.println();
System.out.println("선택할 좌표를 입력하세요. (예: a1)");
return getUserActionInputUser(scanner);
}

private static boolean doesUserLoseTheGame() {
return gameStatus == -1;
}

private static boolean doesUserWinTheGame() {
return gameStatus == 1;
}

private static void checkIfAllCellIsOpened() {
boolean isAllOpened = isAllOpened();
if (isAllOpened) {
changeGameStatusToWin();
}
}

private static void changeGameStatusToWin() {
gameStatus = 1;
}

private static boolean isAllOpened() {
boolean isAllOpend = true;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 10; col++) {
if (BOARD[row][col].equals(CLOSED_CELL_SIGN)) {
isAllOpend = false;
}
}
}
return isAllOpend;
}

private static int convertRowFrom(char cellInputRow) {
return Character.getNumericValue(cellInputRow) - 1;
}

private static int convertColFrom(char cellInputCol) {
return switch (cellInputCol) {
case 'a' ->
// selectedColIndex = 0;
// break;
0;
case 'b' -> 1;
case 'c' -> 2;
case 'd' -> 3;
case 'e' -> 4;
case 'f' -> 5;
case 'g' -> 6;
case 'h' -> 7;
case 'i' -> 8;
case 'j' -> 9;
default -> -1;
};// defualt에서 에러를 던져주는 것으로 바꿔줨
}

private static void showBoard() {
System.out.println(" a b c d e f g h i j");
for (int row = 0; row < 8; row++) {
System.out.printf("%d ", row + 1);
for (int col = 0; col < 10; col++) {
System.out.print(BOARD[row][col] + " ");
}
System.out.println();
}
for (int i = 0; i < 10; i++) {
}

private static void initializeGame() {
for (int row = 0; row < BOARD_ROW_SIZE; row++) {
for (int col = 0; col < BOARD_COLUMN_SIZE; col++) {
BOARD[row][col] = CLOSED_CELL_SIGN;
}
}
// for문의 row, col로 변경, 그리고 밑에 메서드명 지뢰심기?
for (int i = 0; i < LAND_MINE_COUNT; i++) {
int col = new Random().nextInt(10);
int row = new Random().nextInt(8);
landMines[row][col] = true;
LAND_MINES[row][col] = true;
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {

for (int row = 0; row < 8; row++) {
for (int col = 0; col < 10; col++) {
int count = 0;
if (!landMines[i][j]) {
if (i - 1 >= 0 && j - 1 >= 0 && landMines[i - 1][j - 1]) {
if (!LAND_MINES[row][col]) {
// 내 위치 기준으로 왼쪽 대각선위로 지뢰가 있다면 count 증가
if (row - 1 >= 0 && col - 1 >= 0 && LAND_MINES[row - 1][col - 1]) {
count++;
}
if (i - 1 >= 0 && landMines[i - 1][j]) {
if (row - 1 >= 0 && LAND_MINES[row - 1][col]) {
count++;
}
if (i - 1 >= 0 && j + 1 < 10 && landMines[i - 1][j + 1]) {
if (row - 1 >= 0 && col + 1 < 10 && LAND_MINES[row - 1][col + 1]) {
count++;
}
if (j - 1 >= 0 && landMines[i][j - 1]) {
if (col - 1 >= 0 && LAND_MINES[row][col - 1]) {
count++;
}
if (j + 1 < 10 && landMines[i][j + 1]) {
if (col + 1 < 10 && LAND_MINES[row][col + 1]) {
count++;
}
if (i + 1 < 8 && j - 1 >= 0 && landMines[i + 1][j - 1]) {
if (row + 1 < 8 && col - 1 >= 0 && LAND_MINES[row + 1][col - 1]) {
count++;
}
if (i + 1 < 8 && landMines[i + 1][j]) {
if (row + 1 < 8 && LAND_MINES[row + 1][col]) {
count++;
}
if (i + 1 < 8 && j + 1 < 10 && landMines[i + 1][j + 1]) {
if (row + 1 < 8 && col + 1 < 10 && LAND_MINES[row + 1][col + 1]) {
count++;
}
landMineCounts[i][j] = count;
NEARBY_LAND_MINE_COUNTS[row][col] = count;
continue;
}
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] + " ");
}
System.out.println();
}
if (gameStatus == 1) {
System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!");
break;
}
if (gameStatus == -1) {
System.out.println("지뢰를 밟았습니다. GAME OVER!");
break;
}
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("잘못된 번호를 선택하셨습니다.");
NEARBY_LAND_MINE_COUNTS[row][col] = 0;
}
}
}

private static void showGameStartComments() {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("지뢰찾기 게임 시작!");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}

private static void open(int row, int col) {
if (row < 0 || row >= 8 || col < 0 || col >= 10) {
return;
}
if (!board[row][col].equals("□")) {
// 여기서도 뭐 메서드로 추출한다거나, 아니면 if문의 조건을 메서드로 추출
if (!BOARD[row][col].equals(CLOSED_CELL_SIGN)) {
return;
}
if (landMines[row][col]) {
if (LAND_MINES[row][col]) {
return;
}
if (landMineCounts[row][col] != 0) {
board[row][col] = String.valueOf(landMineCounts[row][col]);
if (NEARBY_LAND_MINE_COUNTS[row][col] != 0) {
BOARD[row][col] = String.valueOf(NEARBY_LAND_MINE_COUNTS[row][col]);
return;
} else {
board[row][col] = "■";
BOARD[row][col] = OPENED_CELL_SIGN;
}
// 이것 또한 while문이라던가 for문 혹은 stream으로 재귀호출할 수 있도록 변경 가능하지 않나?
open(row - 1, col - 1);
open(row - 1, col);
open(row - 1, col + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public void run() {

outputHandler.askPassTypeSelection();
StudyCafePassType studyCafePassType = inputHandler.getPassTypeSelectingUserAction();

if (studyCafePassType == StudyCafePassType.HOURLY) {
StudyCafeFileHandler studyCafeFileHandler = new StudyCafeFileHandler();
List<StudyCafePass> studyCafePasses = studyCafeFileHandler.readStudyCafePasses();
List<StudyCafePass> studyCafePasses = studyCafeFileHandler.readStudyCafePasses(); // 모든 정보를 다 끌고옴
List<StudyCafePass> hourlyPasses = studyCafePasses.stream()
.filter(studyCafePass -> studyCafePass.getPassType() == StudyCafePassType.HOURLY)
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cleancode.studycafe.asis.exception;

public class AppException extends RuntimeException {
public class
AppException extends RuntimeException {

public AppException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cleancode.studycafe.asis.model.StudyCafePass;

import java.util.List;
import java.util.logging.Logger;

public class OutputHandler {

Expand Down
Loading