Python 不打印值生成';它的行为很奇怪
首先,很抱歉没有那么描述性的标题,我真的不知道如何称呼这个问题,所以让我们开始 我必须为一个4x4-4的在线游戏设计一个AI,我们使用蒙特卡罗方法来做这个AI,所以基本上我必须对计算机的可能动作进行n次模拟,并确定下一步哪个动作是最好的,我们的教授给了我们一些基本代码来玩4x4-4的在线游戏,我们必须设计人工智能,所以下面是我模拟人工智能的方法: EDIT1:添加了完整程序。Python 不打印值生成';它的行为很奇怪,python,python-2.7,printing,simulation,Python,Python 2.7,Printing,Simulation,首先,很抱歉没有那么描述性的标题,我真的不知道如何称呼这个问题,所以让我们开始 我必须为一个4x4-4的在线游戏设计一个AI,我们使用蒙特卡罗方法来做这个AI,所以基本上我必须对计算机的可能动作进行n次模拟,并确定下一步哪个动作是最好的,我们的教授给了我们一些基本代码来玩4x4-4的在线游戏,我们必须设计人工智能,所以下面是我模拟人工智能的方法: EDIT1:添加了完整程序。 from __future__ import print_function import random from cop
from __future__ import print_function
import random
from copy import deepcopy
import time
board = [
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
]
p1_token = 1
p2_token = -1
draw_token = 0
pool = ThreadPool(processes=1)
def slice_winner(state_slice):
slice_size = len(state_slice)
sums = [sum(row) for row in state_slice]
sums.extend([sum([row[i] for row in state_slice]) for i in range(slice_size)])
if (p1_token * slice_size) in sums:
return p1_token
elif (p2_token * slice_size) in sums:
return p2_token
return 0
def winner(state):
for state_slice in state:
winner_in_slice = slice_winner(state_slice)
if winner_in_slice != draw_token:
return winner_in_slice
state_size = len(state)
for i in range(state_size):
state_slice = []
for j in range(state_size):
state_slice.append([state[j][i][k] for k in range(state_size)])
winner_in_slice = slice_winner(state_slice)
if winner_in_slice != draw_token:
return winner_in_slice
diagonals = [0, 0, 0, 0]
for i in range(state_size):
diagonals[0] += state[i][i][i]
diagonals[1] += state[state_size - 1 - i][i][i]
diagonals[2] += state[i][state_size - 1 - i][i]
diagonals[3] += state[state_size - 1 - i][state_size - 1 - i][i]
if (p1_token * state_size) in diagonals:
return p1_token
elif (p2_token * state_size) in diagonals:
return p2_token
return draw_token
def str_token(cell):
if cell == p1_token:
return "X"
elif cell == p2_token:
return "O"
return "."
def draw_board(state):
result = ""
state_size = len(state)
for y in range(state_size):
for z in range(state_size):
for x in range(state_size):
result += str_token(state[x][y][z]) + " "
result += "\t"
result += "\n"
return result
def isInVector(vector, x, y, z):
n = 0
while (n < len(vector)):
if (vector[n][0] == x and vector[n][1] == y and vector[n][2] == z):
return True
n += 1
return False
def getInVector(vector, x, y, z):
n = 0
while (n < len(vector)):
if (vector[n][0] == x and vector[n][1] == y and vector[n][2] == z):
return n
n += 1
return -1
def getBestPlay(vector):
max_value = -100000
index_of_max = -1
for i in range(len(vector)):
if (vector[i][3] > max_value):
max_value = vector[i][3]
index_of_max = i
return [vector[index_of_max][0], vector[index_of_max][1], vector[index_of_max][2]]
def AISim(main_state, p1x, p1y, p1z, maxIt):
n = 0 # Number of simulations
p2Moves = [] # A vector to hold player 2 moves.
while (n < maxIt): # While 1
x = p1x
y = p1y
z = p1z
player_turn = False # False because simulation will always start with player 2 turn
moves = 0
first_move = [0, 0, 0]
new_state = deepcopy(main_state)
while winner(new_state) == draw_token: # While 2
temp = new_state[x][y][z]
while temp != draw_token: # While 3
x = random.randint(0, 3)
y = random.randint(0, 3)
z = random.randint(0, 3)
temp = new_state[x][y][z]
# THIS IS THE PROBLEEEEEM!!!!
print (temp)
# END while 3
if (moves == 0):
first_move = [x, y, z]
if (not isInVector(p2Moves, x, y, z)):
p2Moves.append([x, y, z, 0])
# END if
# END if
new_state[x][y][z] = (1 if player_turn else -1)
player_turn = not player_turn
moves += 1
# END while 2
if (winner(new_state) == 1):
temPos = getInVector(p2Moves, first_move[0], first_move[1], first_move[2])
p2Moves[temPos][3] -= 1
else:
temPos = getInVector(p2Moves, first_move[0], first_move[1], first_move[2])
p2Moves[temPos][3] += 1
# END if-else
n += 1
# END while 1
return getBestPlay(p2Moves)
# --------------------------------------------------------------------------------------------------------
# ----------------------------------------- MAIN PROGRAM -------------------------------------------------
# --------------------------------------------------------------------------------------------------------
AIMove = [0, 0, 0]
player_1_turn = True
while winner(board) == draw_token:
# Print board state
print ("")
print ("Board:")
print (draw_board(board))
print ("")
# Print
print ("Player %s turn:" % (1 if player_1_turn else 2))
if (player_1_turn):
# Get input
x = int(raw_input("x: "))
y = int(raw_input("y: "))
z = int(raw_input("z: "))
else:
# Player 2 turn
start = time.time()
AIMove = AISim(board, x, y, z, 500)
end = time.time()
print ("Thinking time: %0.4f seconds" % (end - start))
x = AIMove[0]
print ("x:",x)
y = AIMove[1]
print ("y:",y)
z = AIMove[2]
print ("z:",z)
if board[x][y][z] == draw_token:
board[x][y][z] = 1 if player_1_turn else -1
player_1_turn = not player_1_turn
else:
print ("")
print ("ERROR: occupied position, please retry in a new position")
print ("")
print ("Player %s is the winner!" % (1 if winner(board) == 1 else 2))
from\uuuuu future\uuuuu导入打印功能
随机输入
从复制导入deepcopy
导入时间
董事会=[
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
]
p1_令牌=1
p2_标记=-1
提取令牌=0
池=线程池(进程=1)
def切片优胜者(状态切片):
切片大小=len(状态切片)
sums=[状态为_切片的行的sum(行)]
sums.extend([sum([row[i]表示处于状态的行\u slice])表示处于范围内的i(slice\u size)])
如果(p1_标记*切片大小)为总和:
返回p1_令牌
elif(p2_标记*切片大小)之和:
返回p2_令牌
返回0
def获胜者(州):
对于处于状态的状态\u切片:
winner\u in\u slice=slice\u winner(状态\u slice)
如果获胜者在片中提取令牌:
返回赢家\u在\u片中
状态大小=len(状态)
对于范围内的i(状态大小):
state_slice=[]
对于范围内的j(状态_尺寸):
state_slice.append([state[j][i][k]表示范围内的k(state_size)])
winner\u in\u slice=slice\u winner(状态\u slice)
如果获胜者在片中提取令牌:
返回赢家\u在\u片中
对角线=[0,0,0,0]
对于范围内的i(状态大小):
对角线[0]+=状态[i][i][i]
对角线[1]+=状态[state_size-1-i][i]
对角线[2]+=状态[i][state_size-1-i][i]
对角线[3]+=状态[state_size-1-i][state_size-1-i][i]
如果(p1_标记*状态_大小)以对角线表示:
返回p1_令牌
对角线中的elif(p2_标记*状态_大小):
返回p2_令牌
返回取款令牌
def str_令牌(单元):
如果单元格==p1_标记:
返回“X”
elif单元==p2_标记:
返回“O”
返回”
def牵引板(状态):
result=“”
状态大小=len(状态)
对于范围内的y(状态大小):
对于范围内的z(状态大小):
对于范围内的x(状态大小):
结果+=str_标记(状态[x][y][z])+“”
结果+=“\t”
结果+=“\n”
返回结果
def isInVector(向量,x,y,z):
n=0
而(n最大值):
最大值=向量[i][3]
_max=i的索引_
返回[vector[index_of_max][0],vector[index_of_max][1],vector[index_of_max][2]]
def AISim(主状态、p1x、p1y、p1z、maxIt):
n=0#模拟次数
p2Moves=[]#一个用于容纳玩家2个移动的向量。
while(n
我测试过的代码可以正常工作