예제 #1
0
 def testEdgePieces(self):
     testBoard = CheckerBoard()
     redplayer = AI.Strategy('r', CheckerBoard(), 8)
     self.assertEqual(
         redplayer.totalPieces(testBoard), 0,
         'AI.totalPieces failed for red starting board. Count was: ' +
         str(redplayer.totalPieces(testBoard)))
     blackplayer = AI.Strategy('b', CheckerBoard(), 8)
     self.assertEqual(
         blackplayer.totalPieces(testBoard), 0,
         'AI.totalPieces failed for red starting board. Count was: ' +
         str(redplayer.totalPieces(testBoard)))
     testBoard.clearboard()
     testBoard.place(0, 1, 'r')
     testBoard.update_counts()
     self.assertEqual(
         redplayer.totalPieces(testBoard), 1,
         'AI.totalPieces failed for red one piece board. Count was: ' +
         str(redplayer.totalPieces(testBoard)))
     self.assertEqual(
         blackplayer.totalPieces(testBoard), -1,
         'AI.totalPieces failed for black one piece board. Count was: ' +
         str(blackplayer.totalPieces(testBoard)))
     testBoard.place(0, 3, 'b')
     testBoard.update_counts()
     self.assertEqual(
         redplayer.totalPieces(testBoard), 0,
         'AI.totalPieces failed for red two piece board. Count was: ' +
         str(redplayer.totalPieces(testBoard)))
     self.assertEqual(
         blackplayer.totalPieces(testBoard), 0,
         'AI.totalPieces failed for black two piece board. Count was: ' +
         str(blackplayer.totalPieces(testBoard)))
     testBoard.place(0, 5, 'R')
     testBoard.update_counts()
     self.assertEqual(
         redplayer.totalPieces(testBoard), 1,
         'AI.totalPieces failed for red three piece board. Count was: ' +
         str(redplayer.totalPieces(testBoard)))
     self.assertEqual(
         blackplayer.totalPieces(testBoard), -1,
         'AI.totalPieces failed for black three piece board. Count was: ' +
         str(blackplayer.totalPieces(testBoard)))
     testBoard.place(0, 7, 'B')
     testBoard.update_counts()
     self.assertEqual(
         redplayer.totalPieces(testBoard), 0,
         'AI.totalPieces failed for red four piece board. Count was: ' +
         str(redplayer.totalPieces(testBoard)))
     self.assertEqual(
         blackplayer.totalPieces(testBoard), 0,
         'AI.totalPieces failed for black four piece board. Count was: ' +
         str(blackplayer.totalPieces(testBoard)))
예제 #2
0
 def __init__(self, size: tuple, p1, p2, win_length):
     self.checkerboard = CheckerBoard(size)
     self.p1 = p1
     self.p2 = p2
     self.win_length = min(size[0], win_length)
예제 #3
0
 def refresh(self):
     self.checkerboard = CheckerBoard(self.checkerboard.size)
예제 #4
0
def Game(red=ai.Strategy, black=tonto.Strategy, 
         maxplies=5, init=None, verbose=True, firstmove=0):
    """Game(red, black, maxplies, init, verbose, turn)
    Start a game of checkers
    red,black - Strategy classes (not instances)
    maxplies - # of turns to explore (default 10)
    init - Start with given board (default None uses a brand new game)
    verbose - Show messages (default True)
    firstmove - Player N starts 0 (red) or 1 (black).  Default 0. 
    """

    # Don't forget to create instances of your strategy,
    # e.g. black('b', checkerboard.CheckerBoard, maxplies)

    ### Still unsure what firstmove is supposed to do ###

    # Initialize board
    if init is None:
        board = CheckerBoard()  # New game
    else:
        board = init    # Initialize board to given config

    # Display Board
    if verbose:
        print('Initial Board:')
    print(board, '\n')

    # Initialize Players
    red = red('r', board, maxplies)
    black = black('b', board, maxplies)

    # Start playing
    while not board.is_terminal()[0]:   # Nobody has won yet

        state = red.play(board) # Get tuple of new board following an action and the action itself
        board = state[0]
        action = state[1]
        if verbose and action is not None:
            print('Red Move: ', board.get_action_str(action))
        print(board)

        # Player has forfeited -> exit loop
        if action is None:
            print('Red Player has forfeited the game')
            break

        if not board.is_terminal()[0]:  # Still nobody has won yet
            state = black.play(board)   # Get tuple of new board following an action and the action itself
            board = state[0]
            action = state[1]
            if verbose and action is not None:
                print('Black Move: ', board.get_action_str(action))
        print(board)

        # Player has forfeited -> exit loop
        if action is None:
            print('Black Player has forfeited the game')
            break

    if (board.is_terminal()[1] == 'r'):
        print('Red Player wins')
    elif (board.is_terminal()[1] == 'b'):
        print('Black Player wins')
    else:
        print('Game has ended in a stalemate')