Final Program

clear;

clc;

set(0,’DefaultFigureWindowStyle’,’docked’)

load(‘CardDeck.mat’);

 

% =================================================

% To get a card, use the following command:%

% euchre_deck[(number * 4) + suit – 3]

% E.X.:

% euchre_Deck[(nine * 4) + heart – 3]

% =================================================

 

% Order of cards

ace = 1;

nine = 2;

ten = 3;

jack = 4;

queen = 5;

king = 6;

 

% Order of suits

club = 0;

spade = 1;

heart = 2;

diamond = 3;

 

score1 = 0;

score2 = 0;

 

while(1)

% Using the BlueDeck (Ace -> 2 -> King)

shuffled_deck = randperm(24);

hand1 = shuffled_deck(1:5);

hand2 = shuffled_deck(6:10);

hand3 = shuffled_deck(11:15);

hand4 = shuffled_deck(16:20);

kitty = shuffled_deck(21:24);

 

fprintf(‘Showing top of kitty.\n’)

subplot(2,1,1)

imshow([BlueDeck{get_card_by_id(kitty(1))}])

 

trump = -1;

 

while (1)

% Player 1

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand1)}]);

prompt = ‘Do you want to call? 0 for no, 1 for yes: ‘;

call = input(prompt);

while (call ~= 0 && call ~= 1)

fprintf(‘0 for no, 1 for yes\n’)

call = input(prompt);

end

if call == 1

trump = get_suit(get_card_by_id(kitty(1)));

break

end

% Player 2

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand2)}]);

prompt = ‘Do you want to call? 0 for no, 1 for yes: ‘;

call = input(prompt);

while (call ~= 0 && call ~= 1)

fprintf(‘0 for no, 1 for yes\n’)

call = input(prompt);

end

if call == 1

trump = get_suit(get_card_by_id(kitty(1)));

break

end

% Player 3

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand3)}]);

prompt = ‘Do you want to call? 0 for no, 1 for yes: ‘;

call = input(prompt);

while (call ~= 0 && call ~= 1)

fprintf(‘0 for no, 1 for yes\n’)

call = input(prompt);

end

if call == 1

trump = get_suit(get_card_by_id(kitty(1)));

break

end

% Player 4

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand4)}]);

prompt = ‘Do you want to call? 0 for no, 1 for yes: ‘;

call = input(prompt);

while (call ~= 0 && call ~= 1)

fprintf(‘0 for no, 1 for yes\n’)

call = input(prompt);

end

if call == 1

trump = get_suit(get_card_by_id(kitty(1)));

break

end

 

delete(subplot(2,1,1))

 

% Player 1

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand1)}]);

prompt = ‘Do you want to call? 0 is clubs, 1 is spades, 2 is hearts, 3 is diamonds, -1 for no: ‘;

call = input(prompt);

while (call < -1 || call > 3 || call == get_suit(get_card_by_id(kitty(1))))

if call == get_suit(get_card_by_id(kitty(1)))

fprintf(‘You can not call the suit that was already flipped\n’)

else

fprintf(‘-1 for no, 0 for clubs, 1 for spades, 2 for hearts, 3 for diamonds\n’)

end

call = input(prompt);

end

if call ~= -1

trump = call;

break

end

% Player 2

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand2)}]);

prompt = ‘Do you want to call? 0 is clubs, 1 is spades, 2 is hearts, 3 is diamonds, -1 for no: ‘;

call = input(prompt);

while (call < -1 || call > 3 || call == get_suit(get_card_by_id(kitty(1))))

if call == get_suit(get_card_by_id(kitty(1)))

fprintf(‘You can not call the suit that was already flipped\n’)

else

fprintf(‘-1 for no, 0 for clubs, 1 for spades, 2 for hearts, 3 for diamonds\n’)

end

call = input(prompt);

end

if call ~= -1

trump = call;

break

end

% Player 3

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand3)}]);

prompt = ‘Do you want to call? 0 is clubs, 1 is spades, 2 is hearts, 3 is diamonds, -1 for no: ‘;

call = input(prompt);

while (call < -1 || call > 3 || call == get_suit(get_card_by_id(kitty(1))))

if call == get_suit(get_card_by_id(kitty(1)))

fprintf(‘You can not call the suit that was already flipped\n’)

else

fprintf(‘-1 for no, 0 for clubs, 1 for spades, 2 for hearts, 3 for diamonds\n’)

