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.4.0 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.9 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.8 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.7 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.6 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.5 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.4 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.3 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.2 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.1 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.3.0 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.2.0 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.1.2 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.1.1 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.1.0 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.0.10 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.0.9 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.0.8 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.0.7 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.0.6 tracks/coffeescript/exercises/queen-attack/example.coffee