Python:AI并不总是把他的举动放在b-ship中

时间:2015-03-01 20:40:15

标签: python python-3.x

在这个b-ship代码中:

import random
import time

def drawboard(aiboard,playerboard):
    print(' Opponent\'s          Your')
    print('    Ships            Ships')
    print('|   |   |   |    |   |   |   |')
    print('| ' + aiboard[7] + ' | ' + aiboard[8] + ' | ' + aiboard[9] + ' |    | ' + playerboard[7] + ' | ' + playerboard[8] + ' | ' + playerboard[9] + ' |')
    print('|   |   |   |    |   |   |   |')
    print('-------------    -------------')
    print('|   |   |   |    |   |   |   |')
    print('| ' + aiboard[4] + ' | ' + aiboard[5] + ' | ' + aiboard[6] + ' |    | ' + playerboard[4] + ' | ' + playerboard[5] + ' | ' + playerboard[6] + ' |')
    print('|   |   |   |    |   |   |   |')
    print('-------------    -------------')
    print('|   |   |   |    |   |   |   |')
    print('| ' + aiboard[1] + ' | ' + aiboard[2] + ' | ' + aiboard[3] + ' |    | ' + playerboard[1] + ' | ' + playerboard[2] + ' | ' + playerboard[3] + ' |')
    print('|   |   |   |    |   |   |   |')
def playerhitai(aiboard,players_chosen_hit,aishipplaces,playerboard,usershipspots):
    if players_chosen_hit in aishipplaces[0] or players_chosen_hit in aishipplaces[1]:
        aiboard[players_chosen_hit] = 'x'
        drawboard(aiboard,playerboard)
    else:
        aiboard[players_chosen_hit] = 'o'
        drawboard(aiboard,playerboard)

def hitaroundhit(aiboard, playerboard,aishipplaces,players_chosen_hit,usershipspots):
    spots = {1:[2,4],2:[1,3,5],3:[2,6],4:[1,5,7],5:[2,4,6,8],6:[3,5,9],7:[4,8],8:[5,7,9],9:[8,6]}
    spot_dict = random.choice(spots[players_chosen_hit])
    if playerboard[spot_dict] == 's':
        pickX(aiboard,players_chosen_hit,aishipplaces,playerboard,usershipspots)
        playerhitai(aiboard,players_chosen_hit,aishipplaces,playerboard,usershipspots)
    else:
        playerboard[spot_dict] = 'o'
        playerhitai(aiboard,players_chosen_hit,aishipplaces,playerboard,usershipspots)

def pickX(aiboard,players_chosen_hit,aishipplaces,playerboard,usershipspots,aitakenmoves):
    x = [1,2,3,4,5,6,7,8,9]
    firsthitattempt = random.choice(x)
    while x in aitakenmoves:
        firsthitattempt = random.choice(x)
    return firsthitattempt,aitakenmoves

def aihitplayer(aiboard,playerboard, usershipspots,players_chosen_hit,aishipplaces,aitakenmoves):
    x,z = pickX(aiboard,players_chosen_hit,aishipplaces,playerboard,usershipspots,aitakenmoves)
    if (aiboard[aishipplaces[0][0]] == 'x') and (aiboard[aishipplaces[0][1]] == 'x') and (aiboard[aishipplaces[1][0]] == 'x') and (aiboard[aishipplaces[1][1]] == 'x') and (aiboard[aishipplaces[1][2]] == 'x'):
        return z
    else:
        time.sleep(1)
        print("\nComputer's turn.\n")
        time.sleep(1)
        if x in usershipspots:
            playerboard[x] = 'x'
            drawboard(aiboard,playerboard)
        else:
            playerboard[x] = 'o'
            drawboard(aiboard,playerboard)
    return z

def getShipInfo(ship,possibleusershipplaces,usershipspots,playerboard):
    while True:
        try:
            ship = int(input('Enter the 5 locations of your ship. Start with ship one (2 spaces) then do ship two (3 spaces).'))
            if ship in possibleusershipplaces:
                break
        except ValueError:
            time.sleep(0.5)
            print("\nInvalid entry\n")
            time.sleep(0.5)
    while ship in usershipspots:
        try:
            ship = int(input('Spot already taken.'))
            if ship not in possibleusershipplaces:
                while True:
                    try:
                        ship = int(input('1-9 only!'))
                    except (ValueError, IndexError):
                        print('invalid input')
        except (ValueError, IndexError):
            time.sleep(0.5)
            print("\nInvalid entry\n")
            time.sleep(0.5)    
    playerboard[ship] = 's'
    usershipspots.append(ship)
    return playerboard,usershipspots