end

call = input(prompt);

end

if call ~= -1

trump = call;

break

end

% Player 4

subplot(2,1,2)

imshow([BlueDeck{convert_deck_to_cards(hand4)}]);

while(1)

prompt = ‘You must call. 0 is clubs, 1 is spades, 2 is hearts, 3 is diamonds: ‘;

call = input(prompt);

if call == get_suit(get_card_by_id(kitty(1)))

fprintf(‘You can not call the suit that was already flipped\n’)

elseif call < 4 && call > -1

trump = call;

break

else

fprintf(‘You must call; stick the dealer\n’)

end

end

 

break

end

 

delete(subplot(2, 1, 1));

 

cards_played = [];

tricks_t1 = 0;

tricks_t2 = 0;

 

[trick1, hand1, hand2, hand3, hand4] = do_trick(hand1, hand2, hand3, hand4, trump);

[trick2, hand1, hand2, hand3, hand4] = do_trick(hand1, hand2, hand3, hand4, trump);

[trick3, hand1, hand2, hand3, hand4] = do_trick(hand1, hand2, hand3, hand4, trump);

[trick4, hand1, hand2, hand3, hand4] = do_trick(hand1, hand2, hand3, hand4, trump);

[trick5, hand1, hand2, hand3, hand4] = do_trick(hand1, hand2, hand3, hand4, trump);

 

if who_won_trick(trick1, trump) == 0

tricks_t1 = tricks_t1 + 1;

else

tricks_t2 = tricks_t2 + 1;

end

if who_won_trick(trick2, trump) == 0

tricks_t1 = tricks_t1 + 1;

else

tricks_t2 = tricks_t2 + 1;

end

if who_won_trick(trick3, trump) == 0

tricks_t1 = tricks_t1 + 1;

else

tricks_t2 = tricks_t2 + 1;

end

if who_won_trick(trick4, trump) == 0

tricks_t1 = tricks_t1 + 1;

else

tricks_t2 = tricks_t2 + 1;

end

if who_won_trick(trick5, trump) == 0

tricks_t1 = tricks_t1 + 1;

else

tricks_t2 = tricks_t2 + 1;

end

 

if tricks_t1 == 5

fprintf(‘Team 1 took 5 tricks! 2 points.\n’);

score1 = score1 + 2;

elseif tricks_t2 == 5

fprintf(‘Team 2 took 5 tricks!  2 points.\n’);

score2 = score2 + 2;

elseif tricks_t1 > 2

fprintf(‘Team 1 took 3 or more tricks, 1 point!\n’);

score1 = score1 + 1;

elseif tricks_t2 > 2

fprintf(‘Team 2 took 3 or more tricks, 1 point!\n’);

score2 = score2 + 1;

else

fprintf(‘Error.\n’);

end

 

if score1 >= 10

fprintf(‘Team 1 wins!’)

break

elseif score2 >= 10

fprintf(‘Team 2 wins!’)

break

end

end

 

function team=who_won_trick(cards_played, trump)

team = 0;

winning = -1;

starting_suit = get_suit(cards_played(1));

 

for i=1:4

card = cards_played(i);

card_suit = get_suit(card);

 

if card_suit == trump

if card <= 4 && card >= 1

if winning < 11

winning = 11;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 7 && card >= 4

if winning < 7

winning = 7;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 11 && card >= 8

if winning < 8

winning = 8;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 15 && card >= 12

if winning < 13

winning = 13;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 19 && card >= 16

if winning < 9

winning = 9;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 23 && card >= 20

if winning < 10

winning = 10;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

end

elseif card_suit == starting_suit

if card <= 4 && card >= 1

if winning < 6

winning = 6;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 7 && card >= 4

if winning < 1

winning = 1;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 11 && card >= 8

if winning < 2

winning = 2;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 15 && card >= 12

if trump == 0 && card_suit == 1

if winning < 12

winning = 12;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif trump == 1 && card_suit == 0

if winning < 12

winning = 12;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif trump == 2 && card_suit == 3

if winning < 12

winning = 12;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif trump == 3 && card_suit == 2

if winning < 12

winning = 12;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

end

if winning < 8

winning = 8;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 19 && card >= 16

if winning < 4

winning = 4;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

elseif card <= 23 && card >= 20

if winning < 5

winning = 5;

if rem(i, 2) == 1

team = 0;

