Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6个月前关闭。
                                                                                            
                
        
我创建了一个战舰游戏,当计算机选择一个位置时,如果该位置在comp_hits中,则应将星号更改为'X',否则,将星号更改为'O'。但是,无论计算机选择什么,这似乎都不起作用,它将始终将星号更改为'O'。另外,即使我确保每当它猜测already中没有的东西时,计算机似乎都在猜测同一位置一次,如果不是,则将其添加到already中。有什么办法可以解决这两个问题,因为它们都在函数内部,还是我需要完全摆脱这些函数?

#Battleships!
import random
import time
#Asigns Playing to True
playing = True
hits = []
comp_hits = []
already = []
values = []
def checkpos(computer_values, values):
    values = computer_values
    if len(computer_values) == 6:
        for i in range(6):
            if int(ord(computer_values[i][0]))-65 > 5:
                computer_values = input('Invalid - Enter 6 positions')
                computer_values = computer_values.split()
                checkpos(computer_values, values)
            elif int(computer_values[i][1]) > 5:
                computer_values = input('Invalid - Enter 6 positions')
                computer_values = computer_values.split()
                checkpos(computer_values, values)
    else:
        computer_values = input('Invalid - Enter 6 positions')
        computer_values = computer_values.split()
        checkpos(computer_values, values)
    return
def compvals(comp_hits, values):
    comp_hits = values
    comptest(comp_hits, values)
    return
def comptest(comp_hits, values):
    for i in range(len(comp_hits)):
        if comp_hits.count(comp_hits[i]) > 1:
            comp_hits.clear()
            pick(comp_hits)
    return
def comp_shot(grid2, comp_hits):
    first = random.randint(65, 69)
    first = chr(first)
    second = random.randint(1,5)
    second = str(second)
    pos = first+second
    if int(pos[1]) > 5:
        pos = input('Guess a Position: ').upper()
        shotpos(pos, grid2)
    row = int(pos[1])
    column = int(ord(pos[0]))-65
    if pos in comp_hits:
        for i in range(len(comp_hits)):
            if pos == comp_hits[i]:
                break
        grid2[column][row] = 'X'
        comp_hits.remove(pos)
    elif pos not in comp_hits:
        grid2[column][row] = 'O'
    for num in range(6):
        for num2 in range(6):
            print('  ', grid[num][num2], end='')
        print('\t    ', end='')
        for num2 in range(6):
            print('  ', grid2[num][num2], end='')
        print()
    return
#Determines Row number and Position from coordinates
def shotpos(pos, grid, hits):
    try:
        int(pos[1])
    except:
        pos = input('Invalid - Guess a Position: ').upper()
        shotpos(pos, grid, hits)
    else:
        if int(pos[1]) > 5:
            pos = input('Invalid - Guess a Position: ').upper()
            shotpos(pos, grid, hits)
        row = int(pos[1])
        column = int(ord(pos[0]))-65
        if column > 5:
            pos = input('Invalid - Guess a Position: ').upper()
            shotpos(pos, grid, hits)
        if pos in hits:
            for i in range(len(hits)):
                if pos == hits[i]:
                    break
            grid[column][row] = 'X'
            hits.remove(pos)
        elif pos not in hits:
            grid[column][row] = 'O'
        for num in range(6):
            for num2 in range(6):
                print('  ', grid[num][num2], end='')
            print('\t    ', end='')
            for num2 in range(6):
                print('  ', grid2[num][num2], end='')
            print()
    return
def test(hits):
    for i in range(len(hits)):
        if hits.count(hits[i]) > 1:
            hits.clear()
            pick(hits)
def pick(hits):
    for i in range(6):
        first = random.randint(65, 69)
        first = chr(first)
        second = random.randint(1,5)
        second = str(second)
        both = first+second
        hits.append(both)
    test(hits)
