Skip to content

Commit d34e12e

Browse files
committed
add stub functions for cpu movement
Signed-off-by: Ryan1729 <[email protected]>
1 parent 3d4c321 commit d34e12e

File tree

3 files changed

+86
-15
lines changed

3 files changed

+86
-15
lines changed

Extras.elm

+31
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,34 @@ indexOfDefault : List a -> a -> Int
3030
indexOfDefault lst element =
3131
indexOfhelper lst element 0
3232
|> Maybe.withDefault -1
33+
34+
35+
36+
-- from https://github.com/elm-community/list-extra
37+
38+
39+
find : (a -> Bool) -> List a -> Maybe a
40+
find predicate list =
41+
case list of
42+
[] ->
43+
Nothing
44+
45+
first :: rest ->
46+
if predicate first then
47+
Just first
48+
else
49+
find predicate rest
50+
51+
52+
53+
-- from https://github.com/elm-community/maybe-extra
54+
55+
56+
orElseLazy : (() -> Maybe a) -> Maybe a -> Maybe a
57+
orElseLazy fma mb =
58+
case mb of
59+
Nothing ->
60+
fma ()
61+
62+
Just _ ->
63+
mb

Update.elm

+54-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Update exposing (update)
33
import Msg exposing (Msg(..))
44
import Model exposing (..)
55
import Material
6+
import Extras
7+
import Random.Pcg as Random exposing (Seed)
68

79

810
update : Msg -> Model -> ( Model, Cmd Msg )
@@ -22,7 +24,7 @@ update msg model =
2224
, selected = Nothing
2325
}
2426
in
25-
if checkForAnyLines newModel then
27+
if checkForAnyLines newModel.board then
2628
( { newModel | turnState = Win }, Cmd.none )
2729
else
2830
( newModel, Cmd.none )
@@ -39,26 +41,63 @@ update msg model =
3941

4042
cpuTurn : Model -> Model
4143
cpuTurn model =
44+
case model.selected of
45+
Just piece ->
46+
case makeCpuMove piece model.board of
47+
Just newBoard ->
48+
let
49+
newModel =
50+
{ model | board = newBoard }
51+
in
52+
if checkForAnyLines newModel.board then
53+
{ newModel | turnState = Loss }
54+
else
55+
newModel
56+
57+
Nothing ->
58+
model
59+
60+
Nothing ->
61+
model
62+
63+
64+
makeCpuMove piece board =
4265
let
43-
newModel =
44-
makeCpuMove model
66+
moves =
67+
getMoves piece board
4568
in
46-
if checkForAnyLines newModel then
47-
{ newModel | turnState = Loss }
48-
else
49-
newModel
69+
Extras.find (cpuWinningMove board) moves
70+
|> Extras.orElseLazy (\() -> Extras.find (nonLosingMove board) moves)
71+
|> Extras.orElseLazy (\() -> Random.step (Random.sample moves) (Random.initialSeed 42) |> fst)
72+
|> Maybe.map (applyMove board)
73+
74+
75+
applyMove : Board -> ( FloorId, SpaceId, Piece ) -> Board
76+
applyMove board ( floorId, spaceId, Piece shape colour pattern ) =
77+
board
78+
79+
80+
getMoves : Piece -> Board -> List ( FloorId, SpaceId, Piece )
81+
getMoves piece board =
82+
[]
83+
84+
85+
cpuWinningMove : Board -> ( FloorId, SpaceId, Piece ) -> Bool
86+
cpuWinningMove board ( floorId, spaceId, piece ) =
87+
False
5088

5189

52-
makeCpuMove model =
53-
model
90+
nonLosingMove : Board -> ( FloorId, SpaceId, Piece ) -> Bool
91+
nonLosingMove board ( floorId, spaceId, piece ) =
92+
False
5493

5594

56-
checkForAnyLines : Model -> Bool
57-
checkForAnyLines model =
58-
checkMultiFloorLines model.board
59-
|> andCheckFloor Top model.board
60-
|> andCheckFloor Middle model.board
61-
|> andCheckFloor Bottom model.board
95+
checkForAnyLines : Board -> Bool
96+
checkForAnyLines board =
97+
checkMultiFloorLines board
98+
|> andCheckFloor Top board
99+
|> andCheckFloor Middle board
100+
|> andCheckFloor Bottom board
62101

63102

64103
checkMultiFloorLines : Board -> Bool

elm-package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"elm-lang/core": "4.0.5 <= v < 5.0.0",
1313
"elm-lang/html": "1.1.0 <= v < 2.0.0",
1414
"elm-lang/svg": "1.1.1 <= v < 2.0.0",
15+
"mgold/elm-random-pcg": "3.0.4 <= v < 4.0.0",
1516
"robertjlooby/elm-generic-dict": "1.0.1 <= v < 2.0.0"
1617
},
1718
"elm-version": "0.17.1 <= v < 0.18.0"

0 commit comments

Comments
 (0)