Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Created September 11, 2018 22:49
Show Gist options
  • Save dschinkel/aa235317319b854e9d68e1ee29c5d8e8 to your computer and use it in GitHub Desktop.
Save dschinkel/aa235317319b854e9d68e1ee29c5d8e8 to your computer and use it in GitHub Desktop.
miniMax Algorithm in Elm - Tests - next move wins - Implementation 3 - With Fold
module Scorer exposing (..)
import Array exposing (fromList, get, map, set)
import List exposing (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
currentBoard =
fromList node
row1 =
get 0 currentBoard
row1NextState =
set 2 markerX (withDefault (Array.initialize 3 (always empty)) row1)
row2 =
get 1 currentBoard
row2NextState =
set 2 markerX (withDefault (Array.initialize 3 (always empty)) row2)
in
if winnerFound row1NextState then
2
else if winnerFound row2NextState then
5
else
0
markerFound : Char -> Array.Array Char -> Array.Array Bool
markerFound marker row =
Array.map (\cell -> cell == marker) row
winnerFound : Array.Array Char -> Bool
winnerFound row =
Array.foldl (\wasFound found -> wasFound == found) True (markerFound markerX row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment