# Problem with comparing 2 cards in shithead card game

Page 1 of 1

## 0 Replies - 442 Views - Last Post: 29 August 2013 - 12:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=327931&amp;s=3ee746aa601f24e1f6b93753a3420663&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cickozg

Reputation: 0
• Posts: 4
• Joined: 29-August 13

# Problem with comparing 2 cards in shithead card game

Posted 29 August 2013 - 12:39 PM

Hello to all. I'm begginer in python programing and need some help with card game I'm making.

Here is my code so far:

```from Tkinter import *
from PIL import Image, ImageTk
import random

#Velicina ekrana
HEIGHT = 960
WIDTH = 1280

#Dimenzije karata
CARD_SIZE = (73, 98)
CARD_BACK_SIZE = (71, 96)

#Koordinate pozicija
PLAYING_POS = (WIDTH/2,HEIGHT/2)
DECK_POS = (WIDTH-100,HEIGHT/2)

PLAYER_POS = (WIDTH/4,HEIGHT-150)
KOMP_POS = (WIDTH/4,200)
KOMP2_POS = (130,HEIGHT/4)
KOMP3_POS = (WIDTH-130,HEIGHT/4)

PLAYER_POS2 = (WIDTH/2,HEIGHT-100)
KOMP_POS2 = (WIDTH/4,100)
KOMP2_POS2 = (80,HEIGHT/4)
KOMP3_POS2 = (WIDTH-80,HEIGHT/4)

PLAYER_POS3 = (WIDTH/2,HEIGHT-100)
KOMP_POS3 = (WIDTH/4,100)
KOMP2_POS3 = (80,HEIGHT/4)
KOMP3_POS3 = (WIDTH-80,HEIGHT/4)

#Karte
SUITS = ['C', 'S', 'H', 'D']
RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
VALUES = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'J':10, 'Q':11, 'K':12, 'A':13, '10':14}

#Ostale varijable
klik=0
counter=4
thrown_cards=[]

class Cards:
def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
self.karta = None

def get_suit(self):
return self.suit

def get_rank(self):
return self.rank

def __str__(self):
return self.suit + self.rank

def draw(self,position):
CARD = Image.open ("cards.png")
box = [RANKS.index(self.rank) * CARD_SIZE[0], SUITS.index(self.suit) * CARD_SIZE[1], CARD_SIZE[0] * (RANKS.index(self.rank)+1) , CARD_SIZE[1] * (SUITS.index(self.suit)+1)]
cropped = CARD.crop(box)
self.karta = ImageTk.PhotoImage(cropped)
canvas.create_image(position, image=self.karta)

def draw_back(self,position):
CB=Image.open("card_back.png")
self.karta = ImageTk.PhotoImage(CB)
canvas.create_image(position, image=self.karta)

class Deck:
def __init__(self):
self.deck_list = []
# standard 52 card deck
global SUITS
global RANKS
for s in SUITS:
for r in RANKS:
self.deck_list.append(Cards(s, r))

def shuffle(self):
return random.shuffle(self.deck_list)

def deal_card(self):
return self.deck_list.pop(0)

def draw(self):
for c in self.deck_list:
c.draw_back(DECK_POS)

class Hand:
def __init__(self ):
self.card_list=[]
self.card_list2=[]
self.card_list3=[]

self.card_list.append(card)
game_deck.draw()
self.card_list2.append(card)
self.card_list3.append(card)

def choose_card(self):
global VALUES,izbor,choices,thrown_cards
self.choices=[]
if len(thrown_cards) >= 1 :
if len(self.card_list) > 0:
for c in self.card_list:
if (VALUES.get(c.get_rank()) >= VALUES.get(thrown_cards[-1].get_rank())) or c.get_rank()== "2" :
self.choices.append(c)
if len(self.choices) > 0:
izbor = self.choices[0]
self.choices=[]

elif len(self.card_list) == 0 and len(self.card_list2) > 0:
for c in self.card_list:
if VALUES.get(c.get_rank()) >= VALUES.get(thrown_cards[-1].get_rank_rank()) or c.get_rank()== "2":
self.choices.append(c)
if len(self.choices) > 0:
izbor = self.choices[0]
self.choices=[]

elif len(self.card_list) == 0 and len(self.card_list2) == 0 and len(self.card_list3) > 0:
izbor = random.choice(self.card_list3)
self.choices.append(izbor)
else:
if len(self.card_list) > 0:
for c in self.card_list:
self.choices.append(c)
if len(self.choices) > 0:
izbor = self.choices[0]
self.choices=[]
elif len(self.card_list) == 0 and len(self.card_list2) > 0:
for c in self.card_list:
self.choices.append(c)
if len(self.choices) > 0:
izbor = self.choices[0]
self.choices=[]
elif len(self.card_list) == 0 and len(self.card_list2) == 0 and len(self.card_list3) > 0:
izbor = random.choice(self.card_list3)
self.choices.append(izbor)

def throw_card(self):
global thrown_cards,choices
if len(self.choices) == 0 :
self.card_list.append(thrown_cards)
thrown_cards=[]

elif len(self.choices) == 1 and ( self.choices[0].get_rank() < VALUES.get(thrown_cards[-1].get_rank_rank()) or self.choices[0].get_rank() != 2):
thrown_cards.append(izbor)
self.card_list.append(thrown_cards)
thrown_cards=[]

else:
is_valid(izbor)
if len(self.card_list) > 0:
self.card_list.pop(self.card_list.index(izbor))
elif len(self.card_list) == 0 and self.card_list2 >0:
self.card_list2.pop(self.card_list2.index(izbor))
else:
self.card_list3.pop(self.card_list3.index(izbor))

def draw(self,position):
pos = list(position)
for c in self.card_list:
c.draw(pos)
pos[0] += CARD_SIZE[0]/3

def draw2(self,position):
pos = list(position)
for c in self.card_list3:
c.draw_back(pos)
pos[0] += CARD_SIZE[0]/3

def draw3(self,position):
pos = list(position)
for c in self.card_list2:
c.draw(pos)
pos[0] += CARD_SIZE[0]/3

def click(event):
global klik,counter
if counter % 4 == 0:
klik = (event.x-PLAYER_POS[0]-36) //(CARD_SIZE[0]//3)
if (len(player_hand.card_list) == 0) and (len(player_hand.card_list2) == 0) and (HEIGHT-148 < event.y < HEIGHT-52):
is_valid(player_hand.card_list3[klik])
if valid:
player_hand.card_list3.pop(klik)
player_hand.draw2(PLAYER_POS3)

elif (len(player_hand.card_list) == 0) and (len(player_hand.card_list2) > 0) and (HEIGHT-148 < event.y < HEIGHT-52):
is_valid(player_hand.card_list2[klik])
if valid:
player_hand.card_list2.pop(klik)
player_hand.draw3(PLAYER_POS2)

elif (HEIGHT-198 < event.y < HEIGHT-102):
is_valid(player_hand.card_list[klik])
if valid:
player_hand.card_list.pop(klik)
player_hand.draw(PLAYER_POS)

if len(player_hand.card_list) < 3 and len(game_deck.deck_list)>0:
player_hand.draw(PLAYER_POS)
counter +=1
if len(thrown_cards) > 0:
is_10(thrown_cards[-1])
root.after(1500,turn)

def start():
global game_deck,player_hand,komp_hand,komp2_hand,komp3_hand,card_list,thrown_cards
canvas.delete(ALL)
#Shuffle the deck
game_deck = Deck()
game_deck.shuffle()

#Create players hands
player_hand = Hand()
komp_hand = Hand()
komp2_hand = Hand()
komp3_hand = Hand()
thrown_cards=[]

#add three cards to each hand

#Draw players card
player_hand.draw(PLAYER_POS)
player_hand.draw2(PLAYER_POS2)
player_hand.draw3(PLAYER_POS2)
komp_hand.draw3(KOMP_POS2)
komp2_hand.draw3(KOMP2_POS2)
komp3_hand.draw3(KOMP3_POS2)

def turn():
global counter
if counter % 4 == 1:
komp_hand.choose_card()
komp_hand.throw_card()
if len(game_deck.deck_list) > 0:
counter += 1

elif counter % 4 == 2:
komp2_hand.choose_card()
komp2_hand.throw_card()
if len(game_deck.deck_list) > 0:
counter += 1

elif counter %4 == 3:
komp3_hand.choose_card()
komp3_hand.throw_card()
if len(game_deck.deck_list) > 0:
counter += 1
if len(thrown_cards) > 0:
is_10(thrown_cards[-1])
root.after(1500,turn)

def is_valid(card):
global VALUES,thrown_cards,valid
if len(thrown_cards) > 0:
if VALUES.get(card.get_rank()) >= VALUES.get(thrown_cards[-1].get_rank()) or card.get_rank()== 2:
card.draw([640,480])
thrown_cards.append(card)
valid = True
else:
message = "Not a valid card !!"
valid = False
else:
card.draw([640,480])
thrown_cards.append(card)
valid = True

def is_10(card):
global thrown_cards,counter
if card.get_rank()== "10":
thrown_cards = []
counter -= 1

root=Tk()
root.bind("<Button-1>",click)

canvas=Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()

root.mainloop()

```

When bot should throw his card, it gives me an error:

File "C:\Users\Petar\Documents\Python programi\kajla.py", line 114, in choose_card
if (VALUES.get(c.get_rank()) >= VALUES.get(thrown_cards[-1].get_rank())) or c.get_rank()== "2" :

AttributeError: 'list' object has no attribute 'get_rank'

I dont know how should I compare card with the last thrown card? I tried to place thrown_card[-1] in it's own variable but with same result. Any ideas?

Advice in code structure is also welcome />

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }