Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Extend Warship Example #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions warship/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ body {
z-index: 2;
}

.cellShipHit {
opacity: 0.5;
background-color: #00CC00;
z-index: 2;
}

.cellMiss{
opacity: 0.5;
background-color: #008000;
Expand Down
13 changes: 12 additions & 1 deletion warship/warship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Ship {
isVertical = true;
hits = 0;
element: HTMLElement;
cells: Cell[];

constructor(public size: number) {
this.element = $("<div class='ship'></div>")[0];
Expand Down Expand Up @@ -185,6 +186,13 @@ class Board {
}
}

markShipBombed(ship: Ship) {
for (var i = 0; i < ship.size; i++) {
$(ship.cells[i].element).removeClass("cellHit");
$(ship.cells[i].element).addClass("cellShipHit");
}
}

bombCell(cellElem: HTMLElement) {
var cellPos = Cell.parseCellLocation($(cellElem).data("cellLocation"));
var cell = this.cells[cellPos.row][cellPos.column];
Expand All @@ -199,6 +207,7 @@ class Board {
var ship = this.ships[cell.shipIndex];
ship.hits++;
if (ship.isSunk()) {
this.markShipBombed(ship);
if (this.allShipsSunk()) {
this.onEvent.call(this, 'allSunk');
} else {
Expand Down Expand Up @@ -266,14 +275,16 @@ class Board {
var ship = this.ships[index]
ship.hits = 0;
var cells = ship.getCellsCovered();
ship.cells = []
for (var cell = 0; cell < cells.length; cell++) {
var cellPos = Cell.parseCellLocation(cells[cell]);
var targetCell = this.cells[cellPos.row][cellPos.column];
targetCell.shipIndex = index;
ship.cells.push(targetCell);
}
}

$(this.element).children(".cell").removeClass("cellHit cellMiss").addClass("notBombed");
$(this.element).children(".cell").removeClass("cellHit cellMiss cellShipHit").addClass("notBombed");
}

private allShipsSunk() {
Expand Down