-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.js
197 lines (178 loc) · 8.47 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var board = []; //back-end array to hold values that are represented in View's table
var ship = 1; //Sets a ship variable to 1
var shipsRemaining = 3;
var torpedoes = 5; //the # of torpedoes that the user has
function buildTable() { //builds back-end array to hold values that are represented in View's table
for (var i = 0; i < 10; i++) {
board.push([0,0,0,0,0,0,0,0,0,0]);
}
}
function countTorpedoes() { //modifies the # of torpedoes the user has
torpedoes--;
}
function setSingleShips() {
var singleShip = 0;
while (singleShip < 3) { //This loop runs until there are 3 single ships
var row = Math.floor(Math.random()*10);
var column = Math.floor(Math.random()*10);
//check for if row and column are both between 1 and 8
if (row > 0 && row < 9 && column > 0 && column < 9){
if (board[row][column] === 1 || board[row+1][column] === 1 || board[row-1][column] === 1 || board[row][column+1] === 1 || board[row][column-1] === 1 || board[row+1][column+1] === 1 || board[row+1][column-1] === 1 || board[row-1][column-1] === 1 || board[row-1][column+1] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++ //increments the number of ships after 1 is set
}
}
//check for if row is between 1 and 8 and column is 0 (remove column-1)
else if (row > 0 && row < 9 && column < 9){
if (board[row][column] === 1 || board[row+1][column] === 1 || board[row-1][column] === 1 || board[row][column+1] === 1 || board[row+1][column+1] === 1 || board[row-1][column+1] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
//check for if row is between 1 and 8 and column is 9 (remove column+1)
else if (row > 0 && row < 9 && column > 0){
if (board[row][column] === 1 || board[row+1][column] === 1 || board[row-1][column] === 1 || board[row][column-1] === 1 || board[row+1][column-1] === 1 || board[row-1][column-1] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
//check for if row is 0 and column is between 1 and 8 (remove row-1)
else if (row < 9 && column > 0 && column < 9){
if (board[row][column] === 1 || board[row][column-1] === 1 || board[row][column+1] === 1 || board[row+1][column-1] === 1 || board[row+1][column] === 1 || board[row+1][column+1] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
//check for if row is 9 and column is between 1 and 8 (remove row+1)
else if (row > 0 && column > 0 && column < 9){
if (board[row][column] === 1 || board[row][column-1] === 1 || board[row][column+1] === 1 || board[row-1][column-1] === 1 || board[row-1][column] === 1 || board[row-1][column+1] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
//check for corners
else if(row === 0 && column === 0) { // top left corner
if (board[row][column] === 1 || board[row][column+1] === 1 || board[row+1][column+1] === 1 || board[row+1][column] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
else if(row === 0 && column === 9) { //top right corner
if (board[row][column] === 1 || board[row][column-1] === 1 || board[row+1][column-1] === 1 || board[row+1][column] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
else if(row === 9 && column === 0) { //bottom left corner
if (board[row][column] === 1 || board[row][column+1] === 1 || board[row-1][column+1] === 1 || board[row-1][column] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
else if(row === 9 && column === 9) { //bottom right corner
if (board[row][column] === 1 || board[row][column-1] === 1 || board[row-1][column-1] === 1 || board[row-1][column] === 1) {
// if the value of board equals 1, then do not place the ship and reroll random values.
console.log("duplicate: " + row + " " + column);
} else {
board[row][column] = ship;
console.log("row: " + row + " column: " + column);
singleShip++
}
}
} // end of while loop
} // end of function
function remainingShips() {
shipsRemaining--; //Decrements the ships remaining
}
function setFiveBlockShip() {
var row = Math.floor(Math.random()*10);
var column = Math.floor(Math.random()*10);
var verticalOrHorizontal = Math.floor(Math.random()*2); //random number to assign whether the ship is vertical(0) or horizontal (1)
console.log("Vertical or Horizontal: " + verticalOrHorizontal);
console.log("FiveBlock: "+ row + " " + column);
if (verticalOrHorizontal === 0) { //if random number is 0, make vertical ship
while (column + 4 > 9) {//if column + 4 is greater than 9 the ship will go off the board, so generate a new random number instead
column = Math.floor(Math.random()*10); //generate new number until it satisfies the while loop
}
//If the ship will fit, then set a 1 in each square in the vertical column
board[row][column] = ship;
board[row][column+1] = ship;
board[row][column+2] = ship;
board[row][column+3] = ship;
board[row][column+4] = ship;
}
if (verticalOrHorizontal === 1) {
while (row + 4 > 9 ) {//if the 5 block ship goes off the board then generate a new random number
row = Math.floor(Math.random()*10);
}
board[row][column] = ship;
board[row+1][column] = ship;
board[row+2][column] = ship;
board[row+3][column] = ship;
board[row+4][column] = ship;
}
}
//This needs to have a cheker for where it set ships
function setFourBlockShip() {
var fourShip = 0;
while (fourShip < 2) { //This loop runs until there are 3 single ships
var row = Math.floor(Math.random()*10);
var column = Math.floor(Math.random()*10);
var verticalOrHorizontal = Math.floor(Math.random()*2); //random number to assign whether the ship is vertical(0) or horizontal (1)
console.log("Vertical or Horizontal: " + verticalOrHorizontal);
console.log("FourBlock: "+ row + " " + column);
if (verticalOrHorizontal === 0) { //if random number is 1, make vertical ship
while (column + 3 > 9) {//if column + 4 is greater than 9 the ship will go off the board, so generate a new random number instead
column = Math.floor(Math.random()*10); //generate new number
}
//If the ship will fit, then set a 5 in each square in the vertical column
board[row][column] = ship;
board[row][column+1] = ship;
board[row][column+2] = ship;
board[row][column+3] = ship;
fourShip++;
}
if (verticalOrHorizontal === 1) {
while (row + 3 > 9 ) {//if the 5 block ship goes off the board then generate a new random number
row = Math.floor(Math.random()*10);
}
board[row][column] = ship;
board[row+1][column] = ship;
board[row+2][column] = ship;
board[row+3][column] = ship;
fourShip++;
}
}
}