-2

好的,对于初学者来说,这是一个简单的井字游戏,我正在创建一个设计困难的 AI,并且我的代码中有部分 AI(随机)代码。忽略该功能,但我的主要困难是正确地在板上打印动作。出于某种原因,每当我选择我想成为 X 还是 O 时,它都会自动将我提交到 X。此外,如果我选择位置 A 以外的任何东西,它无论如何都会在位置 A 上打印一个 X。如果有人能帮助我告诉我在我的编码中到底哪里出错了,我会很高兴的。另一方面,为了使我的游戏更加清晰和容易,我想在移动后并排打印 Board 和 Board 2。这将显示哪些动作可用,因此如果采取了动作 A 和 B,它们将不会显示在棋盘上。

print "**********************"
print "*THE GAME WILL BEGIN.*"
print "**********************"
import random
indx=0
move_storage=[]
move=["","","","","","","","",""]
#**************************************************
#            This is the original board           *
#**************************************************
def board():
    print "This is the tictactoe board we will be playing on"
    print "         |         |        "
    print "    A    |    B    |    C   "
    print "         |         |        "
    print "------------------------------"
    print "         |         |        "
    print "    D    |    E    |    F   "
    print "         |         |        "
    print "------------------------------"
    print "         |         |        "
    print "    G    |    H    |    I   "
    print "         |         |        "
board()
#****************************************************
#This is the board where the moves will be displayed*
#****************************************************
def board2(move):
    print "  ",move[0],"  |   ",move[1],"     |     ",move[2],"     "
    print "       |          |     "
    print " -----------------------  "
    print "       |          |     "
    print "  ",move[3],"   |    ",move[4],"    |     ",move[5],"     "
    print "       |          |     "
    print " -----------------------  "
    print "       |          |     "
    print "  ",move[6],"   |     ",move[7],"   |    ",move[8],"     "

#This function will store all of the moves inputed.
def movestorage(move_storage, move):
    move_storage= move_storage + [move]
#This function will randomize the computer's move
def computer_move():
    move_random=random.randint(1,9)
    move[move_random]=computer_choice
    movestorage(move_storage, move)
player=raw_input("Would you like to play as x or o-->")
player_order=raw_input("Would you like to go first,y/n?-->")
while indx==0:
    if 'x' or 'X' == player:
        player_choice="X"
        computer_choice="O"
        if 'y' or 'Y' in player_order:
            print 
        elif 'n' or 'N' in player_order:
            print
            print "The computer will go first, press ENTER to see the computers turn"
    elif 'o' or 'O' == player:
        player_choice="O"
        computer_choice="X"
        if 'y' or 'Y' in player_order:
            print
        elif 'n' or 'N' in player_order:
            print
            print "The computer will go first, press ENTER to see the computers turn"
    x=0
    while 2>x:  #This is where the player decides where he wants to go
        choice=raw_input("Choose where you would like to move-->")
        if choice == 'a' or 'A':
            move[0]=player_choice
            movestorage(move_storage, move)
        elif choice == 'b' or 'B':
            move[1]=player_choice
            movestorage(move_storage, move)
        elif choice == 'c' or 'C':
            move[2]=player_choice
            movestorage(move_storage, move)
        elif choice == 'd' or 'D':
            move[3]=player_choice
            movestorage(move_storage, move)
        elif choice == 'e' or 'E':
            move[4]=player_choice
            movestorage(move_storage, move)
        elif choice == 'f' or 'F':
            move[5]=player_choice
            movestorage(move_storage, move)
        elif choice == 'g' or 'G':
            move[6]=player_choice
            movestorage(move_storage, move)
        elif choice == 'h' or 'H':
            move[7]=player_choice
            movestorage(move_storage, move)
        elif choice == 'i' or 'I':
            move[8]=player_choice
            movestorage(move_storage, move)
        board2(move)

此外,我还有一些空白打印声明,以确保整洁,它们不是错误。

4

1 回答 1

3

if 'x' or 'X' == player不意味着要么是要么,而是意味着,因此将两者都测试为布尔值,并测试为布尔值,然后整个表达式是如果第一部分第二部分是。playerxXif ('x') or ('X' == player)'x''X' == playerTrueTrue

由于第一个是长度大于 0 的字符串,所以它总是,并且永远不会True进行第二个测试:

>>> bool('x' or 'X' == 'fiddlesticksandflapdoodles')
True
>>> bool('x')
True
>>> bool('X' == 'fiddlesticksandflapdoodles')
False

利用:

if 'x' == player or 'X' == player:

相反,或者:

if player in ('x', 'X'):

或者:

if player.lower() == 'x':

所有这些都测试同一件事。

这适用于所有if针对大小写字母进行测试的语句,例如'n' or 'N', 和'o' or 'O'

至于你的一长串if choice测试;只需使用映射将字母映射到数字:

choices = {letter: number for number, letter in enumerate('abcdefghi')}

现在您可以使用choices映射到索引:

index = choices.get(choice.lower())
if index is not None:
    move[index] = player_choice
    movestorage(move_storage, move)
于 2013-01-14T21:27:04.087 回答