Programming an OldMaid Card Game python

Click For Summary

Discussion Overview

The discussion revolves around programming issues encountered while developing a card game of Old Maid in Python. Participants are focused on troubleshooting code related to removing pairs of cards from a player's hand, as well as general coding practices and debugging techniques.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their code and expresses difficulty in removing the same cards from their hand in the Old Maid game.
  • Another participant suggests providing more details about where the problem occurs in the code and questions the use of single-letter variable names, specifically 'l' in the remove_pairs function.
  • Multiple participants recommend using a debugger or adding print statements to inspect the cards being passed into the remove_pairs function.
  • A participant references their own article on Python debugging techniques, providing links for further reading.

Areas of Agreement / Disagreement

There is no consensus on the specific issue at hand, as participants are offering suggestions and seeking clarification without resolving the underlying problem. Multiple viewpoints on debugging strategies are presented.

Contextual Notes

Participants have noted the importance of code formatting and variable naming conventions, but there are unresolved questions about the specific implementation details and logic in the provided code.

garr6120
Messages
42
Reaction score
0
I am having trouble removing the same cards from my hand in my game of oldMaid. this is my source:
Mod note: Added code tags to preserve indentation, and modified code slightly (replacing index i in arrays with j.
Python:
    deck=[]
    suits = ['\u2660', '\u2661', '\u2662', '\u2663']
    ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
    for suit in suits:
        for rank in ranks:
            deck.append(rank+suit)
    deck.remove('Q\u2663') # remove a queen as the game requires
    return deck

def shuffle_deck(deck):
    '''(list of str)->None
       Shuffles the given list of strings representing the playing deck  
    '''
    random.shuffle(deck)

#####################################

def deal_cards(deck):

    dealer=[]
    other=[]

    half=len(deck)//2
    for j in range(half):
        dealer.append(deck[j])

    for j in range(half,len(deck)):
        other.append(deck[j])
      
    return (dealer, other)
          
def remove_pairs(l):

    no_pairs=[]
  
    for j in range(1,len(l)):
        if l[j-1][0]!=l[j][0]:
            no_pairs.append(l[j])
          
    random.shuffle(no_pairs)
    return no_pairs

def play_game():
     '''()->None
     This function plays the game'''
  
     deck=make_deck()
     shuffle_deck(deck)
     tmp=deal_cards(deck)

     dealer=tmp[0]
     human=tmp[1]

     print("Hello. My name is Robot and I am the dealer.")
     print("Welcome to my card game!")
     print("Your current deck of cards is:")
     print_deck(human)
     print("Do not worry. I cannot see the order of your cards")

     print("Now discard all the pairs from your deck. I will do the same.")
     wait_for_player()
    
     dealer=remove_pairs(dealer)
     human=remove_pairs(human)
  
# main
play_game()
 
Last edited by a moderator:
Technology news on Phys.org
garr6120 said:
I am having trouble removing the same cards from my hand in my game of oldMaid. this is my source:
Mod note: Added code tags to preserve indentation, and modified code slightly (replacing index i in arrays with j.
Python:
    deck=[]
    suits = ['\u2660', '\u2661', '\u2662', '\u2663']
    ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
    for suit in suits:
        for rank in ranks:
            deck.append(rank+suit)
    deck.remove('Q\u2663') # remove a queen as the game requires
    return deck

def shuffle_deck(deck):
    '''(list of str)->None
       Shuffles the given list of strings representing the playing deck
    '''
    random.shuffle(deck)

#####################################

def deal_cards(deck):

    dealer=[]
    other=[]

    half=len(deck)//2
    for j in range(half):
        dealer.append(deck[j])

    for j in range(half,len(deck)):
        other.append(deck[j])
   
    return (dealer, other)
       
def remove_pairs(l):

    no_pairs=[]
 
    for j in range(1,len(l)):
        if l[j-1][0]!=l[j][0]:
            no_pairs.append(l[j])
       
    random.shuffle(no_pairs)
    return no_pairs

def play_game():
     '''()->None
     This function plays the game'''
 
     deck=make_deck()
     shuffle_deck(deck)
     tmp=deal_cards(deck)

     dealer=tmp[0]
     human=tmp[1]

     print("Hello. My name is Robot and I am the dealer.")
     print("Welcome to my card game!")
     print("Your current deck of cards is:")
     print_deck(human)
     print("Do not worry. I cannot see the order of your cards")

     print("Now discard all the pairs from your deck. I will do the same.")
     wait_for_player()
 
     dealer=remove_pairs(dealer)
     human=remove_pairs(human)
 
# main
play_game()
In the future, please use code tags, especially with Python code, where indentation is crucial.

What I did was add tags like this:
Python:
<Some Python code>
I also modified your code slightly, replacing array indexes of i with j. The reason for this is that our system thinks that arr means that it should display arr, and then start displaying in italics.

Regarding your question, please give more details. Where in the code are you seeing the problem?

Also, single-letter variables are OK for loop control variables, but not OK for any other purpose. In your remove_pairs() function, what does l (letter el) represent?
 
  • Like
Likes   Reactions: jedishrfu
Suggestion: use a debugger or add a command so that, at the beginning of remove_pairs, you can see the cards you passed in.
 
  • Like
Likes   Reactions: jedishrfu
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 22 ·
Replies
22
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
6K
Replies
31
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K