I played cards (poorly) over Christmas, and thought a good way to break through the end-of-year boredom at work would be to write a quick MATLAB script that dealt a given number of cards from a randomly shuffled deck to a given number of players.
I got to use the eval command again, which is always exciting.
% Randomly shuffle a deck of cards
% Distribute to a given number of players (user input)
% Display the hands for each player
% Assign a number for each card
% 1-13 are for Spades (in order)
% 14-26 are for Clubs
% 27-39 are for Diamonds
% 40-52 are for Hearts
card_names{1} = '2 of Spades';
card_names{2} = '3 of Spades';
card_names{3} = '4 of Spades';
card_names{4} = '5 of Spades';
card_names{5} = '6 of Spades';
card_names{6} = '7 of Spades';
card_names{7} = '8 of Spades';
card_names{8} = '9 of Spades';
card_names{9} = '10 of Spades';
card_names{10} = 'Jack of Spades';
card_names{11} = 'Queen of Spades';
card_names{12} = 'King of Spades';
card_names{13} = 'Ace of Spades';
card_names{14} = '2 of Clubs';
card_names{15} = '3 of Clubs';
card_names{16} = '4 of Clubs';
card_names{17} = '5 of Clubs';
card_names{18} = '6 of Clubs';
card_names{19} = '7 of Clubs';
card_names{20} = '8 of Clubs';
card_names{21} = '9 of Clubs';
card_names{22} = '10 of Clubs';
card_names{23} = 'Jack of Clubs';
card_names{24} = 'Queen of Clubs';
card_names{25} = 'King of Clubs';
card_names{26} = 'Ace of Clubs';
card_names{27} = '2 of Diamonds';
card_names{28} = '3 of Diamonds';
card_names{29} = '4 of Diamonds';
card_names{30} = '5 of Diamonds';
card_names{31} = '6 of Diamonds';
card_names{32} = '7 of Diamonds';
card_names{33} = '8 of Diamonds';
card_names{34} = '9 of Diamonds';
card_names{35} = '10 of Diamonds';
card_names{36} = 'Jack of Diamonds';
card_names{37} = 'Queen of Diamonds';
card_names{38} = 'King of Diamonds';
card_names{39} = 'Ace of Diamonds';
card_names{40} = '2 of Hearts';
card_names{41} = '3 of Hearts';
card_names{42} = '4 of Hearts';
card_names{43} = '5 of Hearts';
card_names{44} = '6 of Hearts';
card_names{45} = '7 of Hearts';
card_names{46} = '8 of Hearts';
card_names{47} = '9 of Hearts';
card_names{48} = '10 of Hearts';
card_names{49} = 'Jack of Hearts';
card_names{50} = 'Queen of Hearts';
card_names{51} = 'King of Hearts';
card_names{52} = 'Ace of Hearts';
% Shuffle the deck
shuffled_deck = randperm(52);
% Ask the user for # of players and # of cards per player
clc
num_players = input('How many players? Enter here: ');
num_cards = input('How many cards per player? Enter here: ');
disp('----------------------')
% Create empty player hand arrays
for k=1:1:num_players
plyr_hnd_cmd_str = ['player_hand_',num2str(k), '= [];'];
eval(plyr_hnd_cmd_str);
end
% Deal cards out to the players
total_cards_to_deal = num_players * num_cards;
i=1;
while i < total_cards_to_deal
for j=1:1:num_players
deal_card_cmd_str = ['player_hand_', num2str(j), ' = [player_hand_',num2str(j), ', shuffled_deck(i)];'];
eval(deal_card_cmd_str);
i = i+1;
end
end
% Display each player's hand
for k=1:1:num_players
disp_plyr_str = ['Player ', num2str(k), '''s hand: '];
disp(disp_plyr_str)
for m=1:1:num_cards
card_name_cmd_str = ['card_name_str = card_names{player_hand_', num2str(k), '(m)};'];
eval(card_name_cmd_str)
disp(card_name_str)
end
disp('----------------------')
end