Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

python - Generate random non repeating samples from an array of numbers

I made a battleships game and I now need to make sure that the computer doesn't attack at the same spot twice.

My idea of it is storing each shot's co-ordinates in a variable which gets added to whenever there is a new shot and then I just have to check and make it check if the current shot is in the variable.

This is the code I have for shooting:

if playerNumber == "1":

        eg.msgbox("Player " + str(playerNumber) + " your shot.")

        hit=False

        shotX=eg.enterbox("Enter the x-coordinate for your shot (1-5): ")
        shotY=eg.enterbox("Enter the y-coordinate for your shot (1-5): ")

    else:

        eg.msgbox("The computer will now attack!")

        hit=False
        shotX=str(random.randint(1,5))
        shotY=str(random.randint(1,5))

        eg.msgbox ("The computer shot at " + str(shotX) + ", " + str(shotY) + "")

My idea of making sure the computer chooses something that isn't in the list is using a loop where it keeps choosing a shot until the one it chooses hasn't been chosen already.

The actual code that makes the computer fire his shot is:

hit = haveShot("2", p1ship1, p1ship2 , player2Board)

    if hit:
        p2 = p2 + 1
        eg.msgbox("The Computer has " + str(p2) + " points and Player 1 has " + str(p1) + " points")
        if p2 == 2:    
            eg.msgbox("Unlucky, The computer won. Press enter to continue")
            if platform.system() == "Windows": # ONLY PLAY SOUNDS IF ON WINDOWS OS
                finish()
            break

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I first answer in a language agnostic way.

A very efficient way would be to keep a sequential list of all the possible positions (in your case 5x5=25 positions), use a single random choice in that list and then removes the position from the list. This saves you from the what to do when I hit a position I've already hitted

Python implementation :

First generate the list of positions :

positions = [ (i, j) for i in range(1,6) for j in range(1,6) ]

Then get a random position and remove it from the list

index = random.randrange(len(positions))
shotX, shotY = positions[index]
del positions[index]

Edit :

As a prove it should fork :

>>> import random
>>> positions = [ (i, j) for i in range(1,6) for j in range(1,6)]
>>> for i in range(25):
    index = random.randrange(len(positions))
    print positions[index]
    del positions[index]


(5, 5)
(5, 4)
(1, 4)
(1, 2)
(3, 3)
(1, 1)
(4, 1)
(4, 4)
(5, 1)
(4, 2)
(2, 2)
(2, 4)
(2, 3)
(2, 1)
(3, 2)
(3, 5)
(1, 5)
(5, 3)
(5, 2)
(4, 3)
(4, 5)
(1, 3)
(3, 4)
(2, 5)
(3, 1)
>>> print positions
[]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...