user4480584
user4480584

Reputation:

B-ship game follow up: How to have the computer randomnize their ships?

Follow up from this:
I want to now randomnize the AI boat's locations. Also, can we try to do two boats?

Here's the code if you dont want to click the above link:

def drawboard(hitboard):
    print('|   |   |   |')
    print('| ' + hitboard[7] + ' | ' + hitboard[8] + ' | ' + hitboard[9] + ' |')
    print('|   |   |   |')
    print('-------------')
    print('|   |   |   |')
    print('| ' + hitboard[4] + ' | ' + hitboard[5] + ' | ' + hitboard[6] + ' |')
    print('|   |   |   |')
    print('-------------')
    print('|   |   |   |')
    print('| ' + hitboard[1] + ' | ' + hitboard[2] + ' | ' + hitboard[3] + ' |')
    print('|   |   |   |')

def aicorners(hitboard,spot_hit):
    if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
        hitboard[spot_hit] = 'x'
    else:
        hitboard[spot_hit] = 'o'
    print(drawboard(hitboard))

def aiedges(hitboard,spot_hit):
    if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
        hitboard[spot_hit] = 'x'
    else:
        hitboard[spot_hit] = 'o'

    print(drawboard(hitboard))

def aimiddle(hitboard,spot_hit):
    if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:
        hitboard[spot_hit] = 'x'
    else:
        hitboard[spot_hit] = 'o'

    print(drawboard(hitboard))

def hitplayer():
    pass
def main():
    gameisplaying = True
    while gameisplaying:
        hitboard = [' ' for i in range(10)]
        userready = input('Place your ships. Type done when you finished placing it.')
        while not userready == 'done':
            userready = input('Type done when you locate your ship.  ')
        shipissunk = False
        while shipissunk == False:
            spot_hit = input('Where\'s the hit?: 1-9  ')
            while not (spot_hit in '1 2 3 4 5 6 7 8 9'.split()):
                spot_hit = input ('Please tell me where the hit is: 1-9  ')
            spot_hit = int(spot_hit)
            if (spot_hit in [1,3,7,9]):
                aicorners(hitboard,spot_hit)
            elif (spot_hit in [2,4,6,8]):
                aiedges(hitboard,spot_hit)
            else:
                aimiddle(hitboard,spot_hit)

main()

Notice how I only have one boat, and it's set position of [1,2,3]. I want to change the location every time the user plays it.

Parts of code updated below: (should i replace the original code?)

def aicorners(hitboard,spot_hit,ship_spots,ship_spots2):
    if spot_hit in ship_spots or spot_hit in ship_spots2:
        hitboard[spot_hit] = 'x'
    else:
        hitboard[spot_hit] = 'o'
    print(drawboard(hitboard))
def main():
    import random
    possible_spots = [[1,2,3], [4,5,6], [7,8,9], [7,4,1], [8,5,2], [9,6,3]]
    possible_spots2 = [[1,2],[2,3],[4,5],[5,6],[7,8],[8,9],[1,4],[4,7],[2,5],[5,8],[3,6],[6,9]]
    ship_spots = random.choice(possible_spots)
    ship_spots2 = random.choice(possible_spots2)
    gameisplaying = True
    while gameisplaying:
        hitboard = [' ' for i in range(10)]
        userready = input('Place your ships. Type done when you finished placing it.')
        while not userready == 'done':
            userready = input('Type done when you locate your ship.  ')
        shipissunk = False
        while shipissunk == False:
            spot_hit = input('Where\'s the hit?: 1-9  ')
            while not (spot_hit in '1 2 3 4 5 6 7 8 9'.split()):
                spot_hit = input ('Please tell me where the hit is: 1-9  ')
            spot_hit = int(spot_hit)
            aicorners(hitboard,spot_hit,ship_spots,ship_spots2)
main()

Help is greatly appreciated!

Upvotes: 1

Views: 54

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881843

Right now you're hard-coding the check (in three identical functions with different names -- a mysterious approach!) as:

if spot_hit == 1 or spot_hit == 2 or spot_hit == 3:

this needs to become something like

if spot_hit in ship_spots:

where ship_spots is a global variable you randomly set at the start to one of the possible set of positions for the ship.

I have no idea what sets of positions you want to choose among (never played battleship on a 3 by 3 board!-) but for example:

import random
possible_spots = [[1,2,3], [4,5,6], [7,8,9]]
ship_spots = random.choice(possible_spots)

would give you one of the three horizontal possibilities.

Place this in main just before gameisplaying = True (the import random should more elegantly moved to the top of the module) and there you are.

Of course you would extend possible_spots if ships need not be horizontal, e.g

possible_spots = [[1,2,3], [4,5,6], [7,8,9],
                  [7,4,1], [8,5,2], [9,6,3]]

would allow the three vertical placements as well as the three horizontal ones.

Upvotes: 1

Related Questions