以下代码来自《深度学习》和《 Go游戏》一书 不确定为什么我会收到此错误
完全错误:
回溯(最近一次通话最后一次):文件“ bot_v_bot.py”,第25行,在 main()main中的第20行的文件“ bot_v_bot.py” bot_move = bots [game.next_player] .select_move(game)文件“ C:\ Users \ Max \ Desktop \ books \ pdf \ code \ Go ML \ dlgo \ agent \ naive.py”,行 14,在select_move中 如果是game_state.is_valid_move(Move.play(canidate))而不是is_point_an_eye(game_state.board,canidate,game_state.next_player):
文件“ C:\ Users \ Max \ Desktop \ books \ pdf \ code \ Go ML \ dlgo \ agent \ helpers.py“,第20行,位于is_point_an_eye中 如果board.is_on_grid(corners):文件“ C:\ Users \ Max \ Desktop \ books \ pdf \ code \ Go ML \ dlgo \ goboard_slow.py”,行 71,在is_on_grid中 返回1 <= point.row <= self.num_rows和1 <= point.col <= self.num_cols AttributeError:“列表”对象没有属性“行”
我的目录如下
GO ML
-bot_v_bot.py
-dlgo
---__pychache__
---agent
----__init__.py
----base.py
----helpers.py
----naive.py
---__init__.py
---goboard_slow.py
---gotypes.py
---utils.py
Bot_V_Bot代码
from dlgo.agent import naive
from dlgo import goboard_slow
from dlgo import gotypes
from dlgo.utils import print_board, print_move
import time
def main():
board_size = 9
game = goboard_slow.GameState.new_game(board_size)
bots = {
gotypes.Player.black: naive.RandomBot(),
gotypes.Player.white: naive.RandomBot(),
}
while not game.is_over():
time.sleep(0.3)
print(chr(27) + "[2j")
print_board(game.board)
bot_move = bots[game.next_player].select_move(game)
print_move(game.next_player, bot_move)
game = game.apply_move(bot_move)
if __name__ == '__main__':
main()
goboard_slow代码
import copy
from dlgo.gotypes import Player
from dlgo.gotypes import Point
class GoString():
def __init__(self, color, stones, liberties):
self.color = color
self.stones = set(stones)
self.liberties = set(liberties)
def remove_liberty(self, point):
self.liberties.remove(point)
def add_liberty(self, point):
self.liberties.add(point)
def merged_with(self, go_string):
assert go_string.color == self.color
combined_stones = self.stones | go_string.stones
return GoString(
self.color,
combined_stones,
(self.liberties | go_string.liberties) - combined_stones)
@property
def num_liberties(self):
return len(self.liberties)
def __eq__(self, other):
return isinstance(other, GoString) and \
self.color == other.color and \
self.stones == other.stones and \
self.liberties == other.liberties
class Board():
def __init__(self, num_rows, num_cols):
self.num_rows = num_rows
self.num_cols = num_cols
self._grid = {}
def place_stone(self, player, point):
assert self.is_on_grid(point)
assert self._grid.get(point) is None
adjacent_same_color = []
adjacent_opposite_color = []
liberties = []
for neighbor in point.neighbors():
if not self.is_on_grid(neighbor):
continue
neighbor_string = self._grid.get(neighbor)
if neighbor_string is None:
liberties.append(neighbor)
elif neighbor_string.color == player:
if neighbor_string not in adjacent_same_color:
adjacent_same_color.append(neighbor_string)
else:
if neighbor_string not in adjacent_opposite_color:
adjacent_opposite_color.append(neighbor_string)
new_string = GoString(player, [point], liberties)
for same_color_string in adjacent_same_color:
new_string = new_string.merged_with(same_color_string)
for new_string_point in new_string.stones:
self._grid[new_string_point] = new_string
for other_color_string in adjacent_opposite_color:
other_color_string.remove_liberty(point)
for other_color_string in adjacent_same_color:
if other_color_string.num_liberties == 0:
self._remove_string(other_color_string)
def is_on_grid(self, point):
return 1 <= point.row <= self.num_rows and 1 <= point.col <= self.num_cols
def get(self, point):
string = self._grid.get(point)
if string is None:
return None
return string.color
def get_go_string(self, point):
string = self._grid.get(point)
if string is None:
return None
return string
def _remove_string(self, string):
for point in string.stones:
for neighbor in point.neighbors():
neighbor_string = self._grid.get(neighbor)
if neighbor_string is None:
continue
if neighbor_string is not string:
neighbor_string.add_liberty(point)
self._grid[point] = None
class Move():
def __init__(self, point=None, is_pass=False, is_resign=False):
assert (point is not None) ^ is_pass ^ is_resign
self.point = point
self.is_play = (self.point is not None)
self.is_pass = is_pass
self.is_resign = is_resign
@classmethod
def play(cls, point):
return Move(point=point)
@classmethod
def pass_turn(cls):
return Move(is_pass=True)
@classmethod
def resign(cls):
return Move(is_resign=True)
class GameState():
def __init__(self, board, next_player, previous, move):
self.board = board
self.next_player = next_player
self.previous_state = previous
self.last_move = move
def apply_move(self, move):
if move.is_play:
next_board = copy.deepcopy(self.board)
next_board.place_stone(self.next_player, move.point)
else:
next_board = self.board
return GameState(next_board, self.next_player.other, self, move)
@classmethod
def new_game(cls, board_size):
if isinstance(board_size, int):
board_size = (board_size, board_size)
board = Board(*board_size)
return GameState(board, Player.black, None, None)
def is_over(self):
if self.last_move is None:
return False
if self.last_move.is_resign:
return True
second_last_move = self.previous_state.last_move
if second_last_move is None:
return False
return self.last_move.is_pass and second_last_move.is_pass
def is_move_self_capture(self, Player, move):
if not move.is_play:
return False
next_board = copy.deepcopy(self.board)
next_board.place_stone(Player, move.point)
new_string = next_board.get_go_string(move.point)
return new_string.num_liberties == 0
@property
def situation(self):
return (self.next_player, self.board)
def does_move_validate_ko(self, player, move):
if not move.is_play:
return False
next_board = copy.deepcopy(self.board)
next_board.place_stone(player, move.point)
next_situation = (Player.other, next_board)
past_state = self.previous_state
while past_state is not None:
if past_state.situation == next_situation:
return True
past_state = past_state.previous_state
return False
def is_valid_move(self, move):
if self.is_over():
return False
if move.is_pass or move.is_resign:
return True
return (
self.board.get(move.point) is None and
not self.is_move_self_capture(self.next_player, move) and
not self.does_move_validate_ko(self.next_player, move))
类型代码
import enum
from collections import namedtuple
class Player(enum.Enum):
black = 1
white = 2
@property
def other(self):
return Player.black if self == Player.white else Player.white
class Point(namedtuple('Point', 'row col')):
def neighbors(self):
return [
Point(self.row - 1, self.col),
Point(self.row + 1, self.col),
Point(self.row, self.col - 1),
Point(self.row, self.col + 1),
]
实用程序代码
import random
from dlgo.agent.base import Agent
from dlgo.agent.helpers import is_point_an_eye
from dlgo.goboard_slow import Move
from dlgo.gotypes import Point
class RandomBot(Agent):
def select_move(self, game_state):
""""Choose a random valid move that preserves our own eyes."""
canidates = []
for r in range(1, game_state.board.num_rows + 1):
for c in range(1, game_state.board.num_cols +1):
canidate = Point(row=r, col=c)
if game_state.is_valid_move(Move.play(canidate)) and not is_point_an_eye(game_state.board, canidate, game_state.next_player):
canidates.append(canidate)
if not canidates:
return Move.pass_turn()
return Move.play(random.choice(canidates))
from dlgo import gotypes
COLS = 'ABCDEFGHIJKLMNOPQRST'
STONE_TO_CHAR = {
None: ' . ',
gotypes.Player.black: ' x ',
gotypes.Player.white: ' o ',
}
def print_move(player, move):
if move.is_pass:
move_str = 'passes'
elif move.is_resign:
move_str = 'resigns'
else:
move_str = '%s%d' % (COLS[move.point.col - 1], move.point.row)
print('%s %s' % (player, move_str))
def print_board(board):
for row in range(board.num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, board.num_cols + 1):
stone = board.get(gotypes.Point(row = row, col = col))
line.append(STONE_TO_CHAR[stone])
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:board.num_cols]))
帮助程序代码
from dlgo.gotypes import Point
def is_point_an_eye(board, point, color):
if board.get(point) is not None:
return False
for neighbor in point.neighbors():
if board.is_on_grid(neighbor):
neighbor_color= board.get(neighbor)
if neighbor_color != color:
return False
friendly_corners = 0
off_board_corners = 0
corners = [
Point(point.row - 1, point.col - 1),
Point(point.row - 1, point.col + 1),
Point(point.row + 1, point.col - 1),
Point(point.row + 1, point.col + 1),
]
for corner in corners:
if board.is_on_grid(corners):
corner_color = board.get(corner)
if corner_color == olor:
friendly_corners += 1
else:
off_board_corners += 1
if off_board_corners > 0:
return off_board_corners + friendly_corners == 4
return friendly_corners >= 3
基本代码
class Agent:
def __init(self):
pass
def selec_move(self, game_state):
raise NotImplementedError()
两个Init均为空
答案 0 :(得分:0)
corners
是所有corner
点的列表,角是for
循环中的当前元素。
Barmar我认为这只是一个错字:board.is_on_grid(corners) 应该是board.is_on_grid(corner)
Barmar
这些有效并修复了