← 输入框|github.com|刷新
Skip to content

Chapter 8 - Battleship  #23

Open
Open

Description

@akyjoon

Hi, I'm on chapter 8 where you build a battleship game. I have finished the Viewer and Model part without the Controller part. My problem:
I type in the console model.fire("somecell") to make 3 hits and drown a ship, but I keep getting that the ship is sunk on every hit. It never says "Hit!"

My code:
`var view = {
displayMessage: function(msg){
var messageArea = document.getElementById('messageArea');
messageArea.innerHTML = msg;
},
displayHit: function(location){
var cell = document.getElementById(location);
cell.setAttribute("class", "hit");
},
displayMiss: function(location) {
var cell = document.getElementById(location);
cell.setAttribute("class", "miss");
}
};

var model = {
boardSize : 7,
numShips: 3,
shipLength: 3,
shipsSunk: 0,

ships:[
{locations: ["10", "20", "30"], hits: ["", "", ""]},
{locations: ["32","33","34"], hits:["", "", ""]},
{locations: ["63","64","65"], hits:["", "", ""]}
],

fire: function(guess){
for(var i = 0; i < this.numShips; i++) {
var ship = this.ships[i];
var index = ship.locations.indexOf(guess);

  if (index >=0) {
    //trafiony
    ship.hits[index] = "hit";
    view.displayHit(guess);
    view.displayMessage("Hit!");

    if (this.isSunk(ship)){
      view.displayMessage("Sunk!");
      this.shipsSunk++;
    }
    return true;
  }
}
view.displayMiss(guess);
view.displayMessage("Miss!")
return false;

},
isSunk: function(ship) {
for (var i=0; i < this.shipLength; i++){
if (ship.hits[i] !== "hit") {
return false;
}
return true;
}
}
};
`

I compared the code and it's the same as in the book and don't understand why it won't work. Can you help me?
Btw. this book is awesome. I bought a polish version :)

