game-battleship
Version:
A battleship program
119 lines (87 loc) • 3.28 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: player.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: player.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const {parseCoordinates} = require("./utilities/helpers");
/**
* This entity is representing the player
*
* @class Player
* @constructor
* @param {Object} area - This repreents the battle area of the player
* @param {Array} targets - Array of all the predefined targets
* @param {string} opponentPlayerName - Name of the opponent
* @param {string} name - player's name
*/
class Player {
constructor({area, targets, opponentPlayerName, name}) {
this.area_ = area;
this.targets = targets;
this.opponentPlayerName_ = opponentPlayerName;
this.name = name;
}
/**
* Function to get next target of the player
* @param {Number} targetIndex : index of the desired target
* @returns {string} target at the index
*/
getNextTarget(targetIndex) {
return this.targets[targetIndex];
}
/**
* Function to take a hit on this player
* @param {string} coordinates : hit target
* @returns {boolean} returns true if hit else false
*/
takeHit(coordinates) {
const parsedCoordinates = parseCoordinates(coordinates);
const ship = this.area_.shipYard[parsedCoordinates[0]][parsedCoordinates[1]];
if (ship && !ship.isDestroyed()) {
ship.bombard();
console.log(`${this.opponentPlayerName_} fires a missile with target ${coordinates} which got hit`);
if (ship.isDestroyed()) {
this.area_.decrementShipCount();
}
return true;
}
console.log(`${this.opponentPlayerName_} fires a missile with target ${coordinates} which got miss`);
return false;
}
/**
* Function to check if the player is deafted or not
* @returns {Boolean} returns true if area is destroyed else false
*/
isDefeated() {
return this.area_.isAreaDestroyed();
}
}
module.exports = Player;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Area.html">Area</a></li><li><a href="Player.html">Player</a></li><li><a href="Ship.html">Ship</a></li></ul><h3>Global</h3><ul><li><a href="global.html#battle">battle</a></li><li><a href="global.html#fileReader">fileReader</a></li><li><a href="global.html#parseCoordinates">parseCoordinates</a></li><li><a href="global.html#validateArgs">validateArgs</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Feb 19 2019 14:14:50 GMT+0530 (India Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>