In this activity, you are going to begin working on the GOPS (Game of Pure Strategy) card game. This is Part 1 of the project, and you will finish the remaining code in Part 2.

For a review of the game rules and program requirements, please see Chapter 13, Lesson 1.
Introducing the Main Program Data and Flow
You will begin this project with some starting code in GOPS.py. The main program logic is very simple and has been done for you - no student changes are needed. The main program begins by getting an input number and using it to seed the random number generator.

# MAIN PROGRAM LOGIC
# NO STUDENT CHANGES BELOW THIS POINT

# randomize random number generator with a known seed for repeatability
seed = int(input("Enter random seed: "))
random.seed(seed)
Copy
Next, the main program will initialize 6 lists.

# initialize 3 hands, each with cards 2 - 10
diamonds = [2,3,4,5,6,7,8,9,10]
p1_spades = [2,3,4,5,6,7,8,9,10]
p2_clubs = [2,3,4,5,6,7,8,9,10]

# initialize empty lists of cards that each player captures or that are in the middle
p1_capture = []
p2_capture = []
middle_cards = []
Copy
Each list represents one stack of cards within the game.

diamonds - This list contains the shuffled, face-down stack of diamonds that forms the draw pile. The list begins with all diamonds from 2 through 10.
p1_spades - This list contains the remaining spades that player 1 has available for bidding. The list begins with all spades from 2 through 10.
p2_clubs - This list contains the remaining clubs that player 2 has available for bidding. The list begins with all clubs from 2 through 10.
p1_capture - This list contains the diamond cards that player 1 has captured. It is initially empty.
p1_capture - This list contains the diamond cards that player 2 has captured. It is initially empty.
middle_cards - This list contains the diamond card(s) currently up for bidding. It is initially empty
Next, the main logic will call the shuffle() method to randomize the order of the diamonds list.

# shuffle the deck of diamonds
diamonds = shuffle(diamonds)
Copy
Next, we enter a while() loop that will continue as long as there are any cards left to draw in the diamonds stack.

# game continues while diamonds are left
while len(diamonds) > 0:
play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards)
Copy
When the while() loop ends, the game is over, so we call determine_winner() to calculate game scores and select the winning player.

# all diamonds are gone, so game is over - time to determine winner
determine_winner(p1_capture, p2_capture)
Copy
Exploring the Methods
The GOPS game relies on 5 methods (functions). You are responsible for completing all of these methods! You will focus on one method in this activity, and complete the remainder in Part 2.

shuffle()

The shuffle() method takes an input list and returns a shuffled version of that list. The starting code simply returns the same list with no changes. You will complete this method in Part 2.

# given an input list of cards, shuffle them and return a new shuffled list
def shuffle(deck):
# STUDENT WILL COMPLETE THIS METHOD

return deck
Copy
p1_algorithm()

The p1_algorithm() is responsible for prompting player 1 to select from the list of available spades. The starting code simply returns the first item in the list without prompting the user. You will complete this method in Part 2.

# ensure the human player picks a valid spade from the remaining cards in hand
def p1_algorithm(p1_spades):
# STUDENT WILL COMPLETE THIS METHOD

return p1_spades[0] # replace this line with student code
Copy
p2_algorithm()

The p2_algorithm() is responsible for the algorithm that controls the computer player's selection from the list of available clubs. A "smart" computer opponent could, in theory, take into account many aspects of the current game, including the list of cards in the middle, the remaining diamonds, and the remaining spades and clubs in each player's hand. The starting code simply returns the first item in the list of available clubs. You will complete this method in Part 2.

# given the complete state of the game, this AI algorithm will make a choice
# for the computer player and return the resulting selection.
def p2_algorithm(middle_cards,diamonds,p1_spades,p2_clubs):
# this simple algorithm just makes a random selection from the available cards

# STUDENT WILL COMPLETE THIS METHOD

return p2_clubs[0] # replace this line with student code
Copy
play_round()

You will be completing this method in this activity. See the "Steps to Complete this Activity" below for a detailed description.

