本文整理汇总了Python中board.Board.copy方法的典型用法代码示例。如果您正苦于以下问题:Python Board.copy方法的具体用法?Python Board.copy怎么用?Python Board.copy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.copy方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_resolve_square
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
def test_resolve_square():
shape = (3, 2) # 3 lines & 2 columns
board1 = Board(shape)
board1.current_player = VAMP
board1.proba = 0.5
# HUM: 0, VAMP: 1, WOLV: 2
board1.grid = np.array([
[[1, 0, 0], [0, 2, 0]],
[[0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 2, 2]],
], dtype=np.uint8)
square = (2, 1)
board2 = board1.copy()
outcomes = get_outcomes(board1, square)
for outcome in outcomes:
outcome['result'] = list(outcome['result'])
assert outcomes == [
{'proba': 0.5, 'result': [0, 1, 0]},
{'proba': 0.5, 'result': [0, 0, 1]},
]
boards = resolve_square([board1, board2], outcomes, square)
for board in boards:
assert list(board.grid[square]) in [[0, 1, 0], [0, 0, 1]]
print(board.proba)
assert board.proba == 0.25
示例2: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
def __init__(self):
brd1 = Board(ENGLISH)
brd1[3][3].pick()
brd2 = brd1.copy()
brd2.complement()
init_costs(brd1)
Problem.__init__(self, brd1, brd2)
self.initial.set_observed_class(self.goal[3][3])
self.initial[3][3].pvalue = \
self.goal[3][3].pvalue = len(self.goal.clses) + 1
self.generated = 0
示例3: main
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
def main():
board = Board()
end_game = False
while not end_game:
win_check(end_game, board)
print("Please choose a position (from 1 to 9)")
human_position = input(">")
board.real_move(1, int(human_position) - 1)
print(str(board))
os.system('clear')
win_check(end_game, board)
current_board = board.copy()
current_node = Node(-1, current_board)
value, position = min_max(current_node)
board.real_move(-1, position)
print(str(board))
示例4: TestBoard
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
class TestBoard(TestCase):
def setUp(self):
self.board = Board(5, 7)
def test_n_rows(self):
self.assertEqual(self.board.n_rows, 5)
def test_n_cols(self):
self.assertEqual(self.board.n_cols, 7)
def test_repr(self):
board = Board(0, 0)
self.assertEqual(repr(board), '')
board = Board(3, 3)
self.assertEqual(repr(board), 9 * ' ')
def test_str(self):
board = Board(1, 1)
self.assertEqual(str(board), '+-+\n| |\n+-+\n')
def test_eq(self):
a = Board(5, 5)
b = Board(5, 5)
c = Board(3, 5)
self.assertEqual(a, b)
self.assertNotEqual(a, c)
def test__hash__(self):
c = {Board(5, 5)}
c.add(Board(5, 5))
self.assertEqual(len(c), 1)
def test__contains__(self):
M, N = 5, 7
board = Board(M, N)
self.assertIn(Position(M - 1, N - 1), board)
self.assertNotIn(Position(-1, -1), board)
self.assertNotIn(Position(M, N), board)
def test_place(self):
self.board.place(Rook, Position(3, 4))
self.assertIsInstance(self.board[3][4], Rook)
def test_place_at(self):
self.board.place_at(Rook, 3, 4)
self.assertIsInstance(self.board[3][4], Rook)
def test_place_out_of_range(self):
self.assertRaises(AssertionError, self.board.place, Rook, Position(7, 4))
def test_place_is_empty(self):
self.board.place_at(Rook, 3, 2)
self.assertRaises(AssertionError, self.board.place, list, Position(3, 2))
def test_place_is_subclass(self):
self.assertRaises(AssertionError, self.board.place, list, Position(3, 2))
def test_pieces(self):
self.board.place_at(Rook, 3, 2)
self.board.place_at(Queen, 1, 1)
self.board.place_at(Knight, 2, 4)
self.assertListEqual([str(x) for x in self.board.pieces], ['R', 'Q', 'N'])
def test_copy(self):
self.board.place_at(Rook, 3, 2)
self.board.place_at(Queen, 1, 1)
self.board.place_at(Knight, 2, 4)
tmp = self.board.copy()
self.assertEqual(self.board, tmp)
self.assertIsNot(self.board, tmp)
def test_attacked(self):
self.board.place_at(Rook, 3, 2)
self.board.place_at(Queen, 1, 1)
self.board.place_at(Knight, 2, 4)
self.assertSetEqual(self.board.attacked, set.union(*[x.attacked_positions for x in self.board.pieces]))
def test_at(self):
self.board.place_at(Rook, 3, 2)
self.board.place_at(Queen, 1, 1)
self.board.place_at(Knight, 2, 4)
self.assertEqual(self.board.at(Position(3, 2)), self.board[3][2])
self.assertEqual(self.board.at(Position(1, 1)), self.board[1][1])
self.assertEqual(self.board.at(Position(2, 4)), self.board[2][4])
self.assertEqual(self.board.at(Position(0, 2)), self.board[0][2])
示例5: ScrabbleVision
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
class ScrabbleVision(Thread):
def __init__(self, source):
Thread.__init__(self)
self.daemon = True
self.source = source
self.board = Board()
self.l = Lock()
self.started = False
self.killed = False
def get_current_board(self):
with self.l:
return self.board.copy()
def kill(self):
self.killed = True
def run(self):
self.source.start()
while True:
frame_raw = self.source.read()
if frame_raw is None:
print 'No frame received; terminating.'
return
if self.killed:
print "Vision terminating"
return
reload(configs)
element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (configs.ERODE_RAD,configs.ERODE_RAD))
element2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (configs.DILATE_RAD,configs.DILATE_RAD))
#BEGIN PROCESSING
try:
if configs.VFLIP:
frame = cv2.flip(frame_raw, flipCode=-1)
else:
frame = frame_raw
if configs.DEBUG:
POST("RAW", frame)
luv = cv2.split(cv2.cvtColor(frame, cv2.COLOR_RGB2LUV))
v_chan = luv[2]
if configs.DEBUG:
POST("V", v_chan)
blur = cv2.GaussianBlur(v_chan, (configs.BLUR_RAD,configs.BLUR_RAD), 0)
if configs.DEBUG:
POST("blur", blur)
thresh = cv2.adaptiveThreshold(blur, 255, 0, 1, configs.BOARD_THRESH_PARAM, configs.BOARD_BLOCK_SIZE)
if configs.DEBUG:
POST("thresh", thresh)
erode = cv2.erode(thresh, element)
erode = cv2.dilate(erode, element2)
if configs.DEBUG:
POST("erode", erode)
erode_draw = frame.copy()
contours,hierarchy = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
possible_corners = []
#Find large contour closest to center of image
for cnt in contours:
sz = cv2.contourArea(cnt)
if sz>75 and sz < 650:
ellipse = cv2.fitEllipse(cnt)
((x,y), (w,h), r) = ellipse
ar = w / h if w > h else h / w
if ar > 1.8:
continue
pf = (w * h * 0.75) / sz
if pf > 1.5:
continue
cv2.ellipse(erode_draw,ellipse,(0,255,0),2)
possible_corners.append((x,y))
def get_closest_corner(point):
dst = float("inf")
crnr = None
for pc in possible_corners:
d = distance(point, pc)
if d < dst:
dst = d
crnr = pc
#.........这里部分代码省略.........
示例6: _put
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
original_closed[node.state] = True
for new_node in node.expand(original_problem):
original_fringe.append(new_node)
_put(new_node, original_hashed_fringe)
except StopIteration:
print "Both fringe exhausted!"
break
return None
if __name__ == '__main__':
B = Board(ENGLISH)
GOAL = B.copy()
GOAL.complement()
GOAL[4][3].put()
GOAL[5][3].put()
B[3][3].pick()
P = BoardProblem(B, GOAL)
IP = ReverseBoardProblem(GOAL, B)
T0 = time.clock()
sol = bidirectional_search(P, IP, go, FIFOQueue(), FIFOQueue())
moves = translate2moves(sol)
T = time.clock() - T0
print B
for move in moves:
示例7: ScrabbleVision
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
class ScrabbleVision(Thread):
def __init__(self):
Thread.__init__(self)
self.board = Board()
self.l = Lock()
self.started = False
self.killed = False
def get_current_board(self):
with self.l:
return self.board.copy()
def kill(self):
self.killed = True
def run(self):
vc = cv2.VideoCapture(-1)
if vc.isOpened(): # try to get the first frame
rval, frame_raw = vc.read()
else:
rval = False
while rval:
if self.killed:
print "Vision terminating"
return
reload(configs)
element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (configs.ERODE_RAD,configs.ERODE_RAD))
element2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (configs.DILATE_RAD,configs.DILATE_RAD))
#BEGIN PROCESSING
try:
frame = cv2.flip(frame_raw, flipCode=-1)
POST("RAW", frame)
luv = cv2.split(cv2.cvtColor(frame, cv2.COLOR_RGB2LUV))
v_chan = luv[2]
#POST("V", v_chan)
blur = cv2.GaussianBlur(v_chan, (configs.BLUR_RAD,configs.BLUR_RAD), 0)
#POST("blur", blur)
thresh = cv2.adaptiveThreshold(blur, 255, 0, 1, configs.BOARD_THRESH_PARAM, configs.BOARD_BLOCK_SIZE)
#POST("thresh", thresh)
erode = cv2.erode(thresh, element)
erode = cv2.dilate(erode, element2)
POST("erode", erode)
erode_draw = frame.copy()
contours,hierarchy = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
possible_corners = []
#Find large contour closest to center of image
for cnt in contours:
sz = cv2.contourArea(cnt)
if sz>75 and sz < 650:
ellipse = cv2.fitEllipse(cnt)
((x,y), (w,h), r) = ellipse
ar = w / h if w > h else h / w
if ar > 1.8:
continue
pf = (w * h * 0.75) / sz
if pf > 1.5:
continue
cv2.ellipse(erode_draw,ellipse,(0,255,0),2)
possible_corners.append((x,y))
"""
lines = cv2.HoughLinesP(erode, 1, 3.14/180, 300, minLineLength=200, maxLineGap=100)[0]
m,n = erode.shape
goodlines = lines[:40]
#Calculate intersection points
points = set()
CLUSTER_RAD = 50
clusters = []
for x in goodlines:
for y in goodlines:
i = intersect(x,y)
if i is not None:
added = False
for (ctr, st) in clusters:
if distance(i, ctr) < CLUSTER_RAD:
#.........这里部分代码省略.........
示例8: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
class Scrabble:
def __init__(self, rack):
self.rack = rack
self.board = Board()
def is_word(self, possible_word):
return (possible_word in DICTIONARY)
def _try_letters_at_position(self, board, letters_at_position):
'''Adds the letters to the board at the given position. Returns False if a
non-word is created by any letter.'''
starting_pos = letters_at_position.position_with_direction.position.copy()
direction = letters_at_position.position_with_direction.direction
letters = letters_at_position.letters
try:
board.add_letters(letters, starting_pos, direction)
except OutOfBoundsException:
return False
# Check that there is a word at the original position/direction
if not self.is_word(board.get_word(starting_pos, direction)):
# We didn't make a real word
return False
# We made a real word, now check to make sure we didn't create any
# non-words in the other direction.
other_direction = Position.get_other_direction(direction)
moving_position = starting_pos.copy()
for _ in range(len(letters)):
generated_word = board.get_word(moving_position, other_direction)
moving_position.add_in_direction(1, direction) # for next time
if len(generated_word) < 2:
# This one isn't a word- it's just a letter. We're fine
continue
if not self.is_word(generated_word):
# We accidentally made a non word!
return False
# We made a word, and didn't make any non-words in the other direction!
return True
def try_rack_at_position(self, position_to_try):
'''Try all combinations of the rack letters at position to see if we can
make a word. The minimum word length is the distance to the closest
letter, to make sure we're touching it.'''
good_words = []
minimum_num_letters = self.board.get_spaces_to_next_letter(position_to_try)
for num_letters in range(minimum_num_letters, 8):
for letters_to_try in future_itertools.permutations(self.rack,
num_letters):
# Make a fake board and add these letters to it
temp_board = self.board.copy()
letters_at_position = LettersAtPosition(position_to_try,
letters_to_try)
try:
if self._try_letters_at_position(temp_board, letters_at_position):
# We made a word!
good_words.append(letters_at_position)
except OutOfBoundsException:
# Just skip if we start or end out of bounds
continue
return good_words
def generate_positions_to_try(self, ):
'''Returns all possible places where our 7 letters could be played. This
means the 7 spaces to the top/left of each piece on the board, and 1 space
to the down/right. A position is skipped if it is not on the board or
already occupied by a piece.
Return type is PositionWithDirection, which gives the position and
direction to be tried.'''
positions_to_try = set()
for base_position in self.board.get_position_of_all_letters():
for direction in Position.DIRECTIONS:
# Try 7 to the left (or up), and 1 to the right (or down)
magnitudes_to_try = range(-7, 0) + [1]
for distance_away_from_base in magnitudes_to_try:
position = base_position.copy()
position.add_in_direction(distance_away_from_base, direction)
if self.board.position_is_out_of_bounds(position):
# Can't start at an out of bounds position
continue
if not self.board.is_blank(position):
# Skipping position- already has a letter
continue
positions_to_try.add(PositionWithDirection(position, direction))
return positions_to_try
示例9: GUI
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import copy [as 别名]
class GUI(object):
'Interface for solving Sudoku puzzles.'
def __init__(self):
'Initialize GUI class.'
self.board = Board()
self.restore = None
self.current = None
self.createGUI()
self.setEnabled(True)
try:
self.root.mainloop()
except KeyboardInterrupt:
self.board.terminate()
def createGUI(self):
'Create a TK interface.'
self.root = tk.Tk()
self.root.wm_title('Sudoku')
self.frame = tk.Frame(self.root)
self.frame.pack()
self.frLeft = tk.LabelFrame(self.frame)
self.frLeft.grid(row=0, column=0)
self.frRight = tk.LabelFrame(self.frame)
self.frRight.grid(row=0, column=1, sticky=tk.N)
self.entries = []
for r in range(9):
ex = []
for c in range(9):
e = tk.Entry(self.frLeft, width=2)
e.grid(row=r, column=c)
ex.append(e)
self.entries.append(ex)
self.time = tk.StringVar()
self.update = tk.BooleanVar()
self.findall = tk.BooleanVar()
self.lblTime = tk.Label(self.frame, textvariable=self.time)
self.lblTime.grid(row=1, columnspan=2)
self.time.set('suji wa dokushin ni kagiru')
self.edtGivens = tk.Entry(self.frRight, width=2)
self.edtGivens.insert(tk.END, '23')
self.edtGivens.grid(row=1, column=0, sticky=tk.E)
self.btnRandom = tk.Button(self.frRight, text="Random", command=self.onRandom)
self.btnRandom.grid(row=1, column=1, sticky=tk.EW)
self.btnEmpty = tk.Button(self.frRight, text="Empty", command=self.onEmpty)
self.btnEmpty.grid(row=2, column=0, sticky=tk.EW)
self.btnRestore = tk.Button(self.frRight, text="Restore", command=self.onRestore)
self.btnRestore.grid(row=2, column=1, sticky=tk.EW)
self.btnNext = tk.Button(self.frRight, text="Next", command=self.onNext)
self.btnNext.grid(row=3, column=0, sticky=tk.EW)
self.btnPrevious = tk.Button(self.frRight, text="Previous", command=self.onPrevious)
self.btnPrevious.grid(row=3, column=1, sticky=tk.EW)
self.chkFindAll = tk.Checkbutton(self.frRight, text="Find all", command=self.onFindAll, variable=self.findall)
self.chkFindAll.grid(row=6, column=1, sticky=tk.EW)
self.chkUpdate = tk.Checkbutton(self.frRight, text="Update", command=self.onUpdate, variable=self.update)
self.chkUpdate.grid(row=7, column=1, sticky=tk.EW)
self.btnStop = tk.Button(self.frRight, text="Stop", command=self.onStop)
self.btnStop.grid(row=7, column=0, sticky=tk.EW)
self.btnSolve = tk.Button(self.frRight, text="Solve", command=self.onSolve)
self.btnSolve.grid(row=8, column=0, sticky=tk.EW, columnspan=2)
def onUpdate(self):
self.board.updatefunc = self.update.get() \
and self.scatterBoard \
or None
def onRestore(self):
self.board.copy(self.restore)
self.scatterBoard()
def onNext(self):
if self.current == None and len(self.board.results) > 0:
self.current = 0
elif (self.current + 1) < len(self.board.results):
self.current += 1
else:
return
self.board.copy(self.board.results[self.current])
self.scatterBoard()
def onPrevious(self):
if self.current == None and len(self.board.results) > 0:
self.current = len(self.board.results) - 1
elif self.current > 0:
#.........这里部分代码省略.........