aboutsummaryrefslogtreecommitdiffstats
path: root/battleshipFiles/Battleship.js
blob: 9b2a62a1a4ec4c71d2110eaa9fce8280b12cd4ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Battleship
{
	constructor(name)
	{
		// Set name
		this.name = name;
		//  Initializing game map
		this.gameMap = createGameMap();
		//  Declaring how many of which type ships there would be
		this.shipsMap = new Map();
		this.shipsMap.set(4, NUM_OF_4);
		this.shipsMap.set(3, NUM_OF_3);
		this.shipsMap.set(2, NUM_OF_2);
		this.shipsMap.set(1, NUM_OF_1);
		this.shipsArray = [];
		for(let [key, value] of this.shipsMap)
			for(let i = 0; i < value; i++)
				this.shipsArray.push(key);
		
		this.howManyShips = eval(this.shipsArray.join('+'));
		// creating vars, used later
		this.destroyedShips = 0;
	}
}
function createGameMap()
{
	let gameMap = new Array(MAP_SIZE + 2);
	for(let i = 0; i < MAP_SIZE + 2; i++)
		gameMap[i] = new Array(MAP_SIZE + 2);
	
	for(let i = 0; i < MAP_SIZE + 2; i++)
		for(let j = 0; j < MAP_SIZE + 2; j++)
			gameMap[i][j] = 0;
	
	return gameMap;
}