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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@

## 과제 제출 과정
* [과제 제출 방법](https://github.com/next-step/nextstep-docs/tree/master/ent-precourse)

<기능 목록>

1. 유저 인풋 기능
2. 컴퓨터 난수 생성 기능
3. 스트라이크/볼 체크 기능
4. 게임 재시작 기능
41 changes: 41 additions & 0 deletions src/main/java/Checker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class Checker {
private int strike_arr[];
private int strike;
private boolean isex[];
private int ball;
private Opponent OP;

public Checker(Opponent OP_in){
strike_arr = new int[]{0,0,0};
isex = new boolean[]{false,false,false,false,false,false,false,false,false,false};
strike = 0;
ball = 0;
OP = OP_in;
}
public void compare(int user_input, int opp_number){
check_strike(user_input, opp_number);
check_ball(user_input);
}
public void check_strike(int user_input, int opp_number){
if(get100number(user_input) == get100number(opp_number)) strike_arr[0] = 1;
if(get10number(user_input) == get10number(opp_number)) strike_arr[1] = 1;
if(get1number(user_input) == get1number(opp_number)) strike_arr[2] = 1;
strike = strike_arr[0] + strike_arr[1] + strike_arr[2];
}
public void check_ball(int user_input){
if(strike_arr[0]==0 && OP.getintdict()[get100number(user_input)]) ball++;
if(strike_arr[1]==0 && OP.getintdict()[get10number(user_input)]) ball++;
if(strike_arr[2]==0 && OP.getintdict()[get1number(user_input)]) ball++;
}
public int get100number(int a){
return a / 100;
}
public int get10number(int a){
return a % 100 / 10;
}
public int get1number(int a){
return a % 10;
}
public int getStrike(){return strike;}
public int getBall(){return ball;}
}
37 changes: 37 additions & 0 deletions src/main/java/IOsys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Scanner;

public class IOsys {
private Scanner sc;

public IOsys(){
sc = new Scanner(System.in);
}
public int userIn(){
System.out.print("숫자를 입력해주세요: ");
int userinput = sc.nextInt();
while(userinput < 100 || 1000 <= userinput || check_dup(userinput)){
System.out.println("불가능한 입력입니다.");
System.out.print("숫자를 입력해주세요: ");
userinput = sc.nextInt();
}
return userinput;
}

public boolean check_dup(int userinput){
return userinput/100 == userinput%10 || userinput/100 == userinput%100/10 || userinput%10 == userinput%100/10;
}

public void print_result(Checker C){
if(C.getStrike()!= 0) System.out.printf("%d스트라이크 ", C.getStrike());
if(C.getBall() != 0) System.out.printf("%d볼 ", C.getBall());
if(C.getBall() + C.getStrike() == 0) System.out.printf("낫싱");
System.out.println();
}
public int nextgame(){
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.");
int ContinueFlag = sc.nextInt();

return ContinueFlag;
}
}
6 changes: 6 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
game g = new game();
g.game_start();
}
}
36 changes: 36 additions & 0 deletions src/main/java/Opponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.Random;

public class Opponent {
private int Random_Int;
private boolean int_dict[];// if 1 exists, int_dict[1] = true

public Opponent(){
Random_Int = 0;
int_dict = new boolean[]{false,false,false,false,false,false,false,false,false,false};
}
public void generate_random_int(){
Random random = new Random();

int cur = random.nextInt(10);

while(cur == 0) cur = random.nextInt(10);
Random_Int += cur*100;
cur = check_pre(0, cur, random);
Random_Int += cur*10;
Random_Int += check_pre(Random_Int/100, cur, random);
init_int_dict();
}
public void init_int_dict(){
int_dict[Random_Int/100] = true;
int_dict[Random_Int%100/10] = true;
int_dict[Random_Int%10] = true;
}
public int getRandom_Int() {return this.Random_Int;}
public void setRandom_Int(int a) {this.Random_Int = a;}
public int check_pre(int pprevious, int previous, Random r){
int temp = previous;
while(temp == previous || temp == pprevious) temp = r.nextInt(10);
return temp;
}
public boolean[] getintdict(){return this.int_dict;}
}
35 changes: 35 additions & 0 deletions src/main/java/game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import com.sun.source.tree.ContinueTree;

public class game {
private IOsys IO;
private int Continue_Flag;
private int strike_num;
public game(){
Continue_Flag = 1;
strike_num = 0;
IO = new IOsys();
}
public void game_start(){
while(Continue_Flag == 1){
Opponent OP = new Opponent();
OP.generate_random_int();

strike_num = 0;

play_one_game(OP);

Continue_Flag = IO.nextgame();
}
}
public void play_one_game(Opponent OP){
while(strike_num != 3) {
int userinput = IO.userIn();

Checker chk = new Checker(OP);
chk.compare(userinput, OP.getRandom_Int());
IO.print_result(chk);

strike_num = chk.getStrike();
}
}
}
19 changes: 19 additions & 0 deletions untitled/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
useJUnitPlatform()
}
Binary file added untitled/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions untitled/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading