patternjavascriptMinor
Bare bones battleship game
Viewed 0 times
battleshipgamebonesbare
Problem
This is basically the beginnings of a battleship game. There is only 1 ship which takes up 3 of six locations on a 1 x 6 grid. The locations are created by finding a random location on the grid and then taking the next two points as the other 2 locations.
I basically just make sure the the guess is between 1 and 6, make sure the number has not been guessed before by comparing it to
How would I go about making this code object-oriented?
I basically just make sure the the guess is between 1 and 6, make sure the number has not been guessed before by comparing it to
guessarr (an array that contains all the valid guesses), then compare the guess to the random locations to determine if it's a hit or miss. After guessing all 3 random locations, the user wins.How would I go about making this code object-oriented?
var guess;
var guesses = 0;
var hits = 0;
var isSunk = false;
var guessCheck=-1;
var guessarr = [];
var valid = true;
var randomeloc = Math.floor(Math.random () * 5);
var location1 = randomeloc
var location2 = randomeloc + 1;
var location3 = randomeloc +2;
while (isSunk == false) {
guess = prompt("Enter a guess between 1 and 6");
if (guess 6) {
alert("Enter a valid number between 0 and 6");
}else if(guess > 0 || guess = 0) {
valid = false;
alert("You've already entered this number. Try again");
} else if (valid = true){
guesses = guesses +1;
if (guess == location1 ||
guess == location2 ||
guess == location3) {
hits = hits + 1;
alert ("You got a hit!");
guessarr.push(guess);
}
else {alert("miss");
}
if (hits == 3) {
isSunk= true;
alert("you won!");
}
}
}
}Solution
Be careful that the random can be between 0 and 4 when it should be between 1 and 4. If you want to make it more object oriented, I would separate the interface (alert and prompt) from the game logic. As this is just a game with 1 ship, I would only do a Game and Ship objects to represent it, but for more complex versions other objects may apply. Here is how I would implement it having custom columns:
function Game(columns){
this.guesses;
this.guessarr;
this.columns = columns;
this.ship;
this.ended;
return this;
}
Game.prototype.getRandomNumber = function(min,max){ // interval [)
return Math.random() * (max - min) + min;
}
Game.prototype.getRandomShipPosition = function(shipSize){
return this.getRandomNumber(1,this.columns - shipSize + 2);
}
Game.prototype.init = function(){
this.guesses = 0;
this.guessarr = [];
this.ship = new Ship(this.getRandomShipPosition(3), 3);
this.ended = false;
}
Game.prototype.makeGuess = function(guess){
if(isNaN(Number(guess))){
return "Enter a valid number";
}
if(guess this.columns){
return "Guess has to be between 1 and " + this.columns;
}
if(this.guessarr.indexOf(guess) != -1){
return "You've already entered this number. Try again";
}
this.guessarr.push(guess);
if(this.ship.makeGuess(guess)){
if(this.ship.isSunk){
this.ended = true;
}
return "You got a hit!";
}else{
return "Miss";
}
}
function Ship(start,size){
this.start = start;
this.hits = 0;
this.size = size;
this.isSunk = false;
return this;
}
Ship.prototype.makeGuess = function(guess){
if(guess >= this.start && guess <= this.start + this.size){
this.hits+=1;
if(this.hits == this.size){
this.isSunk = true;
}
return true;
}
return false;
}
var game = new Game(6);
game.init();
while(game.ended == false){
guess = Number(prompt("Enter a guess between 1 and 6"));
alert(game.makeGuess(guess))
}
alert("you won!")Code Snippets
function Game(columns){
this.guesses;
this.guessarr;
this.columns = columns;
this.ship;
this.ended;
return this;
}
Game.prototype.getRandomNumber = function(min,max){ // interval [)
return Math.random() * (max - min) + min;
}
Game.prototype.getRandomShipPosition = function(shipSize){
return this.getRandomNumber(1,this.columns - shipSize + 2);
}
Game.prototype.init = function(){
this.guesses = 0;
this.guessarr = [];
this.ship = new Ship(this.getRandomShipPosition(3), 3);
this.ended = false;
}
Game.prototype.makeGuess = function(guess){
if(isNaN(Number(guess))){
return "Enter a valid number";
}
if(guess <= 0 || guess > this.columns){
return "Guess has to be between 1 and " + this.columns;
}
if(this.guessarr.indexOf(guess) != -1){
return "You've already entered this number. Try again";
}
this.guessarr.push(guess);
if(this.ship.makeGuess(guess)){
if(this.ship.isSunk){
this.ended = true;
}
return "You got a hit!";
}else{
return "Miss";
}
}
function Ship(start,size){
this.start = start;
this.hits = 0;
this.size = size;
this.isSunk = false;
return this;
}
Ship.prototype.makeGuess = function(guess){
if(guess >= this.start && guess <= this.start + this.size){
this.hits+=1;
if(this.hits == this.size){
this.isSunk = true;
}
return true;
}
return false;
}
var game = new Game(6);
game.init();
while(game.ended == false){
guess = Number(prompt("Enter a guess between 1 and 6"));
alert(game.makeGuess(guess))
}
alert("you won!")Context
StackExchange Code Review Q#138798, answer score: 2
Revisions (0)
No revisions yet.