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.2.1.180 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.179 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.178 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.177 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.176 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.175 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.174 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.173 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.172 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.171 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.170 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.169 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.167 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.166 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.165 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.164 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.163 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.162 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.161 tracks/coffeescript/exercises/queen-attack/example.coffee
trackler-2.2.1.160 tracks/coffeescript/exercises/queen-attack/example.coffee