def ascii():
    print('''    dMMMMb  .aMMMb dMMMMMMP dMMMMMMP dMP     dMMMMMP .dMMMb  dMP dMP dMP dMMMMb''')
    time.sleep(0.02)
    print('''   dMP"dMP dMP"dMP   dMP      dMP   dMP     dMP     dMP" VP dMP dMP amr dMP.dMP''')
    time.sleep(0.02)
    print('''  dMMMMK" dMMMMMP   dMP      dMP   dMP     dMMMP    VMMMb  dMMMMMP dMP dMMMMP" ''')
    time.sleep(0.02)
    print(''' dMP.aMF dMP dMP   dMP      dMP   dMP     dMP     dP .dMP dMP dMP dMP dMP''')
    time.sleep(0.02)
    print('''dMMMMP" dMP dMP   dMP      dMP   dMMMMMP dMMMMMP  VMMMP" dMP dMP dMP dMP''')                             
    time.sleep(1)
def playagain():
    x = input("Play again? y/n")
    if x == 'yes' or x == 'y' or x == 'Yes' or x == 'Y':
        main()
    else:
        pass
def main():
    print()
    ascii()
    print()
    time.sleep(1)
    print('''
############################################################################
# Instructions:                                                            #
# You have a 3x3 board. So does the AI.                                    #
# Each person has 2 ships. One has a length of 2, other has a length of 3. #
# You can place the ships horizantally or vertically, not diagonally.      #
# You must try to hit the AI ships. Each move you get one shot.            #
# The player and AI alternates back and forth.                             #
# A hit is X and a miss is O. Your ships are designated with an S.         #
# Whoever hits all of the other\'s ships first, wins the game.              #
############################################################################
''')
    time.sleep(5)
    print()
    aitakenmoves = []
    twoships = [[1,2], [2,1], [1,4], [4,1], [2,3], [3,2], [2,5], [5,2], [3,6], [6,3], [4,5], [5,4], [4,7], [7,4], [5,6], [6,5], [5,8], [8,5], [6,9], [9,6], [7,8], [8,7], [8,9], [9,8]]
    threeships = [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,2,1], [3,1,2], [1,4,7], [1,7,4], [4,1,7], [4,7,1], [7,4,1], [7,1,4], [2,5,8], [2,8,5], [5,8,2], [5,2,8], [8,5,2], [8,2,5], [3,6,9], [3,9,6], [6,9,3], [6,3,9], [9,6,3], [9,3,6], [4,5,6], [4,6,5], [5,6,4], [5,4,6], [6,5,4], [6,4,5], [7,8,9], [7,9,8], [9,8,7], [9,7,8], [8,7,9], [8,9,7]]
    shipspots1 = random.choice(twoships)
    shipspots2 = random.choice(threeships)
    while (shipspots1[0] in shipspots2) or (shipspots1[1] in shipspots2):
        shipspots1 = random.choice(twoships)
        shipspots2 = random.choice(threeships)
    aishipplaces = [shipspots1,shipspots2]
    aiboard = [' ' for i in range(10)]
    playerboard = [' ' for i in range(10)]

    sh1sp1,sh1sp2,sh2sp1,sh2sp2,sh2sp3 = '','','','',''
    possibleusershipplaces = [1,2,3,4,5,6,7,8,9]
    players_inputted_hits = []
    usershipspots = []
    playerboard,usershipspots = getShipInfo(sh1sp1,possibleusershipplaces,usershipspots,playerboard)
    playerboard,usershipspots = getShipInfo(sh1sp2,possibleusershipplaces,usershipspots,playerboard)
    playerboard,usershipspots = getShipInfo(sh2sp1,possibleusershipplaces,usershipspots,playerboard)
    playerboard,usershipspots = getShipInfo(sh2sp2,possibleusershipplaces,usershipspots,playerboard)
    playerboard,usershipspots = getShipInfo(sh2sp3,possibleusershipplaces,usershipspots,playerboard)

    players_inputted_hits = []
    gameisplaying = True
    while gameisplaying:
        playersturn=True
        while playersturn:
            while True:
                try:
                    players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9  '))
                    if players_chosen_hit in possibleusershipplaces:
                        if players_chosen_hit in players_inputted_hits:
                            while True:
                                try:
                                    players_chosen_hit = int(input('Already there! Try somewhere else!'))
                                    if players_chosen_hit not in players_inputted_hits:
                                        break
                                except ValueError:
                                    time.sleep(0.5)
                                    print("\nInvalid entry\n")
                                    time.sleep(0.5)
                        break
                except ValueError:
                    time.sleep(0.5)
                    print("\nInvalid entry\n")
                    time.sleep(0.5)
            players_inputted_hits.append(players_chosen_hit)
            playerhitai(aiboard, players_chosen_hit, aishipplaces, playerboard,usershipspots)
            playersturn = False
        aiturn = True
        while aiturn:
            aitakenmoves = aihitplayer(aiboard, playerboard, usershipspots,players_chosen_hit,aishipplaces,aitakenmoves)
            aiturn = False
        if (playerboard[usershipspots[0]] == 'x') and (playerboard[usershipspots[1]]) == 'x' and (playerboard[usershipspots[2]]) == 'x' and (playerboard[usershipspots[3]]) == 'x' and (playerboard[usershipspots[4]] == 'x'):
            print("AI WINS!")
            gameisplaying = False
        elif (aiboard[aishipplaces[0][0]] == 'x') and (aiboard[aishipplaces[0][1]] == 'x') and (aiboard[aishipplaces[1][0]] == 'x') and (aiboard[aishipplaces[1][1]] == 'x') and (aiboard[aishipplaces[1][2]] == 'x'):
            print("PLAYER WINS!")
            gameisplaying = False
    playagain()
