|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>JSDoc: Source: module/util.js</title>
|
|
|
|
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
|
|
<!--[if lt IE 9]>
|
|
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
|
|
<![endif]-->
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div id="main">
|
|
|
|
|
|
|
|
<h1 class="page-title">Source: module/util.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<article>
|
|
|
|
<pre class="prettyprint source linenums"><code>/** @module util */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum of all possible states of a grid cell.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
export const GridCellState = {
|
|
|
|
// Empty cell, default state
|
|
|
|
Available: 'available',
|
|
|
|
|
|
|
|
// Disabled. Ship cannot be placed here.
|
|
|
|
Disabled: 'disabled',
|
|
|
|
|
|
|
|
// There is a ship in this cell.
|
|
|
|
Ship: 'ship',
|
|
|
|
|
|
|
|
// This cell contains part of a ship which was damaged but not sunk
|
|
|
|
Damaged: 'damaged',
|
|
|
|
|
|
|
|
// This cell contains part of a ship which was sunk
|
|
|
|
Sunk: 'sunk',
|
|
|
|
|
|
|
|
// This cell was targeted, but nothing was hit
|
|
|
|
Missed: 'missed',
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given grid cell state represents a ship in some way.
|
|
|
|
* @param {GridCellState} grid_cell_state
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isShipCell(grid_cell_state) {
|
|
|
|
return [
|
|
|
|
GridCellState.Ship,
|
|
|
|
GridCellState.Damaged,
|
|
|
|
GridCellState.Sunk,
|
|
|
|
].includes(grid_cell_state)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given grid cell state can be fired upon.
|
|
|
|
* @param {GridCellState} grid_cell_state
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isValidTargetCell(grid_cell_state) {
|
|
|
|
return [
|
|
|
|
GridCellState.Ship,
|
|
|
|
GridCellState.Available,
|
|
|
|
].includes(grid_cell_state)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum of all possible players.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
export const Player = {
|
|
|
|
One: 'player_one',
|
|
|
|
Two: 'player_two',
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum of all possible game states. These are player-agnostic.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
export const GameState = {
|
|
|
|
// Both players are choosing the number of ships to play with (1-5)
|
|
|
|
ChoosingNumberOfShips: 'choosing_number_of_ships',
|
|
|
|
|
|
|
|
// A player is placing their ships
|
|
|
|
PlayerSetup: 'player_setup',
|
|
|
|
|
|
|
|
// We are prompting to change to the other player
|
|
|
|
PromptPlayerChange: 'prompt_player_change',
|
|
|
|
|
|
|
|
// It is the player's turn to fire a missle at their opponent
|
|
|
|
PlayerTurn: 'player_turn',
|
|
|
|
|
|
|
|
// A player has won
|
|
|
|
PlayerVictory: 'player_victory',
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The various supported ship types, by dimension.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
export const ShipType = {
|
|
|
|
x1: '1x1',
|
|
|
|
x2: '1x2',
|
|
|
|
x3: '1x3',
|
|
|
|
x4: '1x4',
|
|
|
|
x5: '1x5',
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isShipType(type) {
|
|
|
|
return ['1x1', '1x2', '1x3', '1x4', '1x5'].includes(type)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a deep copy of the value passed in.
|
|
|
|
* @param {*} obj
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
export function clone(obj) {
|
|
|
|
// If it's just a value, return it.
|
|
|
|
if ( typeof obj !== 'object' || obj === null ) return obj
|
|
|
|
|
|
|
|
// If it's an array, copy its values.
|
|
|
|
if ( Array.isArray(obj) ) return obj.map(x => clone(x))
|
|
|
|
|
|
|
|
// If it's an object, copy its properties.
|
|
|
|
const copy = {}
|
|
|
|
for ( const prop in obj ) {
|
|
|
|
copy[prop] = clone(obj[prop])
|
|
|
|
}
|
|
|
|
return copy
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an absolute URL to a file w/in the project directory.
|
|
|
|
* @param {string} path
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
export function appUrl(path) {
|
|
|
|
if ( path.startsWith('/') ) path = path.slice(1)
|
|
|
|
return `${APP_BASE_PATH}${path}`
|
|
|
|
}
|
|
|
|
</code></pre>
|
|
|
|
</article>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<nav>
|
|
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-errors.html">errors</a></li><li><a href="module-lang.html">lang</a></li><li><a href="module-sounds.html">sounds</a></li><li><a href="module-util.html">util</a></li></ul><h3>Classes</h3><ul><li><a href="GameBoardComponent.html">GameBoardComponent</a></li><li><a href="GameStateService.html">GameStateService</a></li><li><a href="GridCellComponent.html">GridCellComponent</a></li><li><a href="module-errors.InvalidAdvanceStateError.html">InvalidAdvanceStateError</a></li><li><a href="module-errors.InvalidMissileFireAttemptError.html">InvalidMissileFireAttemptError</a></li><li><a href="module-errors.InvalidShipPlacementError.html">InvalidShipPlacementError</a></li><li><a href="module-sounds-Sound.html">Sound</a></li><li><a href="ScoreBoardComponent.html">ScoreBoardComponent</a></li><li><a href="TopLevelComponent.html">TopLevelComponent</a></li></ul>
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br class="clear">
|
|
|
|
|
|
|
|
<footer>
|
|
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Sat Sep 12 2020 16:40:09 GMT-0500 (Central Daylight Time)
|
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|