<commit_before>import TicTacToe
import Player exposing (..)
import Board exposing (..)
import SvgUtils
import TicTacToeBase
import TicTacToeBase exposing (strikeThrough, cellSize, boardSize, grid)

import List as L
import Tuple3 as T3
import Html.Attributes
import Html exposing (Html, button, div, text)
import Html.App as App
import Html.Events exposing (onClick)
import Svg exposing (..)
import Svg.Attributes exposing (..)


type alias UltimateTicTacToeBoard = Board TicTacToe.Model

main =
    App.beginnerProgram
        { model = init
        , update = update
        , view = view
        }

-- MODEL

type alias Model =
    { board : UltimateTicTacToeBoard
    , currentPlayer : Player
    , currentBoardCoords : Board.Coords
    }

init : Model
init =
    { board = T3.fill (T3.fill TicTacToe.init)
    , currentPlayer = X
    , currentBoardCoords = (T3.I2, T3.I2) -- FIXME!!!!
    }

boardOwner : TicTacToe.Model -> Maybe Player
boardOwner boardModel =
    case TicTacToe.winner boardModel.board of
        Nothing -> Nothing
        Just winner -> case winner of
            Left player -> Just player
            Right Draw -> Nothing

winner : UltimateTicTacToeBoard -> Maybe Winner
winner = Board.winner boardOwner


-- UPDATE

type Msg = MetaPlaceMark Coords TicTacToe.Msg

update : Msg -> Model -> Model
update msg ({board, currentPlayer, currentBoardCoords} as model) =
    let
        nextPlayer = opponent currentPlayer
    in
       case winner board of
           Just _ -> model
           Nothing ->
            case msg of
                MetaPlaceMark (x,y) ((TicTacToe.PlaceMark cellCoords cellMsg) as msg) ->
                    if (x,y) == currentBoardCoords then
                        { board = indexedMap (\(i,j) subBoard ->
                            if (x,y) == (i,j) then TicTacToe.update msg subBoard else TicTacToe.update TicTacToe.TogglePlayer subBoard) board
                        , currentPlayer = nextPlayer
                        , currentBoardCoords = cellCoords
                        }
                    else
                        model
                _ -> model


-- VIEW

view : Model -> Html Msg
view model =
    let
        size = (toString TicTacToeBase.boardSize)
        divStyle =
          Html.Attributes.style
            [ ("margin", "auto")
            , ("width", size ++ "px")
            ]
    in
        div [ divStyle ]
            [
            svg [ viewBox ("0 0 " ++ size ++ " " ++ size), width (size ++ "px") ]
                [(svgView model)]
            ]


svgView : Model -> Svg Msg
svgView model =
    let
        board = model.board
        vb = svgViewBoard model.currentBoardCoords
        cells = g [] (flatten <| (indexedMap vb board))
        st = case (winningRow boardOwner board) of
            Just (first,middle,last) -> [ strikeThrough cellSize first last ]
            _ -> []
    in
          g [] ([ cells, (grid cellSize) ] ++ st)



svgViewBoard : Coords -> Coords -> TicTacToe.Model -> Svg Msg
svgViewBoard currentBoardCoords ((i,j) as coords) ({board, currentPlayer} as model) =
    let
        node = TicTacToe.svgView model
        nodeWithOpacity = if coords /= currentBoardCoords then g [ opacity "0.25", fill "red" ] [ node ] else node
    in
       nodeWithOpacity
          |> SvgUtils.scale (1.0/3.0)
          |> SvgUtils.translate ((T3.toInt i)*TicTacToeBase.cellSize) ((T3.toInt j)*TicTacToeBase.cellSize)
          |> App.map (MetaPlaceMark coords)<commit_msg>Fix starting board, and sending opponent to an already-won board<commit_after>import TicTacToe
import Player exposing (..)
import Board exposing (..)
import SvgUtils
import TicTacToeBase
import TicTacToeBase exposing (strikeThrough, cellSize, boardSize, grid)

import List as L
import Tuple3 as T3
import Html.Attributes
import Html exposing (Html, button, div, text)
import Html.App as App
import Html.Events exposing (onClick)
import Svg exposing (..)
import Svg.Attributes exposing (..)


