Skip to content

Instantly share code, notes, and snippets.

@johnomarkid
Created May 22, 2016 18:41
Show Gist options
  • Save johnomarkid/3d8c1bccb3af70b4e2e4c04ba98103ef to your computer and use it in GitHub Desktop.
Save johnomarkid/3d8c1bccb3af70b4e2e4c04ba98103ef to your computer and use it in GitHub Desktop.
potentialScore : Location -> Mark -> Board -> Int
potentialScore location mark board =
-- calculate the score for rows, cols, and verticals
-- return the max score
let
rowScore =
get (fst location) board
|> List.map (\v -> if v == mark then 1 else 0)
|> Debug.log "row: "
|> List.sum
colScore =
invertScoreboard board
|> get (snd location)
|> List.map (\v -> if v == mark then 1 else 0)
|> Debug.log "col: "
|> List.sum
-- only look at verticals if our location is on the vertical
upVerticalScore =
if List.member location (upVerticalLocations board) then
scoreUpVertical mark board
|> Debug.log "up vert: "
|> List.sum
else
0
downVerticalScore =
if List.member location (downVerticalLocations board) then
scoreDownVertical mark board
|> Debug.log "down vert: "
|> List.sum
else
0
in
List.maximum [ rowScore, colScore, upVerticalScore, downVerticalScore]
|> fromJust
createPotentialScoreboard : Mark -> Board -> List (List (Int, Location))
createPotentialScoreboard mark board =
(List.map2
(\row items ->
List.map2
(\col item ->
if item == NA then
-- calculate potential score for location if location is empty
(potentialScore (row, col) mark board, (row, col))
else
-- location empty. not a possible move.
(0, (row, col))
)
[0..((List.length board) - 1)]
items
)
[0..((List.length board) - 1)]
board
)
maxLocation : List (List (Int, Location)) -> Location
maxLocation board =
-- have board of potential scores and locations like:
-- [(2, (0, 2)), (1, (1, 2)), ..]
-- find the max score and that is the location we want to place our cpu move
let
loc =
List.concat board
|> List.sortBy fst
|> List.reverse
|> get 0
|> snd
in
loc
omarmax : Model -> (Location, Model)
omarmax model =
let
-- find available locations on board
-- calculate potential X score for each location
-- return the location that X has gained the most points in
turnLocation =
createPotentialScoreboard X model.board
|> Debug.log "scoreboard: "
|> maxLocation
in
(turnLocation, model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment