import os import matplotlib.pyplot as plt import numpy as np import random as rnd from time import time def show_card (ax,s=100,v=1,delay=1.2): suit = suit_names[s//100 -1] fname = dir+str(v)+'-'+suit+'.jpg' im = plt.imread(fname) img = ax.imshow(im) plt.axis('off') plt.draw() plt.pause(delay) plt.ion() plt.show() def show_all_cards (): # shows all cards in jpgs subdirectory for fname in os.listdir(dir): fname = os.path.join(dir, fname) im = plt.imread(fname) img = ax.imshow(im) plt.axis('off') plt.draw() plt.pause(0.001) def shuffle_deck (ax, deck, n_times=2): # create and shuffle n_times a deck of cards # start is the staring number for playing the sequence # if start < 0 then enter explanation mode and show only # the key cards. # create ordered deck n = 0 deck = np.empty(52,dtype=np.int32) # 0..51 for s in range(4): for i in range(1,14): code = (s+1)*100 +i # card code = {100..400} + number deck[n] = code # array of short int n = n + 1 # shuffle n_times the length of deck #print(' input int randomizer') #seed = input() #rnd.seed = seed for iter in range(n_times*len(deck)): r1 = rnd.randrange(0,len(deck)) r2 = rnd.randrange(0,len(deck)) tmp = deck [r1] # swap r1 <-> r2 cards deck[r1] = deck[r2] deck[r2] = tmp return(deck) def display_and_play (ax, deck, start = 9): # print the deck # for k in deck: print(k, k//100, k%100) # if start < 0 then count starts with |start| # and only key cards are drawn i = 0 key = abs(start) # the starting card = 1st key for card in deck: i = i + 1 # number of card in the deck v = card%100 # decipher card value if (start < 0): # show blanks unless a key name = 'blank' if (start > 0 or i == key): # show all cards' jpg's s = card//100 -1 # index, suit unimportant exc. in plots name = str(v)+'-'+suit_names[s] # if i-th card is a key card, update the key (target) if (i == key): last_key = key last_key_card = name if (v <= 10): key = key + v else: key = key + 4 # 6 lines, 10 panels in each ax = fig.add_subplot(6, 10, i) plt.axis('off') im = plt.imread(dir+name+'.jpg') img = ax.imshow(im) # plt.draw() #plt.pause(0.001) plt.axis('off') plt.show() #print('end of show_all_'); iff = ''; input(iff) plt.pause(2.) return(ax, last_key, last_key_card) #__________________________________________________________________ # # main program # # creates and shows a shuffled deck of cards # suites: 100 = spades, 200 = hearts, 300 = diamonds, 400 = clubs # card numbers: 1 = A, 2...10, 11=J, 12=Q, 13=K. #__________________________________________________________________ # prepare graphics window and define constants dir = 'jpgs/' suit_names = ('spades','hearts','diamonds','clubs') deck = ax = 0 # create and shuffle a deck of 52 cards deck = shuffle_deck (ax, deck, 4) # prepare the plot area without axes fig = plt.figure(figsize=(8,7.5)) fig.subplots_adjust (hspace=0.1, wspace=0.1) plt.axis('off') plt.title('shuffled deck of 52 cards') plt.ion() # show the whole shuffled deck ax, last_key, card = display_and_play (ax, deck, 2) starting_num = range(1,11) # 1..10 for start in starting_num: ax, last_key, card = display_and_play (ax, deck, -start) print('starting @',start,' the last card (',last_key,') is: ',card)