好的,对于初学者来说,这是一个简单的井字游戏,我正在创建一个设计困难的 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)
此外,我还有一些空白打印声明,以确保整洁,它们不是错误。