Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Last active September 11, 2018 19:09
Show Gist options
  • Save dschinkel/13466c1b6ffa6b93171debb7b0c89fd7 to your computer and use it in GitHub Desktop.
Save dschinkel/13466c1b6ffa6b93171debb7b0c89fd7 to your computer and use it in GitHub Desktop.
miniMax Algorithm in Elm - Tests - next move wins for row 2 - With a 2-dimensional array instead
module Scorer exposing (..)
import Array exposing (fromList, get, set)
import List exposing (map)
import Maybe exposing (withDefault)
nextBestMove : List (List Char) -> Int
nextBestMove gameNode =
let
node =
map fromList gameNode
currentBoard =
fromList node
row1 =
get 0 currentBoard
row1NextState =
set 2 'X' (withDefault (Array.initialize 3 (always ' ')) row1)
row2 =
get 0 currentBoard
row2NextState =
set 5 'X' (withDefault (Array.initialize 3 (always ' ')) row2)
in
if (get 0 row1NextState == get 1 row1NextState) && (get 1 row1NextState == get 2 row1NextState) then
2
else if (get 0 row2NextState == get 1 row2NextState) && (get 1 row2NextState == get 2 row2NextState) then
5
else
0
module ScorerSpec exposing (..)
import Array exposing (fromList, get)
import ElmTestBDDStyle exposing (..)
import Expect exposing (..)
import Scorer exposing (..)
import Test exposing (..)
markerX =
'X'
empty =
' '
suite : Test
suite =
describe
"Scorer"
[ describe "Next Move"
[ it "next move wins for row 1" <|
let
gameNode =
[ [ markerX, markerX, empty ]
, [ empty, empty, empty ]
, [ empty, empty, empty ]
]
cellThreeIndex =
2
nextMove =
Scorer.nextBestMove gameNode
in
expect nextMove to equal cellThreeIndex
, it "next move wins for row 2" <|
let
gameNode =
[ [ empty, empty, empty ]
, [ markerX, markerX, empty ]
, [ empty, empty, empty ]
]
cellFifthIndex =
5
nextMove =
Scorer.nextBestMove gameNode
in
expect nextMove to equal cellFifthIndex
]
, it "next move blocks" <|
let
dummy =
' '
in
expect 0 to equal 1
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment