コード例 #1
0
def main():
    """
    Main.
    """
    print('Welcome to Cribbage.')
    print()
    dealer = Player('Player A')
    pone = HumanPlayer('Player B')

    print(dealer.name + ' is the dealer.')
    print(pone.name + ' is the pone.')

    def other_player(player):
        if player is dealer:
            return pone
        return dealer

    while True:
        # reset
        crib_cards = []

        # shuffle
        print('Shuffling deck.')
        deck = Deck()
        deck.shuffle()

        # deal
        print('Dealing cards.')
        deal(deck, dealer, pone, 6)

        # throw to crib
        print('Waiting for ' + dealer.name +
              ' to throw 2 cards to the crib...')
        dealer_threw = dealer.throw_to_crib(True)
        print(dealer.name + ' threw ' + str(dealer_threw) + ' into the crib.')
        crib_cards += dealer_threw

        print('Waiting for ' + pone.name + ' to throw 2 cards to the crib...')
        pone_threw = pone.throw_to_crib(False)
        print(pone.name + ' threw ' + str(pone_threw) + ' into the crib.')
        crib_cards += pone_threw

        # cut
        cut_card = deck.draw()
        print('The cut card is ' + str(cut_card) + '.')
        if cut_card.rank == 11:
            print(dealer.name + " receives 2 points for the Jack's heels.")
            add_points(dealer, 2, other_player(dealer))
            check_win(dealer, pone)

        # keep track of count points
        dealer_points = Hand(dealer.hand_cards, cut_card).count()
        pone_points = Hand(pone.hand_cards, cut_card).count()
        crib_points = Hand(crib_cards, cut_card, True).count()

        # peg
        played_cards = []
        player_to_play = pone
        passed = None
        while True:
            print('Waiting for ' + player_to_play.name + ' to play a card...')
            played_card = player_to_play.play_card(played_cards)
            if played_card:
                print(player_to_play.name + ' plays ' + str(played_card) + '.')
                played_cards.append(played_card)
                print('Cards in play: ' + str(played_cards) + ' (' +
                      str(sum(map(lambda card: card.value, played_cards))) +
                      ').')
                peg_points = count_peg_points(played_cards)
                if peg_points != 0:
                    print(player_to_play.name + ' pegs ' + str(peg_points) +
                          ' points.')
                    add_points(player_to_play, peg_points,
                               other_player(player_to_play))
                    check_win(dealer, pone)
                if len(dealer.hand_cards) == 0 and len(pone.hand_cards) == 0:
                    print(player_to_play.name + ' receives a go.')
                    add_points(player_to_play, 1, other_player(player_to_play))
                    check_win(dealer, pone)
                    break
                if not passed:
                    player_to_play = other_player(player_to_play)
            else:
                if not passed:
                    print(player_to_play.name + ' cannot play a card.')
                    passed = player_to_play
                    player_to_play = other_player(player_to_play)
                else:
                    passed = None
                    print(player_to_play.name + ' receives a go.')
                    add_points(player_to_play, 1, other_player(player_to_play))
                    check_win(dealer, pone)
                    player_to_play = other_player(player_to_play)
                    played_cards = []
        print()

        # count
        print(pone.name + ' counts ' + str(pone_points) + ' points.')
        add_points(pone, pone_points, dealer, False)
        check_win(dealer, pone)

        print(dealer.name + ' counts ' + str(dealer_points) + ' points.')
        add_points(dealer, dealer_points, pone, False)
        check_win(dealer, pone)

        print(dealer.name + ' scores ' + str(crib_points) +
              ' points from the crib.')
        add_points(dealer, crib_points, pone)
        check_win(dealer, pone)

        # new dealer
        dealer, pone = pone, dealer
        print()