Skip to content
Open
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
175 changes: 173 additions & 2 deletions src/main/java/leets/land/UpdownApplication.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,179 @@
package leets.land;

import java.io.*;
import java.util.Random;

public class UpdownApplication {

public static void main(String[] args) {
System.out.print("hihi :D");
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int tryCnt = 0;
static int numMin = 1; static int numMax = 100; static int numAnswer = 0;
static char charMin = 'A'; static char charMax = 'z'; static char charAnswer;

public static void main(String[] args) throws Exception {

//๊ฒŒ์ž„ ์‹œ์ž‘ & ๋ฒ„์ „ ์ž…๋ ฅ ๋ฐ›๊ธฐ
System.out.println("์—…๋‹ค์šด ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.\n");
System.out.print("๋ฒ„์ „์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” (์ˆซ์ž ๋ฒ„์ „: 1, ์˜์–ด ๋ฒ„์ „: 2) : ");
Integer version = Integer.parseInt(br.readLine());
if(version == 1){
//์ˆซ์ž ๋ฒ„์ „
updownNumber();
}
else if(version == 2){
//์˜์–ด ๋ฒ„์ „
updownEnglish();
}
else{
throw new IllegalArgumentException("[ERROR] ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.");
}

}

private static void updownNumber() throws IOException {
// ๋žœ๋ค ์ •๋‹ต ์ƒ์„ฑ
Random random = new Random();
numAnswer = random.nextInt(numMax - numMin + 1) + numMin;

//์ •๋‹ต ๋งž์ถœ๋•Œ๊นŒ์ง€ ๊ฒŒ์ž„ ์ง„ํ–‰
boolean isCorrect = false;
while(!isCorrect){
tryCnt += 1;
int userInput = getUserInputNum();

if(userInput == numAnswer){
System.out.println("์ •๋‹ต!");
System.out.println("\n์‹œ๋„ํ•œ ํšŸ์ˆ˜ : "+tryCnt+"ํšŒ");
isCorrect = true;
}
else{
checkRangeNum(userInput);
}
}
}

//์ž…๋ ฅ ์กฐ๊ฑด์„ ํ™•์ธ๋ฉด์„œ ์ž…๋ ฅ ๋ฐ›๊ธฐ
private static int getUserInputNum() throws IOException{
int userInput = 0;
boolean isValidInput = false;

while(!isValidInput){
try{
System.out.print("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”(" + numMin + " ~ " + numMax + ") : ");
userInput = Integer.parseInt(br.readLine());

if(IsRightValueNum(userInput)){
isValidInput = true;
}
else{
tryCnt += 1;
System.out.println("[ERROR] ๋ฒ”์œ„ ๋‚ด์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
}
}
catch (NumberFormatException e){
tryCnt += 1;
System.out.println("[ERROR] ์ž…๋ ฅ ๋ฌธ์ž์˜ ํƒ€์ž…์ด ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}

}

return userInput;
}

//๊ฐ’์ด ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋Š”์ง€ ํ™•์ธ
private static boolean IsRightValueNum(int value) {
if (numMin <= value && value <= numMax){
return true;
}
return false;
}

//์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’์„ ํŒ๋‹จํ•˜์—ฌ up or down ์ถœ๋ ฅ
private static void checkRangeNum(int userInput){
if(userInput > numAnswer){
System.out.println("DOWN");
numMax = userInput-1;
}
else if(userInput < numAnswer){
System.out.println("UP");
numMin = userInput+1;
}
}

private static void updownEnglish() throws IOException {
// ๋žœ๋ค ์ •๋‹ต ์ƒ์„ฑ
Random random = new Random();
if (random.nextBoolean()) {
charAnswer = (char) (random.nextInt(26) + 'A'); // ๋Œ€๋ฌธ์ž A-Z
} else {
charAnswer = (char) (random.nextInt(26) + 'a'); // ์†Œ๋ฌธ์ž a-z
}

//์ •๋‹ต ๋งž์ถœ๋•Œ๊นŒ์ง€ ๊ฒŒ์ž„ ์ง„ํ–‰
boolean isCorrect = false;
while(!isCorrect){
tryCnt += 1;
char userInput = (char) getUserInputChar();

if(userInput == charAnswer){
System.out.println("์ •๋‹ต!");
System.out.println("\n์‹œ๋„ํ•œ ํšŸ์ˆ˜ : "+tryCnt+"ํšŒ");
isCorrect = true;
}
else{
checkRangeChar(userInput);
}
}
}

private static int getUserInputChar() throws IOException{
char userInput = 0;
boolean isValidInput = false;

while(!isValidInput){
System.out.print("์•ŒํŒŒ๋ฒณ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” (" + charMin + " ~ " + charMax + ") : ");
String input = br.readLine();
if(input.length() == 1){
userInput = input.charAt(0);
if (userInput < 'A' || (userInput > 'z') || (userInput > 'Z' && userInput < 'a')) {
tryCnt += 1;
System.out.println("[ERROR] ์ž…๋ ฅ ๋ฌธ์ž์˜ ํƒ€์ž…์ด ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}
else{
if(IsRightValueChar(userInput)){
isValidInput = true;
}
else{
tryCnt += 1;
System.out.println("[ERROR] ๋ฒ”์œ„ ๋‚ด์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
}
}
}
else{
tryCnt += 1;
System.out.println("[ERROR] ํ•œ ๊ธ€์ž๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
}

}
return userInput;
}

private static boolean IsRightValueChar(char userInput) {
if(charMin <= userInput && userInput <= charMax){
return true;
}
return false;
}

private static void checkRangeChar(char userInput){
if(userInput > charAnswer){
System.out.println("DOWN");
charMax = (char) (userInput-1);
}
else if(userInput < charAnswer){
System.out.println("UP");
charMin = (char) (userInput+1);
}
}

}