From 2b5c193ad5b7517014f3b0e4968be9332ce59800 Mon Sep 17 00:00:00 2001 From: msanvido Date: Sun, 20 Nov 2016 22:48:59 -0800 Subject: [PATCH] Extend Warship Example Change the color of each cell once a ship is hit. This makes it easier to see if a ship was sunk. --- warship/styles.css | 6 ++++++ warship/warship.ts | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/warship/styles.css b/warship/styles.css index a43a269..fda9b56 100644 --- a/warship/styles.css +++ b/warship/styles.css @@ -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; diff --git a/warship/warship.ts b/warship/warship.ts index 1988500..551516f 100644 --- a/warship/warship.ts +++ b/warship/warship.ts @@ -28,6 +28,7 @@ class Ship { isVertical = true; hits = 0; element: HTMLElement; + cells: Cell[]; constructor(public size: number) { this.element = $("
")[0]; @@ -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]; @@ -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 { @@ -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() {