Skip to content

Instantly share code, notes, and snippets.

@fotonmoton
Created November 17, 2019 19:37
Show Gist options
  • Save fotonmoton/6c3397c7897a52f234741338f45cab26 to your computer and use it in GitHub Desktop.
Save fotonmoton/6c3397c7897a52f234741338f45cab26 to your computer and use it in GitHub Desktop.
module QueenAttack
let create (row, col) =
row >= 0 && row < 8 && col >= 0 && col < 8
let directions =
[ (-1, 0)
(-1, -1)
(0, -1)
(1, -1)
(1, 0)
(1, 1)
(0, 1)
(-1, 1) ]
let step (xDirection, yDirection) (row, col) =
(row + xDirection, col + yDirection)
let rec validLine direction position line =
match create position with
| false -> line
| true -> validLine direction (step direction position) (position :: line)
let validMoves position =
List.fold
(fun moves direction -> validLine direction position moves)
[]
directions
let same (x1, y1) (x2, y2) = x1 = x2 && y1 = y2
let underAttack moves position =
List.exists (fun move -> same move position) moves
let canAttack queen1 queen2 = underAttack (validMoves queen1) queen2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment