コード例 #1
0
ファイル: tests.py プロジェクト: masijiaqiu/AI-projects
    def setUpClass(cls):
        cls.log = logging.getLogger("tests")

        cls.BASIC_STARTING_BOARD_1 = ConnectFourBoard(board_array=(
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 1, 0, 2, 0, 0),
        ),
                                                      current_player=1)

        cls.BASIC_STARTING_BOARD_2 = ConnectFourBoard(board_array=(
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 2, 0, 0, 0),
            (0, 0, 0, 1, 0, 0, 0),
        ),
                                                      current_player=1)
        cls.Hardmode = ConnectFourBoard(board_array=(
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 2, 1, 0, 0),
        ),
                                        current_player=1)
コード例 #2
0
def get_strategic_winner(board):
    boardForP1 = ConnectFourBoard(board._board_array, current_player = 1)
    boardForP2 = ConnectFourBoard(board._board_array, current_player = 2)
    
    P1_threats = get_threats(boardForP1)
    P2_threats = get_threats(boardForP2)
    
    ODD = len(P2_threats[0]) - len(P1_threats[0])
    
    #Even threats matter only to Player 2
    EVEN = len(P2_threats[1])
    
    #MIXED gives number of shared odd threats    
    MIXED = len(set(P1_threats[0]).intersection(P2_threats[0]))

    if ODD < 0: #P1 has more odd threats
        return 1  
    elif ODD == 0: #P1 and P2 has same no of odd threats
        if MIXED % 2 != 0: 
            return 1 
        else:
            if MIXED == 0:
                if EVEN == 0:
                    return 0
                elif EVEN > 0:
                    return 2
            elif MIXED > 0:
                return 2
    elif ODD == 1: #P2 has one odd threat more than P1
        if MIXED == 0:
            if EVEN == 0:
                return 0
            elif EVEN > 0:
                return 2
        elif MIXED % 2 != 0:
            return 2
        else:
            return 1
    elif ODD > 1: #P2 has greater than 1 odd threats than P1
        return 2   
コード例 #3
0
ファイル: tests.py プロジェクト: masijiaqiu/AI-projects
    def setUpClass(cls):
        cls.log = logging.getLogger("tests")

        # Obvious win
        cls.WINNING_BOARD = ConnectFourBoard(board_array=(
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 1, 0, 0, 0, 0, 0),
            (0, 1, 0, 0, 0, 2, 0),
            (0, 1, 0, 0, 2, 2, 0),
        ),
                                             current_player=1)

        # 2 can win, but 1 can win a lot more easily
        cls.BARELY_WINNING_BOARD = ConnectFourBoard(board_array=(
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 2, 2, 1, 1, 2, 0),
            (0, 2, 1, 2, 1, 2, 0),
            (2, 1, 2, 1, 1, 1, 0),
        ),
                                                    current_player=2)
コード例 #4
0
ファイル: tests.py プロジェクト: bloughran618/MITx
from connectfour import ConnectFourBoard
from tester import make_test, get_tests
from time import time
import tree_searcher

# Obvious win
WINNING_BOARD = ConnectFourBoard(board_array =
                                 ( ( 0,0,0,0,0,0,0 ),
                                   ( 0,0,0,0,0,0,0 ),
                                   ( 0,0,0,0,0,0,0 ),
                                   ( 0,1,0,0,0,0,0 ),
                                   ( 0,1,0,0,0,2,0 ),
                                   ( 0,1,0,0,2,2,0 ),
                                   ),
                                 current_player = 1)

# 2 can win, but 1 can win a lot more easily
BARELY_WINNING_BOARD = ConnectFourBoard(board_array =
                                        ( ( 0,0,0,0,0,0,0 ),
                                          ( 0,0,0,0,0,0,0 ),
                                          ( 0,0,0,0,0,0,0 ),
                                          ( 0,2,2,1,1,2,0 ),
                                          ( 0,2,1,2,1,2,0 ),
                                          ( 2,1,2,1,1,1,0 ),
                                          ),
                                        current_player = 2)

