本文整理汇总了Python中board.Board.print_board方法的典型用法代码示例。如果您正苦于以下问题:Python Board.print_board方法的具体用法?Python Board.print_board怎么用?Python Board.print_board使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.print_board方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
class Game():
"""Game clas for Battleship."""
def __init__(self):
"""Initialize the game."""
self.initialize_game()
self.set_up_boards()
self.set_up_fleets()
self.play_game()
def initialize_game(self):
"""Initialize game basics."""
self.level = 'normal'
self.name = input("Please enter your name: ")
self.done = False
def set_up_boards(self):
"""Set up the playing surface."""
self.user_board = Board(self.name)
self.computer_board = Board("Computer")
self.guess_board = Board("Guesses")
def set_up_fleets(self):
"""Set up the two fleets for the battleship game."""
self.user_fleet = Fleet()
self.user_fleet.set_fleet(self.user_board)
if self.user_fleet.quit_flag:
self.done = True
else:
self.computer_fleet = Fleet()
self.computer_fleet.set_fleet_auto(self.computer_board)
def print_game_status(self):
"""Print the current game status."""
self.guess_board.print_board()
# self.computer_board.print_board()
print('=' * self.user_board.columns * 4)
self.user_board.print_board()
def play_game(self):
"""Battleship Game mechanics."""
turn_count = 0
# print(self.computer_fleet)
while not self.done:
functions.clear()
self.print_game_status()
user_guess = input("Please Enter a Guess: ")
user_guess = Guess(user_guess)
user_guess.check_if_on_board(self.user_board)
if user_guess.quit_flag:
break
self.computer_board.check_damage(user_guess,
self.guess_board,
self.computer_fleet)
turn_count += 1
if self.computer_fleet.all_ships_sank():
msg = "Congratulations you won! It took you {} turns."
print(msg.format(turn_count))
self.done = True
示例2: main
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def main():
ttt_board = Board()
turn = Board.CIRCLE
move_number = 0
print_menu()
ttt_board.print_board()
while True:
move = raw_input(" Player %d what is your move? " % turn).lower()
while not ttt_board.is_valid_move(move):
move = raw_input(" Incorrect command. Try again...Player %d what is your move? " % turn).lower()
if move == 'exit':
return
row = ord(move[0]) - ord('a')
col = int(move[1]) - 1
ttt_board.make_move(row, col, turn)
ttt_board.print_board()
if ttt_board.check_for_win(row, col, turn):
print " Player %d has won! Congratulations!" % turn
return
else:
if move_number == (Board.N ** 2 - 1):
print " The game ended in a draw!"
return
turn = Board.CROSS if turn == Board.CIRCLE else Board.CIRCLE
move_number += 1
示例3: main
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def main():
"""
Run the main program!
"""
game_board = Board()
while True:
print "Classic Console Tic-Tac-Toe"
print ""
print "Make a selection:"
print ""
print "n) new game"
print "p) print the board"
print "q) quit"
print "0-8) make the given move"
print ""
print "Enter your selection: "
selection = raw_input()
if selection in ("q", "Q"):
break
elif selection in ("p", "P"):
game_board.print_board()
elif selection in ("n", "N"):
game_board = Board()
if not is_user_first():
game_board.add_mark(game_board.find_next_move("X"), "X")
elif selection in ('0', '1', '2', '3', '4', '5', '6', '7', '8'):
position = int(selection)
take_turn(game_board, position, "O")
示例4: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
class FanoronaGame:
"""This class represents the game as is it."""
def __init__(self):
"""Ctor"""
self.board = Board()
def start(self):
"""Starts the game with GUI"""
gui = GUI(self.board)
gui.start_main_loop()
def start_text(self):
"""Starts the game in text mode"""
black = False # white begin the game
while not self.board.game_is_finished():
self.board.print_board()
if black:
prompt = "black> "
else:
prompt = "white> "
move = raw_input(prompt)
self.board.move_piece(move, black)
black = not black
示例5: main
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def main(arguments):
if len(arguments) > 4:
print "Sorry, there were too many arguments."
print "You only need to include the number of rows, columns and the required length to win."
print "In that order."
return 0
elif len(arguments) < 4:
print "Sorry, there were not enough arguments"
print "You need to include number of rows, columns and the required length to win the game."
print "In that order."
return 0
try:
num_rows = int(arguments[1])
num_cols = int(arguments[2])
length_to_win = int(arguments[3])
except ValueError:
print "Please input only numbers"
return 0
print "Welcome to Connect 4 in python!"
load_or_save = str(raw_input("Would you like to continue your old game? Yes or no? :"))
if load_or_save.lower() == 'yes':
board = Board(0,0)
board.load_board()
else:
board = Board(num_rows, num_cols)
board.create_board()
engine = Engine(num_rows, num_cols, length_to_win, board)
board.print_board()
while(1):
try:
player_number = int(raw_input("Enter the number of players: "))
if player_number < 1:
print "Please use a number larger than 0!"
else:
break
except ValueError:
print "Please use a number!"
print "If at any time you would like to save your game, simply type save."
while (True):
for player in range(player_number):
column = -1
input = raw_input("Pick a column for your token player %d: " % (player))
if input.lower() == 'save':
board.save_board()
print "Your game has been saved."
return 0
break
try:
column = int(input)
print engine.place_token(column, player)
except ValueError:
print "You didn't save or use an acceptable number. You're turn is over."
board.print_board()
if engine.winner() != -1:
print "Congratulations player %d!!\nYou won conncect 4!" %(player)
return 0
示例6: test_check_print_board_empty
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def test_check_print_board_empty():
"""
Check to see if printing empty board works right
"""
test_board = Board()
out = StringIO()
test_board.print_board(out=out)
expected = ("0 "*8+"\n")*8+"\n"
assert expected == out.getvalue()
示例7: main
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def main():
"""Executes the main application.
Keyword arguments:
<None>
"""
# Instantiate the x & y position variables.
x = 9
y = 9
# Take x & y initial inputs. Loop while invalid.
while (x < 1 or x > BOARD_SIZE) or (y < 1 or y > BOARD_SIZE):
x, y = input("Enter x & y (1-" + str(BOARD_SIZE)
+ ") separated by a space (e.g. 4 4): ").split()
x, y = [int(x), int(y)]
if x < 1 or x > BOARD_SIZE:
print("ERROR:: x must be 1-" + str(BOARD_SIZE))
if y < 1 or y > BOARD_SIZE:
print("ERROR:: y must be 1-" + str(BOARD_SIZE))
print()
# Create a Knight object to move around the board.
knight = Knight(x, y)
# Instantiate a board and set the initial Knight position with move 1.
board = Board(BOARD_SIZE)
board.place_knight(1, knight.x, knight.y)
# Test special cases for all the moves.
for current_move in range(2, MAX_MOVES + 1):
num_possibilities, next_x, next_y = get_num_possibilities(knight, board)
min_exits_idx = 0
# If there are no possibilities left, then end the tour prematurely.
if num_possibilities == 0:
print("The knight's tour ended prematurely at (" + str(knight.x + 1)
+ "," + str(knight.y + 1) + ") during move #"
+ str(current_move - 1) + ".")
print()
break
# If there is more than 1 possibility, then find the next squares with the
# minimum number of exits.
elif num_possibilities > 1:
exits = find_min_exits(board, num_possibilities, next_x, next_y)
min_exits_idx = get_idx_smallest_num_exits(num_possibilities, exits)
# Move the knight and mark its position on the board.
knight.move(next_x[min_exits_idx], next_y[min_exits_idx])
board.place_knight(current_move, knight.x, knight.y)
# Print out the board.
board.print_board()
示例8: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
class Game(object):
def __init__(self):
self.b = Board()
self.manager = Manager(self.b)
self.player = Player("Scott", self.b)
self.ai = Player("AI", self.b)
self.tilebag = Tilebag("setup/tileset.txt")
def print_board(self):
self.b.print_board()
示例9: run
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def run(self):
# x and y of playing field
width = input('enter the width of the board')
width = int(width)
height = input('enter the height of the board')
height = int(height)
board = Board(width, height)
curr_play = 0
init_pos_player_1 = input('enter the inital position of player 1 as a tuple (x, y)')
init_pos_player_2 = input('enter the inital position of player 2 as a tuple (x, y)')
p1_split = init_pos_player_1.split(',')
p2_split = init_pos_player_2.split(',')
player_1 = Queen()
player_1.set_position(int(p1_split[0]), int(p1_split[1]))
board.set_tile(1, player_1.get_position()[0], player_1.get_position()[1])
player_2 = Queen()
player_2.set_position(int(p2_split[0]), int(p2_split[1]))
board.set_tile(2, player_2.get_position()[0], player_2.get_position()[1])
# inital position of player 1
# inital pos of player 2
players = [player_1, player_2]
while len(players[curr_play].valid_moves(board)) != 0:
#print board
board.print_board()
print('valid moves')
avail_moves = players[curr_play].valid_moves(board)
print(avail_moves)
#player to pick a move
in_pos_player = input('enter the move of player {0} as a tuple (x, y)'.format(curr_play+1))
split = in_pos_player.split(',')
move = (int(split[0]), int(split[1]))
#if move is valid
if move in avail_moves:
#update board
current_pos = players[curr_play].get_position()
board.set_tile(-1, current_pos[0], current_pos[1])
players[curr_play].set_position(move[0], move[1])
#new position
current_pos = players[curr_play].get_position()
board.set_tile(curr_play+1, current_pos[0], current_pos[1])
else:
raise ValueError('not in move set')
#update turn
curr_play += 1
curr_play %= 2
#end of loop
print('player {0} lost! player {1} won!'.format(curr_play+1, (curr_play+2)%3))
示例10: test_print_with_few_pieces
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def test_print_with_few_pieces():
"""
Print the board that contains a few pieces
"""
test_board = Board()
test_board.add_piece(0, 2)
test_board.add_piece(5, 1)
test_board.add_piece(6, 2)
out = StringIO()
test_board.print_board(out=out)
expected = ("0 "*test_board.COLUMNS+"\n")*(test_board.ROWS-1)
expected += "2 0 0 0 0 1 2 " + ("0 "*(test_board.COLUMNS-7))+"\n\n"
assert expected == out.getvalue()
示例11: test_print_board_in_play
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def test_print_board_in_play(self, mock_stdout):
"""
Test printing the game board when in play
"""
board = Board()
board.add_mark(0, "X")
board.add_mark(6, "O")
board.add_mark(1, "X")
board.add_mark(7, "O")
board.print_board()
printed_text = self.get_printed_boards(mock_stdout)[0]
self.assertNotIn("Winner", printed_text)
self.assertEquals(printed_text.count("X"), 2)
self.assertEquals(printed_text.count("O"), 2)
示例12: test_print_board_with_winner
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def test_print_board_with_winner(self, mock_stdout):
"""
Test printing the game board after a winner has been determined.
"""
board = Board()
board.add_mark(0, "X")
board.add_mark(6, "O")
board.add_mark(1, "X")
board.add_mark(7, "O")
board.add_mark(2, "X")
board.print_board()
printed_text = self.get_printed_boards(mock_stdout)[0]
self.assertIn("Winner", printed_text)
self.assertEquals(printed_text.count("X"), 4)
self.assertEquals(printed_text.count("O"), 2)
示例13: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
class Game:
def __init__(self, interface, colors, player_types):
self.interface = interface
self.playing = True
self.winner = None
self.colors = colors
self.board = Board(self.interface)
self.players = []
for player in player_types:
self.players.append(eval(player)(self._get_color(), self.colors, self.interface))
def _get_color(self):
if not self.players:
return choice(self.colors)
else:
colors = list(self.colors)
colors.remove(self.players[0].color)
return colors[0]
def _move(self, player, board):
try:
column = player.get_move(board)
move = self.board.make_move(column, player.color)
except error.InvalidMoveError:
return None
return move
def _order_players(self, player):
if self.players.index(player) != 0:
self.players.reverse()
def play(self):
self._order_players(self.interface.new_game(self.players, self.board))
while self.playing:
for player in self.players:
move = None
while move is None:
move = self._move(player, self.board)
self.board.print_board()
if self.board.player_won(player.color):
self.winner = player
self.playing = False
break
if self.board.is_full():
self.winner = None
self.playing = False
break
self.interface.end_game(self.winner, self.board)
示例14: run_game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
def run_game():
"""
runs the game :D
"""
game_board = Board()
player1 = Human("1")
player2 = AI("2")
print(player1, "vs", player2)
# players_turn = 1
while not game_board.has_winner:
col = player1.get_move(game_board)
game_board.add_piece(col, player1.piece)
col = player2.get_move(game_board)
game_board.add_piece(col, player2.piece)
game_board.print_board()
示例15: Sudoku
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import print_board [as 别名]
class Sudoku(object):
def __init__(self):
self.board = Board()
self.input_reader = InputReader()
self.judge = Judge(self.board)
def start_game(self):
print "Welcome to Sudoku!"
while not self.board.is_game_over():
self.board.print_board()
move = self.input_reader.read_move()
if move.is_quit():
quit()
if self.judge.is_valid_move(move.row_index, move.col_index, move.number):
self.board.place_number(move.row_index, move.col_index, move.number)
else:
print "That is not a valid movement. Try again!"