Ejemplo n.º 1
0
def game_turn(players, leader):
    ''' This is a function to play a turn '''

    # Declare global variables
    global hands

    lead = []

    # Display the leader's hand
    print("Player ", leader + 1, " you have the lead.")

    hand = hands[leader]

    helpers.display_hand(hand, leader + 1)

    # Get the lead from the player
    lead_play = input("Which card(s) would you like to lead? (eg: nnynnnn) ")

    # Ensure that the user's lead is appropriate
    while helpers.lead(lead_play, leader):
        for index, i in enumerate(lead_play):
            if i == 'y':
                lead.append(hand[index])
                hand.pop(index)
    else:
        print(
            "That is not a valid lead. Leads of more than 1 card must be of the same value."
        )
        lead_play = input(
            "Which card(s) would you like to lead? (eg: nnynnnn) ")

    helpers.display_play(lead)

    for j in range(players):
        pass
Ejemplo n.º 2
0
def toss(hand):
    ''' This is a function to apply the rules listed above for the initial deal. '''

    # These cards are tossed no matter what
    toss_list = ['5', '6', '7', '8']

    # Create a list that will make operations easier
    face_list = []
    for card in hand:
        face_list.append(card['face'])

    # Setup the necessary variables for the popping operation
    hand_len = len(face_list)
    rev_hand = reversed(face_list)

    # Pass through and remove the cards that are never kept
    for index, card in enumerate(rev_hand):
        if card in toss_list:
            face_list.pop(hand_len - 1 - index)

    # Pass through and remove cards that are not high value pairs
    if face_list.count('J') > 1:
        pass
    else:
        face_list = [x for x in face_list if x != 'J']

    # Pass through and remove cards that are not high value triplets
    if face_list.count('10') > 2:
        pass
    else:
        face_list = [x for x in face_list if x != '10']

    # Pass through and remove cards that are not high value quads
    if face_list.count('9') > 3:
        pass
    else:
        face_list = [x for x in face_list if x != '9']

    # Create and return hand based on remaining items in face_list
    new_hand = []
    for value in face_list:
        card = next((x for x in helpers.cards_list if x['face'] == value),
                    None)
        new_hand.append(card)

    helpers.display_hand(new_hand, 1)
    return new_hand
Ejemplo n.º 3
0
Archivo: ai1.py Proyecto: zachsirera/22
def lead(hand):
	''' This is a function to apply the rules listed above for the lead '''
	
	trash_cards = ['5', '6', '7', '8', '9', '10', 'J']

	sorted_hand = sorted(hand, key=lambda k: k['value'])

	helpers.display_hand(trash_cards, 1)
	helpers.display_hand(sorted_hand, 1)
	
	for index, card in enumerate(sorted_hand):
		# Lead the lowest single trash card
		if card['value'] in trash_cards:
			lead = sorted_hand.pop(index)
			helpers.display_hand(lead, 1)
			helpers.display_play(lead)
			return lead

	for index, card in enumerate(sorted_hand):
		# Lead the highest-order, lowest-value multiple
		# Instantiate a counter
		j = 0
		i = sum(cards['value'] = card for cards in sorted_hand)
Ejemplo n.º 4
0
def game_deal():
    ''' This is a function to initiate the deal and dealback of the game of 22 '''

    # Recall the global variables
    global deck
    global hands
    global players

    # Get the number of players from CLI input
    players = helpers.get_players()

    # Generate the deck
    deck = helpers.generate_deck()

    # For the first hand, 7 cards are dealt.
    deal_cards = 7

    # Execute the initial deal
    hands = helpers.deal(players, deal_cards)

    # Create player objects and initialize score. Yes this is ugly.
    player1 = player(hands[0], 0)
    player2 = player(hands[1], 0)

    if players >= 3:
        player3 = player(hands[2], 0)

    if players == 4:
        player4 = player(hands[3], 0)

    # Clear the screen
    print(chr(27) + "[2J")

    # Display each player's hand and allow them to dealback
    for i in range(players):

        sortedhand = sorted(hands[i], key=lambda k: k['value'])

        helpers.display_hand(sortedhand, i + 1)

        dealback_string = input(
            "Please indicate whether you would like to keep each card ( eg. ynnynyy): "
        )

        while len(dealback_string) != len(sortedhand):
            dealback_string = input(
                "Please indicate whether you would like to keep each card ( eg. ynnynyy): "
            )
        else:

            # Create iterator for dealback
            k = 0

            for m in dealback_string:
                if m == "n":
                    k = k + 1

            if k > len(deck):
                print("You can only take ", len(deck), " cards.")
                helpers.display_hand(sortedhand, i + 1)
                dealback_string = input(
                    "Please indicate whether you would like to keep each card ( eg. ynnynyy): "
                )
            else:

                # Reverse order in order to perform popping operation without error
                rev_db_string = reversed(dealback_string)

                for index, j in enumerate(rev_db_string):
                    if j == "n":
                        sortedhand.pop(6 - index)

                # Get dealback cards
                dealback = helpers.dealback(k)

                # Concatenate the two lists
                new_hand = sortedhand + dealback

                # sort the new hand
                sorted_new_hand = sorted(new_hand, key=lambda k: k['value'])
                hands[i] = sorted_new_hand

                # Clear the deal back
                for l in range(len(dealback)):
                    dealback.pop(0)

                # Clear the screen
                print(chr(27) + "[2J")

                # Display the new hand
                helpers.display_hand(sorted_new_hand, i + 1)

                if i != players - 1:
                    input("Press enter to move to the next player. ")

                    # Clear the screen
                    print(chr(27) + "[2J")
                else:

                    input("Press enter to begin. Player 1 will lead.")

                    # Clear the screen
                    print(chr(27) + "[2J")

                    return 0