def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
# STUDENT WILL COMPLETE THIS METHOD

diamonds.clear() # replace this line with student code
Copy
determine_winner()

This method is called after all diamonds have been dealt from the middle deck. It will calculate the game score for player 1 and player 2 based on the lists of captured cards and display the appropriate winning messages. You will complete this method in Part 2.

def determine_winner(p1_capture, p2_capture):
print("===GAME OVER===")

# STUDENT WILL COMPLETE THIS METHOD
Copy
Starting Code
You will start with the GOPS.py source file shown below. All main program logic is complete - do not make any changes to the main program flow. All your work will go in the predefined methods. You will finish play_round() in this activity and complete the other methods in Part 2.

If you are working offline, you can copy and paste the code below into a "GOPS.py" source file that you create. But, if you are working with CompuScholar's auto-graded system, you will find this file waiting for you when you click on the activity link in the course interface.

```

import random

# given an input list of cards, shuffle them and return a new shuffled list
def shuffle(deck):
random.shuffle(deck)
return deck

# ensure the human player picks a valid spade from the remaining cards in hand
def p1_algorithm(p1_spades):
valid_selection = False

while not valid_selection:
print("Player 1 - choose a spade from your hand: ", p1_spades)
selection = int(input())

if selection in p1_spades:
valid_selection = True
return selection
else:
print("Invalid selection. Try again.")

# given the complete state of the game, this AI algorithm will make a choice
# for the computer player and return the resulting selection.
def p2_algorithm(middle_cards, diamonds, p1_spades, p2_clubs):
# this simple algorithm just makes a random selection from the available cards
return random.choice(p2_clubs)

def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
# flip the next diamond card from the deck
card = diamonds.pop()
middle_cards.append(card)

print("Middle cards: ", middle_cards)

# player 1 selects a spade
p1_selection = p1_algorithm(p1_spades)
print("Player 1 selected: ", p1_selection)

# player 2 (computer) selects a club
p2_selection = p2_algorithm(middle_cards, diamonds, p1_spades, p2_clubs)
print("Player 2 selected: ", p2_selection)

# determine the winner of the round
if p1_selection > p2_selection:
print("Player 1 wins the round!")
p1_capture.extend(middle_cards)
elif p2_selection > p1_selection:
print("Player 2 wins the round!")
p2_capture.extend(middle_cards)
else:
print("It's a tie!")

# clear the middle cards for the next round
middle_cards.clear()

# MAIN PROGRAM LOGIC
# NO STUDENT CHANGES BELOW THIS POINT

# randomize random number generator with a known seed for repeatability
seed = int(input("Enter random seed: "))
random.seed(seed)

# initialize 3 hands, each with cards 2 - 10
diamonds = [2,3,4,5,6,7,8,9,10]
p1_spades = [2,3,4,5,6,7,8,9,10]
p2_clubs = [2,3,4,5,6,7,8,9,10]

# initialize empty lists of cards that each player captures or that are in the middle
p1_capture = []
p2_capture = []
middle_cards = []

# shuffle the deck of diamonds
diamonds = shuffle(diamonds)

# game continues while diamonds are left
while len(diamonds) > 0:
play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards)

# all diamonds are gone, so game is over - time to determine winner
determine_winner(p1_capture, p2_capture)
```

Steps to Complete this Activity

This is Part 1 of the GOPS project. You begin with the starting code shown above, and if you run the code right away, you will be prompted for a random number and will immediately see part of the game-over message.

Enter random seed: 1
===GAME OVER===
In this activity, all your work will go into the play_round() method. This method receives all 6 lists that make up the game data, and it is called from within the main while() loop to handle one game round.

def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
# STUDENT WILL COMPLETE THIS METHOD

diamonds.clear() # replace this line with student code
Copy
Review the GOPS game rules in Lesson 1. Each of the input lists represents an important part of the game, and the play_round() method will update each of them as needed. The output from a single game round should follow this pattern:

