다트 게임
카카오톡에 뜬 네 번째 별! 심심할 땐? 카카오톡 게임별~
Game Star
카카오톡 게임별의 하반기 신규 서비스로 다트 게임을 출시하기로 했다. 다트 게임은 다트판에 다트를 세 차례 던져 그 점수의 합계로 실력을 겨루는 게임으로, 모두가 간단히 즐길 수 있다.
갓 입사한 무지는 코딩 실력을 인정받아 게임의 핵심 부분인 점수 계산 로직을 맡게 되었다. 다트 게임의 점수 계산 로직은 아래와 같다.
0~10의 정수와 문자 S, D, T, *, #로 구성된 문자열이 입력될 시 총점수를 반환하는 함수를 작성하라.
"점수|보너스|[옵션]"으로 이루어진 문자열 3세트.
예) 1S2D*3T
3번의 기회에서 얻은 점수 합계에 해당하는 정수값을 출력한다.
예) 37
import java.util.Stack;
public class Solution {
public int solution(String dartResult) {
int answer = 0;
char[] resultArr = dartResult.toCharArray();
// 점수를 담음 score 변수 선언
String score = "";
Stack<Integer> stack = new Stack<Integer>();
// 임시 점수를 담을 tempScore 선언
int tempScore = 0;
// 이전 결과를 담을 tempResult 선언
char tempResult = 0;
// *이 연속으로 나오는지 확인할 count 변수 선언
int count = 0;
// 점수 계산 시작
for (int i = 0; i < resultArr.length; i++) {
// 숫자이면 실행
if (48 <= resultArr[i] && resultArr[i] <= 57) {
// 이전 결과가 *이나 #이 아니라면 answer에 바로 추가
if (65 < tempResult) {
answer += stack.peek();
}
// 점수 담기
score += resultArr[i];
// 문자이면 실행
} else if (65 <= resultArr[i] && resultArr[i] <= 90) {
// 문자 결과에 따라 다른 점수 입력
switch (resultArr[i]) {
case 83:
stack.push(Integer.parseInt(String.valueOf(score)));
break;
case 68:
stack.push((int) Math.pow(Integer.parseInt(String.valueOf(score)), 2));
break;
case 84:
stack.push((int) Math.pow(Integer.parseInt(String.valueOf(score)), 3));
break;
}
// 문자 결과가 끝나면 점수 초기화
score = "";
// 마지막 값이 문자이면 바로 answer에 값 추가
if (i == resultArr.length - 1) {
answer += stack.peek();
}
// * 이면 실행
} else if (resultArr[i] == 42) {
tempScore = stack.pop();
if (!stack.empty()) {
// *이 한 번인지 두 번인지 확인
answer += count > 0 ? stack.peek() * 2 : stack.peek();
}
stack.push(tempScore);
answer += tempScore * 2;
count++;
// # 이면 실행
} else if (resultArr[i] == 35) {
tempScore = stack.pop();
answer -= tempScore;
stack.push(-tempScore);
count--;
}
tempResult = resultArr[i];
}
return answer;
}
}