Sha256: 0c180182892c88ceafb5fe7a2f4484de8a93719df951146c6f90fa4e28f2793d

Contents?: true

Size: 1.1 KB

Versions: 396

Compression:

Stored size: 1.1 KB

Contents

module.exports = class Queens
  constructor: (options) ->
    if options == undefined
      options = { white: [0,3], black: [7,3] }

    if (options.white[0] == options.black[0] && options.white[1] == options.black[1])
      throw "Queens cannot share the same space"

    @white = options.white
    @black = options.black

  canAttack: ->
    canAttack = false

    canAttack = true if @white[0] == @black[0]
    canAttack = true if @white[1] == @black[1]

    yDiagonal = @white[0] - @black[0]
    xDiagonal = @white[1] - @black[1]

    canAttack = true if xDiagonal == yDiagonal
    canAttack = true if -xDiagonal == yDiagonal
    canAttack

  toString: ->
    @boardRepresentation()

  boardRow: (rowNumber) ->
    row = (@boardPosition(rowColumn, rowNumber) for rowColumn in [0..7])
    row.join(" ")

  boardPosition: (rowNumber, rowColumn) ->
    if @white[0] == rowNumber && @white[1] == rowColumn
      "W"
    else if @black[0] == rowNumber && @black[1] == rowColumn
      "B"
    else
      "_"

  boardRepresentation: ->
    boardRepresentation = (@boardRow row for row in [0..7])
    boardRepresentation.join("\n")

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.0.6.40 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.39 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.38 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.37 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.36 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.35 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.34 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.33 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.32 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.31 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.30 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.29 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.28 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.27 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.26 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.25 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.24 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.23 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.22 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.6.21 tracks/coffeescript/exercises/queen-attack/example.coffee