Avatar of dantetekanem

dantetekanem's solution

to Queen Attack in the TypeScript Track

Published at May 02 2020 · 0 comments
Instructions
Test suite
Solution

Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.

In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.

A chessboard can be represented by an 8 by 8 array.

So if you're told the white queen is at (2, 3) and the black queen at (5, 6), then you'd know you've got a set-up like so:

_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _

You'd also be able to answer whether the queens can attack each other. In this case, that answer would be yes, they can, because both pieces share a diagonal.

Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xit to it.

Source

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

queen-attack.test.ts

import QueenAttack from "./queen-attack"

describe("Queens", () => {
  it("initialized with specific placement", () => {
    const queens = new QueenAttack({ white: [3, 7], black: [6, 1] })
    expect(queens.white).toEqual([3, 7])
    expect(queens.black).toEqual([6, 1])
  })

  xit("cannot occupy the same space", () => {
    const positioning: { black: [number, number]; white: [number, number] } = {
      black: [2, 4],
      white: [2, 4]
    }
    const expectedError = "Queens cannot share the same space"
    expect(() => new QueenAttack(positioning)).toThrow(expectedError)
  })

  xit("toString representation", () => {
    const positioning: { black: [number, number]; white: [number, number] } = {
      white: [2, 4],
      black: [6, 6]
    }
    const queens = new QueenAttack(positioning)
    const board = [
      "_ _ _ _ _ _ _ _",
      "_ _ _ _ _ _ _ _",
      "_ _ _ _ W _ _ _",
      "_ _ _ _ _ _ _ _",
      "_ _ _ _ _ _ _ _",
      "_ _ _ _ _ _ _ _",
      "_ _ _ _ _ _ B _",
      "_ _ _ _ _ _ _ _\n"
    ].join("\n")
    expect(queens.toString()).toEqual(board)
  })

  xit("queens cannot attack", () => {
    const queens = new QueenAttack({ white: [2, 3], black: [4, 7] })
    expect(queens.canAttack()).toEqual(false)
  })

  xit("queens can attack when they are on the same row", () => {
    const queens = new QueenAttack({ white: [2, 4], black: [2, 7] })
    expect(queens.canAttack()).toEqual(true)
  })

  xit("queens can attack when they are on the same column", () => {
    const queens = new QueenAttack({ white: [5, 4], black: [2, 4] })
    expect(queens.canAttack()).toEqual(true)
  })

  xit("queens can attack diagonally", () => {
    const queens = new QueenAttack({ white: [1, 1], black: [6, 6] })
    expect(queens.canAttack()).toEqual(true)
  })

  xit("queens can attack another diagonally", () => {
    const queens = new QueenAttack({ white: [0, 6], black: [1, 7] })
    expect(queens.canAttack()).toEqual(true)
  })

  xit("queens can attack yet another diagonally", () => {
    const queens = new QueenAttack({ white: [4, 1], black: [6, 3] })
    expect(queens.canAttack()).toEqual(true)
  })

  xit("queens can attack on a north-east/south-west diagonal", () => {
    const queens = new QueenAttack({ white: [7, 0], black: [0, 7] })
    expect(queens.canAttack()).toEqual(true)
  })

  xit("queens can attack on another ne/sw diagonal", () => {
    const queens = new QueenAttack({ white: [2, 6], black: [5, 3] })
    expect(queens.canAttack()).toEqual(true)
  })
})
interface Positions {
  white: [number, number],
  black: [number, number]
}

export default class QueenAttack {
  white: [number, number];
  black: [number, number];
  boardSize: number = 8;

  constructor(positions: Positions) {
    if (this._inTheSameSpace(positions)) {
      throw new Error('Queens cannot share the same space');
    }

    this.white = positions.white;
    this.black = positions.black;
  }

  toString() {
    let columns = new Array();
    for (let i = 0; i < this.boardSize; i++) {
      let rows = new Array();
      for (let k = 0; k < this.boardSize; k++) {
        if (i == this.white[0] && k == this.white[1]) {
          rows[k] = 'W';
        } else if (i == this.black[0] && k == this.black[1]) {
          rows[k] = 'B';
        } else {
          rows[k] = '_';
        }
      }
      columns[i] = rows.join(' ');
    }

    return columns.join('\n') + '\n';
  }

  canAttack() {
    // on the same row or same column
    if (this.white[0] == this.black[0] || this.white[1] == this.black[1]) {
      return true;
    }
    // diagonal attack
    let slope: number = Math.abs((this.black[1] - this.white[1]) / (this.black[0] - this.white[0]));
    if (slope === 1) {
      return true;
    }

    return false;
  }

  private _inTheSameSpace(pos: Positions): boolean {
    return pos.white[0] == pos.black[0] && pos.white[1] == pos.black[1]
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?