else

team = 1;

end

end

end

end

end

end

 

function [cards_played, hand1, hand2, hand3, hand4]=do_trick(hand1, hand2, hand3, hand4, trump)

load(‘CardDeck.mat’);

% Player 1 play

card = prompt_player(hand1, 1);

cards_played = [card];

hand1 = hand1(find(hand1~=card));

subplot(2, 1, 1);

imshow([BlueDeck{convert_deck_to_cards(cards_played)}]);

% Player 2 play

card = prompt_player(hand2, 2);

while (check_valid(card, hand2, trump, cards_played) == 1)

fprintf(‘You can not play that card!\n’)

card = prompt_player(hand2,2);

end

cards_played = [cards_played card];

hand2 = hand2(find(hand2~=card));

subplot(2, 1, 1);

imshow([BlueDeck{convert_deck_to_cards(cards_played)}]);

% Player 3 play

card = prompt_player(hand3, 3);

while (check_valid(card, hand3, trump, cards_played) == 1)

fprintf(‘You can not play that card!\n’)

card = prompt_player(hand3,3);

end

cards_played = [cards_played card];

hand3 = hand3(find(hand3~=card));

subplot(2, 1, 1);

imshow([BlueDeck{convert_deck_to_cards(cards_played)}]);

% Player 4 play

card = prompt_player(hand4, 4);

while (check_valid(card, hand4, trump, cards_played) == 1)

fprintf(‘You can not play that card!\n’)

card = prompt_player(hand4,4);

end

cards_played = [cards_played card];

hand4 = hand4(find(hand4~=card));

subplot(2, 1, 1);

imshow([BlueDeck{convert_deck_to_cards(cards_played)}]);

 

delete(subplot(2, 1, 1));

end

 

function result=check_valid(card_to_play, hand, trump, cards_played)

suit1 = get_suit(card_to_play);

suit2 = get_suit(cards_played(1));

 

result = 0;

 

if suit1 ~= trump

if suit1 ~= suit2

for i=1:length(hand)

if suit2 == get_suit(hand(i))

result = 1;

return

end

end

end

else

% Account for jacks of opposite color

if suit1 ~= suit2

for i=1:length(hand)

if suit2 == get_suit(hand(i))

result = 1;

return

end

for i=1:length(hand)

if trump == 0

if hand(i) == 14

result = 1;

return

end

elseif trump == 1

if hand(i) == 13

result = 1;

return

end

elseif trump == 2

if hand(i) == 16

result = 1;

return

end

elseif trump == 3

if hand(i) == 15

result = 1;

return

end

end

end

end

end

end

end

 

function result=prompt_player(hand, player)

load(‘CardDeck.mat’);

subplot(2, 1, 2);

imshow([BlueDeck{convert_deck_to_cards(hand)}]);

while(1)

prompt = sprintf(‘Player %d: What card would you like to play? 1-%d: ‘, player, length(hand));

selection = input(prompt);

 

good = 1;

 

if selection > length(hand)

fprintf(‘Needs to be between 1 and %d\n’, length(hand))

good = 0;

end

 

if selection < 1

fprintf(‘Needs to be between 1 and 5\n’)

good = 0;

end

 

if good == 1

result = hand(selection);

break

end

end

end

 

function suit=get_suit(card_id)

card_id = card_id – 1;

if rem(card_id, 4) == 0

suit = 0;

elseif rem(card_id, 4) == 1

suit = 1;

elseif rem(card_id, 4) == 2

suit = 2;

else

suit = 3;

end

end

 

function suit_name=suit_to_human(suit)

if suit == 0

suit_name = ‘clubs’;

elseif suit == 1

suit_name = ‘spades’;

elseif suit == 2

suit_name = ‘diamonds’;

elseif suit == 3

suit_name = ‘hearts’;

end

end

 

function card=get_card_by_name_suit(number, suit)

% Using the BlueDeck (Ace -> 2 -> King)

euchre_deck = [1, 2, 3, 4, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52];

card = euchre_deck((number * 4) + suit – 3);

end

function card=get_card_by_id(id)

euchre_deck = [1, 2, 3, 4, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52];

card = euchre_deck(id);

end

 

function card=convert_deck_to_cards(hand)

euchre_deck = [1, 2, 3, 4, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52];

card = euchre_deck(hand);

end

 

Leave a Reply

Your email address will not be published. Required fields are marked *