Skip to content

Commit

Permalink
Merge pull request #7 from lGrom/updateGraphicalTypes
Browse files Browse the repository at this point in the history
Update graphical types
  • Loading branch information
lGrom committed Feb 4, 2024
2 parents cf232e1 + 03cd1be commit f239d92
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 29 deletions.
14 changes: 2 additions & 12 deletions src/Board/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export default class Board extends React.Component {
ship.setPlayType(newType);
this.setState({ board: this.state.board.setShip(index, ship) });
}

this.state.board.computeGraphicalTypes();
}

displayBoard () {
Expand All @@ -49,15 +51,3 @@ export default class Board extends React.Component {
);
}
}

export const RelativePositions = {
TOP_LEFT: 0,
TOP: 1,
TOP_RIGHT: 2,
LEFT: 3,
// CENTER: 4, (this)
RIGHT: 5,
BOTTOM_LEFT: 6,
BOTTOM: 7,
BOTTOM_RIGHT: 8
};
74 changes: 72 additions & 2 deletions src/Board/BoardBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Ship, { PLAY_TYPES } from './Ship';
import Ship, { GRAPHICAL_TYPES, INTERNAL_TYPES, PLAY_TYPES } from './Ship';

/**
* The underlying Board class
Expand All @@ -16,6 +16,76 @@ export default class BoardBuilder {
this.boardState = createBoardState(this.width, this.height, this.preset);
}

computeGraphicalTypes () {
const board = this.boardState;

// uses setInternalType because that also runs setGraphicalType
for (let i = 0; i < board.length; i++) {
const board = this.boardState;
function setType (type) {
board[i].setInternalType(type);
}

const [isShip, isUnkown, isWater] = [BoardBuilder.isShips, BoardBuilder.isUnkown, BoardBuilder.isWater];

if (this.boardState[i].playType !== PLAY_TYPES.SHIP) continue;

// makes the edges act as water
const left = this.getRelativeShip(i, RELATIVE_POSITIONS.LEFT) || new Ship(PLAY_TYPES.WATER);
const top = this.getRelativeShip(i, RELATIVE_POSITIONS.TOP) || new Ship(PLAY_TYPES.WATER);
const right = this.getRelativeShip(i, RELATIVE_POSITIONS.RIGHT) || new Ship(PLAY_TYPES.WATER);
const bottom = this.getRelativeShip(i, RELATIVE_POSITIONS.BOTTOM) || new Ship(PLAY_TYPES.WATER);

// now just do all the logic from here and have a grand old time
if (isWater([left, top, right, bottom])) setType(GRAPHICAL_TYPES.SINGLE);
else if (
isShip([left, right]) ||
(isShip(left) && isUnkown(right)) ||
(isShip(right && isUnkown(left)))) setType(INTERNAL_TYPES.HORIZONTAL);
else if (
isShip([top, bottom]) ||
(isShip(top) && isUnkown(bottom)) ||
(isShip(bottom) && isUnkown(top))) setType(INTERNAL_TYPES.VERTICAL);

// now for the billion ship ship water cases
// else if (isShip(left) && )
else if (isShip(left) && isWater(right)) setType(GRAPHICAL_TYPES.LEFT);
else if (isShip(top) && isWater(bottom)) setType(GRAPHICAL_TYPES.UP);
else if (isShip(right) && isWater(left)) setType(GRAPHICAL_TYPES.RIGHT);
else if (isShip(bottom) && isWater(top)) setType(GRAPHICAL_TYPES.DOWN);
}
}

/**
* Returns true if all provided squares are a certain type
* @param {Ship | Ship[]} squares
* @param {number} type
* @returns
*/
static isPlayType (squares, type) {
if (Array.isArray(squares)) {
for (const square of squares) {
if (square.playType !== type) return false;
}

return true;
} else {
return squares.playType === type;
}
}

static isWater (squares) {
return BoardBuilder.isPlayType(squares, PLAY_TYPES.WATER);
}

static isShips (squares) {
return BoardBuilder.isPlayType(squares, PLAY_TYPES.SHIP);
}

static isUnkown (squares) {
return BoardBuilder.isPlayType(squares, PLAY_TYPES.UKNOWN);
}