Activity

  1. bethrobson commented on Aug 8, 2017

    @bethrobson
    Owner

    Hi,
    I just ran the code from the book and it works fine, so you must have a bug somewhere! Use the console to help track it down.

    Glad you like the book!

  2. akyjoon commented on Aug 8, 2017

    @akyjoon
    Author

    Thanks for the reply. Yes I ran it again today and it works! Yesterday it didn't run and console didn't show anything... Is there any possibility that the browser remembered the old wrong code and didn't run properly the one that actually should run?

  3. bethrobson commented on Aug 8, 2017

    @bethrobson
    Owner

    Oh good! Yes, sometimes the browser will cache the JavaScript. If you shift-reload, that can help.

  4. sssony commented on Dec 1, 2017

    @sssony

    Hello,
    I can not find the download link for chapter 8 for Buttleship with images.
    There is no Dowload menu in the http://www.wickedlysmart.com/hfjs/
    I would be very appreciate for yor help.

  5. padraigofearraigh commented on Dec 1, 2017

    @padraigofearraigh
  6. padraigofearraigh commented on Dec 1, 2017

    @padraigofearraigh
  7. sssony commented on Dec 2, 2017

    @sssony
  8. juwie5 commented on Dec 6, 2018

    @juwie5

    Hello, Good day am on Chapter 8 where you implement the parseGuess function I'm having issue as am not getting the required results in my console as in the book.

    hf javascript ch8
    here is the code
    function parseGuess (guess) {
    var alphabet = ["A", "B", "C", "D", "E", "F", "G"];

    if (guess === null || guess.lenght !== 2) {
        alert("Oops, please enter a letter and a number on the board."); 
    } else {
       firstChar = guess.charAt(0);
       var row = alphabet.indexOf(firstChar);
       var column = guess.charAt(1);
    
       if (isNaN(row) || isNaN(column)) {
            alert("Oops, that isnt on the board.");
       } else if (row < 0 ||  row >= model.boardSize ||
                  column < 0 || column >= model.boardSize) {
            alert("Oops, that's off the board!");
        } else {
           return row + column; 
        }
    } 
    return null; 
    

    }

    console.log(parseGuess("A0"));
    console.log(parseGuess("B6"));
    console.log(parseGuess("G3"));

  9. bethrobson commented on Dec 6, 2018

    @bethrobson
    Owner
  10. juwie5 commented on Dec 7, 2018

    @juwie5

    Thank you very much my bad

  11. juwie5 commented on Dec 18, 2018

    @juwie5

    good day still need help in understanding this code

    var view = {
    displayMessage: function(msg) {
    var messageArea = document.getElementById("messageArea");
    messageArea.innerHTML = msg;
    },

    displayHit: function(location) {
        var cell = document.getElementById(location);
        cell.setAttribute("class", "hit");
    },
    
    displayMiss: function(location) {
        var cell = document.getElementById(location);
        cell.setAttribute("class", "miss");
    }
    

    };

    view.displayMessage("Tap tap, is this thing on?");

    var model = {
    boardSize: 7,
    numShips: 3,
    shipsSunk: 0,
    shipLenght: 3,
    ships: [ {locations:["06", "16", "26"], hits:["", "", ""] },
    {locations:["24", "34", "44"], hits:["", "", ""] },
    {locations:["10", "11", "12"], hits:["", "", ""] }
    ],

    fire:function(guess) {
        for (var i = 0; i < this.numShips; i++) {
            var ship = this.ships[i];
            var index = ship.locations.indexOf(guess);
            if (index >= 0){
                ship.hits[index] = "hit";
                view.displayHit(guess);
                view.displayMessage("HIT!");
                if (this.isSunk(ship)) {
                    view.displayMessage("You sank my battleship!");
                    this.shipsSunk++;
                }
                return true;
            }
        }
        view.displayMiss(guess);
        view.displayMessage("You missed.");
        return false;
    },
    isSunk: function(ship) {
        for (var i = 0; i < this.shipLenght; i++) {
            if (ship.hits[i] !== "hit"){
                return false;
            }
        }
        return true;
    } 
    

    };

    function parseGuess (guess) {
    var alphabet = ["A", "B", "C", "D", "E", "F", "G"];

    if (guess === null || guess.length !== 2) {
        alert("Oops, please enter a letter and a number on the board."); 
    } else {
       firstChar = guess.charAt(0);
       var row = alphabet.indexOf(firstChar);
       var column = guess.charAt(1);
    
       if (isNaN(row) || isNaN(column)) {
            alert("Oops, that isnt on the board.");
       } else if (row < 0 ||  row >= model.boardSize ||
                  column < 0 || column >= model.boardSize) {
            alert("Oops, that's off the board!");
        } else {
           return row + column; 
        }
    } 
    return null; 
    

    }

    var controller = {
    guesses : 0,

    processGuess: function(guess) {
        var location = parseGuess(guess);
        if(location) {
            this.guesses++;
            var hit = model.fire(location);
            if (hit && model.shipsSunk === model.numShips){
                view.displayMessage ("You sank all my battleships, in " + this.guesses + "guesses");
            }
        }
    }
    

    };

    controller.processGuess("A0");

    chapter 8

  12. jonathanfowler commented on Jul 27, 2019

    @jonathanfowler

    Hello, I just started this exercise and so saw tables being used. I'm surprised...aren't tables old practice. Surely better to use divs with css flex layout??

  13. bethrobson commented on Jul 29, 2019

    @bethrobson
    Owner

    Jonathan, there's nothing wrong with using tables - they are a valid way to display data. Flex works great especially if you need flexibility in cell sizing or row sizing, but we didn't need that for this grid. The concepts of modifying HTML and CSS with JavaScript are the similar no matter what underlying structure you use, and in this case, using a table was easier and more straightforward. In addition, the book is about JavaScript, not HTML & CSS, and we wanted to focus on the JS concepts rather than explaining flex which is trickier to understand for people not familiar with it.

    Thanks for your comment.

  14. Geisel-Diesel commented on Aug 26, 2020

    @Geisel-Diesel

    Hi, I'm getting this error?
    image

  15. bethrobson commented on Aug 26, 2020

    @bethrobson
    Owner

    You're missing a "this" in front of "ships", it should be "this.ships".

  16. Geisel-Diesel commented on Aug 26, 2020

    @Geisel-Diesel

    Sweet thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions