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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745391623a4625687.html

相关推荐

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

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

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信