/**
* @param {Array<Number>} coordinates - An array starting at 1 as [x, y]
* @returns {Number}
Expand Down Expand Up @@ -135,7 +205,7 @@ export default class BoardBuilder {
}
}

export const RelativePositions = {
export const RELATIVE_POSITIONS = {
TOP_LEFT: 0,
TOP: 1,
TOP_RIGHT: 2,
Expand Down
22 changes: 11 additions & 11 deletions src/Board/BoardBuilder.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-undef */
import BoardBuilder, { RelativePositions } from './BoardBuilder';
import BoardBuilder, { RELATIVE_POSITIONS } from './BoardBuilder';
import Ship, { PLAY_TYPES } from './Ship';
import { act } from 'react-dom/test-utils';

Expand Down Expand Up @@ -48,18 +48,18 @@ test('getShip', () => {
test('relativePositionToIndex', () => {
const board = new BoardBuilder(4, 4);

expect(board.relativePositionToIndex([2, 3], RelativePositions.RIGHT)).toBe(10);
expect(board.relativePositionToIndex([2, 3], RelativePositions.TOP_LEFT)).toBe(4);
expect(board.relativePositionToIndex([2, 3], RelativePositions.BOTTOM)).toBe(13);
expect(board.relativePositionToIndex([2, 3], RELATIVE_POSITIONS.RIGHT)).toBe(10);
expect(board.relativePositionToIndex([2, 3], RELATIVE_POSITIONS.TOP_LEFT)).toBe(4);
expect(board.relativePositionToIndex([2, 3], RELATIVE_POSITIONS.BOTTOM)).toBe(13);
});

test('setRelativeShip', () => {
const board = new BoardBuilder(4, 4);
const ship = new Ship(PLAY_TYPES.SHIP);

expect(board.setRelativeShip([2, 3], RelativePositions.TOP_LEFT, ship).getShip([1, 2])).toEqual(ship);
expect(board.setRelativeShip([2, 3], RelativePositions.RIGHT, ship).getShip([3, 3])).toEqual(ship);
expect(board.setRelativeShip([2, 3], RelativePositions.BOTTOM, ship).getShip([2, 4])).toEqual(ship);
expect(board.setRelativeShip([2, 3], RELATIVE_POSITIONS.TOP_LEFT, ship).getShip([1, 2])).toEqual(ship);
expect(board.setRelativeShip([2, 3], RELATIVE_POSITIONS.RIGHT, ship).getShip([3, 3])).toEqual(ship);
expect(board.setRelativeShip([2, 3], RELATIVE_POSITIONS.BOTTOM, ship).getShip([2, 4])).toEqual(ship);
});

test('getRelativeShip', () => {
Expand All @@ -71,9 +71,9 @@ test('getRelativeShip', () => {
board1.setShip([3, 1], ship1);
board2.setShip([3, 3], ship2);

expect(board1.getRelativeShip([2, 2], RelativePositions.TOP_RIGHT)).toBe(ship1);
expect(board2.getRelativeShip([2, 2], RelativePositions.BOTTOM_RIGHT)).toBe(ship2);
expect(board1.getRelativeShip([2, 2], RELATIVE_POSITIONS.TOP_RIGHT)).toBe(ship1);
expect(board2.getRelativeShip([2, 2], RELATIVE_POSITIONS.BOTTOM_RIGHT)).toBe(ship2);

expect(board1.getRelativeShip([1, 1], RelativePositions.TOP)).toBeNull();
expect(board1.getRelativeShip([1, 4], RelativePositions.BOTTOM)).toBeNull();
expect(board1.getRelativeShip([1, 1], RELATIVE_POSITIONS.TOP)).toBeNull();
expect(board1.getRelativeShip([1, 4], RELATIVE_POSITIONS.BOTTOM)).toBeNull();
});
22 changes: 18 additions & 4 deletions src/Board/Ship.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ export default class Ship {
}

toString () {
switch (this.playType) {
case PLAY_TYPES.UKNOWN:
switch (this.internalType) {
case INTERNAL_TYPES.UKNOWN:
return 'Uknown';
case PLAY_TYPES.WATER:
case INTERNAL_TYPES.WATER:
return 'Water';
case PLAY_TYPES.SHIP:
case INTERNAL_TYPES.SHIP:
return 'Ship';
case INTERNAL_TYPES.DOWN:
return 'Down';
case INTERNAL_TYPES.HORIZONTAL:
return 'Horizontal';
case INTERNAL_TYPES.LEFT:
return 'Left';
case INTERNAL_TYPES.RIGHT:
return 'Right';
case INTERNAL_TYPES.SINGLE:
return 'Single';
case INTERNAL_TYPES.UP:
return 'Up';
case INTERNAL_TYPES.VERTICAL:
return 'Vertical';
default:
return this.playType;
}
Expand Down

0 comments on commit f239d92

Please sign in to comment.