对于初学者来说,这是一个简单的tic tac toe版本,我正在创建一个设计困难的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)
我也有一些空白的打印声明,但它们不是错误。
答案 0 :(得分:3)
if 'x' or 'X' == player
不意味着player
是x
还是X
,而是if ('x') or ('X' == player)
,所以请测试{{} {1}}作为布尔值,并将'x'
测试为布尔值,如果第一个或第二个部分为'X' == player
,则整个表达式为True
。
由于第一个字符串的长度大于0,因此总是为True
,并且永远不会进行第二次测试:
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