Last active
September 20, 2018 05:27
-
-
Save dschinkel/c34267aa9cbdd0623e52527c4272357d to your computer and use it in GitHub Desktop.
Elm - Dictionaries
This file contains 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
-- 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