[programmers] 혼자서 하는 틱택토
in Algorithm on Algorithm
https://school.programmers.co.kr/learn/courses/30/lessons/160585
class Solution {
public int solution(String[] board) {
int answer = 1;
int ocnt = 0;
int xcnt = 0;
boolean osuccess = false;
boolean xsuccess = false;
// 대각 검증(위에서 아래)
int ocnt3 = 0;
int xcnt3 = 0;
// 대각 검증(아래에서 위)
int ocnt4 = 0;
int xcnt4 = 0;
for(int x = 0 ; x < 3 ; x++){
// 가로 검증
int ocnt1 = 0;
int xcnt1 = 0;
// 세로 검증
int ocnt2 = 0;
int xcnt2 = 0;
for(int y = 0 ; y < 3 ; y++){
// 가로 검증
if(board[x].charAt(y) == 'O'){
ocnt++;
ocnt1++;
}else if(board[x].charAt(y) == 'X'){
xcnt++;
xcnt1++;
}
// 세로 검증
if(board[y].charAt(x) == 'O'){
ocnt2++;
}else if(board[y].charAt(x) == 'X'){
xcnt2++;
}
// 대각 검증
if(x==y) {
if(board[x].charAt(y) == 'O'){
ocnt3++;
}else if(board[x].charAt(y) == 'X'){
xcnt3++;
}
}
if( (x==0 && y==2) || (x==1 && y==1) || (y==0 && x==2) ){
if(board[x].charAt(y) == 'O'){
ocnt4++;
}else if(board[x].charAt(y) == 'X'){
xcnt4++;
}
}
}
if(xcnt1 == 3 || xcnt2 == 3) xsuccess = true;
if(ocnt1 == 3 || ocnt2 == 3) osuccess = true;
}
if(xcnt3 == 3 || xcnt4 == 3) xsuccess = true;
if(ocnt3 == 3 || ocnt4 == 3) osuccess = true;
// O이 완성인데 숫자가 같을 경우
if( osuccess && xcnt == ocnt) return 0;
// x가 완성인데 O이 더 많을 경우
if( xsuccess && ocnt > xcnt) return 0;
// x가 더 많을 경우 0
if( xcnt > ocnt ) return 0;
// 1개 이상 차이날 경우 0
if( Math.abs(ocnt-xcnt) > 1 ) return 0;
// 둘다 성공 0
if( osuccess && xsuccess ) return 0;
// 아무것도 없을 경우 1
return answer;
}
}