filename = crazy.py
import random
import subprocess as sp
from time import sleep
# set up the constants
HEARTS = chr(9829)
SPADES = chr(9824)
DIAMONDS = chr(9830)
CLUBS = chr(9827)
BACKSIDE = 'backside'
temp = sp.call('clear',shell=True)
def main():
print()
print(
""" \u001b[4mCrazy Eights\033[0m, the familiar card game written in Python.
By cogiz (cogiz@protonmailmail.com)
\u001b[4mRules\033[0m:
- Each player is dealt 8 cards
- If a '2' is played, the opposing player must pick up 2 cards.
- If a 'Jack' or 'J' is played then the opposing player loses
their turn.
- If the "Queen of Spades or the 'Bitch' is played then the
opposing player must pick up 5 cards.
- The game is over when either the player or Computer has 0 cards
left in their hand.
- The player to play first will be determined by random choice.
******************************************************************""")
sleep(5)
while True: # main game loop
# give dealer and player each 8 cards from the deck and turn up 'active card'.
deck = getdeck()
computerhand = [deck.pop(), deck.pop(), deck.pop(), deck.pop(), deck.pop(),deck.pop(), deck.pop(), deck.pop()]
playerhand = [deck.pop(), deck.pop(), deck.pop(), deck.pop(), deck.pop(),deck.pop(), deck.pop(), deck.pop()]
activecard = deck.pop()
while True:
temp = sp.call('clear',shell=True)
displayhands(playerhand, computerhand, False)
print("\033[10;0HActive card")
activecard()
def getdeck():
""" Return a list of (rank,suit) tuples for all 52 cards."""
deck = []
for suit in (HEARTS, SPADES, DIAMONDS, CLUBS):
for rank in range(2, 11):
deck.append((str(rank), suit)) # add the numbered cards
for rank in ('J', 'Q', 'K', 'A'):
deck.append((rank, suit)) # add the Royals
random.shuffle(deck)
return deck
def displayhands(playerhand, computerhand, showcomputerhand):
"""Show the player's and computer's cards. Show only backs of computer's cards
if showdealerhand is False."""
print()
if showcomputerhand:
print('COMPUTER:')
displaycards(computerhand)
else:
print('COMPUTER:')
displaycards([BACKSIDE] * len(computerhand))
# Show the player's cards:
print('\033[17;0HPLAYER:')
displaycards(playerhand)
print('\033[12;0HActive card', (activecard))
#activecard(card)
def displaycards(cards):
"""Display all the cards in the cards list."""
rows = ['', '', '', '', '', ''] # The text to display on each row.
for i, card in enumerate(cards):
rows[0] += ' ___ ' # Print the top line of the card.
if card == BACKSIDE:
# Print the card's back:
rows[1] += '|** | '
rows[2] += '|***| '
rows[3] += '|_**| '
else:
# Print the card's front.
rank, suit = card # The card is a tuple data structure.
rows[1] += '|{} | '.format(rank.ljust(2))
rows[2] += '| {} | '.format(suit)
rows[3] += '|_{}| '.format(rank.rjust(2, '_'))
# print each row on the screen:
for row in rows:
print(row)
def activecard(card):
for i, card in enumerate(cards):
rank, suit = card
print('___')
print('|{} |'.format(rank.ljust(2)))
print('| {} |'.format(suit))
print('|_{}|'.format(rank.rjust(2), '_'))
if __name__ == '__main__':
main()
in the above code, I can’t seem to get the ‘active card’ to print as a single card
in between ‘COMPUTER’ and ‘PLAYER’ on the screen. I keep getting card is not
defined error.
Does it have something to do with the enumerate() function?
see screenshot attached:
I know I have probably bitten off more than I can chew with this project, but I am
learning alot.
any help is greatly appreciated.
Thank you,
cogiz