Javascript Battleship Game












-1















I made a simple battleship game in javascript but there is not workimg correctly. I put the locations in an array and tried to delete guessed locations from that array by using splice method. But it alerts "MISS!" when i enter any number between 0 and 6.



Edit: I solved the problem by using parseInt method for promt.



var location1 = Math.floor(Math.random() * 5);
var location2 = location1 + 1;
var location3 = location2 + 1;

var locations = ;
locations.push([location1, location2, location3]);

var guess;
var hits = 0;
var guesses = 0;

var isSunk = false;

while (isSunk == false) {
guess = prompt("Enter your guess(0 - 6)");
if (guess < 0 || guess > 6) {
alert("Please enter a valid number!");
} else {
guesses++;

if (locations.indexOf(guess) > 0) {
locations.splice(locations.indexOf(guess), 1);
hits++;
alert("HIT!");
if (hits == 3) {
isSunk = true;
alert("You sank my battleship!");
}
} else {
alert("MISS!");
}
}
}

var stats = "You took " + guesses + " guesses to sink the battleship, which
means your shooting accuracy was " + (3 / guesses);
alert(stats);









share|improve this question




















  • 1





    The .indexOf() test should be >= 0

    – Pointy
    Nov 24 '18 at 14:25






  • 1





    Also prompt() always returns a string.

    – Pointy
    Nov 24 '18 at 14:27











  • @Pointy That's true but is javascript doing conversions between string and number automatically?

    – H.Ozer
    Nov 24 '18 at 14:31






  • 1





    Sometimes yes, sometimes no. The .indexOf method does === comparisons.

    – Pointy
    Nov 24 '18 at 14:36











  • @Pointy Thank you.

    – H.Ozer
    Nov 24 '18 at 14:38
















-1















I made a simple battleship game in javascript but there is not workimg correctly. I put the locations in an array and tried to delete guessed locations from that array by using splice method. But it alerts "MISS!" when i enter any number between 0 and 6.



Edit: I solved the problem by using parseInt method for promt.



var location1 = Math.floor(Math.random() * 5);
var location2 = location1 + 1;
var location3 = location2 + 1;

var locations = ;
locations.push([location1, location2, location3]);

var guess;
var hits = 0;
var guesses = 0;

var isSunk = false;

while (isSunk == false) {
guess = prompt("Enter your guess(0 - 6)");
if (guess < 0 || guess > 6) {
alert("Please enter a valid number!");
} else {
guesses++;

if (locations.indexOf(guess) > 0) {
locations.splice(locations.indexOf(guess), 1);
hits++;
alert("HIT!");
if (hits == 3) {
isSunk = true;
alert("You sank my battleship!");
}
} else {
alert("MISS!");
}
}
}

var stats = "You took " + guesses + " guesses to sink the battleship, which
means your shooting accuracy was " + (3 / guesses);
alert(stats);









share|improve this question




















  • 1





    The .indexOf() test should be >= 0

    – Pointy
    Nov 24 '18 at 14:25






  • 1





    Also prompt() always returns a string.

    – Pointy
    Nov 24 '18 at 14:27











  • @Pointy That's true but is javascript doing conversions between string and number automatically?

    – H.Ozer
    Nov 24 '18 at 14:31






  • 1





    Sometimes yes, sometimes no. The .indexOf method does === comparisons.

    – Pointy
    Nov 24 '18 at 14:36











  • @Pointy Thank you.

    – H.Ozer
    Nov 24 '18 at 14:38














-1












-1








-1








I made a simple battleship game in javascript but there is not workimg correctly. I put the locations in an array and tried to delete guessed locations from that array by using splice method. But it alerts "MISS!" when i enter any number between 0 and 6.



Edit: I solved the problem by using parseInt method for promt.



var location1 = Math.floor(Math.random() * 5);
var location2 = location1 + 1;
var location3 = location2 + 1;

var locations = ;
locations.push([location1, location2, location3]);

var guess;
var hits = 0;
var guesses = 0;

var isSunk = false;

while (isSunk == false) {
guess = prompt("Enter your guess(0 - 6)");
if (guess < 0 || guess > 6) {
alert("Please enter a valid number!");
} else {
guesses++;

if (locations.indexOf(guess) > 0) {
locations.splice(locations.indexOf(guess), 1);
hits++;
alert("HIT!");
if (hits == 3) {
isSunk = true;
alert("You sank my battleship!");
}
} else {
alert("MISS!");
}
}
}

