aboutsummaryrefslogtreecommitdiffstats
path: root/battleshipFiles/battleshipUserFunctions.js
blob: 86cb12f63f2cbbab87a0925f8a7c5bc9c12491e0 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function showUserShipPosition(givenTr, givenTrNumber, givenTd, givenTdNumber, player)
{
	// Preventing ability to place all ships on one td
	window.vars.wasShipPlaced = false;
	
	if(givenTr == null)
	{
		if(typeof window.vars.givenTr === "undefined")
			throw "window.vars.givenTr === undefined";
	}
	else
	{
		window.vars.givenTr = givenTr;
		window.vars.givenTd = givenTd;
		window.vars.givenTrNumber = givenTrNumber;
		window.vars.givenTdNumber = givenTdNumber;
	}
	
	let ship = window.vars.selectedShip.children.length;
	let newShip = ship;
	
	if(window.vars.selectedTd === undefined)
		window.vars.selectedTd = new Array(MAP_SIZE);
	
	clearUserShipPosition();
	
	let color;
	if(checkPositionForShip(givenTrNumber + 1, givenTdNumber + 1, window.vars.shipDirection, ship, player))
	{
		color = "bg-success";
		window.vars.isPlaceable = true;
		//givenTd.onclick = () => putShipOnGameMap(player, givenTdNumber + 1, givenTrNumber + 1, window.vars.shipDirection, ship, true);
	}
	else
	{
		color = "bg-danger";
		window.vars.isPlaceable = false;
	}
	
	if(window.vars.shipDirection === "vertically")
	{
		if(ship + givenTrNumber > MAP_SIZE)
			newShip = MAP_SIZE - givenTrNumber;
		
		for(let i = givenTrNumber - (ship - newShip); i < givenTrNumber + newShip; i++)
		{
			let td = givenTr.parentNode.children[i].children[givenTdNumber];
			window.vars.selectedTd[i] = [td, td.className];
			td.className = color;
		}
	}
	else if(window.vars.shipDirection === "horizontally")
	{
		if(ship + givenTdNumber > MAP_SIZE)
			newShip = MAP_SIZE - givenTdNumber;
		
		for(let i = givenTdNumber - (ship - newShip); i < givenTdNumber + newShip; i++)
		{
			window.vars.selectedTd[i] = [givenTr.children[i], givenTr.children[i].className];
			givenTr.children[i].className = color;
		}
	}
	// }
}

function clearUserShipPosition()
{
	//	Clear selection from table, used with onMouseLeave
	window.vars.selectedTd.forEach(oldTd => {
		if(oldTd !== undefined)
			oldTd[0].className = oldTd[1];
	});
}

function checkPositionForShip(x, y, direction, ship, player)
{
	// Changing x and y to match them with start of ship shown with onmouseenter
	[x, y] = correctXY(x, y, ship, direction);
	
	if(direction === "vertically")
	{
		for(let i = -1; i <= ship; i++)
			for(let j = -1; j < 2; j++)
				if(player.gameMap[x + i][y + j] === 1 || player.gameMap[x + i][y + j] === -1)
					return false;
	}
	else if(direction === "horizontally")
	{
		for(let i = -1; i <= ship; i++)
			for(let j = -1; j < 2; j++)
				if(player.gameMap[x + j][y + i] === 1 || player.gameMap[x + j][y + i] === -1)
					return false;
	}
	
	return true;
}