Skip to content

Commit 5f75bd7

Browse files
committed
fixed bug in battleship code
1 parent 750eb80 commit 5f75bd7

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

chapter8/battleship.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ var model = {
7373

7474
if (direction === 1) { // horizontal
7575
row = Math.floor(Math.random() * this.boardSize);
76-
col = Math.floor(Math.random() * (this.boardSize - this.shipLength));
76+
col = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1));
7777
} else { // vertical
78-
row = Math.floor(Math.random() * (this.boardSize - this.shipLength));
78+
row = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1));
7979
col = Math.floor(Math.random() * this.boardSize);
8080
}
8181

chapter8/battleship_tester.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ var model = {
9999

100100
if (direction === 1) { // horizontal
101101
row = Math.floor(Math.random() * this.boardSize);
102-
col = Math.floor(Math.random() * (this.boardSize - this.shipLength));
102+
col = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1));
103103
} else { // vertical
104-
row = Math.floor(Math.random() * (this.boardSize - this.shipLength));
104+
row = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1));
105105
col = Math.floor(Math.random() * this.boardSize);
106106
}
107107

0 commit comments

Comments
 (0)