문제 혼자서 하는 틱택토
class Solution {
public int solution(String[] board) {
int answer = -1;
//후공부터 시작한 경우
int cnt[] = getFristLast(board);
int first = cnt[0];
int last = cnt[1];
//안되는 경우
//1. 후공의 갯수 선공보다 더 많은 경우
if(first < last )
{
return 0;
}
//2. 선공이 후공보다 2번 더한경우 선공3 후공 1
if(first > last+1)
{
return 0;
}
//3. 선공이 끝난 경우
if(isFinishGame(board,'O') && first == last)
{
return 0;
}
//4. 후공이 끝난 경우
if(isFinishGame(board,'X') && first > last)
{
return 0;
}
return 1;
}
//빙고가 된 경우
public boolean isFinishGame(String []board , char c)
{
//가로 ㅡ
for(int i = 0; i<3;i++)
{
if(board[i].charAt(0) == c && board[i].charAt(1) == c && board[i].charAt(2) == c)
{
return true;
}
}
//세로 |
for(int i = 0; i<3;i++)
{
if(board[0].charAt(i) == c && board[1].charAt(i) == c && board[2].charAt(i) == c)
{
return true;
}
}
//대각선 /
if(board[0].charAt(2) == c && board[1].charAt(1) == c && board[2].charAt(0) == c )
return true;
//대각선 `
if(board[0].charAt(0) == c && board[1].charAt(1) == c && board[2].charAt(2) == c )
return true;
return false;
}
public int[] getFristLast(String []board)
{
int first = 0;
int last = 0;
for(int i = 0;i < board.length; i++)
{
for(int j =0;j<board[i].length();j++)
{
if('O' == board[i].charAt(j))
{
first++;
}else if('X' == board[i].charAt(j)){
last++;
}
}
}
return new int[]{first,last};
}
}