Skip to content

Commit

Permalink
Merge pull request #84 from landonp1203/board_validation
Browse files Browse the repository at this point in the history
Board validation
  • Loading branch information
marcellocierro authored Feb 21, 2018
2 parents d8ac212 + 9e08b6b commit 531c069
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 13 deletions.
31 changes: 27 additions & 4 deletions Server Backend/helpers/Gameboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Gameboard {
this._initialized = false
}

set board(board) {
this._board = board
}

/**
* Size getter
*/
Expand Down Expand Up @@ -60,20 +64,39 @@ class Gameboard {
}

/**
* Method that places a word on the baord
* @param {Object} startCoords - object structured as such: {x: x, y: y}
* @param {Object} endCoords - object structured as such: {x: x, y: y}
* @param {String} word - word that will be placed on the board
*/
placeWord(startCoords, endCoords, word) {
const tempBoard = _.cloneDeep(this.board)
const val = this.validatePosition

for (let i = startCoords.x; i <= endCoords.x; i++) {
for (let j = startCoords.y; j <= endCoords.y; j++) {
if (startCoords.x === endCoords.x) {
this._board[j][i].letter = word[j - startCoords.y].toUpperCase()
} else {
this._board[j][i].letter = word[i - startCoords.x].toUpperCase()
let l = tempBoard[j][i].letter
let w = startCoords.x === endCoords.x ? word[j - startCoords.y].toUpperCase() : word[i - startCoords.x].toUpperCase()

if (!val(l, w)) {
return false
}

tempBoard[j][i].letter = w
}
}

this.board = tempBoard
return true
}

/**
* Validates whether a letter can be placed in the current position
* @param {String} currentLetter - the letter that is currently on the board
* @param {String} toBePlacedLetter - the letter that is to be placed on the board
*/
validatePosition(currentLetter, toBePlacedLetter) {
return currentLetter === '.' || currentLetter === toBePlacedLetter
}
}

Expand Down
140 changes: 131 additions & 9 deletions Server Backend/tests/test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
const GB = require('../helpers/Gameboard')
const Tile = require('../helpers/Tile')
/**
* Imports the lodash library
*/
const _ = require('lodash')

describe('Gameboard tests', () => {
it('Gameboard should be created', () => {
expect(new GB(15)).toBeTruthy()
expect(new GB(11)).toBeTruthy()
})

it('Gameboard should be initialized', () => {
const g = new GB(15)
const g = new GB(11)

g.init()

expect(g.initialized).toBe(true)
})

it('Gameboard should not be re-initialized', () => {
const g = new GB(15)
const g = new GB(11)

g.init()

expect(g.init()).toBe(true)
})

it('Gameboard size should be 15', () => {
const g = new GB(15)
it('Gameboard size should be 11', () => {
const g = new GB(11)

expect(g.size).toBe(15)
expect(g.size).toBe(11)
})

it('Gameboard should place word "OSWEGO" horizontally from (2,2) to (7, 2)', () => {
const g = new GB(15)
const g = new GB(11)

g.init()

Expand All @@ -49,14 +53,14 @@ describe('Gameboard tests', () => {
})

it('Gameboard should place word "OSWEGO" vertically from (2,2) to (2, 7)', () => {
const g = new GB(15)
const g = new GB(11)

g.init()

const word = 'OSWEGO'
const startX = 2
const startY = 2
const endX = 2
const startY = 2
const endY = 7

g.placeWord({ x: startX, y: startY }, { x: endX, y: endY }, word)
Expand All @@ -67,6 +71,124 @@ describe('Gameboard tests', () => {
}
}
})

it('Gameboard should not place a horizontal word in the cross section of a vertical word', () => {
const g = new GB(11)

g.init()

// Vertical Word
const word = 'OSWEGO'
const startX = 2
const startY = 2
const endX = 2
const endY = 7

// Horizontal Word
const word2 = 'BAD'
const startX2 = 1
const startY2 = 5
const endX2 = 3
const endY2 = 5

g.placeWord({ x: startX, y: startY }, { x: endX, y: endY }, word)
const validBoard = _.cloneDeep(g.board)
g.placeWord({ x: startX2, y: startY2 }, { x: endX2, y: endY2 }, word2)

for (let i = 0; i < g.board.length; i++) {
for (let j = 0; j < g.board[0].length; j++) {
expect(g.board[j][i].letter.toUpperCase()).toEqual(validBoard[j][i].letter.toUpperCase())
}
}
})

it('Gameboard should place a horizontal word in the cross section of a vertical word', () => {
const g = new GB(11)

g.init()

// Vertical Word
const word = 'OSWEGO'
const startX = 2
const startY = 2
const endX = 2
const endY = 7

// Horizontal Word
const word2 = 'BED'
const startX2 = 1
const startY2 = 5
const endX2 = 3
const endY2 = 5

g.placeWord({ x: startX, y: startY }, { x: endX, y: endY }, word)
g.placeWord({ x: startX2, y: startY2 }, { x: endX2, y: endY2 }, word2)

for (let i = startX2; i <= endX2; i++) {
for (let j = startY2; j <= endY2; j++) {
expect(g.board[j][i].letter.toUpperCase()).toEqual(word2[i - startX2])
}
}
})

it('Gameboard should not place a vertical word in the cross section of a horizontal word', () => {
const g = new GB(11)

g.init()

// Horitontal Word
const word = 'BAD'
const startX = 1
const startY = 5
const endX = 3
const endY = 5

// Vertical Word
const word2 = 'OSWEGO'
const startX2 = 2
const startY2 = 2
const endX2 = 2
const endY2 = 7

g.placeWord({ x: startX, y: startY }, { x: endX, y: endY }, word)
const validBoard = _.cloneDeep(g.board)
g.placeWord({ x: startX2, y: startY2 }, { x: endX2, y: endY2 }, word2)

for (let i = 0; i < g.board.length; i++) {
for (let j = 0; j < g.board[0].length; j++) {
expect(g.board[j][i].letter.toUpperCase()).toEqual(validBoard[j][i].letter.toUpperCase())
}
}
})

it('Gameboard should place a vertical word in the cross section of a horizontal word', () => {
const g = new GB(11)

g.init()

// Horizontal Word
const word = 'BED'
const startX = 1
const startY = 5
const endX = 3
const endY = 5

// Vertical Word
const word2 = 'OSWEGO'
const startX2 = 2
const startY2 = 2
const endX2 = 2
const endY2 = 7

g.placeWord({ x: startX, y: startY }, { x: endX, y: endY }, word)
g.placeWord({ x: startX2, y: startY2 }, { x: endX2, y: endY2 }, word2)

for (let i = startX2; i <= endX2; i++) {
for (let j = startY2; j <= endY2; j++) {
expect(g.board[j][i].letter.toUpperCase()).toEqual(word2[j - startY2])
}
}
})
})

describe('Tile tests', () => {
Expand Down

0 comments on commit 531c069

Please sign in to comment.