本文整理汇总了Python中board.Board.game_over方法的典型用法代码示例。如果您正苦于以下问题:Python Board.game_over方法的具体用法?Python Board.game_over怎么用?Python Board.game_over使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.game_over方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ConnectFour
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
class ConnectFour(object):
def __init__(self, cell_list=None, columns=7, column_size=6, human=False, level='easy'):
self.adversaries = [HumanPlayer(), HumanPlayer(False, 'O')]
if not human:
self.adversaries[1] = ComputerPlayer(level=level)
self.board = Board()
def read_player_move(self, ):
return self.current_player.move(self.board)
def play(self, ):
self.current_player = [player for player in self.adversaries
if player.turn is True][0]
while True:
print self.board
move = self.read_player_move()
if move in ('q', 'Q'):
sys.exit(0)
self.board.set_cell(move, self.current_player.name)
over = self.board.game_over()
if over:
break
self.current_player = self.get_next_player()[0]
print self.board
print "Cat's Game!" if over == 'tie' else \
self.current_player.name + " won the game!"
def get_next_player(self, ):
for player in self.adversaries:
player.turn = not player.turn
return [player for player in self.adversaries if player.turn is True]
示例2: random_play
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
def random_play():
random.seed()
num_games = 20000
batch_size = 10000
games = [] #game = (list of board configs, winner)
current_batch = 0
for i in tqdm(range(num_games)):
board = Board()
boards = [copy.copy(board.board)]
turn = constants.X_PIECE
while not board.game_over():
row, col = random.sample(board.next_moves, 1)[0]
board.add_piece(row, col, turn)
turn = (turn + 1) % 2
boards.append(copy.copy(board.board))
games.append((boards, board.board_winner()))
current_batch += 1
if current_batch == batch_size:
with open('{}-saved_games.pkl'.format(time.time()), 'wb') as f:
pickle.dump(games, f)
current_batch = 0
games = []
示例3: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
class Game(object):
def __init__(self,player_one,player_two,display_method=print):
self.gameboard = Board()
self.player_one = player_one
self.player_two = player_two
self.display_method = display_method
def run(self):
while not self.__over__():
self.__round_set__()
self.__print_board_if_game_not_over__()
self.display_method(self.gameboard)
def __setup__(self,input_source=PlayerInput()):
validator = InputValidator(input_source)
self.display_method("Would you like to go first or second (1,2): ")
def __round_set__(self):
self.__round__(self.player_one)
self.__round__(self.player_two)
def __round__(self,current_player):
if not self.__over__():
self.__move__(current_player.next_move(),current_player)
def __print_board_if_game_not_over__(self):
if not self.__over__():
self.display_method(self.gameboard)
def __over__(self):
return self.gameboard.game_over()
def __move__(self,space,player):
self.gameboard.make_move(space,player.token)
示例4: drive
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
def drive():
num_games = 5
player_1 = "a"
player_2 = "b"
player_1_wins = 0
player_2_wins = 0
stalemates = 0
for j in range(num_games):
board = Board()
player_a = Algo10(player_1)
player_b = Algo7(player_2)
if j % 2 == 0:
current_player = player_a
else:
current_player = player_b
i = 0
while(not board.game_over()):
#print "Round:",str(i)," Player:",current_player
board_for_player = copy.deepcopy(board)
move = current_player.next_move(board_for_player)
#print "before_move",board
#print move
board.make_move(move)
#print "after_move ",board
if(not board.game_over()):
if current_player == player_a:
current_player = player_b
else:
current_player = player_a
i += 1
if(board.is_winning_arrangement()):
if player_1 in str(current_player):
player_1_wins += 1
else:
player_2_wins += 1
else:
stalemates += 1
print player_a,"_wins:",player_1_wins
print player_b,"_wins:",player_2_wins
print "stalemates:",stalemates
示例5: user_play
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
def user_play():
board = Board()
turn = constants.X_PIECE
print board
while not board.game_over():
row, col = [int(x) for x in raw_input().split()]
board.add_piece(row, col, turn)
turn = (turn + 1) % 2
print board
示例6: random_against_model
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
def random_against_model(ngames=100):
from keras.models import model_from_json
random.seed()
model = model_from_json(open('keras_model.json').read())
model.load_weights('model_weights.h5')
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
outcomes = []
for _ in xrange(ngames):
board = Board()
prev_move = np.zeros(81, dtype='int32')
turn = constants.X_PIECE
while not board.game_over():
if turn == constants.X_PIECE:
x1 = np.asarray(board.board.flatten())
x2 = prev_move
X = np.asarray([np.hstack([x1, x2])])
probs = model.predict_proba(X)[0]
#we need to eliminate any moves that are not allowed
probs = [p if np.unravel_index(p_i, (9, 9)) in board.next_moves else 0\
for p_i, p in enumerate(probs)]
probs = my_normalize(probs)
idx = range(len(probs))
#predicted move to make
move_idx = np.random.choice(idx, p=probs)
row, col = np.unravel_index(move_idx, (9, 9))
board.add_piece(row, col, turn)
elif turn == constants.O_PIECE:
try:
row, col = random.sample(board.next_moves, 1)[0]
board.add_piece(row, col, turn)
except ValueError:
print 'Try again'
continue
else:
raise ValueError('Mistakes have been made')
turn = (turn + 1) % 2
print '{} Won the game!'.format(board.board_winner())
outcomes.append(board.board_winner())
print 'AI Won {:0.02f}% of the games!'.format(sum(1 if i == constants.X_PIECE else 0 for i in outcomes)/float(len(outcomes)))
print '{:0.02f}% ties'.format(sum(1 if i == -1 else 0 for i in outcomes)/float(len(outcomes)))
示例7: play_against_model
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
def play_against_model():
from keras.models import model_from_json
model = model_from_json(open('keras_model.json').read())
model.load_weights('model_weights.h5')
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
board = Board()
prev_move = np.zeros(81, dtype='int32')
turn = constants.X_PIECE
while not board.game_over():
if turn == constants.X_PIECE:
x1 = np.asarray(board.board.flatten())
x2 = prev_move
X = np.asarray([np.hstack([x1, x2])])
probs = model.predict_proba(X)[0]
#we need to eliminate any moves that are not allowed
probs = [p if np.unravel_index(p_i, (9, 9)) in board.next_moves else 0\
for p_i, p in enumerate(probs)]
probs = my_normalize(probs)
idx = range(len(probs))
#predicted move to make
move_idx = np.random.choice(idx, p=probs)
row, col = np.unravel_index(move_idx, (9, 9))
board.add_piece(row, col, turn)
elif turn == constants.O_PIECE:
print 'Allowed:'
print board.next_moves
try:
row, col = [int(x) for x in raw_input('User move:').split()]
board.add_piece(row, col, turn)
except ValueError:
print 'Try again'
continue
else:
raise ValueError('Mistakes have been made')
turn = (turn + 1) % 2
print board
print '{} Won the game!'.format(board.board_winner())
示例8: generate_random_games
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
def generate_random_games(num_games, seed=1337):
random.seed(seed)
games = []
for i in tqdm(range(num_games)):
board = Board()
boards = [copy.copy(board.board)]
turn = constants.X_PIECE
while not board.game_over():
row, col = random.sample(board.next_moves, 1)[0]
board.add_piece(row, col, turn)
turn = (turn + 1) % 2
boards.append(copy.copy(board.board))
games.append((boards, board.board_winner()))
return games
示例9: KeyMap
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_GREEN)
curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLUE)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_YELLOW)
curses.init_pair(5, curses.COLOR_RED, curses.COLOR_RED)
try:
KEYS = KeyMap()
KEYS.load_from_file(CONFIG_FNAME)
GAME_BOARD = Board(STDSCR, [0, 0], [20, 20])
SCORE_BOARD = ScoreBoard(GAME_BOARD.status, 5, 25)
GAME_QUIT = False
LAST_GAME_STEP = time()
while not GAME_QUIT and not GAME_BOARD.game_over():
CURRENT_TIME = time()
if CURRENT_TIME - LAST_GAME_STEP > STEP_TIME:
GAME_BOARD.advance_block()
LAST_GAME_STEP = CURRENT_TIME
USR_INPUT = STDSCR.getch()
if USR_INPUT == KEYS.quit:
GAME_QUIT = True
elif USR_INPUT == KEYS.lshift:
GAME_BOARD.lshift_block()
elif USR_INPUT == KEYS.rshift:
GAME_BOARD.rshift_block()
elif USR_INPUT == KEYS.land:
GAME_BOARD.land_block()
elif USR_INPUT == KEYS.rotate:
示例10: trained_model_play
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import game_over [as 别名]
def trained_model_play():
"""
NOTE: The models expect the board to be presented as player X's turn
Algo:
1. Start with 20000 randomly generated games
2. Train a model to predict "winning" moves
3. Generate 20000 new games, playing the model against itself
4. Go to 2
"""
BOARD_DIM = 81 #i.e. 9x9
POSS_MOVE_DIM = 81 #ie. same as board size
INPUT_DIM = BOARD_DIM + POSS_MOVE_DIM #board, last_move
OUTPUT_DIM = POSS_MOVE_DIM #which move should we make?
NB_EPOCH = 5
NB_ITER = 5 #number of reinforcement learning iterations
#NOTE: X_PIECE always went first in the training data
model = Sequential()
model.add(Dense(2 * INPUT_DIM, input_dim=INPUT_DIM, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(2 * INPUT_DIM, activation='tanh'))
model.add(Dropout(0.2))
model.add(Dense(OUTPUT_DIM))
model.add(Activation('softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
num_games = 20000
#game = (list of board configs, winner)
games = generate_random_games(num_games)
#we only want games with a definitive winner
won_games = [(g, w) for g, w in games if w != constants.NO_PIECE]
print 'Using {} games that have winner'.format(len(won_games))
#we can easily scale up the number of games by transposing them
won_games.extend(transpose_batch(won_games))
train_model_on_games(model, won_games, nb_epoch=NB_EPOCH)
for j in range(NB_ITER):
games = []
for i in range(num_games):
board = Board()
boards = [board.board]
prev_move = np.zeros(BOARD_DIM, dtype='int32')
turn = constants.X_PIECE
while not board.game_over():
if turn == constants.X_PIECE:
x1 = np.asarray(board.board.flatten())
elif turn == constants.O_PIECE:
board_rep = invert_board(board.board)
x1 = np.asarray(board_rep.flatten())
else:
raise ValueError('Mistakes have been made')
x2 = prev_move
X = np.asarray([np.hstack([x1, x2])])
probs = model.predict_proba(X)[0]
#we need to eliminate any moves that are not allowed
probs = [p if np.unravel_index(p_i, (9, 9)) in board.next_moves else 0\
for p_i, p in enumerate(probs)]
probs = my_normalize(probs)
idx = range(len(probs))
#predicted move to make
move_idx = np.random.choice(idx, p=probs)
row, col = np.unravel_index(move_idx, (9, 9))
board.add_piece(row, col, turn)
turn = (turn + 1) % 2
prev_move = to_categorical([move_idx], 81)[0]
boards.append(copy.copy(board.board))
games.append((boards, board.board_winner()))
won_games = [(g, w) for g, w in games if w != constants.NO_PIECE]
print 'Using {} games that have winner after reinforcement iter {}'.format(len(won_games), j)
#we can easily scale up the number of games by transposing them
won_games.extend(transpose_batch(won_games))
train_model_on_games(model, won_games, nb_epoch=NB_EPOCH)
with open('keras_model.json', 'w') as f:
f.write(model.to_json())
model.save_weights('model_weights.h5')