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.0 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.18 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.17 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.16 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.15 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.14 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.13 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.12 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.11 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.10 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.9 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.8 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.7 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.6 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.5 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.4 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.3 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.2 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.1 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.0.5.0 tracks/coffeescript/exercises/queen-attack/example.coffee