Tic Tac Toeのゲームの 1 つの位置を表す新しいクラスを作成しました。基本的に私がやろうとしているのは、各ノードがPosition
オブジェクトであるゲーム位置のすべての可能性のツリーを作成し、ミニマックス アルゴリズムを使用してプレーヤーにとって最適な動きを見つけることです。Position クラスが必要に応じて機能していないため、ミニマックス アルゴリズムは以下に示されていません。
このPosition
クラスには、現在の位置から到達できるオブジェクトgenerate_children
のリストを作成するメソッドがあります。Position
プログラムを実行すると、各反復後pos_matrix
に現在のPosition
オブジェクトの が変化しているという出力が得られますが、これは望ましくありません。pos_matrix
ループ内の現在の Position オブジェクトには触れておらず、play_move
混乱を避けるためにマトリックスのコピーを作成しています。それでも、pos_matrix
繰り返しごとに変化しています。
何が起こっている?どうすればデバッグできますか?
試した:play_move
クラスの外に出ましたが、うまくいきませんでした。
注: 0
pos_matrix の A は空の正方形を1
表し、"X" を-1
表し、"O" を表します。「誰のチャンス」という意味
もあります。kiska_chance
:P
class Position:
def __init__(self, parent_):
self.parent = parent_
self.children = []
self.best_move = []
self.pos_matrix = []
self.last_move = []
def set_pos_matrix(self, pos_matrix_):
self.pos_matrix = list(pos_matrix_)
# Avoiding copying problems by creating copy of list
def set_last_move(self, last_move_):
self.last_move = list(last_move_)
# Avoiding copying problems by creating copy of list
def play_move(self, move, kiska_chance):
m2 = list(self.pos_matrix)
x, y = move
m2[x][y] = kiska_chance
return m2
def generate_children(self, kiska_chance):
children_ = []
for move in self.get_possible_moves():
# Passing a Position object into the possible moves with
# parent as self.
pos_temp = Position(self)
pos_temp.set_pos_matrix(self.play_move(move, kiska_chance))
pos_temp.set_last_move(move)
print self.pos_matrix
children_.append(pos_temp)
self.children = children_
return children_
def get_possible_moves(self):
dem_moves = []
for i in xrange(3):
for j in xrange(3):
if self.pos_matrix[i][j]==0:
dem_moves.append([i, j])
return dem_moves
pos = Position(None)
pos.set_pos_matrix([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
pos.generate_children(1)