ANSWER1_getargs = "ANSWER1"

def ANSWER1_testanswer(val, original_val = None):
    return ( val == 3 )
コード例 #5
0
    def __call__(self, *args, **kwargs):
        self.count += 1
        self.fn(*args, **kwargs)

    def get_count(self):
        return self.count


    
# Some sample boards, useful for testing:
# Obvious win
WINNING_BOARD = ConnectFourBoard(4, False, board_array =
                                 ( ( 0,0,0,0,0,0,0 ),
                                   ( 0,0,0,0,0,0,0 ),
                                   ( 0,0,0,0,0,0,0 ),
                                   ( 0,1,0,0,0,0,0 ),
                                   ( 0,1,0,0,0,2,0 ),
                                   ( 0,1,0,0,2,2,0 ),
                                   ),
                                 current_player = 1)

# 2 can win, but 1 can win a lot more easily
BARELY_WINNING_BOARD = ConnectFourBoard(4, False, board_array =
                                        ( ( 0,0,0,0,0,0,0 ),
                                          ( 0,0,0,0,0,0,0 ),
                                          ( 0,0,0,0,0,0,0 ),
                                          ( 0,2,2,1,1,2,0 ),
                                          ( 0,2,1,2,1,2,0 ),
                                          ( 2,1,2,1,1,1,0 ),
                                          ),
                                        current_player = 2)
コード例 #6
0
ファイル: main.py プロジェクト: Jeadie/miniature-guacamole
    elif args.mode == 'O':
        run_game(basic_player, human_player)
    elif args.mode == 'computer':
        run_game(basic_player, basic_player)
    elif args.mode == 'quick':
        run_game(basic_player, quick_to_win_player)
    elif args.mode == 'alphabeta':
        run_game(human_player, alpha_beta_player)
    elif args.mode == 'my_player':
        # watch your player play a game
        run_game(my_player, my_player)
    elif args.mode == 'my_player_vs_basic':
        run_game(my_player, basic_player)
    elif args.mode == 'debug_evaluate':
        board_tuples = (
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 0, 0, 0, 0, 0, 0),
            (0, 2, 2, 1, 1, 2, 0),
            (0, 2, 1, 2, 1, 2, 0),
            (2, 1, 2, 1, 1, 1, 0),
        )
        test_board_1 = ConnectFourBoard(board_array=board_tuples,
                                        current_player=1)
        test_board_2 = ConnectFourBoard(board_array=board_tuples,
                                        current_player=2)
        # better evaluate from player 1
        print("{} => {}".format(test_board_1, better_evaluate(test_board_1)))
        # better evaluate from player 2
        print("{} => {}".format(test_board_2, better_evaluate(test_board_2)))
コード例 #7
0
    def get_most_recent_val(self):
        """ Return the most-recent return value of the thread function """
        try:
            return self._most_recent_val
        except AttributeError:
            print(
                "Error: You ran the search function for so short a time that it couldn't even come up with any answer at all!  Returning a random column choice..."
            )
            import random
            return random.randint(0, 6)


my_func = lambda root, depth: alpha_beta_iterative_w_root(
    root, depth, eval_fn=better_evaluate, get_next_moves_fn=NEXT_MOVES)
root = get_alpha_beta_root(ConnectFourBoard(), NEXT_MOVES)
eval_t = MyThread(target=my_func, init_root=root)

eval_t.setDaemon(True)
eval_t.start()

eval_t.join(1)
print(eval_t.get_most_recent_val())
eval_t.join(1)  # 2
print(eval_t.get_most_recent_val())
eval_t.join(3)  # 5
print(eval_t.get_most_recent_val())
eval_t.join(2)  # 7
print(eval_t.get_most_recent_val())
eval_t.join(3)  # 10
print(eval_t.get_most_recent_val())