Skip to content

Commit

Permalink
Merge pull request #5 from lGrom/board-abstraction
Browse files Browse the repository at this point in the history
forgot to switch branches
  • Loading branch information
lGrom committed Jan 28, 2024
2 parents 4688b45 + ac2e1ac commit cf232e1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TODO:
- [X] functions to get surrounding ships
- [X] make it playable (left/right click functions)
- [X] add support for pre-existing ships
- [ ] make setShip accept playtypes as well as ship objects
- [X] make setShip accept playtypes as well as ship objects
- [ ] make graphical types auto-compute
- [ ] make an automatic solver
- [ ] make a solvable board generator with with options for difficulty and guess and check
Expand Down
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Board from './Board/Board';
import BoardBuilder from './Board/BoardBuilder';
import Ship, { PLAY_TYPES } from './Board/Ship';

const preset = new BoardBuilder(4, 4).setShip([1, 3], new Ship(PLAY_TYPES.SHIP)).setShip([3, 1], new Ship(PLAY_TYPES.WATER));
const preset = new BoardBuilder(4, 4).setShip([1, 3], PLAY_TYPES.SHIP).setShip([3, 1], PLAY_TYPES.WATER);

class App extends React.Component {
render () {
Expand Down
16 changes: 8 additions & 8 deletions src/Board/BoardBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ export default class BoardBuilder {
setShip (position, value) {
const index = this.positionToIndex(position);

if (!(value instanceof Ship)) throw new Error('Invalid input: value must be instance of ship');
let ship = value;

if (value instanceof Ship) ship = value;
else if (typeof value === 'number') ship = new Ship(value);
else throw new Error('value should be an instance of Ship or a ship type');

const tmpBoard = this.boardState;
tmpBoard[index] = value;
tmpBoard[index] = ship;
this.boardState = tmpBoard;

return this;
Expand All @@ -84,6 +88,7 @@ export default class BoardBuilder {
if (index % this.width === 0 && relativePosition % 3 === 0) return null;
if (index % this.width === this.width - 1 && relativePosition % 3 === 2) return null;

// base vertical offset horizontal offset
const absIndex = (index) + ((Math.floor(relativePosition / 3) - 1) * this.width) + (relativePosition % 3 - 1);

// check absIndex is within the board
Expand Down Expand Up @@ -111,14 +116,9 @@ export default class BoardBuilder {
setRelativeShip (position, relativePosition, value) {
const index = this.relativePositionToIndex(position, relativePosition);

if (!(value instanceof Ship)) throw new Error('Invalid input: value must be instance of ship');
if (index === null) throw new Error('Index is not within board dimensions');

const tmpBoard = this.boardState;
tmpBoard[index] = value;
this.boardState = tmpBoard;

return this;
return this.setShip(index, value);
}

displayBoard () {
Expand Down
15 changes: 10 additions & 5 deletions src/Board/Ship.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/* eslint-disable no-unused-expressions */
/**
* The ship class for the board
* @param {Number} type - The play, graphical, or internal type of the ship
*/
export default class Ship {
playType;
graphicalType;
internalType;

constructor (playType, graphicalType, internalType) {
this.playType = playType;
this.graphicalType = graphicalType || this.playType;
this.internalType = internalType || this.playType;
constructor (type) {
// determine if it's a play, graphical, or internal type and use the respective function
if (type <= PLAY_TYPES.SHIP) this.setPlayType(type);
else if (type <= GRAPHICAL_TYPES.LEFT) this.setGraphicalType(type);
else if (type <= INTERNAL_TYPES.HORIZONTAL) this.setInternalType(type);
else throw new Error('type should be a play, graphical, or internal type');
}

toString () {
Expand Down

0 comments on commit cf232e1

Please sign in to comment.