Python deals the cards

By | December 26, 2020

About 10 years ago I applied to software developer position and received the following coding exercise:
Using Java/Python, please design and implement the classes for a card game (pick any game, Poker for example), which uses an ordered deck of cards, containing 52 cards divided in 13 ranks (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K) with four suits: spades, hearts, diamonds and clubs. The cards can be, at a minimum, shuffled, cut, and dealt (feel free to implement additional ones that are required by the game).
I do not remember exactly but there was some time limit to complete the task: 3 or 4 hours. I did not implement game itself because there was not enough time and I am not a poker player. What I did: created 2 top-level classes: Deck (for 52-card deck) and Game. The class Desk has 2 functions: Shuffle and Cat. The class Game has only one function: Dealt. This is the code (to download click here):


# Poker 2010
import random
#class to define suit of card
class Suit:
   def __init__(self, suit):
       self.s = suit
#class to define rank of card
class Rank:
   def __init__(self,rank):
      self.r = rank;
   def getRank(self):
      return self.r
#class to define card
class Card(Suit,Rank):
   def __init__(self,index,suit,rank):
      self.i = index
      Suit.__init__(self,suit)
      Rank.__init__(self,rank)

#class to define deck
class Deck:
   cards = {}
   order = {} #low order on the top of the deck
   def __init__(self):
      suits =['spade', 'heart', 'diamond','club']
      ranks = ['A', '2', '3','4','5','6','7','8','9','10','J','D','K']
      for s in range(len(suits)):
         for r in range(len(ranks)):
            self.cards[s*len(ranks)+r] = Card(s*len(ranks)+r,suits[s],ranks[r])
            self.order[s*len(ranks)+r] = self.cards[s*len(ranks)+r]
   def Shuffle(self):
      random.shuffle(self.order)
   def Cut(self):
         orderTmp = {}
         cutIndex = random.randint(1, 50) #at least 1 card from top or buttom
         #print cutIndex
         index = 0
         for i in range(cutIndex,len(self.order)):
            orderTmp[index] = self.order[i]
            index += 1
         #print orderTmp
         for i in range(0,cutIndex):
            orderTmp[index] = self.order[i]
            index += 1
         #print orderTmp
         for i in range(0,len(self.order)):
            self.order[i] = orderTmp[i]
# class player
class Player:
   def __init__(self,deck,index):
      self.pcards = {}
#     print("————")
      for i in range(5):
         self.pcards[i] = deck[index+i]
#        print(self.pcards[i].s)
#        print(self.pcards[i].r)
# class game with only 1 method Dealt
class Game:
   gcards = {}
   p = {}
   def __init__(self,nplayers, deck):
      self.gcards = deck
      self.nplayers = nplayers
   def Dealt(self):
      self.index=0
      for n in range(self.nplayers):
         self.p[n] = Player(self.gcards,self.index)
         self.index += 5
# create Deck, Suffle cards, cut deck and make dealt (4 players)
d = Deck()
d.Shuffle()
d.Cut()
nplayers = 4
g = Game(nplayers,d.order) #4 players
g.Dealt()
# result of dealt
for i in range(nplayers):
   print("————-Player: ",i+1)
   for j in range(5):
      print(g.p[i].pcards[j].s)
      print(g.p[i].pcards[j].r)

The results show how code works:

Test 1 Test 2 Test 3
# python poker.py
————-Player: 1
diamond
4
heart
A
club
8
diamond
7
diamond
A
————-Player: 2
spade
5
heart
5
club
3
diamond
K
heart
D
————-Player: 3
spade
8
club
2
spade
K
heart
10
club
7
————-Player: 4
spade
3
spade
6
club
K
heart
3
diamond
5

# python poker.py
————-Player: 1
heart
K
diamond
6
diamond
J
heart
6
diamond
D
————-Player: 2
heart
3
club
4
club
9
spade
D
diamond
2
————-Player: 3
club
D
heart
5
club
3
spade
5
heart
J
————-Player: 4
club
6
diamond
9
spade
K
heart
2
spade
8
# python poker.py
————-Player: 1
club
4
heart
2
spade
7
spade
10
diamond
8
————-Player: 2
club
A
club
D
club
2
spade
2
diamond
2
————-Player: 3
club
5
heart
9
spade
8
heart
4
spade
5
————-Player: 4
club
10
spade
K
spade
3
heart
A
heart
10

I sent my code to them and they replied me in one month and invited me to an in-person interview. However I already got employment and I decided not to go. And I did not practicing in poker and other card game programming anymore.

♣            ♦            ♥            ♠

Leave a Reply

Your email address will not be published. Required fields are marked *