Skip to content

Instantly share code, notes, and snippets.

@ayaysir
Created September 17, 2019 12:14
Show Gist options
  • Save ayaysir/e3b81dde9639496272cf679a6403ad6a to your computer and use it in GitHub Desktop.
Save ayaysir/e3b81dde9639496272cf679a6403ad6a to your computer and use it in GitHub Desktop.
package blog;
import java.util.Scanner;
public class UpAndDown {
private static int firstRun = 1;
private static int meWin = 0;
private static int comWin = 0;
private static final String ABOVE = "* Input is A B O V E from target *";
private static final String BELOW = "* Input is B E L O W from target *";
private static Scanner s = new Scanner(System.in);
public static int generateRandomByRange(int min, int max) {
return (int) ( Math.random() * ( max - min + 1 ) + min );
}
public static void main(String[] args) {
while(true){
System.out.println("==== Up & Down Game II+ ====");
System.out.println(" (Human vs Kizuna Ai) ");
System.out.println();
System.out.println("1. Game Start");
System.out.println("2. Game Score");
System.out.println("3. End Game");
System.out.print("\n> ");
int menuSelect = Integer.parseInt(s.nextLine());
if(menuSelect==1) game();
else if(menuSelect==2) {
switch(firstRun) {
case 1:
System.out.println("There is no recorded score. "
+ "First, play a game! \n");
break;
default:
System.out.println("Me Win: " + meWin);
System.out.println("Com(Ai) Win: " + comWin);
System.out.println();
}
} else if(menuSelect==3) {
System.out.println("\nThank you for playing!");
System.exit(0);
} else {
System.out.println("Enter correct number please! (1~3)\n");
}
}
}
public static void game() {
boolean isFirstTurn = true;
int upNum = 99;
int downNum = 1;
int pick = generateRandomByRange(1, 99);
System.out.println("<< Game Start >>");
while(true) {
// 0: 컴퓨터, 1: 나
int whoTheFirst = generateRandomByRange(0, 1);
if(isFirstTurn && whoTheFirst==0)
System.out.println("First player is Ai!\n");
else if(isFirstTurn && whoTheFirst==1)
System.out.println("First Player is YOU!\n");
int sw1 = 0; // ABOVE, BELOW 스위치 (1: above, -1: below)
int meInputNum = 0;
if (!isFirstTurn || whoTheFirst == 1)
{
System.out.print("Input Number(Me): ");
meInputNum = Integer.parseInt(s.nextLine());
// UP = 1; DOWN = -1;
if ( 0 >= meInputNum || meInputNum >= 100 )
System.out.println("Enter number between 1~99 please!\n");
else if ( pick < meInputNum ) {
// 입력한 값이 픽값보다 클 경우
System.out.println(ABOVE + "\n");
sw1 = 1;
if ( upNum > meInputNum)
upNum = meInputNum;
} else if ( pick > meInputNum ) {
// 입력한 값이 픽값보다 작을 경우
System.out.println(BELOW + "\n");
sw1 = -1;
if ( downNum < meInputNum)
downNum = meInputNum;
} else if ( pick == meInputNum ) {
System.out.println("* C O R R E C T *");
System.out.println("* Y O U W I N ! *");
System.out.println();
meWin++;
firstRun = 0;
System.out.println("Your victory count is: " + meWin + "\n");
break;
}
}
// computer
try {
Thread.sleep(generateRandomByRange(500, 1000)); // 생각하는 척
} catch (InterruptedException e) {
e.printStackTrace();
}
int comNum = 0;
if (isFirstTurn && whoTheFirst == 1) {
// 첫판인데 먼저 시작한 사람이 '나'인 경우
if ( sw1 == -1 )
comNum = generateRandomByRange(meInputNum + 1, 99);
else if( sw1 == 1 )
comNum = generateRandomByRange(1, meInputNum - 1);
/*
* 앞에 '나'의 결과가 below인 경우: meInputNum + 1부터 99까지 던짐
* 앞에 '나'의 결과가 above인 경우: 1부터 meInputNum - 1까지 던짐
*/
} else if (isFirstTurn && whoTheFirst == 0) {
// 만약 컴퓨터가 첫 턴인 경우 아무 숫자나 던진다.
comNum = generateRandomByRange(1, 99);
} else {
comNum = generateRandomByRange((downNum + 1), (upNum - 1));
// else의 경우 downNum(나온 숫자 중 최소값) + 1부터 upNum(나온 숫자 중 최대값) - 1까지의 수를 던짐
}
System.out.println("Ai's Input: " + comNum );
if ( pick < comNum ) {
System.out.println(ABOVE + "\n");
sw1 = 1;
if ( upNum > comNum )
upNum = comNum;
} else if ( pick > comNum ) {
System.out.println(BELOW + "\n");
sw1 = -1;
if ( downNum < comNum )
downNum = comNum;
} else if ( pick == comNum ) {
System.out.println("* C O R R E C T *");
System.out.println("* Winner is AI!! *");
System.out.println("* You lose. *");
System.out.println();
comWin++;
firstRun = 0;
break;
}
isFirstTurn = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment