[프로그래머스] [1차] 다트게임

2020. 3. 31. 12:38프로그래머스/카카오

import java.util.*;

class Solution {
  public int solution(String dartResult) {
      int answer = 0;
      int flag = 0;
      int count =0;
      
      List<String> strList = new ArrayList<>();
      int[] score = new int[3];
      
      for(int i=2; i<dartResult.length(); i++){
         
          if(dartResult.charAt(i)-'0'>=0 && dartResult.charAt(i)-'0'<=9){
              strList.add(dartResult.substring(flag,i));
              flag = i;
              i++;
              count++;
          }
          if(count==2){
              strList.add(dartResult.substring(flag));
              break;
          }
      }
      
      
      for(int i=0; i<strList.size(); i++){
          String num = "";
          for(int j=0; j<strList.get(i).length(); j++){
              if(strList.get(i).charAt(j)-'0'>=0 && strList.get(i).charAt(j)-'0'<=9){
                  num += Character.toString(strList.get(i).charAt(j));
              }
          }
          score[i] = Integer.parseInt(num);
      }
      
     for(int i=0; i<strList.size(); i++){
         for(int j=0; j<strList.get(i).length(); j++){
             if(strList.get(i).charAt(j) == 'S'){
                 continue;
             }else if(strList.get(i).charAt(j)=='D'){
                 score[i] = score[i] * score[i];
             }else if(strList.get(i).charAt(j)=='T'){
                 score[i] = score[i] * score[i] * score[i];
             }else{
                 continue;
             }
         }
     }
      
      for(int i=0; i<strList.size(); i++){
          for(int j=0; j<strList.get(i).length(); j++){
              if(strList.get(i).charAt(j)=='*'){
                  if(i==0){
                      score[i] = score[i] *2;
                  }else{
                      score[i] = score[i] *2;
                      score[i-1] = score[i-1] *2;
                  }
              }else if(strList.get(i).charAt(j) == '#'){
                  score[i] = score[i] * -1;
              }else{
                  continue;
              }
          }
          
      }
      
     for(int i=0; i<score.length; i++){
         answer += score[i];
     }
      
      return answer;
  }
}

1. 각 라운드의 점수, SDT, *#을 묶어서 배열로 만듬

2. SDT에 따른 점수를 계산

3. * 또는 #에 따른 점수에 변동을 줌