main()
人工智能并不总是采取行动。这可能是因为他选择的移动x = pickX()已经在aiboard中。我已经制作了一个列表aitakenmoves以试图缓解这种情况,但它仍然存在。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

问题出现在您尝试使用pickX()变量的函数aitakenmoves中。有两个问题:

  1. 该行

    aitakenmoves = []
    

    删除每次运行该功能时所采取的动作记录,因此无论先前采取了什么动作,总是选择AI选择的第一步。

  2. aitakenmoves变量未存储在全局范围内(这可能是您每次调用函数时决定初始化它的原因),因此变量在pickX()函数后消失通话结束。您要么想要跟踪aitakenmoves

    • A)main()函数中的一个变量,您传递给aihitplayer(),后者又将变量传递给pickX()。然后,您可以使用代码中其他位置的多次返回,将选项从main()传递回aihitplayer()。或
    • B)一个真正的全局变量,在您致电main()之前已初步化,然后您在pickX()中使用该行

      进行访问

      global aitakenmoves

      您可以直接在pickX()

    • 中更新
  3. 我个人建议选择A,即使实施选择B肯定会更容易编程,因为弄乱global可能会非常混乱。

    P.S。至少在这里给出的代码中,当玩家获胜时,AI仍然会轮到他。

    编辑:如何正确实施选择A的简要说明。

    def pickX(aitakenmoves):
        firsthitattempt = random.randint(1, 9)
        while firsthitattempt in aitakenmoves:
            firsthitattempt = random.randint(1, 9)
        return firsthitattempt
    
    def aihitplayer(aiboard,playerboard, usershipspots,players_chosen_hit,aishipplaces, aitakenmoves):
        x = pickX(aitakenmoves)
    
        time.sleep(1)
        print("\nComputer's turn.\n")
        time.sleep(1)
        if x in usershipspots:
            playerboard[x] = 'x'
            drawboard(aiboard,playerboard)
            return True, x #AI makes a hit
        else:
            playerboard[x] = 'o'
            drawboard(aiboard,playerboard)
            return False, x #AI misses
    
    def main():
        # Initialization code
        aitakenmoves = []
        while gameisplaying:
            # Game loop
            ai_hit, location = aihitplayer(aiboard, playerboard, usershipspots,players_chosen_hit,aishipplaces,aitakenmoves)
            aitakenmoves.append(location)
            # Rest of game loop