Middle card(s): [4]
Player 1 available: [2, 3, 4, 5, 6, 7, 8, 9, 10]
Enter player 1 bid: 5
Player 2 available: [2, 3, 4, 5, 6, 7, 8, 9, 10]
Player 2 bid: 3
Player 1 wins bid, has captured [4]
Notice that all output messages except the first are indented two spaces to the right. This lets players easily identify the start of a new round. Your output will not look exactly like this until you finish the Part 2 activity, but this sample demonstrates several messages you will display in play_round().

Remove the default statement from play_round() and add your own logic. A single game round should take these actions:

Remove the first item from the diamonds list
It is critical that play_round() always reduces the number of cards left in the diamonds list. Otherwise, your main program logic will get stuck in an infinite while() loop!
Add that first item to the list of middle_cards and display the current contents of that list as follows:
"Middle card(s): X", where X is the list of items in middle_cards

You will need to display the items in a list in several places. You can generate strings like "[4]" or "[2, 3, 4, 5, 6, 7, 8, 9, 10]" by calling the str() method with a list variable as the parameter as follows: str(middle_cards).
Display the items in the p1_spades list as follows:
" Player 1 available: X", where X is the list of items in p1_spades

Call the p1_algoirthm() method (passing in the appropriate parameter) to get the player 1 bid for this round. Save the resulting bid selection in a local variable.
Remove the selected player 1 bid value from the p1_spades list
Call the p2_algoirthm() method (passing in the appropriate parameters) to get the player 2 bid for this round. Save the resulting bid selection in a local variable.
Remove the selected player 2 bid value from the p1_clubs list
Display the player 2 bid value as follows:
" Player 2 bid: X", where X is the value of the player 2 bid

At this point, your method has gathered the bid values for both player 1 and player 2, so it's time to compare those bids and see which player (if any) wins the middle diamond cards this round.

If the player 1 bid value is greater than the player 2 bid value:
Add all of the items in the middle_cards list to the end of the p1_capture list as follows:
While there are cards left in middle_cards list, pop the first item from the front of middle_cards and append the value to the end of the p1_capture list
Reset the middle_cards to an empty list
Print a message showing player 1 has won the bid and all of the cards that player 1 has now captured
" Player 1 wins bid, has captured X", where X is the list of items in p1_capture

If the player 2 bid value is greater than the player 1 bid value:
Add all of the items in the middle_cards list to the end of the p2_capture list (same approach as above)
Reset the middle_cards to an empty list
Print a message showing player 2 has won the bid and all of the cards that player 2 has now captured
" Player 2 wins bid, has captured X", where X is the list of items in p2_capture

If the two bid values are equal:
Print the message " Tie bid - middle card remains"
You have now completed your first method, and when you run the code, the program output should be very different. Your play_round() method should be called 9 times from the main while() loop and display 9 game rounds. The game is not complete yet, as there are still several methods you will need to code in the Part 2 activity.

Activity Results
Here is an example program run that shows the expected output for this Part 1 activity.

Enter random seed: 1
Middle card(s): [2]
Player 1 available: [2, 3, 4, 5, 6, 7, 8, 9, 10]
Player 2 bid: 2
Tie bid - middle card remains
Middle card(s): [2, 3]
Player 1 available: [3, 4, 5, 6, 7, 8, 9, 10]
Player 2 bid: 3
Tie bid - middle card remains
Middle card(s): [2, 3, 4]
Player 1 available: [4, 5, 6, 7, 8, 9, 10]
Player 2 bid: 4
Tie bid - middle card remains
Middle card(s): [2, 3, 4, 5]
Player 1 available: [5, 6, 7, 8, 9, 10]
Player 2 bid: 5
Tie bid - middle card remains
Middle card(s): [2, 3, 4, 5, 6]
Player 1 available: [6, 7, 8, 9, 10]
Player 2 bid: 6
Tie bid - middle card remains
Middle card(s): [2, 3, 4, 5, 6, 7]
Player 1 available: [7, 8, 9, 10]
Player 2 bid: 7
Tie bid - middle card remains
Middle card(s): [2, 3, 4, 5, 6, 7, 8]
Player 1 available: [8, 9, 10]
Player 2 bid: 8
Tie bid - middle card remains
Middle card(s): [2, 3, 4, 5, 6, 7, 8, 9]
Player 1 available: [9, 10]
Player 2 bid: 9
Tie bid - middle card remains
Middle card(s): [2, 3, 4, 5, 6, 7, 8, 9, 10]
Player 1 available: [10]
Player 2 bid: 10
Tie bid - middle card remains
===GAME OVER===
Remember, you have not yet written code for the following methods:

