quiz حل الأسئلة الجامعية manage_search الأرشيف

تم الحل ✓
categoryهندسة الحاسبات schoolبكالوريوس event_available2026-07-15

السؤال

Transcribed Image Text:

Checkpoint B (40 points) For Checkpoint B, you will be shuffling the deck and dealing the cards to both the "human user" and the "computer user". Then, the human user and computer user will take turns and try to win the game by getting a Racko. The logic used by computer to play its turn is provided. For the purpose of this checkpoint, you need to define two global variables (just after the import statements will work): # Define a global variable to denote the total number of cards. # In the actual game, this will be equal to 60 but for testing we use smaller numbers numCards 9 #9, 12, 60 # Define a global variable to denote the size of rack (number of cards in user's hand). # In the actual game, this will be equal to 10 but for testing we use smaller numbers rackSize = 3 # 3, 4, 10 For the purpose of this checkpoint, implement the following functions: • • shuffle(card_stack) - shuffle the deck to start the game. Also called to shuffle the discard pile if we ever run out of cards and need to 'restart' the game. This function does not return anything. You can import the random module and just use random.shuffle. For example: shuffle(Ist) may randomly update list Ist (shown under section "Actual Program") to be 1st [33, 17, 25, 38, 3, 30, 46, 49, 11] ⚫ deal_initial_hands(deck) - start the game off by dealing two hands of rackSize cards each from the deck (note: rackSize is a global variable and already defined above). This is done by dealing the top card from deck to the computer and then to the user and then back to computer and so on until rackSize cards have been dealt to each user. This function returns two lists - one representing the user's hand and the other representing the computer's hand - in a tuple form. It will take the following form: #Deals initial hands; alternates card distribution #The computer is always the first person that gets dealt to def deal_initial_hands (deck): #Initialize empty lists for user's hand and computer's hand # Append to the lists one-by-one rackSize cards from the deck # return the two lists in a tuple form (user_hand, computer_hand) For example: if the deck = [9, 3, 1, 8, 4, 2, 7, 6, 5], deal_initial_hands(deck) will return ([6, 2, 8], [5, 7, 4]) and deck becomes [9, 3, 1] ⚫ check_racko(hand) - given a hand (this will be either the user's or the computer's) determine if Racko has been achieved. Remember that Racko means that the cards are in ascending order. This function returns a boolean value (True if Racko has been achieved, False otherwise) ⚫ replace Deck(card_stack) - when the deck is empty, this function is used to shuffle the discard pile and make that as the new deck. The top card from the new deck becomes the new discard pile. This function returns two lists - the new deck and the new discard pile - in a tuple form. Note: this function only needs the discard pile as input parameter (because the deck is empty anyway, so no need to pass it). ⚫ computer_play(computer_hand, deck, discard): This is the function where the computer uses some strategy to play its move. This function updates all the three lists computer_hand, deck, and discard. Most of the function is provided below. You need to insert code where you see "...". The code that you need to insert is very similar to what you did in checkpoint A for the user's hand. ⚫ main(): a function that puts it all together. This is updated from Checkpoint A and incorporates the above functions so that the user and computer take turns one after another and the program concludes when anyone achieves a Racko. See the updated specification of this function below. See the sample output below. Note, you can get the same output if you insert the statement random.seed(26) at the beginning of your main() function. Sample Output 1 (using numCards = 9, rackSize = 3) in which human wins deck: [3,2] discard pile: [7] Your current hand: [9, 5, 1] Do you want this discard card: 7 Enter yes or no: no The card you get from the deck is 2 Do you want to keep this card? Enter yes or no: yes Enter the number of the card you want to kick out: 9 Your new hand is: [2, 5, 1] deck: [3] discard pile: [7,9] Computer's current hand: [4, 8, 6] Computer: Chooses top card from the deck 3 Computer: Chooses top deck card 3 Computer: Replacing it with 4 Computer's new hand is: [3, 8, 6] Computer: WOAH! Deck is empty. Shuffling discard pile and using that as the new deck. deck: [9, 7] discard pile: [4] Your current hand: [2, 5, 1] Do you want this discard card: 4 Enter yes or no: no The card you get from the deck is 7 Do you want to keep this card? Enter yes or no: yes Enter the number of the card you want to kick out: Your new hand is: [2, 5, 7] HUMAN WINS! with hand of [2, 5, 7]

check_circle الجواب — حل مفصل خطوة بخطوة

hourglass_top