-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
167 lines (153 loc) · 5.15 KB
/
main.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
// 10
let shipSunk = false;
// Provides the user visual feedback
let display = {
displayMessage: function(msg) {
let messageArea = document.querySelector('.message-area');
messageArea.innerHTML = msg;
},
displayHit: function(location) {
let hitMarker = document.getElementById(location);
hitMarker.setAttribute("class", "hit");
},
displayMiss: function(location) {
let missMarker = document.getElementById(location);
missMarker.setAttribute("class", "miss");
},
};
// Manages the data for game
let gameModel = {
boardsize: 7,
numShips: Math.floor(Math.random() * 3 + 2),
shipsSunk: 0,
numGuesses: 0,
shipLength: (Math.floor(Math.random() * 3) + Math.floor(Math.random() * 2) + 1),
ships: [{locations: [], hits: ["", "", ""]},
{locations: [], hits: ["", "", ""]},
{locations: [], hits: ["", "", ""]}],
spawnPoints: function() {
let locations;
for (let i = 0; i < this.numShips; i++) {
do {
locations = brain.createShip();
} while (this.collision(locations));
this.ships[i].locations = locations;
}
},
collision: function(locations) {
for (let i = 0; i < gameModel.numShips; i++) {
let ship = this.ships[i];
for (let x = 0; x < locations.length; x++) {
if (ship.locations.indexOf(locations[x]) >=0) {
return true;
}
}
}
return false;
},
fire: function(guess) {
for (let i = 0; i < this.numShips; i++) {
let ship = this.ships[i];
let index = ship.locations.indexOf(guess);
if (index >= 0) {
ship.hits[index] = "x";
display.displayHit(guess);
display.displayMessage("HIT!");
if (this.isSunk(ship)) {
display.displayMessage("You've sunken my\n battleship!");
this.shipsSunk++;
}
return true;
}
}
display.displayMessage("MISS!")
display.displayMiss(guess);
return false;
},
isSunk: function(ship){
for (let i = 0; i < gameModel.shipLength; i++) {
if (ship.hits[i] !== 'x') {
return false;
}
}
return true;
}
}
let brain = {
parseGuess: function(guess) {
let alphabet = ["A", "B", "C", "D", "E", "F", "G"];
if (guess == null || guess.length !== 2) {
display.displayMessage("Invalid input")
};
row = alphabet.indexOf(guess.charAt(0));
column = guess.charAt(1);
if (isNaN(row) || isNaN(column)) {
alert("That is not a valid input");
} else if (row < 0 || row > gameModel.boardsize || column < 0 || column > gameModel.boardsize) {
alert("That is not a valid input");
} else {
gameModel.numGuesses++;
return row + column;
}
return null;
},
processGuess: function(guess) {
let location = this.parseGuess(guess);
if (location) {
gameModel.numGuesses++;
let shot = gameModel.fire(location);
if (shot && gameModel.shipsSunk === gameModel.numShips) {
display.displayMessage("You've sunken all my ships.");
}
return shot;
}
return false;
},
createShip: function() {
// ship direction: 0 = vertical, 1 = horizontal
let direction = Math.floor(Math.random() * 2);
let row;
let col;
if (direction === 1) {
row = Math.floor(Math.random() * gameModel.boardsize);
col = Math.floor(Math.random() * (gameModel.boardsize - 2));
} else {
col = Math.floor(Math.random() * gameModel.boardsize);
row = Math.floor(Math.random() * (gameModel.boardsize - 2));
}
let newShipLocations = [];
for (let i = 0; i < gameModel.shipLength; i++) {
if (direction === 1) {
newShipLocations.push(row + "" + (col + i));
} else {
newShipLocations.push((row + i) + "" + col);
}
}
console.log(newShipLocations)
return newShipLocations;
}
};
function fireButtonHandler() {
let guessInput = document.getElementById('userInput');
let guess = guessInput.value;
brain.processGuess(guess);
//reset form
guessInput.value = "";
}
function init() {
let fireButton = document.getElementById('fireButton');
fireButton.onclick = fireButtonHandler;
let guessInput = document.getElementById('userInput');
guessInput.addEventListener('keydown', (event) => {
if (event.code == 'Enter') {
event.preventDefault();
fireButtonHandler();
}
});
gameModel.spawnPoints();
display.displayMessage(`Be alert cadet`);
display.displayMessage(`${gameModel.numShips} enemies inbound`);
}
window.onload = init;
//let stats = "It took you " + guesses + " shots to sink the ship.\n" + "Accuracy: " + (3/guesses);
//alert(stats)