Skip to content

Commit 33dba98

Browse files
committed
Detect overlapping ships
1 parent 03e26ee commit 33dba98

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

web/static/js/components/game/my_board.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default class MyBoard extends Board {
4242

4343
const { size, orientation } = selectedShip;
4444

45-
const className = this._outOfBounds(y, x, orientation, size) ? 'ship-shape-invalid' : 'ship-shape';
45+
const className = this._validCoords(y, x, orientation, size) ? 'ship-shape' : 'ship-shape-invalid';
4646

4747
return (e) => {
4848
for (var i = 0; i < size; i++) {
@@ -80,11 +80,26 @@ export default class MyBoard extends Board {
8080
return ref;
8181
}
8282

83-
_outOfBounds(y, x, orientation, size) {
83+
_validCoords(y, x, orientation, size) {
84+
const { data } = this.props;
85+
let inbounds;
86+
8487
if (orientation === 'horizontal') {
85-
return (x + size) > 10;
88+
inbounds = (x + size) <= 10;
8689
} else {
87-
return (y + size) > 10;
90+
inbounds = (y + size) <= 10;
91+
}
92+
93+
let overlapping = false;
94+
95+
for (var i = 0; i < size; i++) {
96+
const coords = orientation === 'horizontal' ? `${y}${x + i}` : `${y + i}${x}`;
97+
if (data.grid[coords] != Constants.GRID_VALUE_WATER) {
98+
overlapping = true;
99+
break;
100+
}
88101
}
102+
103+
return inbounds && !overlapping;
89104
}
90105
}

0 commit comments

Comments
 (0)