1
SHIPS = (('AirCraftCarrier', 5), ('Battleship', 4),
         ('Submarine', 3), ('Destroyer', 3), ('PatrolBoat', 2))
position = ('v', 'h')

def setShip(self, board, graphics):
        maxval = board.getSize() - 1
        coords = []
        sizes = [(v) for k, v in SHIPS]
        for i in sizes:
            legal = False
            while not legal:
                pos = choice(position)
                if pos == 'v':
                    x = randint(0, maxval-1)
                    y = some kind of code to change y while keeping x same
                if pos == 'h':
                    x = some kind of code to change x while keeping y same 
                    y = randint(0, maxval-1)

                if not board.isOccupied(x, y):
                    legal = True
                    coords.append((pos, x, y))
            #return grid.displayShip(x, y)
            return coords

Now if the choice v=vertical is chosen the y values must change while the x values stay the same. This will cause my ship to placed vertically. I don't know any way to make this work. I need the first value to be chosen randomly and then the values after that to just go the length of the ship, however the last value cannot be greater then 9, since my grid can only go to 9.

3
  • you only change value of y without doing anything with x, isn't that what you need? so like if pos == 'v': y = newy, no mention of x. Dec 16, 2011 at 3:30
  • no, but I still need a random value for x but that will stay the same as value changes.. like if you were to draw a vertical line on a graph, the x values will stay the same while the y values change
    – Suki
    Dec 16, 2011 at 4:48
  • 1
    What @yosukesabai is saying is that the x and y can be calculated independent of each other. Pick your random x, then (suppose your ship is length 4), pick a random starting y between 1 and 6. Then let your ship start at that y, so it occupies positions y,y+1,y+2,y+3. By design this won't be over 9 since you picked a starting y between 1 and 6. Dec 16, 2011 at 5:08

1 Answer 1

0

You must take out the variables you want not to change from the while loop that is checking the available positions. You can do this in more than one way. One option is this:

def setShip(self, board, graphics):
        maxval = board.getSize() - 1
        coords = []
        sizes = [v for k, v in SHIPS]
        for i in sizes:
            legal = False
            x = randint(0, maxval-1)
            y = randint(0, maxval-1)
            pos = choice(position)
            while not legal:
                if pos == 'v':
                    y = y+1 if y < 9 else 0
                if pos == 'h':
                    x = x+1 if x < 9 else 0
                if not board.isOccupied(x, y):
                    legal = True
                    coords.append((pos, x, y))

            return coords

Note that I also limited the value the position can take. If the boat disappears by one side it will appear from the other. The problem with this code is that the boat goes always in one direction (from 0 -> 9). You should change this maybe to select the direction you want. for example:

sense = choice([1, -1])

and then in the inner loop, foor example:

y = y + sense if y < 9 else 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.