Skip to content

Commit

Permalink
added options for pinned ships
Browse files Browse the repository at this point in the history
  • Loading branch information
lGrom committed Feb 8, 2024
1 parent 1f9d5cb commit d771950
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ TODO:
- [X] add support for pre-existing ships
- [X] make setShip accept playtypes as well as ship objects
- [X] make graphical types auto-compute
- [ ] add testing for pinned ships
- [ ] make an automatic solver
- [ ] make a solvable board generator with with options for difficulty and guess and check
- [ ] add styling to webpage
Expand Down
20 changes: 12 additions & 8 deletions src/Board/BoardBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ export default class BoardBuilder {
this.boardState = createBoardState(this.width, this.height, this.preset);
}

// consistency in syntax and whatnot could use some work here
computeGraphicalTypes () {
const board = this.boardState;

// uses setInternalType because that also runs setGraphicalType
for (let i = 0; i < board.length; i++) {
if (board[i].pinned) return;

// for legiability
const [isShip, isUnkown, isWater] = [Ship.isShips, Ship.isUnkown, Ship.isWater];
if (!isShip(this.boardState[i])) continue;
if (!isShip(board[i])) continue;

const board = this.boardState;
function setType (type) {
board[i].setInternalType(type);
}
Expand Down Expand Up @@ -96,16 +98,17 @@ export default class BoardBuilder {

/**
* @param {Array<Number>|Number} position - An index or array starting at 1 as [x, y]
* @param {Ship} value
* @param {Ship|number} value - The ship object or type
* @param {boolean} [pinned] - Should updateGraphicalTypes ignore the ship (only works if value is a ship type)
* @returns {Board} this
*/
setShip (position, value) {
setShip (position, value, pinned) {
const index = this.positionToIndex(position);

let ship = value;

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

const tmpBoard = this.boardState;
Expand Down Expand Up @@ -150,15 +153,16 @@ export default class BoardBuilder {
/**
* @param {Array<Number>|Number} position - An index or array starting at 1 as [x, y]
* @param {Number} relativePosition - The index relative to position
* @param {Ship} value
* @param {Ship|Number} value - The ship object or type
* @param {boolean} [pinned] - Should updateGraphicalTypes ignore the ship (only if value is a ship type)
* @returns {Board} this
*/
setRelativeShip (position, relativePosition, value) {
setRelativeShip (position, relativePosition, value, pinned) {
const index = this.relativePositionToIndex(position, relativePosition);

if (index === null) throw new Error('Index is not within board dimensions');

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

displayBoard () {
Expand Down
8 changes: 6 additions & 2 deletions src/Board/Ship.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/**
* 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 (type) {
/**
* @param {Number} type - The play, graphical, or internal type of the ship
* @param {boolean} [pinned] - Should the Ship's graphical type change (used for presets)
*/
constructor (type, pinned) {
// sets the play and graphical types
this.setInternalType(type);
this.pinned = pinned || false;
}

toString () {
Expand Down

0 comments on commit d771950

Please sign in to comment.