//
//  Board.swift
//  TTTKit
//
//  Created by Shadowfacts on 12/21/22.
//

import Foundation

public protocol Board {
    subscript(_ column: Int, _ row: Int) -> Mark? { get }
}

extension Board {
    subscript(_ point: (column: Int, row: Int)) -> Mark? {
        get {
            self[point.column, point.row]
        }
    }
    
    public var full: Bool {
        for column in 0..<3 {
            for row in 0..<3 {
                if self[column, row] == nil {
                    return false
                }
            }
        }
        return true
    }
    
    public var win: Win? {
        for points in Win.allPoints {
            if let mark = self[points[0]],
               self[points[1]] == mark && self[points[2]] == mark {
                return Win(mark: mark, points: points)
            }
        }
        return nil
    }
    
    public var won: Bool {
        win != nil
    }
    
    public var tied: Bool {
        full && !won
    }
    
    public var ended: Bool {
        won || tied
    }
}
