팀 과제 '숫자 야구 프로그래밍 만들기'
1일 1커밋 20231213
‘숫자 야구 프로그래밍 만들기’
개인 목표: 최대한 강의에서 배운 내용을 활용하여 진행한다
To do
1. 컴퓨터가 0과 9사이의 서로 다른 숫자 3개를 무작위로 생성
random함수를 사용할 수 있지만 강의에서 나온 HashSet을 사용해보자
1 2 import java.util.HashSet; import java.util.Set;
- set 을 사용하기 위해서는 위와 같이
java.util.HashSet
와java.util.Set
를 추가해줘야 한다- (
import java.util.*;
으로 정리함)
1
Set<Integer> intSet = new HashSet<>(); // 선언 및 생성
- HashSet 으로 생성한 intSet 에 3번 돌면서 0~9까지의 수를 중복과 순서를 고려 하지 않고 랜덤 배열 한다.
- (HashSet 의 특징을 이용함)
문제점
그러나 계속 자동으로 오름차순 정렬돼서 나옴
- 트러블 슈팅:
ArrayList
를 생성하고Collections.shuffle();
로.shuffle
를 넣어주니 랜덤 정렬이 되었다. - 아래와 같이 코드 변경
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
public class Main {
public static void main(String[] args) {
// 1. 컴퓨터 랜덤 함수 생성, 랜덤 정렬한 ArrayList를 int배열로 변환
Set<Integer> intSet = new HashSet<>();
while (intSet.size() < 3) {
int a = (int) (Math.random() * 10); // Math.random()함수는 0.xxxx ~ 0.9xxx(부동소수점 난수) 까지의 값을 반환하기 때문에 10을 곱해준다
intSet.add(a);
}
ArrayList<Integer> computerArray = new ArrayList<>(intSet);
//순번값(인덱스)로 값을 하나씩 조회 가능
Collections.shuffle(computerArray); // shuffle을 사용해서 랜덤 정렬
int[] computer = computerArray.stream().mapToInt(Integer::intValue).toArray();
// ArrayList computerArray를 int 배열로 변환
System.out.println(Arrays.toString(computer)); //컴퓨터 랜덤 배열 출력 해보기
System.out.println("컴퓨터가 숫자를 생성하였습니다. 답을 맞춰보세요!");
2. 사용자가 3개의 숫자 입력 시도
사용자는 Scanner 함수를 이용해 3개의 숫자를 입력한다(0~9 숫자가 적힌 카드를 뽑는다고 생각하면 된다. 즉, 0도 맨 앞에 올 수 있음)
1
import java.util.Scanner;
1
Scanner sc = new Scanner(System.in);
- 전체 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 2. 사용자가 3개의 숫자 입력 시도 int n = 0; Scanner sc = new Scanner(System.in); int[] user = new int[3]; //사용자값 초기설정 {0,0,0} while (true) { //while문 사용(전체) n++; //(올바른 형식으로) 시도할 때마다 카운트 업 System.out.println(n + "번째 시도: "); String temp = sc.nextLine(); // 입력값을 int가 아닌 spring으로 받음(0이 앞에 나오더라도 인식하게 하기위해서) user = new int[temp.length()]; // temp 길이 만큼 int 배열 생성 for (int i = 0; i < temp.length(); i++) { user[i] = temp.charAt(i) - '0'; //아스키코드 참조, temp에 있는값을 하나씩 user 배열 인덱스 순서대로 넣어줌. } // System.out.println(Arrays.toString(user)); //[int1,int2,int3] 사용자 입력값이 배열에 잘 들어가는지 확인 System.out.println(Arrays.stream(user).distinct().count()); // if (user.length == 3 && Arrays.stream(user).distinct().count() == 3){ // Arrays.stream() : 스트림생성, .distinct(): 중간연산, 중복 없앰, .count(): 최종연산, 남은 자리수 카운트셈 // System.out.println("올바른 값을 입력했습니다"); }else { n = 0; System.out.println("다시 입력하세요. 입력 값은 3자리 수여야 하며 중복 값이 없어야 합니다"); }
3. 사용자의 입력값과 컴퓨터 랜덤값 비교 구문
위치와 값 일치 여부에 따라 s++, b++
1
2
3
4
5
6
7
8
9
10
11
12
13
// 3. 사용자의 입력값과 컴퓨터 랜덤값 비교 구문
if(n!=0) {
int s = 0;
int b = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (user[i] == computer[j]) {
if (i == j) {
s++;
} else b++;
}
}
}
- 올바른 형식으로 입력했을 경우 n은 1이기 때문에 for문 돌아감
2가지 경우로 나눠 그에 맞는 값을
++
한다 (이중 for문과 이중 if문을 사용한다)- 이중 for문으로 i와 j를 0~2까지 돌린다
- 첫번째 조건(이중 IF문 통과 경우), 입력값의 i번째 숫자와 컴퓨터 랜덤값 j번째 숫자가 동일 && i = j 인덱스(=위치)도 같을 때
"S" 출력 후 s++;
- 두번째 조건(첫번째 IF문 통과 후 else 경우), 입력값의 i번째 숫자와 컴퓨터 랜덤값 j번째 숫자가 동일 && i != j 인덱스(=위치)는 다를 때
"B" 출력 후 b++;
4. s,b 연산
1 2 3 4 5 6 7 if (s > 0 && b == 0) { System.out.println(s + "S"); } else if (b > 0 && s == 0) { System.out.println(b + "B"); } else { System.out.println(b + "B" + s + "S"); }
5. 스트라이크 3번일 경우 계산
break로 끝나는 if문
1 2 3 4 5 6 7 8 9 10 // 5. 스트라이크가 3번 나왔을 경우 break로 while문 끝냄. if (s == 3) { System.out.println(n + "번 만에 맞히셨습니다."); System.out.println("게임을 종료합니다."); break; } } } } }
6. return 되는 값 형식
1
2
3
4
5
6
7
8
9
10
11
12
System.out.println("컴퓨터가 숫자를 생성하였습니다. 답을 맞춰보세요!");
System.out.println( n + "번째 시도:");
//사용자가 숫자 3개를 입력하면 아래와 같이 출력
System.out.println( b + "B" + s + "S");
//사용자가 정답을 맞췄을 경우(=3S) 아래와 같이 출력
System.out.println( n + "번 만에 맞히셨습니다.");
System.out.println("게임을 종료합니다.");
This post is licensed under CC BY 4.0 by the author.