var stats = "You took " + guesses + " guesses to sink the battleship, which
means your shooting accuracy was " + (3 / guesses);
alert(stats);









share|improve this question
















I made a simple battleship game in javascript but there is not workimg correctly. I put the locations in an array and tried to delete guessed locations from that array by using splice method. But it alerts "MISS!" when i enter any number between 0 and 6.



Edit: I solved the problem by using parseInt method for promt.



var location1 = Math.floor(Math.random() * 5);
var location2 = location1 + 1;
var location3 = location2 + 1;

var locations = ;
locations.push([location1, location2, location3]);

var guess;
var hits = 0;
var guesses = 0;

var isSunk = false;

while (isSunk == false) {
guess = prompt("Enter your guess(0 - 6)");
if (guess < 0 || guess > 6) {
alert("Please enter a valid number!");
} else {
guesses++;

if (locations.indexOf(guess) > 0) {
locations.splice(locations.indexOf(guess), 1);
hits++;
alert("HIT!");
if (hits == 3) {
isSunk = true;
alert("You sank my battleship!");
}
} else {
alert("MISS!");
}
}
}

var stats = "You took " + guesses + " guesses to sink the battleship, which
means your shooting accuracy was " + (3 / guesses);
alert(stats);






javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 14:57







H.Ozer

















asked Nov 24 '18 at 14:24









H.OzerH.Ozer

375




375








  • 1





    The .indexOf() test should be >= 0

    – Pointy
    Nov 24 '18 at 14:25






  • 1





    Also prompt() always returns a string.

    – Pointy
    Nov 24 '18 at 14:27











  • @Pointy That's true but is javascript doing conversions between string and number automatically?

    – H.Ozer
    Nov 24 '18 at 14:31






  • 1





    Sometimes yes, sometimes no. The .indexOf method does === comparisons.

    – Pointy
    Nov 24 '18 at 14:36











  • @Pointy Thank you.

    – H.Ozer
    Nov 24 '18 at 14:38














  • 1





    The .indexOf() test should be >= 0

    – Pointy
    Nov 24 '18 at 14:25






  • 1





    Also prompt() always returns a string.

    – Pointy
    Nov 24 '18 at 14:27











  • @Pointy That's true but is javascript doing conversions between string and number automatically?

    – H.Ozer
    Nov 24 '18 at 14:31






  • 1





    Sometimes yes, sometimes no. The .indexOf method does === comparisons.

    – Pointy
    Nov 24 '18 at 14:36











  • @Pointy Thank you.

    – H.Ozer
    Nov 24 '18 at 14:38








1




1





The .indexOf() test should be >= 0

– Pointy
Nov 24 '18 at 14:25





The .indexOf() test should be >= 0

– Pointy
Nov 24 '18 at 14:25




1




1





Also prompt() always returns a string.

– Pointy
Nov 24 '18 at 14:27





Also prompt() always returns a string.

– Pointy
Nov 24 '18 at 14:27













@Pointy That's true but is javascript doing conversions between string and number automatically?

– H.Ozer
Nov 24 '18 at 14:31





@Pointy That's true but is javascript doing conversions between string and number automatically?

– H.Ozer
Nov 24 '18 at 14:31




1




1





Sometimes yes, sometimes no. The .indexOf method does === comparisons.

– Pointy
Nov 24 '18 at 14:36





Sometimes yes, sometimes no. The .indexOf method does === comparisons.

– Pointy
Nov 24 '18 at 14:36













@Pointy Thank you.

– H.Ozer
Nov 24 '18 at 14:38





@Pointy Thank you.

– H.Ozer
Nov 24 '18 at 14:38












1 Answer
1






active

oldest

votes


















0














Do one thing create Array with element like



var location=[1,2,3,4,5]


and instead of deleting index of array delete element from it






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459130%2fjavascript-battleship-game%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Do one thing create Array with element like



    var location=[1,2,3,4,5]


    and instead of deleting index of array delete element from it






    share|improve this answer




























      0














      Do one thing create Array with element like



      var location=[1,2,3,4,5]


      and instead of deleting index of array delete element from it






      share|improve this answer


























        0












        0








        0







        Do one thing create Array with element like



        var location=[1,2,3,4,5]


        and instead of deleting index of array delete element from it






        share|improve this answer













        Do one thing create Array with element like



        var location=[1,2,3,4,5]


        and instead of deleting index of array delete element from it







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 14:41









        Varun_AmbhireVarun_Ambhire

        93




        93






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459130%2fjavascript-battleship-game%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks