|
| 1 | +var browserViewportHeight = $(window).height(); |
| 2 | +var browserViewportWidth = $(window).width(); |
| 3 | + |
| 4 | +function Board(name) { |
| 5 | + this.tileMap = []; |
| 6 | + this.name = name; |
| 7 | + this.ships = []; |
| 8 | + this.bombedTiles = []; |
| 9 | +} |
| 10 | + |
| 11 | +Board.prototype.placeShipsRandomly = |
| 12 | + function (arrayOfShips) { |
| 13 | + arrayOfShips.forEach(ship => { |
| 14 | + let directions = ["horizontal", "vertical"]; |
| 15 | + let direction = Math.floor(Math.random() * 2); |
| 16 | + |
| 17 | + let randomTile = this.getRandomTile(); |
| 18 | + while (!randomTile.ship === undefined) { |
| 19 | + randomTile = this.getRandomTile(); |
| 20 | + } |
| 21 | + |
| 22 | + let iRow = 0; |
| 23 | + let iCol = 0; |
| 24 | + switch (direction) { |
| 25 | + case 0: |
| 26 | + iCol = 1; |
| 27 | + break; |
| 28 | + case 1: |
| 29 | + iRow = 1; |
| 30 | + break; |
| 31 | + } |
| 32 | + |
| 33 | + let shipStartTile = { X: 0, Y: 0 }; |
| 34 | + let canPlaceshipFlag = this.canPlaceShip(ship.length, randomTile, shipStartTile, iRow, iCol); |
| 35 | + while (canPlaceshipFlag === false) { |
| 36 | + randomTile = this.getRandomTile(); |
| 37 | + canPlaceshipFlag = this.canPlaceShip(ship.length, randomTile, shipStartTile, iRow, iCol); |
| 38 | + } |
| 39 | + ship.placeShip(this.tileMap[shipStartTile.X][shipStartTile.Y], directions[direction], this); |
| 40 | + this.ships.push(ship); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | +Board.prototype.canPlaceShip = |
| 45 | + function (shipLenght, tile, startTile, iRow, iCol) { |
| 46 | + let result = false; |
| 47 | + let freeSpots = 0; |
| 48 | + startTile.X = tile.X; |
| 49 | + startTile.Y = tile.Y; |
| 50 | + |
| 51 | + let fitsInDirection = false; |
| 52 | + // Horizontaly |
| 53 | + if (iCol > 0) { |
| 54 | + //Right |
| 55 | + if (tile.Y + shipLenght <= this.tileMap[0].length) { |
| 56 | + fitsInDirection = true; |
| 57 | + } //Left |
| 58 | + else if (tile.Y - shipLenght >= 0) { |
| 59 | + fitsInDirection = true; |
| 60 | + iCol = -1; |
| 61 | + } |
| 62 | + } |
| 63 | + // Vertically |
| 64 | + else { |
| 65 | + //Down |
| 66 | + if (tile.X + shipLenght <= this.tileMap.length) { |
| 67 | + fitsInDirection = true; |
| 68 | + } |
| 69 | + //Up |
| 70 | + else if (tile.X - shipLenght >= 0) { |
| 71 | + fitsInDirection = true; |
| 72 | + iRow = -1; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (fitsInDirection) { |
| 77 | + let i = 0; |
| 78 | + let movRow = 0; |
| 79 | + let movCol = 0; |
| 80 | + while (freeSpots != shipLenght || i < shipLenght) { |
| 81 | + if (this.tileMap[tile.X + movRow][tile.Y + movCol].taken === |
| 82 | + false) { |
| 83 | + freeSpots++; |
| 84 | + } else { |
| 85 | + console.log(this.tileMap[tile.X + movRow][tile.Y + movCol].X + " Y:" + |
| 86 | + this.tileMap[tile.X + movRow][tile.Y + movCol].Y) |
| 87 | + return false; |
| 88 | + } |
| 89 | + i++; |
| 90 | + movCol = movCol + iCol; |
| 91 | + movRow = movRow + iRow; |
| 92 | + } |
| 93 | + //Direction is vertical up |
| 94 | + if (iRow < 0) { |
| 95 | + startTile.X -= shipLenght - 1; |
| 96 | + } |
| 97 | + //Direction is horizontal left |
| 98 | + else if (iCol < 0) { |
| 99 | + startTile.Y -= shipLenght - 1; |
| 100 | + } |
| 101 | + result = true; |
| 102 | + } |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | +Board.prototype.damageTile = |
| 107 | + function (tile) { |
| 108 | + if (tile.damageType === undefined) { |
| 109 | + console.log("stop"); |
| 110 | + } |
| 111 | + if (tile.taken === true) { |
| 112 | + tile.damageType = "tile-hit"; |
| 113 | + tile.classList = "tile-hit"; |
| 114 | + tile.ship.takeDamage(tile); |
| 115 | + } else { |
| 116 | + tile.damageType = "tile-missed-hit"; |
| 117 | + tile.classList = "tile-missed-hit"; |
| 118 | + } |
| 119 | + if (gameEngine.checkForWinner()) { |
| 120 | + return; |
| 121 | + } |
| 122 | + setTimeout(function () { |
| 123 | + console.log("Tile at X:" + tile.X + ", Y:" + tile.Y + " was damaged!"); |
| 124 | + }, 1000); |
| 125 | + } |
| 126 | + |
| 127 | +Board.prototype.getRandomTile = |
| 128 | + function () { |
| 129 | + let row = Math.floor(Math.random() * this.tileMap |
| 130 | + .length); |
| 131 | + let col = Math.floor(Math.random() * this.tileMap[0] |
| 132 | + .length); |
| 133 | + let tile = this.tileMap[row][col]; |
| 134 | + return tile; |
| 135 | + } |
| 136 | + |
| 137 | +Board.prototype.generatePlayground = |
| 138 | + function () { |
| 139 | + let container = document.createElement("div"); |
| 140 | + container.className = "container"; |
| 141 | + let board = document.createElement("div"); |
| 142 | + board.className = "board"; |
| 143 | + container.appendChild(board); |
| 144 | + |
| 145 | + container.style.height = Math.floor(browserViewportHeight * 0.8) + "px"; |
| 146 | + container.style.width = Math.floor(browserViewportHeight * 0.8) + "px"; |
| 147 | + |
| 148 | + //Create top row notation tiles |
| 149 | + let tile = document.createElement("div"); |
| 150 | + tile.className = "tile notation"; |
| 151 | + board.appendChild(tile); |
| 152 | + for (let i = 0; i < 10; i++) { |
| 153 | + let notString = String.fromCharCode(65 + i); |
| 154 | + tile = document.createElement("div"); |
| 155 | + tile.className = "tile notation"; |
| 156 | + tile.id = notString; |
| 157 | + tile.innerText = notString; |
| 158 | + board.appendChild(tile); |
| 159 | + } |
| 160 | + //Create all other rows |
| 161 | + for (let row = 1; row < 11; row++) { |
| 162 | + tile = document.createElement("div"); |
| 163 | + tile.className = "tile notation"; |
| 164 | + tile.id = row; |
| 165 | + tile.innerText = row; |
| 166 | + board.appendChild(tile); |
| 167 | + |
| 168 | + this.tileMap.push([]); |
| 169 | + for (let d = 0; d < 10; d++) { |
| 170 | + tile = document.createElement("div"); |
| 171 | + tile.className = "tile"; |
| 172 | + tile.X = row - 1; |
| 173 | + tile.Y = d; |
| 174 | + tile.taken = false; |
| 175 | + tile.damageType = "tile"; |
| 176 | + tile.owner = this; |
| 177 | + |
| 178 | + board.appendChild(tile); |
| 179 | + this.tileMap[row - 1].push(tile); |
| 180 | + } |
| 181 | + } |
| 182 | + this.container = container; |
| 183 | + |
| 184 | + let boardsCont = document.getElementById("boards-container"); |
| 185 | + boardsCont.appendChild(container); |
| 186 | + } |
| 187 | + |
| 188 | +Board.prototype.removeShips = |
| 189 | + function () { |
| 190 | + this.ships.forEach(ship => { |
| 191 | + for (let i = 0; i < ship.tiles.length; i++) { |
| 192 | + ship.tiles[i].classList.remove(ship.type + (i + 1), ship.direction); |
| 193 | + |
| 194 | + } |
| 195 | + ship.tiles = []; |
| 196 | + }); |
| 197 | + while (this.ships.length > 0) { |
| 198 | + this.ships.pop(); |
| 199 | + } |
| 200 | + } |
0 commit comments