Title: Chapter 8 - Battleship game. · Issue #26 · bethrobson/Head-First-JavaScript-Programming · GitHub
Open Graph Title: Chapter 8 - Battleship game. · Issue #26 · bethrobson/Head-First-JavaScript-Programming
X Title: Chapter 8 - Battleship game. · Issue #26 · bethrobson/Head-First-JavaScript-Programming
Description: I am trying to hard code the ships and try to hit and sunk them, but the message that Ships were either, hit, or sunk, or were missed is not displaying on the webpage. Instead, it's displaying as Ships were sank all the time on top. I ch...
Open Graph Description: I am trying to hard code the ships and try to hit and sunk them, but the message that Ships were either, hit, or sunk, or were missed is not displaying on the webpage. Instead, it's displaying as S...
X Description: I am trying to hard code the ships and try to hit and sunk them, but the message that Ships were either, hit, or sunk, or were missed is not displaying on the webpage. Instead, it's displaying ...
Opengraph URL: https://github.com/bethrobson/Head-First-JavaScript-Programming/issues/26
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Chapter 8 - Battleship game. ","articleBody":"I am trying to hard code the ships and try to hit and sunk them, but the message that Ships were either, hit, or sunk, or were missed is not displaying on the webpage. Instead, it's displaying as Ships were sank all the time on top. I checked the console for Errors, and there were none. I don't know what's wrong in my code. \r\n\r\nSo far my code looks like this. And the console is not showing any errors.\r\n\r\n```\r\n\r\n\r\nvar view = { \r\ndisplayMessage: function(msg){\r\n\tvar messageArea = document.getElementById(\"messageArea\");\r\n\tmessageArea.innerHTML = msg;\r\n},\r\ndisplayHit: function(location){\r\n\tvar cell = document.getElementById(location);\r\n\tcell.setAttribute(\"class\",\"hit\");\r\n},\r\ndisplayMiss: function(location){\r\n\tvar cell = document.getElementById(location);\r\n\tcell.setAttribute(\"class\",\"miss\");\r\n}\r\n\r\n};\r\n\r\n\r\nview.displayMiss(\"00\");\r\nview.displayHit(\"34\");\r\nview.displayHit(\"12\");\r\nview.displayMiss(\"25\");\r\n\r\nview.displayMessage(\"are you on?\");\r\n\r\nvar model = {\r\n\tboardsize: 7,\r\n\tnumships: 3,\r\n\tshipLength: 3,\r\n\tshipSunk: 0,\r\n ships: [{ locations: [\"06\", \"16\", \"26\"], hits: [\"hit\", \"\", \"\"] },\r\n { locations: [\"24\", \"34\", \"44\"], hits: [\"\", \"\", \"\"] },\r\n { locations: [\"10\", \"11\", \"12\"], hits: [\"\", \"\", \"\"] }],\r\nfire: function(guess) {\r\n\tfor(var i = 0; i \u003c this.numships; i++){\r\n\t\tvar ship = this.ships[i];\r\n\t\tvar index = ship.locations.indexOf(guess);\r\n\t\tif(index \u003e=0){\r\n\t\t\tship.hits[index] = \"hit\";\r\n\t\t\tview.displayHit(guess);\r\n\t\t\tview.displayMessage(\"HIT!\");\r\n\t\t\tif (this.isSunk(ship)){\r\n\t\t\t\tview.displayMessage(\"You sank my warship\");\r\n\t\t\t\tthis.shipSunk++;\r\n\t\t\t}\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n\tview.displayMiss(guess);\r\n\tview.displayMessage(\"You Missed.\");\r\n\treturn false;\r\n\t\r\n\t},\r\n\tisSunk: function(ship) {\r\n\t\tfor (var i = 0; i \u003c this.shipLength; i++){\r\n\t\t\tif (ship.hits[i] !== \"hit\"){\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn true;\r\n\r\n\t}\r\n};\r\n/*\r\nmodel.fire(\"53\");\r\nmodel.fire(\"06\");\r\nmodel.fire(\"16\");\r\nmodel.fire(\"26\");\r\nmodel.fire(\"34\");\r\nmodel.fire(\"24\");\r\nmodel.fire(\"44\");\r\nmodel.fire(\"12\");\r\nmodel.fire(\"11\");\r\nmodel.fire(\"10\");\r\n*/\r\nfunction parseGuess(guess){\r\n\tvar alphabet = [\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\"];\r\n\tif(guess === null || guess.length !== 2){\r\n\t\talert(\"Enter a valid number and letter\");\r\n\t\t} else{\r\n\t\t\tvar row = alphabet.indexOf(guess.charAt(0));\r\n\t\t\tvar column = guess.charAt(1);\r\n\t\t\t\r\n\t\t\tif (isNaN(row) || isNaN(column)){\r\n\t\t\talert(\"invalid number\");\r\n\t\t\t}\r\n\t\t\telse if (row \u003c 0 || row \u003e= model.boardsize || column \u003c 0 || column \u003e= model.boardsize){\r\n\t\t\t\talert(\"off the board\");\r\n\t\t} else {\r\n\t\t\treturn row + column;\r\n\t\t}\r\n\r\n\t\t}\r\n\t\treturn null;\r\n\t\t}\r\n\t\r\nvar controller = {\r\n\tguesses: 0,\r\n\tprocessGuess: function(guess){\r\n\t\tvar location = parseGuess(guess);\r\n\t\tif(location){\r\n\t\t\tthis.guesses++;\r\n\t\t\tvar hit = model.fire(location);\r\n\t\t\tif(hit \u0026\u0026 model.shipSunk === model.numships) {\r\n\t\t\t\tview.displayMessage(\"You sank the warship, in\" + this.guesses + \"guesses\");\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\ncontroller.processGuess(\"A0\");\r\ncontroller.processGuess(\"A6\");\r\ncontroller.processGuess(\"B6\");\r\ncontroller.processGuess(\"C6\");\r\ncontroller.processGuess(\"C4\");\r\ncontroller.processGuess(\"D5\");\r\ncontroller.processGuess(\"E4\");\r\ncontroller.processGuess(\"B0\");\r\ncontroller.processGuess(\"B1\");\r\ncontroller.processGuess(\"B2\");\r\n\r\nfunction init(){\r\n\tvar fireButton = document.getElementById(\"fireButton\");\r\n\tfireButton.onclick = handleFireButton;\r\n\tguessInput.onkeypress = handleKeyPress;\r\n}\r\n\t\r\n\tfunction handleKeyPress(e){\r\n\t\tvar fireButton = document.getElementById(\"fireButton\");\r\n\t\tif(e.keycode === 13){\r\n\t\t\tfireButton.click();\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\tfunction handleFireButton(){\r\n\t\tvar guessInput = document.getElementById(\"guessInput\");\r\n\t\tvar guess = guessInput.value;\r\n\t\tcontroller.processGuess(guess);\r\n\t\tguessInput.value = \"\";\r\n\t}\r\n\twindow.onload = init;\r\n\r\n```\r\n\r\n \r\n \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n```","author":{"url":"https://github.com/KU740","@type":"Person","name":"KU740"},"datePublished":"2020-10-09T13:32:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/26/Head-First-JavaScript-Programming/issues/26"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6f184156-50ca-f0c4-2953-05227efbd395 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C864:2B8087:7DFA4C:A917FF:697314C7 |
| html-safe-nonce | 728390cd7324c4c78c7d0690b17205f42ce9640d275c2ee1915b09b181a1baee |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODY0OjJCODA4Nzo3REZBNEM6QTkxN0ZGOjY5NzMxNEM3IiwidmlzaXRvcl9pZCI6IjQ5MDkyNjU5NjQ2MjYwMjM2MjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | aadf2dc6b67268c0eb110bba0ab93244cd63d5f8661b894a7e078d7b52acc99b |
| hovercard-subject-tag | issue:718149368 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/bethrobson/Head-First-JavaScript-Programming/26/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2f8197a02e64e7a3df386311738d8a83bba5c4bd47690da53a4e652dafafa525/bethrobson/Head-First-JavaScript-Programming/issues/26 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2f8197a02e64e7a3df386311738d8a83bba5c4bd47690da53a4e652dafafa525/bethrobson/Head-First-JavaScript-Programming/issues/26 |
| og:image:alt | I am trying to hard code the ships and try to hit and sunk them, but the message that Ships were either, hit, or sunk, or were missed is not displaying on the webpage. Instead, it's displaying as S... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KU740 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 44ab3188c1dcfe3be0f9c3feca2e04e14fb79f120939ce2395e4f15ab96ec1d4 |
| turbo-cache-control | no-preview |
| go-import | github.com/bethrobson/Head-First-JavaScript-Programming git https://github.com/bethrobson/Head-First-JavaScript-Programming.git |
| octolytics-dimension-user_id | 30760 |
| octolytics-dimension-user_login | bethrobson |
| octolytics-dimension-repository_id | 8868903 |
| octolytics-dimension-repository_nwo | bethrobson/Head-First-JavaScript-Programming |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 8868903 |
| octolytics-dimension-repository_network_root_nwo | bethrobson/Head-First-JavaScript-Programming |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a5e2b48bd1260476599758f5d253b5d24092ab84 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width