Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Last active September 20, 2018 05:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dschinkel/c34267aa9cbdd0623e52527c4272357d to your computer and use it in GitHub Desktop.
Save dschinkel/c34267aa9cbdd0623e52527c4272357d to your computer and use it in GitHub Desktop.
Elm - Dictionaries
-- instead of using a 2D list or 2D array to represent a game node/board, lets use a Dict instead
-- with Dict unlike List or Array, it's much easier to insert, find, and update values
{--
repl:
make sure you have no trailing spaces after \. Also, might need to uses spaces not tab in Sublime text
import Dict
board = Dict.fromList \
[ \
((0,0),'X') \
,((0,1),' ') \
,((0,2),' ') \
,((1,1),'O') \
,((1,2),'O') \
,((1,3),' ') \
,((2,0),' ') \
,((2,1),' ') \
,((2,3),'X') \
]
-- repl output: Dict.fromList [((0,0),'X'),((0,1),' '),((0,2),' '),((1,1),'O'),((1,2),'O'),((1,3),' '),((2,0),' '),((2,1),' '),((2,3),'X')]
: Dict.Dict ( number, number1 ) Char
--}
empty : Char
empty =
' '
board = Dict.fromList
[
((0,0), 'X')
,((0,1), ' ')
,((0,2), ' ')
,((1,1), 'O')
,((1,2), 'O')
,((1,3), ' ')
,((2,0), ' ')
,((2,1), ' ')
,((2,3), 'X')
]
-- gets a list of all keys for open positions on the board
-- returns: [(0,1),(0,2),(1,3),(2,0),(2,1)] : List ( number, number1 )
openPositionCoords = Dict.filter (\k v -> v == ' ') board |> Dict.keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment