本文整理汇总了Python中board.Board.place_piece方法的典型用法代码示例。如果您正苦于以下问题:Python Board.place_piece方法的具体用法?Python Board.place_piece怎么用?Python Board.place_piece使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.place_piece方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_board_does_not_move_when_move_validator_disallows_it
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
def test_board_does_not_move_when_move_validator_disallows_it(self):
move_validator = fmv()
b = Board(5,5,'0',move_validator)
start_place = (0,0,0)
end_place = (0,1,-1)
p = Piece('_')
b.place_piece(lp(p, start_place,0))
b.move_piece(start_place, end_place)
self.assertTrue(b.pieces_dict[start_place] == p
and end_place not in b.pieces_dict)
示例2: play_game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
def play_game(a, b, verbose=False):
players = ["X", "O"]
player_interfaces = {"X": a, "O": b}
player_pieces = {"X": deepcopy(all_pieces), "O": deepcopy(all_pieces)}
board = Board()
pass_count = 0
for player in itertools.cycle(players):
piece = player_interfaces[player].move(board, player, player_pieces)
if piece is None: # Player passes
pass_count += 1
if pass_count >= len(players):
break
continue
else:
pass_count = 0
available_pieces = player_pieces[player]
c_piece = piece.canonical()
if c_piece in available_pieces:
available_pieces.remove(c_piece)
else:
raise ValueError("Piece not available.")
board.place_piece(piece, player)
if verbose:
display_board(board, player)
scores = {}
for player in players:
remaining = player_pieces[player]
scores[player] = sum(len(piece) for piece in remaining)
if verbose:
print("game over")
print("scores:")
for player, score in scores.items():
print("{} ({}) - {}".format(player_interfaces[player].__name__, player, score))
min_score = min(scores.values())
for player, score in scores.items():
if score == min_score:
print("winner:", player_interfaces[player].__name__)
return tuple(scores[p] for p in players)
示例3: TestBoard
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
class TestBoard(unittest.TestCase):
def setUp(self):
self.board = Board()
def test_default_board_size(self):
self.assertEqual(SIZE, len(self.board.board))
def test_num_spaces(self):
self.assertEqual(self.board.num_spaces(),
self.board.size * self.board.size)
def test_board_empty(self):
for i in xrange(SIZE):
for j in xrange(SIZE):
self.assertEqual(self.board.board[i][j], EMPTY)
def test_clear_board(self):
self.board.board[0][1] = 1
self.board.clear_board()
self.assertEqual(self.board.board[0][1], EMPTY)
def test_place_piece(self):
self.board.place_piece('x', 1, 0)
self.assertEqual(self.board.board[1][0], 'x')
def test_blank_spaces(self):
self.board.board[0][2] = 'x'
self.assertEqual(len(self.board.blank_spaces()),
self.board.size*self.board.size-1)
def test_piece_at_position(self):
self.board.board[0][2] = 'x'
self.assertEqual(self.board.piece_at_position(0, 2), 'x')
def test_flatten_board(self):
self.assertEqual(len(self.board.flatten_board()),
self.board.size*self.board.size)
示例4: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
class GoClient:
def __init__(self, IP='127.0.0.1', port=5005):
self.root = tk.Tk()
self.IP = IP
self.port = port
self.players = []
def start_game(self, player, size):
self.players.append(player)
self.board = Board(size)
print 'beginning game as player %d' % player
self.make_display()
self.gui.start_game()
def made_move(self, i, j):
self.board.place_piece(i, j)
self.gui.made_move()
def passed_turn(self):
self.board.pass_turn()
self.gui.passed_turn()
def game_over(self, score1, score2):
self.gui.set_message('Game Over, Black: %d White: %d' % (score1, score2))
self.board.gameover = True
def on_click(self, i, j):
if self.board.turn in self.players:
self.send('MAKEMOVE %d %d' % (i, j))
# self.receive('MADEMOVE %d %d' % (i, j))
def on_quit(self):
send('QUIT')
self.gui.parent.destroy()
def on_pass(self):
if self.board.turn in self.players:
self.send('PASSTURN')
def run(self):
# self.start_game(1)
# self.start_game(2)
self.connect_to_server()
self.root.title('Python Online Five-In-A-Row')
# root.resizable(0,0)
self.root.mainloop()
# print 'received data:', data
def make_display(self):
self.gui = BoardGui(parent=self.root, board=self.board, players=self.players)
self.gui.on_click.append(self.on_click)
self.gui.on_pass.append(self.on_pass)
self.gui.pack(side='top', fill='both', expand='true', padx=4, pady=4)
def receive(self, data):
print 'receiving [%s]' % data
data = data.split()
if not data:
return
message = data[0]
if message == 'BEGINGAME':
self.start_game(int(data[1]), int(data[2]))
elif message == 'MADEMOVE':
i, j = map(int, data[1:3])
self.made_move(i, j)
elif message == 'PASSEDTURN':
self.passed_turn()
elif message == 'GAMEOVER':
a, b = map(int, data[1:3])
self.game_over(a, b)
def send(self, data):
print 'sending %s' % data
self.skt.send(data)
def connect_to_server(self):
BUFFER_SIZE = 1024
self.skt = Socket(AF_INET, SOCK_STREAM)
self.skt.connect((self.IP, self.port))
self.skt.setblocking(0)
def listen():
try:
data = self.skt.recv(BUFFER_SIZE)
except SocketError:
pass
else:
if not data:
return
for line in data.split('\n'):
self.receive(line)
self.root.after(500, listen)
listen()
示例5: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
class Game(object):
name = 'Tic Tac Toe'
def __init__(self, player1, player2):
self.player1 = player1
self.player2 = player2
self.player1.marker = 'O'
self.player2.marker = 'X'
self.player1.games_won = 0
self.player2.games_won = 0
self.next_player = player1
def player_to_play(self):
to_play = self.next_player
if to_play == self.player1:
self.next_player = self.player2
else:
self.next_player = self.player1
return to_play
def player_from_marker(self, marker):
if self.player1.marker == marker:
return self.player1
else:
return self.player2
def game_ended(self):
won = self.board.someone_has_one()
if won:
player_won = self.player_from_marker(won)
player_won.games_won += 1
self.print_game(player_won)
return True
if self.board.full():
self.print_game('none')
return True
return False
def print_game(self, last_won=None):
print chr(27) + "[2J"
o = self.player_from_marker('O')
x = self.player_from_marker('X')
o_won = colored('WINNER!!', 'red', attrs=['bold']) if last_won == o else ''
x_won = colored('WINNER!!', 'red', attrs=['bold']) if last_won == x else ''
if last_won == 'none':
o_won = x_won = colored('STALEMATE!!', 'red', attrs=['bold'])
print colored('O:', 'green', attrs=['bold']) + " %s %s %s" % (colored(o.name, 'green', attrs=['bold']), colored('(%s)' % o.games_won, 'yellow'), o_won)
print colored('X:', 'red', attrs=['bold']) + " %s %s %s" % (colored(x.name, 'red', attrs=['bold']), colored('(%s)' % x.games_won, 'yellow'), x_won)
print
self.board.print_board()
def play(self):
for i in range(5):
self.game_run = i
self.board = Board()
while (not self.game_ended()):
self.print_game()
sleep(0.5)
player_to_play = self.player_to_play()
index = player_to_play.place_piece(self.board.board)
self.board.place_piece(index, player_to_play.marker)
self.print_game()
sleep(2)
self.player1.marker, self.player2.marker = self.player2.marker, self.player1.marker
self.next_player = self.player_from_marker('O')
print
if self.player1.games_won > self.player2.games_won:
print "%s WON %s to %s." % (self.player1.name, self.player1.games_won, self.player2.games_won)
elif self.player2.games_won > self.player1.games_won:
print "%s WON %s to %s." % (self.player2.name, self.player2.games_won, self.player1.games_won)
else:
print "%s and %s tied." % (self.player1.name, self.player2.name)
示例6: test_place_piece
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
def test_place_piece(self):
board = Board()
board.place_piece('x',0)
with self.assertRaises(BoardException):
board.place_piece('A',0)
示例7: test_place_piece
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
def test_place_piece(self):
b = Board(5,5,60)
place = (2,-4,2)
piece = Piece('some')
b.place_piece(lp(piece,place,60))
self.assertTrue(b.pieces_dict[place] == piece)
示例8: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import place_piece [as 别名]
class GoServer:
def __init__(self, IP='127.0.0.1', port=5005, BUFFER_SIZE=1024, size=19):
self.TCP_IP = IP
self.TCP_PORT = port
self.BUFFER_SIZE = BUFFER_SIZE
self.board = Board(size)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((self.TCP_IP, self.TCP_PORT))
s.listen(1)
self.size = size
self.s = s
def receive_players(self):
self.conn = [None]*2
self.addr = [None]*2
for i in range(2):
conn, addr = self.s.accept()
print 'made connection %d: %s ' % (i, addr)
self.conn[i] = conn
self.addr[i] = addr
self.game_num = {} # maps connection number {0, 1} to game number {1, 2}
self.conn_num = {} # maps game number {1, 2} to connection number {0, 1}
first_player_num = random.randint(1, 2)
self.game_num[0] = first_player_num
self.game_num[1] = 2 if first_player_num == 1 else 1
self.conn_num[1] = 0 if first_player_num == 1 else 1
self.conn_num[2] = 1 if first_player_num == 1 else 0
def terminate(self):
print 'closing connection'
self.s.shutdown(socket.SHUT_RDWR)
self.s.close()
print 'connection closed'
for conn in self.conn:
if conn:
conn.close()
def run_game(self):
for i in range(2):
self.send('BEGINGAME %d %d\n' % (self.game_num[i], self.size), i)
while True:
curr_player = self.board.turn
curr_conn_num = self.conn_num[curr_player]
data = self.conn[curr_conn_num].recv(self.BUFFER_SIZE)
if not data:
break
print 'received %s from %s' % (data, self.addr[curr_conn_num])
if data.startswith('MAKEMOVE'):
tokens = data.split()
if len(tokens) == 3:
try:
i = int(tokens[1])
j = int(tokens[2])
except ValueError:
continue
if self.board.place_piece(i, j):
self.send_all('MADEMOVE ' + str(i) + ' ' + str(j) + '\n')
elif data.startswith('PASSTURN'):
if self.board.pass_turn():
self.send_all('PASSEDTURN' + '\n')
if self.board.gameover:
score_white, score_black = self.board.score()
self.send_all('GAMEOVER %d %d \n' % (score_black, score_white))
break
elif data.startswith('QUIT'):
self.board.gameover = True
score_white, score_black = self.board.score()
self.send_all('GAMEOVER %d %d \n' % (score_black, score_white))
break
print 'done serving'
def send(self, message, player):
self.conn[player].send(message)
def send_all(self, message):
for conn in self.conn:
conn.send(message)