shuffle()
p1_algorithm()
p2_algorithm()
determine_winner()
Therefore, your program output for this Part 1 activity will show the following behavior:

The diamonds deck is not yet shuffled, so the middle cards will always be drawn in the initial order [2, 3, 4....10].
Both player 1 and player 2 will always select the same card - the first one available in their p1_spades and p1_clubs lists. This means every game round is a tie and you will see the "Tie bid" message each round. The list of middle_cards will also grow each round as neither player can win a bid.
No additional output is displayed yet regarding the game winner.
Because the two methods (shuffle() and p2_algorithm()) that will make use of the random number generator are not done, the random seed value entered at the very beginning will have no impact on the program output.
Activity Rubric
If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:

Points Description
9 Use pop() on the diamonds list
9 Use append() on the middle_cards list
9 Call the p1_algorithm() with the correct parameters
9 Call the p2_algorithm() with the correct parameters
9 Call remove() on the p1_spades list
9 Call remove() on the p2_clubs list
9 Print the " Player 1 wins..." message
9 Print the " Player 2 wins..." message
9 Print the " Tie bid..." message
19 Verify program output


Hints for getting full credit:

If your program is being automatically graded by our system, it is important to match the requirements as exactly as possible to receive full credit. Each line in the activity rubric above represents a test case that will pass - and award full points - or fail and award 0 points.

Do not change any method definitions or parameter names
Double-check your output statements for correct spelling, capitalization, spacing, and content.

To complete this activity and implement the play_round() method according to the steps provided, you can use the following code:

```python
def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
card = diamonds.pop()
middle_cards.append(card)

print("Middle card(s): " + str(middle_cards))
print(" Player 1 available: " + str(p1_spades))

p1_selection = p1_algorithm(p1_spades)
p1_spades.remove(p1_selection)

p2_selection = p2_algorithm(middle_cards, diamonds, p1_spades, p2_clubs)
p2_clubs.remove(p2_selection)

print(" Player 2 bid: " + str(p2_selection))

if p1_selection > p2_selection:
p1_capture.extend(middle_cards)
middle_cards.clear()
print(" Player 1 wins bid, has captured " + str(p1_capture))
elif p2_selection > p1_selection:
p2_capture.extend(middle_cards)
middle_cards.clear()
print(" Player 2 wins bid, has captured " + str(p2_capture))
else:
print(" Tie bid - middle card remains")

# Main Program Logic (below this line, no changes needed)
```

You can copy and paste this updated play_round() method into your GOPS.py file. This code implements the logic described in the instructions for handling a game round in the GOPS card game. This will allow you to progress with this activity and complete Part 1 successfully.

GOPS.py

# Copyright CompuScholar, Inc.
# Do not distribute or post in any public or private venue.

# Student Name

import random

# given an input list of cards, shuffle them and return a new shuffled list
def shuffle(deck):
# STUDENT WILL COMPLETE THIS METHOD

return deck

# ensure the human player picks a valid spade from the remaining cards in hand
def p1_algorithm(p1_spades):
# STUDENT WILL COMPLETE THIS METHOD

return p1_spades[0] # replace this line with student code

# given the complete state of the game, this AI algorithm will make a choice
# for the computer player and return the resulting selection.
def p2_algorithm(middle_cards,diamonds,p1_spades,p2_clubs):
# this simple algorithm just makes a random selection from the available cards

# STUDENT WILL COMPLETE THIS METHOD

return p2_clubs[0] # replace this line with student code

def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
# STUDENT WILL COMPLETE THIS METHOD

diamonds.clear() # replace this line with student code