type alias UltimateTicTacToeBoard = Board TicTacToe.Model

main =
    App.beginnerProgram
        { model = init
        , update = update
        , view = view
        }

-- MODEL

type alias Model =
    { board : UltimateTicTacToeBoard
    , currentPlayer : Player
    , currentBoardCoords : Maybe Board.Coords
    }

init : Model
init =
    { board = T3.fill (T3.fill TicTacToe.init)
    , currentPlayer = X
    , currentBoardCoords = Nothing
    }

boardOwner : TicTacToe.Model -> Maybe Player
boardOwner boardModel =
    case TicTacToe.winner boardModel.board of
        Nothing -> Nothing
        Just winner -> case winner of
            Left player -> Just player
            Right Draw -> Nothing

winner : UltimateTicTacToeBoard -> Maybe Winner
winner = Board.winner boardOwner


-- UPDATE

type Msg = MetaPlaceMark Coords TicTacToe.Msg

update : Msg -> Model -> Model
update msg ({board, currentPlayer, currentBoardCoords} as model) =
    let
        nextPlayer = opponent currentPlayer
    in
       case winner board of
           Just _ -> model
           Nothing ->
            case msg of
                MetaPlaceMark (x,y) ((TicTacToe.PlaceMark cellCoords cellMsg) as msg) ->
                    case currentBoardCoords of
                        Nothing ->
                            { board = indexedMap (\(i,j) subBoard ->
                                if (x,y) == (i,j) then TicTacToe.update msg subBoard else TicTacToe.update TicTacToe.TogglePlayer subBoard) board
                            , currentPlayer = nextPlayer
                            , currentBoardCoords = if (TicTacToe.winner (get board cellCoords).board == Nothing) then Just cellCoords else Nothing
                            }
                        Just c ->
                            if (x,y) == c then
                                { board = indexedMap (\(i,j) subBoard ->
                                    if (x,y) == (i,j) then TicTacToe.update msg subBoard else TicTacToe.update TicTacToe.TogglePlayer subBoard) board
                                , currentPlayer = nextPlayer
                                , currentBoardCoords = if (TicTacToe.winner (get board cellCoords).board == Nothing) then Just cellCoords else Nothing
                                }
                    else
                        model
                _ -> model


-- VIEW

view : Model -> Html Msg
view model =
    let
        size = (toString TicTacToeBase.boardSize)
        divStyle =
          Html.Attributes.style
            [ ("margin", "auto")
            , ("width", size ++ "px")
            ]
    in
        div [ divStyle ]
            [
            svg [ viewBox ("0 0 " ++ size ++ " " ++ size), width (size ++ "px") ]
                [(svgView model)]
            ]


svgView : Model -> Svg Msg
svgView model =
    let
        board = model.board
        vb = svgViewBoard model.currentBoardCoords
        cells = g [] (flatten <| (indexedMap vb board))
        st = case (winningRow boardOwner board) of
            Just (first,middle,last) -> [ strikeThrough cellSize first last ]
            _ -> []
    in
          g [] ([ cells, (grid cellSize) ] ++ st)



svgViewBoard : (Maybe Coords) -> Coords -> TicTacToe.Model -> Svg Msg
svgViewBoard currentBoardCoords ((i,j) as coords) ({board, currentPlayer} as model) =
    let
        node = TicTacToe.svgView model
        nodeWithOpacity = case currentBoardCoords of
            Just cds -> if (coords /= cds) then (g [ opacity "0.15" ] [ node ]) else node
            Nothing -> node
    in
       nodeWithOpacity
          |> SvgUtils.scale (1.0/3.0)
          |> SvgUtils.translate ((T3.toInt i)*TicTacToeBase.cellSize) ((T3.toInt j)*TicTacToeBase.cellSize)
          |> App.map (MetaPlaceMark coords)
{'fraction_non_alphanumeric': 0.09203722854188211, 'fraction_numerical': 0.0043950361944157185, 'mean_word_length': 1.8602587800369685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5408866', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2276, 'n_words': 649}