def compshot(grid2, comp_hits, already):
    row = random.randint(1,5)
    column = chr(random.randint(65,69))
    pos = column+str(row)
    column = ord(column)-65
    if pos in already:
        compshot(grid2, comp_hits, already)
    elif pos in comp_hits:
        for i in range(len(comp_hits)):
            if pos == comp_hits[i]:
                break
        grid2[column][row] = 'X'
        comp_hits.remove(pos)
        already.append(pos)
    elif pos not in comp_hits:
        grid2[column][row] = 'O'
        already.append(pos)
    for num in range(6):
        for num2 in range(6):
            print('  ', grid[num][num2], end='')
        print('\t    ', end='')
        for num2 in range(6):
            print('  ', grid2[num][num2], end='')
        print()
    return
#Takes User name and Capitalize it.
user_name = (input('Please Enter Your Name: ')+'.').capitalize()
#Outputs the user_name and asks if wanting to play.
print('Hello,', user_name, 'Are You ready to play Battleships?')
#Gets User answer and Checks if want to play
if (input('  '))[0].lower() == 'n':
    #If the answer is no, Quit the Game
    print('Ok, That\'s Fine')
    SystemExit()
#Grid of Asterisks That makes up the Playing Field
grid = [['A ','*','*','*','*','*'],
        ['B ','*','*','*','*','*'],
        ['C ','*','*','*','*','*'],
        ['D ','*','*','*','*','*'],
        ['E ','*','*','*','*','*'],
        ['  ','1','2','3','4','5']]
grid2 = [['A ','*','*','*','*','*'],
        ['B ','*','*','*','*','*'],
        ['C ','*','*','*','*','*'],
        ['D ','*','*','*','*','*'],
        ['E ','*','*','*','*','*'],
        ['  ','1','2','3','4','5']]
computer_values = input('Enter 6 positions')
computer_values = computer_values.split()
values = computer_values
checkpos(computer_values, values)
compvals(comp_hits, values)
#Prints out Grid
for num in range(6):
    for num2 in range(6):
        print('  ', grid[num][num2], end='')
    print('\t    ', end='')
    for num2 in range(6):
        print('  ', grid2[num][num2], end='')
    print()
pick(hits)
while hits:
    pos = input('Guess a position: ').upper()
    shotpos(pos, grid, hits)
    print('Computer is thinking...')
    time.sleep(1)
    compshot(grid2, comp_hits, already)

最佳答案

我花了一段时间尝试将其拼凑起来。我无法在您的代码中找到任何内容附加到comp_hits的地方,也许您可​​以转到您认为类似于comp_hits.append()的地方并验证它是否在那里。可能是正在打印而不是保存。我认为您应该尝试使用此草稿来重写此代码。这次尝试执行以下操作:

1)尝试结构化代码,以便您的函数返回某些内容。您不必返回任何内容,但是以这种方式构造代码将使调试变得更容易。

2)尽量避免使用带有长链ifs,elifs和if-breaks的函数。您应该能够找到逻辑上等效但更简单的东西。看起来像
可能有效,但如果不这样做,则很难调试逻辑。

3)不要在自己内部调用定义的函数。我认为如果compshot()的情况是随机生成的数字生成了先前使用的位置,则您尝试从顶部重新启动。通过使用while循环(未测试),您可以解决这种情况。

while gen == True:
    row = random.randint(1,5)
    column = chr(random.randint(65,69))
    pos = column+str(row)
    column = ord(column)-65
    if pos not in already:
        gen = False


4)尝试将功能分解为较小的功能。

5)尝试在助手功能中具有层次结构。因此,例如,test()和pick()不应调用另一个,反之亦然。

6)将开发板保存到字典或数据框中,然后以这种方式进行操作。看来您可能每次都在重新计算板。我可能是错的。通常,最好避免这种情况,因为如果在更新电路板时遇到问题,您应该知道在哪里看!

这次编写每个函数时,可以传入一些测试用例,以查看它是否按预期工作。希望您下次可以附加“ X”。抱歉,我没有更多帮助。

10-06 07:09
查看更多