diff --git a/build/app.js b/build/app.js index cab8c30..e4984c5 100644 --- a/build/app.js +++ b/build/app.js @@ -8985,6 +8985,18 @@ var _user$project$MyEvent$Clicked = function (a) { return {ctor: 'Clicked', _0: a}; }; +var _user$project$GameLogic$noneUnclaimed = function (ary) { + return _elm_lang$core$Native_Utils.eq( + 0, + _elm_lang$core$Array$length( + A2( + _elm_lang$core$Array$filter, + function (x) { + return _elm_lang$core$Native_Utils.eq(x, _user$project$Player$Unclaimed); + }, + ary))); +}; + var _user$project$EventHandlers_OnClicked$onClicked = F2( function (model, index) { var leaveBoardIntact = function (player) { @@ -9207,17 +9219,6 @@ var _user$project$Update$playerWon = F2( return _elm_lang$core$Result$Err('Not there yet!'); } }); -var _user$project$Update$noneUnclaimed = function (ary) { - return _elm_lang$core$Native_Utils.eq( - 0, - _elm_lang$core$Array$length( - A2( - _elm_lang$core$Array$filter, - function (x) { - return _elm_lang$core$Native_Utils.eq(x, _user$project$Player$Unclaimed); - }, - ary))); -}; var _user$project$Update$changePlayer = function (p) { var _p1 = p; switch (_p1.ctor) { @@ -9240,7 +9241,7 @@ var _user$project$Update$update = F2( {ctor: '[]'}); case 'CheckWinner': var _p5 = _p2._0; - if (_user$project$Update$noneUnclaimed(model.boxes)) { + if (_user$project$GameLogic$noneUnclaimed(model.boxes)) { return A2( _elm_lang$core$Platform_Cmd_ops['!'], _elm_lang$core$Native_Utils.update( diff --git a/src/EventHandlers/OnClicked.elm b/src/EventHandlers/OnClicked.elm index 193c82f..489fa2d 100644 --- a/src/EventHandlers/OnClicked.elm +++ b/src/EventHandlers/OnClicked.elm @@ -4,13 +4,14 @@ module EventHandlers.OnClicked exposing ( onClicked ) @docs onClicked -} -import Array exposing (..) -import Maybe exposing (..) -import Model exposing (..) -import MyEvent exposing (..) -import Player exposing (..) -import Task exposing (..) -import Time exposing (..) +import Array exposing (..) +import Maybe exposing (..) +import Model exposing (..) +import MyEvent exposing (..) +import Player exposing (..) +import Task exposing (..) +import Time exposing (..) +import GameLogic exposing (noneUnclaimed) {-| onClicked -} onClicked : Model -> Int -> (Model, Cmd MyEvent) diff --git a/src/GameLogic.elm b/src/GameLogic.elm new file mode 100644 index 0000000..e89b7d5 --- /dev/null +++ b/src/GameLogic.elm @@ -0,0 +1,7 @@ +module GameLogic exposing (noneUnclaimed) + +import Array exposing (..) +import Player exposing (..) + +noneUnclaimed : Array Player -> Bool +noneUnclaimed ary = (0 == length(filter (\x -> x == Unclaimed) ary)) diff --git a/src/Update.elm b/src/Update.elm index a64ab73..2349456 100644 --- a/src/Update.elm +++ b/src/Update.elm @@ -1,11 +1,11 @@ module Update exposing ( update , changePlayer - , noneUnclaimed , currentPlayerWinning ) import Array exposing (..) import EventHandlers.OnClicked +import GameLogic exposing (noneUnclaimed) import Model exposing (..) import MyEvent exposing (..) import Player exposing (..) @@ -16,8 +16,6 @@ changePlayer p = case p of B -> A _ -> A -noneUnclaimed : Array Player -> Bool -noneUnclaimed ary = (0 == length(filter (\x -> x == Unclaimed) ary)) type alias GameStatus = Result String Player @@ -57,8 +55,8 @@ update msg model = CheckWinner player currentPlayerShouldChange _ -> if noneUnclaimed model.boxes then - -- it's a draw { model | winner = Just Unclaimed } ! [] + else case currentPlayerShouldChange of -- Legal move diff --git a/tests/Tests.elm b/tests/Tests.elm index d240665..df2c0bc 100644 --- a/tests/Tests.elm +++ b/tests/Tests.elm @@ -6,9 +6,10 @@ import Fuzz exposing (list, int, tuple, string) import String import List exposing (append) -import Unit.ModelTests exposing (modelTests) -import Unit.PlayerTests exposing (playerTests) -import Unit.UpdateTests exposing (updateTests) +import Unit.ModelTests exposing (modelTests) +import Unit.PlayerTests exposing (playerTests) +import Unit.UpdateTests exposing (updateTests) +import Unit.GameLogicTests exposing (gameLogicTests) all : Test @@ -16,4 +17,5 @@ all = modelTests |> append playerTests |> append updateTests + |> append gameLogicTests |> describe "Test Suite" diff --git a/tests/unit/GameLogicTests.elm b/tests/unit/GameLogicTests.elm new file mode 100644 index 0000000..9527f54 --- /dev/null +++ b/tests/unit/GameLogicTests.elm @@ -0,0 +1,21 @@ +module Unit.GameLogicTests exposing (gameLogicTests) + +import Test exposing (..) +import Expect +import Array +import GameLogic exposing (noneUnclaimed) +import Model exposing (Model, initialModel) +import Player exposing (Player (A, B, Unclaimed)) + +gameLogicTests : List Test +gameLogicTests = [ describe "GameLogic.noneUnclaimed" + [ test "returns True if none of the boxes are unclaimed" <| + \() -> + Expect.equal True + <| noneUnclaimed <| Array.fromList <| [A, B, A] + , test "returns False if at least one box is unclaimed" <| + \() -> + Expect.equal False + <| noneUnclaimed <| Array.fromList <| [A, Unclaimed, A] + ] + ] diff --git a/tests/unit/UpdateTests.elm b/tests/unit/UpdateTests.elm index 6741cde..7c8bd03 100644 --- a/tests/unit/UpdateTests.elm +++ b/tests/unit/UpdateTests.elm @@ -3,26 +3,55 @@ module Unit.UpdateTests exposing (updateTests) import Test exposing (..) import Expect import Array -import Player exposing (Player (A, B, Unclaimed)) -import Update exposing (changePlayer, noneUnclaimed, currentPlayerWinning) +import Model exposing (initialModel) +import MyEvent exposing (MyEvent(Clicked, CheckWinner, Reset)) +import Player exposing (Player (A, B, Unclaimed)) +import Update exposing (changePlayer, currentPlayerWinning, update) +import Time exposing (second) updateTests : List Test -updateTests = [ describe "Update.changePlayer" - [ test "switches turn from A to B" <| +updateTests = [ describe "Update.update" + [ test "Handles Reset" <| \() -> - Expect.equal B <| Update.changePlayer A - , test "switches turn from B to A" <| + Expect.equal (initialModel ! []) + <| update Reset initialModel + , test "Handles CheckWinner -- none unclaimed" <| \() -> - Expect.equal A <| Update.changePlayer B - ] - , describe "Update.noneUnclaimed" - [ test "returns True if none of the boxes are unclaimed" <| + let + message = (CheckWinner A False Time.second) + previousModel = { initialModel + | boxes = (Array.fromList [A, A, A, A, B, B, B, A, B]) + } + (newModel, _) = update message previousModel + in + Expect.equal (Just Unclaimed) (newModel.winner) + , test "Handles CheckWinner -- legal move with winner" <| + \() -> + let + message = (CheckWinner A True Time.second) + previousModel = { initialModel + | boxes = (Array.fromList [A, A, A, B, B, Unclaimed, Unclaimed, Unclaimed, Unclaimed]) + } + (newModel, _) = update message previousModel + in + Expect.equal (Just A) newModel.winner + , test "Handles CheckWinner -- legal move with no winner" <| \() -> - Expect.equal True - <| noneUnclaimed <| Array.fromList <| [A, B, A] - , test "returns False if at least one box is unclaimed" <| + let + message = (CheckWinner A True Time.second) + previousModel = { initialModel + | boxes = (Array.fromList [A, A, Unclaimed, B, B, Unclaimed, Unclaimed, Unclaimed, Unclaimed]) + } + (newModel, _) = update message previousModel + in + Expect.equal Nothing newModel.winner + , test "Handles CheckWinner -- illegal move" <| \() -> - Expect.equal False - <| noneUnclaimed <| Array.fromList <| [A, Unclaimed, A] + let + msg = (CheckWinner A False Time.second) + model = initialModel + in + Expect.equal (initialModel ! []) + <| update msg model ] ]