Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Created September 11, 2018 23:41
Show Gist options
  • Save dschinkel/fef311191471743f5fc0e38a25f3141f to your computer and use it in GitHub Desktop.
Save dschinkel/fef311191471743f5fc0e38a25f3141f to your computer and use it in GitHub Desktop.
miniMax Algorithm in Elm - Tests - next move wins - Implementation 3 - Fold using List.all instead
module Scorer exposing (..)
import Array exposing (fromList, get, map, set, toList)
import List exposing (all, foldl, map)
import Maybe exposing (withDefault)
markerX : Char
markerX =
'X'
empty : Char
empty =
' '
nextBestMove : List (List Char) -> Int
nextBestMove gameNode =
let
node =
List.map fromList gameNode
gameNodeAsArray =
fromList node
row1 =
get 0 gameNodeAsArray
row1NextState =
set 2 markerX (withDefault (Array.initialize 3 (always empty)) row1)
row2 =
get 1 gameNodeAsArray
row2NextState =
set 2 markerX (withDefault (Array.initialize 3 (always empty)) row2)
in
if winnerFound markerX row1NextState then
2
else if winnerFound markerX row2NextState then
5
else
0
winnerFound : Char -> Array.Array Char -> Bool
winnerFound marker row =
let
rowAsList =
toList row
in
all (\cell -> cell == marker) rowAsList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment