Advertisement
hiddenGem

Game: Card Trick

Mar 18th, 2021 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 8.28 KB | None | 0 0
  1. %% Magic Trick
  2. %{
  3. I'm back! New and improved!
  4. JK I'm still undercooked.
  5. anyways comments for the good of the order are at the end
  6. %}
  7.  
  8. clc, clear
  9. Names = {'Ace ','Two ','Three ', 'Four ', 'Five ', 'Six ', 'Seven ', 'Eight ', 'Nine ', 'Ten ', 'Jack ', 'Queen ', 'King '};
  10. Suits = {'Hearts', 'Diamond', 'Clubs', 'Spades'};
  11.  
  12. q = 1;  % Random Number
  13. while q < 53 % Creates set values of cards
  14.     for column = 1:4
  15.         for row = 1:13    
  16.         Set(q) = strcat(Names(row), Suits(column));      
  17.         q = q + 1;
  18.         row = row + 1;
  19.         end
  20.     end
  21. end
  22.  
  23. Set = reshape(Set, 13,4);   % reshaping the Set array
  24. ran_cards = randperm(52,21);    % Drawing random cards for game
  25.  
  26. for i = 1:length(ran_cards)     % Giving random number card values
  27.     game_cards(i) = Set(ran_cards(i));
  28. end
  29.  
  30. % This  took 90 min to make... moving on...
  31.  
  32. fprintf('\n Choose a card \n')
  33. Set_up = reshape(game_cards, 7,3)   % Shows cards to viewer
  34. % Allocating columns to vectors for first redeal
  35. x1 = Set_up(:,1);
  36. y1 = Set_up(:,2);
  37. z1 = Set_up(:,3);
  38.  
  39. First = input(['Which column is your card in? \n'...
  40.             'Left, Middle, Right \n\n'],'s');     % First shuffle
  41. a1 = strcmp(First,'Left');   % placeholder
  42. b1 = strcmp(First,'Middle');  % placeholder
  43. c1 = strcmp(First,'Right');  % placeholder
  44.  
  45. if  a1 == 1 || b1== 1 ||c1 == 1    % Making sure column is chosen
  46.     fprintf('\n Great! Shuffling... \n\n')
  47. else
  48.     while a1 ~= 1 && b1 ~= 1 && c1 ~= 1
  49.         First = input(['Which column is your card in? \n'...
  50.                         'Left, Middle, Right \n\n'],'s');     % First shuffle
  51.         a1 = strcmp(First,'Left');   % placeholder
  52.         b1 = strcmp(First,'Middle');  % placeholder
  53.         c1 = strcmp(First,'Right');  % placeholder
  54.        
  55.     end
  56.     fprintf('\n Great! Shuffling... \n')
  57. end
  58.  
  59. % That took another 60 min...
  60.  
  61.  switch First
  62.         case 'Left'
  63.         yxz1 = [y1; x1; z1];
  64.             q1 = 1;
  65.             while q1 < 22
  66.                 for row1 = 1:7
  67.                     for column1 = 1:3
  68.                         ReDeal_1(row1,column1) = yxz1(q1);
  69.                         q1 = q1 + 1;
  70.                         column1 = column1 + 1;
  71.                     end
  72.                 end
  73.             end
  74.         case 'Middle'
  75.         xyz1 = [x1; y1; z1];
  76.         q1 = 1;
  77.             while q1 < 22
  78.                 for row1 = 1:7
  79.                     for column1 = 1:3
  80.                         ReDeal_1(row1,column1) = xyz1(q1);
  81.                         q1 = q1 + 1;
  82.                         column1 = column1 + 1;
  83.                     end
  84.                 end
  85.             end
  86.         case 'Right'
  87.         xzy1 = [x1; z1; y1];
  88.         q1 = 1;
  89.             while q1 < 22
  90.                 for row1 = 1:7
  91.                     for column1 = 1:3
  92.                         ReDeal_1(row1,column1) = xzy1(q1);
  93.                         q1 = q1 + 1;
  94.                         column1 = column1 + 1;
  95.                     end
  96.                 end
  97.             end
  98.  end
  99. % Shows first redeal
  100. fprintf('First Redeal\n')
  101. reshape(ReDeal_1, 7,3)
  102.  
  103. % Allocating columns to vectors for second redeal
  104. x2 = ReDeal_1(:,1);
  105. y2 = ReDeal_1(:,2);
  106. z2 = ReDeal_1(:,3);
  107.  
  108. Second = input(['Which column is your card in? \n'...
  109.             'Left, Middle, Right \n\n'],'s');     % Second shuffle
  110. a2 = strcmp(Second,'Left');   % placeholder
  111. b2 = strcmp(Second,'Middle');  % placeholder
  112. c2 = strcmp(Second,'Right');  % placeholder
  113.  
  114. if  a2 == 1 || b2== 1 ||c2 == 1    % Making sure column is chosen
  115.     fprintf('\n Great! Shuffling... \n\n')
  116. else
  117.     while a2 ~= 1 && b2 ~= 1 && c2 ~= 1
  118.         Second = input(['Which column is your card in? \n'...
  119.                         'Left, Middle, Right \n\n'],'s');     % Second shuffle
  120.         a2 = strcmp(Second,'Left');   % placeholder
  121.         b2 = strcmp(Second,'Middle');  % placeholder
  122.         c2 = strcmp(Second,'Right');  % placeholder
  123.        
  124.     end
  125.     fprintf('\n Great! Shuffling... \n')
  126. end
  127.  
  128. switch Second
  129.         case 'Left'
  130.         yxz2 = [y2; x2; z2];
  131.         q2 = 1;
  132.             while q2 < 22
  133.                 for row2 = 1:7
  134.                     for column2 = 1:3
  135.                         ReDeal_2(row2,column2) = yxz2(q2);
  136.                         q2 = q2 + 1;
  137.                         column2 = column2 + 1;
  138.                     end
  139.                 end
  140.             end
  141.         case 'Middle'
  142.         xyz2 = [x2; y2; z2];
  143.         q2 = 1;
  144.             while q2 < 22
  145.                 for row2 = 1:7
  146.                     for column2 = 1:3
  147.                         ReDeal_2(row2,column2) = xyz2(q2);
  148.                         q2 = q2 + 1;
  149.                         column2 = column2 + 1;
  150.                     end
  151.                 end
  152.             end
  153.         case 'Right'
  154.         xzy2 = [x2; z2; y2];
  155.         q2 = 1;
  156.             while q2 < 22
  157.                 for row2 = 1:7
  158.                     for column2 = 1:3
  159.                         ReDeal_2(row2,column2) = xzy2(q2);
  160.                         q2 = q2 + 1;
  161.                         column2 = column2 + 1;
  162.                     end
  163.                 end
  164.             end
  165.  end
  166. % Shows second redeal
  167. fprintf('Second Redeal\n')
  168. reshape(ReDeal_2, 7, 3)
  169.  
  170. % Allocating columns to vectors for third redeal
  171. x3 = ReDeal_2(:,1);
  172. y3 = ReDeal_2(:,2);
  173. z3 = ReDeal_2(:,3);
  174.  
  175. Third = input(['Which column is your card in? \n'...
  176.             'Left, Middle, Right \n\n'],'s');     % Third shuffle
  177. a3 = strcmp(Third,'Left');   % placeholder
  178. b3 = strcmp(Third,'Middle');  % placeholder
  179. c3 = strcmp(Third,'Right');  % placeholder
  180.  
  181. if  a3 == 1 || b3 == 1 ||c3 == 1    % Making sure column is chosen
  182.     fprintf('\n Great! Shuffling... \n\n')
  183. else
  184.     while a3 ~= 1 && b3 ~= 1 && c3 ~= 1
  185.         Third = input(['Which column is your card in? \n'...
  186.                         'Left, Middle, Right \n\n'],'s');     % Third shuffle
  187.         a3 = strcmp(Third,'Left');   % placeholder
  188.         b3 = strcmp(Third,'Middle');  % placeholder
  189.         c3 = strcmp(Third,'Right');  % placeholder
  190.        
  191.     end
  192.     fprintf('\n Great! Shuffling... \n')
  193. end
  194.  
  195. switch Third
  196.         case 'Left'
  197.         yxz3 = [y3; x3; z3];
  198.             q3 = 1;
  199.             while q3 < 22
  200.                 for row3 = 1:7
  201.                     for column3 = 1:3
  202.                         ReDeal_3(row3,column3) = yxz3(q3);
  203.                         q3 = q3 + 1;
  204.                         column3 = column3 + 1;
  205.                     end
  206.                 end
  207.             end
  208.         case 'Middle'
  209.         xyz3 = [x3; y3; z3];
  210.         q3 = 1;
  211.             while q3 < 22
  212.                 for row3 = 1:7
  213.                     for column3 = 1:3
  214.                         ReDeal_3(row3,column3) = xyz3(q3);
  215.                         q3 = q3 + 1;
  216.                         column3 = column3 + 1;
  217.                     end
  218.                 end
  219.             end
  220.         case 'Right'
  221.         xzy3 = [x3; z3; y3];
  222.         q3 = 1;
  223.             while q3 < 22
  224.                 for row3 = 1:7
  225.                     for column3 = 1:3
  226.                         ReDeal_3(row3,column3) = xzy3(q3);
  227.                         q3 = q3 + 1;
  228.                         column3 = column3 + 1;
  229.                     end
  230.                 end
  231.             end
  232. end
  233.  
  234. fprintf('Is This your card?')
  235. ReDeal_3(11)
  236.  
  237. % Total elapsed time : 3 hours 30 min
  238. %{
  239. For best viewing experience
  240.     I recommend undocking command window to
  241.     play through the code.
  242.     I spent way too much time on lines that could be
  243.     ignored from not undocking.
  244.    
  245. IMPORTANT
  246.     Clear the variables after every run.
  247.     Code relies on user input every time.
  248.  
  249. SIDE NOTES 
  250.     Edited code to remove final reshuffle.
  251.         Every card ended up in the middle row instead of
  252.         any of the three rows. Should work the same
  253.     Code is based off card trick using 21 different cards.
  254.         Seven rows of three condensed and redealt 2x.
  255.         Usually ends with a minigame but that extra coding
  256.         would take me a LOT longer.
  257.             So the short version it is. Hope you enjoyed it!
  258.     I know that others could've 'done it faster'
  259.     give me a break though.
  260.         I haven't used MATLAB since last semester,
  261.             and I only used it for Simulink
  262.             so my coding is a little rusty.
  263.    
  264.             A lot rusty.
  265.     My goal is to remake this code to read tarot cards
  266.     one day. Maybe I'll do that over the summer.
  267.     Maybe I'll make the minigame.
  268.    
  269.         I'll probably do neither.
  270. %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement