Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 혼자서 하는 틱택토 #119

Closed
hwangJi-dev opened this issue Feb 26, 2023 · 0 comments
Closed

[Algorithm] 혼자서 하는 틱택토 #119

hwangJi-dev opened this issue Feb 26, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Feb 26, 2023

💬 문제

혼자서 하는 틱택토


💬 Idea

  • 머쓱이가 실수할 법한 경우의 수를 생각하자
    1. OX의 수가 잘못된 경우
      • X(후공)의 개수가 O(선공)의 개수보다 많을 경우
      • O(선공)의 개수가 X(후공)의 개수보다 많지만 차례로 번갈아가면서 놓지 않고 O(선공)가 더 많이 놓은 경우
    2. 종료 상황인데 끝나지 않은 경우 == 가로, 세로, 대각선으로 3개가 같은 표시가 만들어졌는데 끝나지 않은 경우
      • 이 경우를 위해서 3x3의 보드를 0, 1, 2, 3, 4, 5, 6, 7, 8의 정수값을 가지는 숫자판으로 가정한다.

        0 1 2
        3 4 5
        6 7 8
        • bingoArr에 미리 계산해둔 bingo의 가능성을 정수 배열로 저장해둔다.
          • 가로 : [0,1,2], [3,4,5], [6,7,8]
          • 대각선 : [0,4,8], [2,4,6]
          • 세로 : [0,3,6], [1,4,7], [2,5,8]
      • O, X가 나올때마다 각각의 좌표를 정수로 변환하여 oBoard, xBoard에 append해주었다.

      • 이후 oBoard, xBoard의 수가 각각 3을 넘어갈 경우 미리 계산해둔 bingo의 가능성이 저장되어있는 bingoArr를 차례로 돌면서 bingo가 되었을 때 게임이 종료됨을 알리는 isOXEnd 변수의 값을 true로 바꾸고 반복문을 탈출한다.

    • 이후 1,2 조건을 판별해준다.

💬 풀이

func solution(board:[String]) -> Int {
    var oxCount = (0,0)
    var oBoard: [Int] = []
    var xBoard: [Int] = []
    var isOXEnd = (false, false)
    let bingoArr = [[0,1,2], [3,4,5], [6,7,8], [0,4,8], [2,4,6], [0,3,6], [1,4,7], [2,5,8]]
    
    for (bdx, b) in board.enumerated() {
        for (tdx, t) in b.enumerated() {
            if t == "O" {
                oxCount.0 += 1
		// 보드의 좌표값이 아닌 정수로 0, 1, 2, 3, 4, 5, 6, 7, 8
                oBoard.append((bdx * 3) + tdx)
            } else if t == "X" {
                oxCount.1 += 1
                xBoard.append((bdx * 3) + tdx)
            }
            
            if oBoard.count >= 3 {
                for a in bingoArr {
                    if oBoard.count - oBoard.filter({ !a.contains($0) }).count == 3 {
                        isOXEnd.0 = true
                        break
                    }
                }
            }
            
            if xBoard.count >= 3 {
                for a in bingoArr {
                    if xBoard.count - xBoard.filter({ !a.contains($0) }).count == 3 {
                        isOXEnd.1 = true
                        break
                    }
                }
            }
        }
    }
    
    // 조건 판별
    return oxCount.0 < oxCount.1 || oxCount.0 > oxCount.1 + 1 || (oxCount.0 <= oxCount.1 && isOXEnd.0) || (oxCount.0 > oxCount.1 && isOXEnd.1) ? 0 : 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant