헬린코린이
First Project 본문
첫 번째 프로젝트
주사위를 굴려 게임을 만들었다.
package firstproject;
import java.util.*;
public class teammadedicegame {
public static void main(String[] args) {
int tScore = 0; // 총점
int ch = 0;
int cnt = 0; // 더블 횟수
int round = 1;
int rRound = 1; // 잔여라운드
// 게임 규칙 설명
System.out.println("주사위 게임에 오신 여러분 환영합니다.~");
delay(1500); //메서드 하나 정의 1.5초간 딜레이.
System.out.println("저는 이 게임에 진행자입니다.");
delay(1500);
System.out.println("게임을 진행하기 앞서 규칙을 설명하겠습니다.");
delay(1500);
System.out.println("총 2개의 주사위를 굴려, 두 주사위의 합이 본인 점수가 됩니다.");
delay(1500);
System.out.println("매라운드의 점수는 누적되어 최종 점수를 받게 됩니다.");
delay(1500);
System.out.println("만약 두 주사위가 동일한 \"더블\"이 나올 경우, 해당 round의 점수는 2배가 됩니다.");
delay(1500);
System.out.println("★단, 더블이 두 라운드 연속해서 나올 경우 총점 0점이 되며, 잔여 라운드 관계 없이 게임이 종료됩니다.★");
delay(1500);
System.out.println("\n자 그럼 게임을 시작하겠습니다.");
delay(1500);
while (rRound != 0) {
int money = 0;
while(true) { //돈을 잘못 넣었을때 다시 돌아오는 반복문.
Scanner sc = new Scanner(System.in); // try~catch 문을 넣어서 다시 진행할 때 초기화 필요!! 그래서 안으로 옮겨줌
System.out.println("\n얼마를 넣으시겠습니까? 1게임에 500원입니다.");
money = 0;
try {
money = sc.nextInt();
} catch (Exception e) {
System.out.println("숫자를 입력해주세요~^^");
continue;
}
round = money / 500;
rRound = round;
ch = money - (500 * round);
//int last = 500-coin;
if(500>money) { //coin이 500원보다 낮다면 실행.
System.out.println("한 판에 500원입니다...");
}else if(money>10000){ //coin이 10000원보다 높으면 실행.
System.out.println("과한 도박은 정신에 해롭습니다.");
}else { //제대로된 값을 입력하면 실행.
System.out.println(money+"원 넣으셨습니다. "+round+"판 가능하십니다.");
break;
}
System.out.print("환불해 줄테니 다시 넣어주세요.");
//money = sc.nextInt();
}
for (int i = 1; i <= round; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("");
System.out.println("===========================================================================");
System.out.println(i + "라운드입니다. " + rRound + "라운드 남았습니다. (Enter)를 누르면 진행됩니다."); // i대신 round로 쓰면 계속 총
// 라운드로만 출력됨(증가가 안됨)
System.out.println("===========================================================================");
sc.nextLine();
//delay(1500); // 메서드 하나 정의 1.5초간 딜레이.
Random rand = new Random();
int d1 = rand.nextInt(6) + 1;
int d2 = rand.nextInt(6) + 1;
System.out.println("\n" + i + "라운드 결과입니다.");
System.out.println("[" + d1 + "], [" + d2 + "]");
int rScore = 0; // 라운드 스코어 변수
if (d1 == d2) {
rScore = (d1 + d2) * 2;
cnt++;
round++;
rRound++;
if (cnt == 2) {
System.out.println("더블이 연속 두번 나왔습니다.");
tScore = 0;
rRound = 0;
break;
} else {
System.out.println("더블! 한 게임 더!"); // 원래 더블일 경우 바로 출력되는 곳에 있었지만, 더블 연속일 경우 굳이 출력할 필요가 없기 때문에
// 이곳으로 이동시킴!
}
} else {
cnt = 0; // 연속 더블이 아닌 경우, 종료되지 않기 때문에 cnt 초기화
rScore = d1 + d2;
}
tScore += rScore;
rRound--;
System.out.println(i + "라운드 점수 : " + rScore + " 총점 : " + tScore);
} // for문 끝
// rule = 3;
// while(true)문 종료
System.out.println();
System.out.println("게임을 종료합니다. 총점은 " + tScore + "입니다.");
if(money%500!=0) {
System.out.println("짤랑~💰 거스름돈이 나왔습니다. " + ch + "원을 가져가세요!"); // 석지언니 아이디어★★★★★
}
}
}
static void delay(long millis) {// 딜레이 메서드
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}
}
설명을 코드에 써놓아서 다른 설명은 생략하겠다.