def determine_winner(p1_capture, p2_capture):
print("===GAME OVER===")

# STUDENT WILL COMPLETE THIS METHOD

# MAIN PROGRAM LOGIC
# NO STUDENT CHANGES BELOW THIS POINT

# randomize random number generator with a known seed for repeatability
seed = int(input("Enter random seed: "))
random.seed(seed)

# initialize 3 hands, each with cards 2 - 10
diamonds = [2,3,4,5,6,7,8,9,10]
p1_spades = [2,3,4,5,6,7,8,9,10]
p2_clubs = [2,3,4,5,6,7,8,9,10]

# initialize empty lists of cards that each player captures or that are in the middle
p1_capture = []
p2_capture = []
middle_cards = []

# shuffle the deck of diamonds
diamonds = shuffle(diamonds)

# game continues while diamonds are left
while len(diamonds) > 0:
play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards)

# all diamonds are gone, so game is over - time to determine winner
determine_winner(p1_capture, p2_capture)

This is the code you’re supposed to fix.

To complete the play_round() and determine_winner() methods in the GOPS.py code, you can use the following implementations:

```python
def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
card = diamonds.pop(0)
middle_cards.append(card)

print("Middle card(s): " + str(middle_cards))
print(" Player 1 available: " + str(p1_spades))

p1_selection = p1_algorithm(p1_spades)
p1_spades.remove(p1_selection)

p2_selection = p2_algorithm(middle_cards, diamonds, p1_spades, p2_clubs)
p2_clubs.remove(p2_selection)

print(" Player 2 bid: " + str(p2_selection))

if p1_selection > p2_selection:
p1_capture.extend(middle_cards)
middle_cards.clear()
print(" Player 1 wins bid, has captured " + str(p1_capture))
elif p2_selection > p1_selection:
p2_capture.extend(middle_cards)
middle_cards.clear()
print(" Player 2 wins bid, has captured " + str(p2_capture))
else:
print(" Tie bid - middle card remains")

def determine_winner(p1_capture, p2_capture):
print("===GAME OVER===")

p1_score = sum(p1_capture)
p2_score = sum(p2_capture)

if p1_score > p2_score:
print("Player 1 wins with a score of", p1_score)
elif p2_score > p1_score:
print("Player 2 wins with a score of", p2_score)
else:
print("It's a tie!")

# Rest of the code remains the same
```

These changes will allow your game to properly handle each game round, update the card decks and captures, and determine the winner at the end of the game. You can insert these implementations into your GOPS.py file and run the program to see the game in action.

Wrong code

Here is the correct code for the play_round() and determine_winner() methods in your GOPS.py program:

```python
def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
card = diamonds.pop(0)
middle_cards.append(card)

print("Middle card(s): " + str(middle_cards))
print(" Player 1 available: " + str(p1_spades))

p1_selection = p1_algorithm(p1_spades)
p1_spades.remove(p1_selection)

p2_selection = p2_algorithm(middle_cards, diamonds, p1_spades, p2_clubs)
p2_clubs.remove(p2_selection)

print(" Player 2 bid: " + str(p2_selection))

if p1_selection > p2_selection:
p1_capture.extend(middle_cards)
middle_cards.clear()
print(" Player 1 wins bid, has captured " + str(p1_capture))
elif p2_selection > p1_selection:
p2_capture.extend(middle_cards)
middle_cards.clear()
print(" Player 2 wins bid, has captured " + str(p2_capture))
else:
print(" Tie bid - middle card remains")

def determine_winner(p1_capture, p2_capture):
print("===GAME OVER===")

p1_score = sum(p1_capture)
p2_score = sum(p2_capture)

if p1_score > p2_score:
print("Player 1 wins with a score of", str(p1_score))
elif p2_score > p1_score:
print("Player 2 wins with a score of", str(p2_score))
else:
print("It's a tie!")
```

You can substitute this code into your GOPS.py file to complete the play_round() and determine_winner() methods. Remember that the remaining methods shuffle() and p1_algorithm() need to be completed in Part 2 of the project.