当前位置: 首页>>代码示例>>Python>>正文


Python Board.make_move方法代码示例

本文整理汇总了Python中board.Board.make_move方法的典型用法代码示例。如果您正苦于以下问题:Python Board.make_move方法的具体用法?Python Board.make_move怎么用?Python Board.make_move使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在board.Board的用法示例。


在下文中一共展示了Board.make_move方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AITests

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
class AITests(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def test_minimax(self):
        tests_count = 5
        ai_wins = 0
        user_wins = 0

        for test in range(tests_count):
            is_game_over = Board.NOT_OVER
            while is_game_over is Board.NOT_OVER:
                if self.board.is_user_turn:
                    moves = self.board.get_available_moves()
                    pos = moves[randint(0, len(moves) - 1)]
                    self.board.make_move(Board.USER, *pos)
                else:
                    moves = self.board.get_available_moves()
                    best = AI(self.board, self.board.is_user_turn).best_move()
                    self.board.make_move(Board.AI, *moves[best])
                is_game_over = self.board.is_game_over()

            if is_game_over is Board.AI_WIN:
                ai_wins += 1
            elif is_game_over is Board.USER_WIN:
                user_wins += 1
            self.board = Board()        # refresh board

        self.assertGreaterEqual(ai_wins, user_wins)
开发者ID:antonpetkoff,项目名称:Programming101,代码行数:31,代码来源:ai_tests.py

示例2: main

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [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
开发者ID:Morphage,项目名称:TicTacToe-Python,代码行数:35,代码来源:tic_tac_toe.py

示例3: Game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [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)
开发者ID:skatenerd,项目名称:tic_tac_toe,代码行数:37,代码来源:game.py

示例4: testAddRandom2

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
 def testAddRandom2(self):
     random.seed(0)
     b = Board()
     b.make_move(b.LEFT)
     b.make_move(b.RIGHT)
     b2 = Board()
     b2.board_from_array([[0,0,0,2],[2,0,0,2],[0,0,0,2],[0,0,0,0]])
     self.assertEqual(str(b),str(b2))
开发者ID:samshuster,项目名称:2048_Explore,代码行数:10,代码来源:board_tester.py

示例5: get_successors

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
	def get_successors(self, state):
		"""
		Computes and returns all possible successor states of the
		input state.
		"""
		successors = []
		for column in range(len(state.piece_matrix[0])):
			piece_matrix_copy = deep_copy(state.piece_matrix)
			successor = Board(piece_matrix_copy, state.turn_player)
			if successor.is_valid_move(column, state.turn_player):
				successor.make_move(column, state.turn_player)
				successors.append((column, successor))
		return successors
开发者ID:sharmaster96,项目名称:m4stermind,代码行数:15,代码来源:search.py

示例6: __init__

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [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))
开发者ID:antonpetkoff,项目名称:Programming101,代码行数:52,代码来源:tic_tac_toe.py

示例7: play_game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
def play_game(agent1, agent2):
	"""
	Method that handles actual gameplay by two agents.
	"""
	board = Board()
	agent1.color, agent2.color = 'r', 'b'
	winner = board.is_won()
	while not winner:
		if board.turn_player == agent1.color:
			column = agent1.select_move(board.piece_matrix)
			board.make_move(column, agent1.color)
		elif board.turn_player == agent2.color:
			column = agent2.select_move(board.piece_matrix)
			board.make_move(column, agent2.color)
		winner = board.is_won()
	board.display_board()
	return winner
开发者ID:sharmaster96,项目名称:m4stermind,代码行数:19,代码来源:play.py

示例8: drive

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [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
开发者ID:garbados,项目名称:Quarto,代码行数:46,代码来源:game.py

示例9: Game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
class Game():
    def __init__(self):
        self.ai = Ai()
        self.board = Board(9)
        self.player = Player()
        self.on_turn = self.player
   
    def start(self):
        in_progres = True
        while in_progres:
            if self.on_turn == self.player:
                player_choice = self.player.ask_for_move(self.board)
                try:
                    self.board.make_move(player_choice, self.player.symbol)
                except ValueError:
                    return self.player.ask_for_move(self.board)
                self.on_turn = self.ai
                print(self.board.draw())
            else:
                print('Ai move:')
                ai_move = self.ai.get_ai_move(self.board)
                self.board.make_move(ai_move, self.ai.symbol)
                self.on_turn = self.player
                
                print(self.board.draw())

            if self.board.check_winning(self.player.symbol):
                print('Congrats, You win!!!')
                break
            elif self.board.check_winning(self.ai.symbol):
                print('You are LOOOOOOOOOOSER :P')
                break
            elif self.board.is_full():
                print('TIE')
                
            in_progres = not self.board.is_full()
开发者ID:Shosh,项目名称:hb_tic_tac_toe,代码行数:38,代码来源:game.py

示例10: __init__

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [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)
开发者ID:andschwa,项目名称:uidaho-cs470-connectfour,代码行数:50,代码来源:game.py

示例11: main

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
def main():
    pygame.init()
    pygame.display.set_caption('Pent')
    screen = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    clock = pygame.time.Clock()

    # Sprite groups
    all_sprites = pygame.sprite.RenderPlain()

    board = Board(screen.get_rect().center)
    all_sprites.add(board)

    # White goes first for now
    current_player = WHITE

    # Main event loop
    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                shut_down(screen)

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    shut_down(screen)

            if event.type == pygame.MOUSEBUTTONDOWN:
                if board.make_move(current_player, pygame.mouse.get_pos()):
                    winner = board.winner()
                    if winner == TIE:
                        print "Tie game..."
                    elif winner == WHITE:
                        print "White won..."
                    elif winner == BLACK:
                        print "Black won..."
                    current_player = -current_player

        # Clear screen
        screen.fill(pygame.Color('black'))

        all_sprites.update()
        all_sprites.draw(screen)

        # Flip screen
        pygame.display.flip()

        # Pause
        clock.tick(FPS)
开发者ID:keithfancher,项目名称:Pent,代码行数:50,代码来源:pent.py

示例12: Board

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
'''
Created on Mar 14, 2014

@author: samshuster
'''
from board import Board

b = Board()
print b
move = raw_input()
while(True):
    try:
        b.make_move(move,non_random=True)
    except KeyError:
        pass
    else:
        print b.score
        print b
    move = raw_input()

开发者ID:samshuster,项目名称:2048_Explore,代码行数:21,代码来源:manual_driver.py

示例13: testNoMove

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
 def testNoMove(self):
     b = Board()
     b.board_from_array([[0,0,0,3],[0,0,0,2],[0,0,0,0],[0,0,0,0]])
     ans = b.make_move(b.RIGHT)
     self.assertEqual(ans,False)
开发者ID:samshuster,项目名称:2048_Explore,代码行数:7,代码来源:board_tester.py

示例14: BoardTests

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]
class BoardTests(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def __fill_rows(self, rows, mark):
        for row in rows:
            for col in range(3):
                self.board.make_move(mark, row, col)

    def test_is_user_turn(self):
        self.assertTrue(self.board.is_user_turn)
        self.board.make_move(Board.USER, 1, 1)
        self.assertFalse(self.board.is_user_turn)

    def test_make_move(self):
        self.__fill_rows(range(1), Board.USER)
        self.assertEqual(self.board.state[0], [Board.USER]*3)

    def test_is_pos_empty(self):
        self.assertTrue(self.board.is_pos_empty(0, 0))
        self.board.make_move(Board.AI, 0, 0)
        self.assertFalse(self.board.is_pos_empty(0, 0))

    def test_get_available_moves(self):
        self.__fill_rows(range(1), Board.USER)
        result = []
        for row in range(1, 3):
            for col in range(0, 3):
                result.append((row, col))
        self.assertEqual(self.board.get_available_moves(), result)

    def test_is_complete(self):
        self.assertFalse(self.board.is_complete())
        self.__fill_rows(range(3), Board.USER)
        self.assertTrue(self.board.is_complete())

    def test_has_3_equal_marks_all_outcomes(self):
        self.assertEqual(self.board._has_3_equal_marks(self.board.state[0]),
                         None)

        self.__fill_rows(range(1), Board.USER)
        self.assertEqual(self.board._has_3_equal_marks(self.board.state[0]),
                         Board.USER_WIN)

        self.__fill_rows(range(1), Board.AI)
        self.assertEqual(self.board._has_3_equal_marks(self.board.state[0]),
                         Board.AI_WIN)

    def test_is_game_over_all_outcomes_rows(self):
        self.assertEqual(self.board.is_game_over(), Board.NOT_OVER)

        self.__fill_rows(range(1), Board.USER)
        self.assertEqual(self.board.is_game_over(), Board.USER_WIN)

        self.__fill_rows(range(1), Board.AI)
        self.assertEqual(self.board.is_game_over(), Board.AI_WIN)

        self.board.state = [[Board.USER, Board.USER, Board.AI],
                            [Board.AI,   Board.AI,   Board.USER],
                            [Board.USER, Board.AI,   Board.USER]]
        self.assertEqual(self.board.is_game_over(), Board.DRAW)

    def test_is_game_over_diagonals(self):
        self.board.state = [[Board.USER, Board.EMPTY, Board.AI],
                            [Board.AI,   Board.USER,  Board.AI],
                            [Board.USER, Board.AI,    Board.USER]]
        self.assertEqual(self.board.is_game_over(), Board.USER_WIN)

        self.board.state = [[Board.USER, Board.USER, Board.AI],
                            [Board.AI,   Board.AI,   Board.USER],
                            [Board.AI,   Board.USER, Board.EMPTY]]
        self.assertEqual(self.board.is_game_over(), Board.AI_WIN)

    def test_is_game_over_columns(self):
        self.board.state = [[Board.USER, Board.EMPTY, Board.AI],
                            [Board.USER, Board.AI,    Board.USER],
                            [Board.USER, Board.AI,    Board.AI]]
        self.assertEqual(self.board.is_game_over(), Board.USER_WIN)

    def test_get_score_all_outcomes(self):
        self.assertEqual(self.board.get_score(5), 0)

        self.board.state = [[Board.USER, Board.EMPTY, Board.AI],
                            [Board.USER, Board.AI,    Board.USER],
                            [Board.USER, Board.AI,    Board.AI]]
        self.assertEqual(self.board.get_score(3), -7)

        self.board.state = [[Board.USER, Board.USER, Board.AI],
                            [Board.AI,   Board.AI,   Board.USER],
                            [Board.AI,   Board.USER, Board.EMPTY]]
        self.assertEqual(self.board.get_score(2), 8)
开发者ID:antonpetkoff,项目名称:Programming101,代码行数:93,代码来源:board_tests.py

示例15: Game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import make_move [as 别名]

#.........这里部分代码省略.........
    
    def first_state(self):
        state = list()
        state.append(np.zeros((9, 9)))
        state.append(np.zeros((9, 9)))
        state.append(np.ones((9, 9)))
        state.append(np.ones((9, 9)))
        for a in range(0, 34):
            state.append(np.zeros((9, 9)))
        return np.asarray(state, dtype=np.float)
		
    def first_statevalue(self):
        state = list()
        state.append(np.zeros((9, 9)))
        state.append(np.zeros((9, 9)))
        state.append(np.ones((9, 9)))
        state.append(np.ones((9, 9)))
        for a in range(0, 34):
            state.append(np.zeros((9, 9)))
        state.append(np.ones((9, 9)))
		
        return np.asarray(state, dtype=np.float)
        
    def set_komi(self, komi):
        self.komi = komi
        if config.use_c:
            c_board.set_komi(komi)

    def make_unchecked_move(self, move):
        """This is utility method.
           This does not check legality.
           It makes move in current_board and returns undo log and also key of new board
        """
        self.current_board.make_move(move)
        undo_log = self.current_board.undo_log[:]
        board_key = self.current_board.key()
        return undo_log, board_key

    def legal_move(self, move):
        """check whether move is legal
           return truth value
           first check move legality on current board
           then check for repetition (situational super-ko)
        """
        if move==PASS_MOVE:
            return True
        if not self.current_board.legal_move(move):
            return False
        undo_log, board_key = self.make_unchecked_move(move)
        self.current_board.undo_move(undo_log)
        if config.repeat_check and board_key in self.position_seen:
            return False
        return True

    def make_colored_move(self, color, move):
        if self.current_board.side!=color:
            self.make_move(PASS_MOVE)
        return self.make_move(move)
        
    def make_move(self, move):
        """make given move and return new board
           or return None if move is illegal
           First check move legality.
           This checks for move legality for itself to avoid extra make_move/make_undo.
           This is a bit more complex but maybe 2-3x faster.
           Then make move and update history.
开发者ID:Aleum,项目名称:MiniGo,代码行数:70,代码来源:game.py


注:本文中的board.Board.make_move方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。