本文整理汇总了Python中board.Board.draw_board方法的典型用法代码示例。如果您正苦于以下问题:Python Board.draw_board方法的具体用法?Python Board.draw_board怎么用?Python Board.draw_board使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.draw_board方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import draw_board [as 别名]
class TicTacToe:
def __init__(self):
self.board = Board()
def prompt_integer(self, msg):
while True:
command = input('Enter {} digit: '.format(msg))
if (len(command) != 1) or (not command.isdigit()) or\
(not int(command) in range(1, 4)):
print('Error: Enter 1 digit in the range [1,3]')
else:
return int(command)
def prompt_user_mark(self):
print('Enter X,Y coordinates for your move:')
while True:
x = self.prompt_integer('row')
y = self.prompt_integer('column')
if not self.board.is_pos_empty(x - 1, y - 1):
print('Error: Position not available! Choose another!')
else:
return (x - 1, y - 1)
def get_outcome(self, status):
if status == Board.USER_WIN:
return 'Congratulations! You WIN!!!'
elif status == Board.AI_WIN:
return 'Loooooser! You LOSE!'
else:
return 'The game is a DRAW!'
def main_loop(self):
print('Welcome to Tic-Tac-Toe!')
print(self.board.draw_board())
is_game_over = Board.NOT_OVER
while is_game_over is Board.NOT_OVER:
if self.board.is_user_turn:
pos = self.prompt_user_mark()
self.board.make_move(Board.USER, *pos)
else:
print('AI takes turn...')
moves = self.board.get_available_moves()
best_move = AI(self.board, self.board.is_user_turn).best_move()
self.board.make_move(Board.AI, *moves[best_move])
print(self.board.draw_board())
is_game_over = self.board.is_game_over()
print(self.get_outcome(is_game_over))
示例2: run
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import draw_board [as 别名]
def run():
# Setup the images
setup_images()
# Create the game board
try:
board = Board(width=game.GAME_WIDTH,
height=game.GAME_HEIGHT,
tile_width=TILE_WIDTH,
tile_height=TILE_HEIGHT,
screen_width=SCREEN_X,
screen_height=SCREEN_Y)
board.IMAGES = IMAGES
board.draw_board()
except (AttributeError) as e:
board = Board()
game.GAME_BOARD = board
# Set up an fps display
try:
if game.DEBUG == True:
fps_display = pyglet.clock.ClockDisplay()
draw_list.append(fps_display)
except AttributeError:
pass
# Add the board and the fps display to the draw list
draw_list.append(board)
# Set up the update clock
pyglet.clock.schedule_interval(update, 1/2.)
game.initialize()
pyglet.app.run()
示例3: Player
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import draw_board [as 别名]
class Player(object):
'''
classdocs
'''
def __init__(self, number, name):
'''
Constructor
'''
self._number = number
self._name= name
self._my_board = Board()
self._their_board = Board()
def _clear_screen(self):
for line in range(100): print("")
def set_opponent(self, opponent):
self._opponent = opponent
def setup_ship(self, name, length):
while True :
print("Add a " + name + " (length {0}):".format(length))
start = input("Enter a start coordinate : ")
end = input("Enter the end coordinate: ")
ship = Ship(Coord(start), Coord(end))
if ship.length() != length :
print("That's not the right length ({0}). Try again.".format(ship.length()))
else :
break
self._my_board.add_ship(ship)
def setup_ships(self):
self.setup_ship("MTB", 2)
self._my_board.draw_board()
self.setup_ship("Frigate", 3)
self._my_board.draw_board()
input("Press <enter>")
self._clear_screen()
def play(self):
print("Player {0}: ".format(self._number) + self._name)
print("=============================================================")
self._my_board.draw_board()
print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
self._their_board.draw_board()
print("Fire torpedo!")
guess = input("Enter the coordinates : ")
result = self._opponent.fire(Coord(guess))
if result == Ship.HIT:
print(guess + " is a HIT!")
self._their_board.record_hit(Coord(guess))
elif result == Ship.SUNK:
print(guess + " has SUNK the ship!")
self._their_board.record_hit(Coord(guess))
else: # Ship.MISS
print(guess + " is a MISS")
self._their_board.record_miss(Coord(guess))
input("Hit <enter> to continue...")
self._clear_screen()
def fire(self, rc):
return self._my_board.fire(rc)
def lost(self):
return self._my_board.all_sunk()
示例4: BattleshipGame
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import draw_board [as 别名]
class BattleshipGame(object):
def __init__(self):
self.start()
self.board_selection()
self.boat_selection()
self.board_in_action = True
while self.board_in_action is True:
self.take_a_turn()
self.print_end()
def start(self):
print '------------'
print 'start'
print '------------'
#let the user type something:
print "type your name"
#lets them type: Raw input
name = raw_input()
if name == '':
name = 'Garrett'
# Do different things based on what the user typed
if name == "Garrett":
print "hello Garrett"
if name != "Garrett":
print name
def board_selection(self):
print "choose a board (easy, medium or hard)"
difficulty_level = raw_input()
if difficulty_level == '':
difficulty_level = 'easy'
self.board = Board (difficulty_level)
self.board.draw_board()
def boat_selection(self):
self.boats = []
for counter in range(4):
boat_length = counter + 2
message = "type the coordinates you want your boat to go (capitol letter then number no spaces for the coordinates) " + str(boat_length) + " this is the length of your boat"
spot = self.choose_spot(message)
self.board.place_boat(spot[0], spot[1])
self.board.draw_board()
#print self.boats
def take_a_turn(self):
message = "write the place you want to hit (capitol letter then number no spaces). Type 'end' to quit."
spot = self.choose_spot(message)
if spot is None:
return None
self.board.place_shot(spot[0], spot[1])
self.board.draw_board()
def choose_spot(self, message):
good_choice = False
while good_choice is False:
print message
player_chose = raw_input() # Expected input: numberletter
good_choice = self.validate(player_chose)
# Check to see if they typed a letter then number.
if player_chose != 'end':
row_letter = player_chose[0] # 'A'
col_number = int(player_chose[1:]) # 3
return [row_letter, col_number]
else:
self.board_in_action = False
return None
def validate(self, player_chose ):
if player_chose != '':
# player_chose[0] is the letter, player_chose[1:] is the rest of the number
if player_chose == 'end' or (player_chose[0] in self.board.letter_list and int(player_chose[1:]) in range(13)): # if its a letter then number
return True
#.........这里部分代码省略.........
示例5: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import draw_board [as 别名]
class Game(object):
"""
Top level class, the game object contains all of the other
class instances such as the pieces, the board etc.
"""
move_dict = {'king': [[i, j] for i in range(-1, 2) for j in range(-1, 2) if i != 0 or j != 0],
'rook': [[i, 0] for i in range(-8, 9) if i != 0] +
[[0, i] for i in range(-8, 9) if i != 0],
'bishop': [[i, i] for i in range(-8, 9) if i != 0] +
[[i, i * -1] for i in range(-8, 9) if i != 0],
'knight': [[i, j] for i in range(-2, 3) for j in range(-2, 3)
if abs(i) + abs(j) == 3],
'pawn': [[1, i, msg] for i in [-1, 1] for msg in ['on_take', 'en_passant']] +
[[2, 0, 'on_first'], [1, 0]]
}
move_dict['queen'] = move_dict['rook'] + move_dict['bishop']
def __init__(self, turn_limit=200, custom_start_positions=None, default_logging=False):
"""
Initialise game object and create required member objects
"""
start_pos = custom_start_positions or deepcopy(DEFAULT_START_POSITIONS)
# need a copy here otherwise DEFAULT_START_POSITIONS gets changes and reused in next game
self.logging = default_logging if default_logging else LOGGING
self.board = Board(start_pos)
self.pieces = self.__create_pieces(Game.move_dict, start_pos)
# initialise variables that will be needed later
self.check = False
self.checkmate = False
self.draw = False
self.turns = 0
self.current_team = None
self.last_piece_to_move = None
self.turn_limit = turn_limit
def __to_json(self):
"""
Output entire object contents as json.
"""
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
@staticmethod
def __create_pieces(move_dict, start_pos):
"""
Creates a object for each piece and creates a dictionary to
enable access to the pieces via their ref.
The source of this data and the refs is the positions list, in
the game object.
"""
pieces = {}
for row, row_content in start_pos.items():
if row > 0:
for col, piece_ref in row_content.items():
if piece_ref:
team = TEAMS[piece_ref[0]]
name = PIECE_CODES[piece_ref[1]]
# doesn't matter what actual move_cnt is, just needs to be >= 1 if moved
if DEFAULT_START_POSITIONS[row][col] == start_pos[row][col]:
move_cnt = 0
else:
move_cnt = 1
pieces[piece_ref] = Piece(piece_ref, name, team, row, col,
move_dict[name], move_cnt)
return pieces
def take_turn(self, team, prompt=None, move=None):
"""
Interact with player to facilitate moves, capture data and
identify/store information common to all potential moves.
Also includes optional param to specify a prompt to run
automatically or a move object (for interface from external
scripts).
"""
global LOG
self.turns += 1
self.current_team = team
occupied, our_team, their_team = self.get_occupied()
validated, found_issue = False, False
if self.check:
user_feedback = shout(team + ' team in check',
print_output=False, return_output=True)
else:
user_feedback = None
# repeat prompt until a valid move is given...
while not validated:
# skip set up if a move object is passed in...
if move:
piece, up, right = move.piece, move.up, move.right
else:
if not prompt:
print(self.board.draw_board())
#.........这里部分代码省略.........