프로그래머스(Java)- 다트 게임

민지킴·2021년 3월 16일
0

프로그래머스

목록 보기
3/42
post-thumbnail

문제 링크

https://programmers.co.kr/learn/courses/30/lessons/17682

문제 풀이

다트는 총 3번 쏜다.

각 한번 쏘는것의 점수, 보너스영역, 옵션을 클래스로 만들어서 추후에 사용할 계획을 세웠다.


	class Score {

		int num;
		String bonus;
		String option;

		// 각 기회의 점수, 보너스영역(제곱), 옵션(#,*)
		public Score(int num, String bonus, String option) {
			this.num = num;
			this.bonus = bonus;
			this.option = option;
		}

	}

주어진 String을 한글자씩 분석하며
숫자가 나오면 그것을 num에다가 붙였고
그 다음 처음 등장하는것은 s, d, t이므로 그것은 bonus에 담았고
그 다음 숫자가 아닌 #, *이 등장한다면 option에 넣었고

option이 등장하지 않을경우에는 한번 던진것이 끝나게 되므로 그것을
Score class에 변수로 넣고 arr 에 담아주었다.

for (int i = 0; i < len; i++) {
			String temp = dartResult.substring(i, i + 1);
			if (dartResult.charAt(i) <= 57 && dartResult.charAt(i) >= 48) {
				num += temp;
			} else {
				if (i == (len - 1)) {
					if (temp.equals("S") || temp.equals("D") || temp.equals("T")) {
						bonus = temp;
					} else {
						option = temp;
					}
					arr.add(new Score(Integer.parseInt(num), bonus, option));
				} else {
					if (temp.equals("S") || temp.equals("D") || temp.equals("T")) {
						bonus = temp;
					} else {
						option = temp;
					}

					if (dartResult.charAt(i + 1) <= 57 && dartResult.charAt(i + 1) >= 48) {
						arr.add(new Score(Integer.parseInt(num), bonus, option));
						num = "";
						bonus = "";
						option = "";
					}
				}
			}
		}

코드

package coding_test.kakao_blind_recruitment_2018;

import java.util.*;

public class Dart_Game {

	class Score {

		int num;
		String bonus;
		String option;

		// 각 기회의 점수, 보너스영역(제곱), 옵션(#,*)
		public Score(int num, String bonus, String option) {
			this.num = num;
			this.bonus = bonus;
			this.option = option;
		}

	}

	public int solution(String dartResult) {
		int answer = 0;
		List<Score> arr = new ArrayList<>();

		String num = "";
		String bonus = "";
		String option = "";
		int len = dartResult.length();
		int cnt = 0;

		for (int i = 0; i < len; i++) {
			String temp = dartResult.substring(i, i + 1);
			if (dartResult.charAt(i) <= 57 && dartResult.charAt(i) >= 48) {
				num += temp;
			} else {
				if (i == (len - 1)) {
					if (temp.equals("S") || temp.equals("D") || temp.equals("T")) {
						bonus = temp;
					} else {
						option = temp;
					}
					arr.add(new Score(Integer.parseInt(num), bonus, option));
				} else {
					if (temp.equals("S") || temp.equals("D") || temp.equals("T")) {
						bonus = temp;
					} else {
						option = temp;
					}

					if (dartResult.charAt(i + 1) <= 57 && dartResult.charAt(i + 1) >= 48) {
						arr.add(new Score(Integer.parseInt(num), bonus, option));
						num = "";
						bonus = "";
						option = "";
					}
				}
			}
		}

		int[] score = new int[3];

		for (int i = 0; i < score.length; i++) {
			if (arr.get(i).option.equals("*")) {
				score[i] = calc(arr.get(i).num, arr.get(i).bonus, arr.get(i).option);
				if (i != 0) {
					score[i - 1] *= 2;
				}
			} else {
				score[i] = calc(arr.get(i).num, arr.get(i).bonus, arr.get(i).option);
			}
		}

		for (int i = 0; i < score.length; i++) {
			answer += score[i];
		}

		return answer;
	}

	public int calc(int num, String bonus, String option) {
		int res = 0;
		if (bonus.equals("S")) {
			res = num;
		} else if (bonus.equals("D")) {
			res = num * num;
		} else {
			res = num * num * num;
		}

		if (option.equals("*")) {
			res *= 2;
		} else if (option.equals("#")) {
			res *= -1;
		} else {

		}

		return res;
	}

}

profile
하루하루는 성실하게 인생 전체는 되는대로

0개의 댓글