import java.util.Scanner;
import java.util.Stack;
public class Run {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 맵은 스택으로 만듬.
// 블럭이 들어갈 라인 개수 지정
System.out.println("블록이 들어갈 라인의 개수를 입력하세요");
int mapLine = sc.nextInt();
Stack[] tetrisLine = new Stack[mapLine];
for (int i = 0; i < mapLine; i++) {
tetrisLine[i] = new Stack<Integer>();
}
int count=0;
int index=0;
while (true){
// 블럭 (1~라인개수*2 중 랜덤)
int rNum = (int) (Math.random() * mapLine * 2 + 1);
// 블럭이 들어갈 라인 번호 지정
System.out.println(rNum + " 블록이 들어갈 라인의 번호를 입력하세요.");
index = sc.nextInt();
count++; // 게임 횟수 증가
//데이터 값이 없지 않고 입력된 랜덤 값과 최근 저장된 값이 같으면 제거됨.
if(!(tetrisLine[index].empty())&&tetrisLine[index].peek() == (Integer)rNum) {
tetrisLine[index].pop();
}else {
//입력된 라인 안에 숫자를 넣어 줌.
tetrisLine[index].push(rNum);
}
if(tetrisLine[index].size()>5) {
break;
}
for (int i = 0; i < tetrisLine.length; i++) {
System.out.println(tetrisLine[i]);
}
}
System.out.println(count+"회 만에 "+index+"라인이 꽉 찼습니다.");
sc.close();
}
}
*
-랜덤 값과 스택에 담겨있는 값을 비교할 때 랜덤 값 타입은 int 기본형이고 스택의 값은 Integer 타입이기 때문에 형변환을 한 뒤 값을 비교해야 함.
'연습문제 및 실습 자료' 카테고리의 다른 글
자바 Set구조 문제 - 로또 만들기 (0) | 2023.08.07 |
---|---|
자바 맵(MAP) 문제 - 키오스크 응용(8) (0) | 2023.08.07 |
자바 리스트 문제 - 키오스크 응용(6) (0) | 2023.08.03 |
자바 다차원 배열 실습 - 사다리 게임 (0) | 2023.08.03 |
자바 배열 문제 - 키오스크 응용(5) (0) | 2023.08.02 |