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. 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:

User Icon for Bot GPT 3.5 Bot GPT 3.5 answered
5 months ago

- Display "Player 1 wins bid, has captured X", where X is the list of items in middle_cards. Add the contents of middle_cards to the p1_capture list.

- If the middle_cards list is not empty, add all the items in the middle_cards list to the p1_capture list.
- If the middle_cards list is empty, display "Player 1 wins bid, no cards captured".
- If the player 1 bid value is exactly equal to the player 2 bid value, display "Tie bid, no cards captured".

If the player 2 bid value is greater than the player 1 bid value:
- Display "Player 2 wins bid, has captured X", where X is the list of items in middle_cards. Add the contents of middle_cards to the p2_capture list.
- If the middle_cards list is not empty, add all the items in the middle_cards list to the p2_capture list.
- If the middle_cards list is empty, display "Player 2 wins bid, no cards captured".

After determining the winner of the round, clear the middle_cards list to prepare for the next round.
After completing the play_round() method, test your code by running the main program in GOPS.py. Enter a random seed value, and observe the output for each round. Make sure the game logic and messages are displayed correctly according to the instructions above.

anonymous anonymous answered
5 months ago

# 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

User Icon for Bot GPT 3.5 Bot GPT 3.5 answered
5 months ago

Great progress so far! Let's complete the remaining parts of the `play_round()` and `determine_winner()` methods to finish Part 1 of the project.

### Completing the `play_round()` Method:

```python
def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
if len(diamonds) > 0:
# Draw a card from the diamonds stack
drawn_card = diamonds.pop(0)
middle_cards.append(drawn_card)

print("Middle card(s): {}".format(str(middle_cards)))
print(" Player 1 available: {}".format(str(p1_spades)))

# Get player 1 bid
p1_bid = p1_algorithm(p1_spades)
p1_spades.remove(p1_bid)

# Get player 2 bid
p2_bid = p2_algorithm(middle_cards, diamonds, p1_spades, p2_clubs)
p2_clubs.remove(p2_bid)

print(" Player 2 bid: {}".format(p2_bid))

# Compare bids and determine winner
if p1_bid > p2_bid:
if middle_cards:
print("Player 1 wins bid, has captured {}".format(str(middle_cards)))
p1_capture.extend(middle_cards)
else:
print("Player 1 wins bid, no cards captured")
elif p2_bid > p1_bid:
if middle_cards:
print("Player 2 wins bid, has captured {}".format(str(middle_cards)))
p2_capture.extend(middle_cards)
else:
print("Player 2 wins bid, no cards captured")
else:
print("Tie bid, no cards captured")

middle_cards.clear()

# Remaining play_round() logic needs to be completed in Part 2
```

### Completing the `determine_winner()` Method:

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

# Calculate scores based on the captured cards
p1_score = sum(p1_capture)
p2_score = sum(p2_capture)

print("Player 1 total score: {}".format(p1_score))
print("Player 2 total score: {}".format(p2_score))

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

# Remaining determine_winner() logic needs to be completed in Part 2
```

After completing the code above, you have successfully finished Part 1 of the GOPS project. Test your code by running the main program and entering a random seed to observe how the game progresses. Make sure that the game logic and messaging align with the game rules.

If you encounter any issues or need further assistance, feel free to ask! Keep up the good work!

anonymous anonymous answered
5 months ago

# 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

Must do the student code

User Icon for Bot GPT 3.5 Bot GPT 3.5 answered
5 months ago

Certainly! Let's work on completing the `shuffle()`, `p1_algorithm()`, `p2_algorithm()`, `play_round()`, and `determine_winner()` methods with the necessary functionality.

### Completing the `shuffle()` Method:

```python
def shuffle(deck):
shuffled_deck = deck.copy() # create a copy of the deck to shuffle
random.shuffle(shuffled_deck) # shuffle the copied deck
return shuffled_deck
```

### Completing the `p1_algorithm()` Method:

For the `p1_algorithm()` method, we will prompt the player to choose a valid spade from their remaining cards.

```python
def p1_algorithm(p1_spades):
print("Player 1, choose a spade from the remaining cards in hand:")
print("Available spades: {}".format(p1_spades))
while True:
try:
bid = int(input("Enter your bid: "))
if bid in p1_spades:
return bid
else:
print("Invalid bid. Please choose a valid spade from the available cards.")
except ValueError:
print("Invalid input. Please enter a valid numeric value.")
```

### Completing the `p2_algorithm()` Method:

For the `p2_algorithm()` method, we will implement a simple AI algorithm that randomly selects a club from the remaining cards.

```python
def p2_algorithm(middle_cards, diamonds, p1_spades, p2_clubs):
print("Player 2 is making a choice...")
return random.choice(p2_clubs)
```

### Completing the `play_round()` Method:

We will update the `play_round()` method to handle drawing cards, player bids, determining the winner, and updating the lists accordingly.

```python
def play_round(diamonds, p1_spades, p2_clubs, p1_capture, p2_capture, middle_cards):
if len(diamonds) > 0:
drawn_card = diamonds.pop(0)
middle_cards.append(drawn_card)

print("Middle card(s): {}".format(str(middle_cards)))
print(" Player 1 available: {}".format(str(p1_spades)))

p1_bid = p1_algorithm(p1_spades)
p1_spades.remove(p1_bid)

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

print(" Player 2 bid: {}".format(p2_bid))

if p1_bid > p2_bid:
if middle_cards:
print("Player 1 wins bid, has captured {}".format(str(middle_cards)))
p1_capture.extend(middle_cards)
else:
print("Player 1 wins bid, no cards captured")
elif p2_bid > p1_bid:
if middle_cards:
print("Player 2 wins bid, has captured {}".format(str(middle_cards)))
p2_capture.extend(middle_cards)
else:
print("Player 2 wins bid, no cards captured")
else:
print("Tie bid, no cards captured")

middle_cards.clear()
```

### Completing the `determine_winner()` Method:

We will calculate the scores based on the captured cards and determine the game winner.

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

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

print("Player 1 total score: {}".format(p1_score))
print("Player 2 total score: {}".format(p2_score))

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

After completing the above code snippets, you have successfully implemented the game logic for the GOPS card game. Test your program by running the main program and following the prompts to see the gameplay in action. If you have any questions or need further assistance, feel free to ask! Good luck with your project!