-
Notifications
You must be signed in to change notification settings - Fork 13
[BE] ์ด๋ํ ๐์ ๋ค์ด #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
92b9296
f385137
66e3d43
db42483
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| package leets.land; | ||
|
|
||
| import leets.land.controller.GameController; | ||
|
|
||
| public class UpdownApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.print("hihi :D"); | ||
| GameController gameController = new GameController(); | ||
| gameController.startGame(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package leets.land.controller; | ||
|
|
||
| import leets.land.model.Game; | ||
| import leets.land.view.InputView; | ||
| import leets.land.view.OutputView; | ||
|
|
||
| public class GameController { | ||
| private Game game; | ||
| private InputView inputView; | ||
| private OutputView outputView; | ||
|
|
||
| public GameController() { | ||
| this.game = new Game(); | ||
| this.inputView = new InputView(); | ||
| this.outputView = new OutputView(); | ||
| } | ||
|
|
||
| public void startGame() { | ||
| outputView.displayWelcomeMessage(); | ||
| int gameType = inputView.promptForGameType(); | ||
| game.initializeGame(gameType); | ||
|
|
||
| while (!game.isGameOver()) { | ||
| String range = game.getCurrentRange(); | ||
| outputView.displayPrompt("๊ฐ์ ์ ๋ ฅํด์ฃผ์ธ์ " + range + " : "); | ||
| String guess = inputView.promptForGuess(); | ||
| if (!game.isValidGuess(guess)) { | ||
| outputView.displayError("๋ฒ์ ๋ด์ ์ฌ๋ฐ๋ฅธ ๊ฐ์ ์ ๋ ฅํ์ธ์."); | ||
| continue; | ||
| } | ||
| game.processGuess(guess); // ๋ณ๊ฒฝ๋ ๋ถ๋ถ | ||
| if (game.isGameOver()) { | ||
| outputView.displayGameOverMessage(game.getGuessCount()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package leets.land.model; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class Game { | ||
| private int targetNumber; | ||
| private char targetLetter; | ||
| private int minNumber = 1; | ||
| private int maxNumber = 100; | ||
| private char minLetter = 'A'; | ||
| private char maxLetter = 'Z'; | ||
| private int guessCount = 0; | ||
| private boolean gameOver = false; | ||
| private int gameType; | ||
|
|
||
| public void initializeGame(int gameType) { | ||
| this.gameType = gameType; | ||
| Random rand = new Random(); | ||
| if (gameType == 1) { | ||
| targetNumber = rand.nextInt(maxNumber - minNumber + 1) + minNumber; | ||
| } else { | ||
| minLetter = 'A'; | ||
| maxLetter = 'z'; | ||
| targetLetter = (char) (rand.nextInt('z' - 'A' + 1) + 'A'); | ||
| } | ||
| } | ||
|
|
||
| public boolean isValidGuess(String guess) { | ||
| if (gameType == 1) { | ||
| try { | ||
| int guessNum = Integer.parseInt(guess); | ||
| return guessNum >= minNumber && guessNum <= maxNumber; | ||
| } catch (NumberFormatException e) { | ||
| return false; | ||
| } | ||
| } else { | ||
| if (guess.length() != 1) return false; | ||
| char guessChar = guess.charAt(0); | ||
| return (guessChar >= minLetter && guessChar <= maxLetter) || | ||
| (guessChar >= Character.toLowerCase(minLetter) && guessChar <= Character.toLowerCase(maxLetter)); | ||
| } | ||
| } | ||
|
|
||
| public void processGuess(String guess) { | ||
| guessCount++; | ||
| if (gameType == 1) { | ||
| int guessNum; | ||
| try { | ||
| guessNum = Integer.parseInt(guess); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("[ERROR] ์ ์ ๊ฐ์ ์ ๋ ฅํ์ธ์."); | ||
| return; | ||
| } | ||
|
|
||
| if (guessNum < targetNumber) { | ||
| minNumber = guessNum + 1; | ||
| System.out.println("UP"); | ||
| } else if (guessNum > targetNumber) { | ||
| maxNumber = guessNum - 1; | ||
| System.out.println("DOWN"); | ||
| } else { | ||
| gameOver = true; | ||
| System.out.println("์ ๋ต!"); | ||
| } | ||
| } else { | ||
| char guessChar = guess.charAt(0); | ||
| if (guessChar >= 'A' && guessChar <= 'Z') { | ||
| if (guessChar < targetLetter) { | ||
| minLetter = (char) (guessChar + 1); | ||
| System.out.println("UP"); | ||
| } else if (guessChar > targetLetter) { | ||
| maxLetter = (char) (guessChar - 1); | ||
| System.out.println("DOWN"); | ||
| } else { | ||
| gameOver = true; | ||
| } | ||
| } else if (guessChar >= 'a' && guessChar <= 'z') { | ||
| if (guessChar < targetLetter) { | ||
| minLetter = (char) (guessChar + 1); | ||
| System.out.println("UP"); | ||
| } else if (guessChar > targetLetter) { | ||
| maxLetter = (char) (guessChar - 1); | ||
| System.out.println("DOWN"); | ||
| } else { | ||
| gameOver = true; | ||
| } | ||
| } else { | ||
| System.out.println("[ERROR] ๋ฒ์ ๋ด์ ์ฌ๋ฐ๋ฅธ ๊ฐ์ ์ ๋ ฅํ์ธ์."); | ||
| } | ||
|
|
||
| if (maxLetter >= '[' && maxLetter <= '`') { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ํ์ง ๋ชปํ ๋ถ๋ถ์ ์ฒ๋ฆฌํ ๋ชจ์ต์ด ์ข์์ต๋๋ค! |
||
| maxLetter = 'Z'; | ||
| } else if (minLetter > 'Z' && minLetter < 'a') { | ||
| minLetter = 'a'; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ฒ์๊ฐ ์ถ๋ ฅ์ ๋์๋ฌธ์ ๋์ด๊ฐ๋ ๋ถ๋ถ ์ ๊ณ ๋ คํ์ ๊ฒ ๊ฐ์ต๋๋ค !!! |
||
| } | ||
| } | ||
|
|
||
| public boolean isGameOver() { | ||
| return this.gameOver; | ||
| } | ||
|
|
||
| public int getGuessCount() { | ||
| return this.guessCount; | ||
| } | ||
|
|
||
| public String getCurrentRange() { | ||
| if (gameType == 1) { | ||
| return String.format("(%d ~ %d)", minNumber, maxNumber); | ||
| } else { | ||
| return String.format("(%c ~ %c)", minLetter, maxLetter); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package leets.land.view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private Scanner scanner; | ||
|
|
||
| public InputView() { | ||
| this.scanner = new Scanner(System.in); | ||
| } | ||
|
|
||
| public int promptForGameType() { | ||
| System.out.print("๋ฒ์ ์ ์ ๋ ฅํด์ฃผ์ธ์ (์ซ์ ๋ฒ์ : 1, ์์ด ๋ฒ์ : 2) : "); | ||
| return scanner.nextInt(); | ||
| } | ||
|
|
||
| public String promptForGuess() { | ||
| return scanner.next(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package leets.land.view; | ||
|
|
||
| public class OutputView { | ||
| public void displayWelcomeMessage() { | ||
| System.out.println("์ ๋ค์ด ๊ฒ์์ ์์ํฉ๋๋ค."); | ||
| } | ||
|
|
||
| public void displayPrompt(String message) { | ||
| System.out.print(message); | ||
| } | ||
|
|
||
| public void displayFeedback(String feedback) { | ||
| System.out.println(feedback); | ||
| } | ||
|
|
||
| public void displayError(String errorMessage) { | ||
| System.out.println("[ERROR] " + errorMessage); | ||
| } | ||
|
|
||
|
|
||
| public void displayGameOverMessage(int guessCount) { | ||
| System.out.println("์ ๋ต!\n"); | ||
| System.out.println("์๋ํ ํ์ : " + guessCount + "ํ"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด์ํ ๊ฐ์ ์ ๋ ฅํด๋ ๊ฒ์์ด ์งํ๋ฉ๋๋คใ ใ