最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a data model for a boat? - Stack Overflow

programmeradmin2浏览0评论

How would one create a model for a boat in javascript that exists as a grid reference in a cartesian plane?

I would like to learn javascript by creating clone of the popular game Battleship!

To this end I need assistance in my quest to start programming boats!

How would one create a model for a boat in javascript that exists as a grid reference in a cartesian plane?

I would like to learn javascript by creating clone of the popular game Battleship!

To this end I need assistance in my quest to start programming boats!

Share Improve this question edited Nov 14, 2015 at 21:32 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 5, 2011 at 4:37 azureazure 311 silver badge2 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

Here's something to get you started:

function Boat(name, length) {
  this.name = name
  this.pegs = new Array(length)
  this.sunk = false
}

Boat.prototype.place = function (x, y, orientation) {
  // Before calling this method you'd need to confirm
  // that the position is legal (on the board and not
  // conflicting with the placement of existing ships).
  // `x` and `y` should reflect the coordinates of the
  // upper-leftmost peg position.
  for (var idx = 0, len = this.pegs.length; idx < len; idx++) {
    this.pegs[idx] = {x: x, y: y, hit: false}
    if (orientation == 'horizontal') x += 1
    else                             y += 1
  }
}

Boat.prototype.hit = function (x, y) {
  var sunk = true
  var idx = this.pegs.length
  while (idx--) {
    var peg = this.pegs[idx]
    if (peg.x == x && peg.y == y) peg.hit = true
    // If a peg has not been hit, the boat is not yet sunk!
    if (!peg.hit) sunk = false
  }
  return this.sunk = sunk // this is assignment, not parison
}

Usage:

var submarine = new Boat('submarine', 3)
submarine.place(2, 6, 'horizontal')
submarine.hit(2, 6) // false
submarine.hit(3, 6) // false
submarine.hit(4, 6) // true

Storing pegs as objects with x, y, and hit keys is not necessarily the best approach. If you wanted to be clever you could, for example, store the upper-leftmost coordinates on the object along with the orientation. Then, the hits could be stored in an array. Something like:

name: 'submarine'
x: 2
y: 6
orientation: 'horizontal'
pegs: [0, 0, 0]

After a hit at (2, 6), the boat's properties would be:

name: 'submarine'
x: 2
y: 6
orientation: 'horizontal'
pegs: [1, 0, 0]

I'd start off by creating an array (or two, one for each side) to hold the boats. This can be pretty simple, and just use the boat number as the array entry for "filled" positions.

My boat model would have a length (n "pegs"), a position (x, y), an orientation (vertical or horizontal), and a hit counter. Another option would be to just store each array position the boat occupies, which would make some stuff a little easier.

发布评论

评论列表(0)

  1. 暂无评论