Created
September 11, 2018 22:49
-
-
Save dschinkel/aa235317319b854e9d68e1ee29c5d8e8 to your computer and use it in GitHub Desktop.
miniMax Algorithm in Elm - Tests - next move wins - Implementation 3 - With Fold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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