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

 

Matlab Euchre Manual

The Pack

The standard 52-card pack can be stripped to make a deck of 32 cards A, K, Q, J, 10, and 9 of each

Object of the Game

The goal is to win at least three tricks. If the side that fixed the trump fails to get three tricks, it is said to be “euchred.” Winning all five tricks gets extra points.

Rank of Cards

The highest trump is the jack of the trump suit.  The second-highest trump is the jack of the other suit of the same color.  The remaining trumps, and also the plain suits, rank as follows: A (high), K, Q, J, 10, 9.

Card Values/Scoring

The following shows all scoring situations:

Partnership making trump wins 3 or 4 tricks – 1 point
Partnership making trump wins 5 tricks – 2 points
Lone hand wins 3 or 4 tricks – 1 point
Lone hand wins 5 tricks – 4 points
Partnership or lone hand is euchred, opponents score 2 points

The first player or partnership to score 5, 7 or 10 points, as agreed beforehand, wins the game. In the 5-point game, a side is said to be “at the bridge” when it has scored 4 and the opponents have scored 2 or less.

Keeping Score with Low Card Markers

An elegant and widespread method of keeping score is with cards lower than those used in play. When game is 5 points, each side uses a three-spot and a four-spot as markers. To indicate a score of 1, the four is placed face down on the three, with one pip left exposed. For a score of 2, the three is placed face down on the four, with two pips left exposed. For a score of 3, the three is placed face up on the four. For a score of 4, the four is placed face up on the three.

 

 

The Deal

The cards are dealt clockwise, to the left, beginning with the player to the left of the dealer. Each player receives five cards. The dealer may give a round of three at a time, then a round of two at a time, or may give two, then three; but the dealer must adhere to whichever distribution plan he begins with. After the first deal, the deal passes to the player on the dealer’s left.

On completing the deal, the dealer places the rest of the pack in the center of the table and turns the top card face up. Should the card turned up be accepted as trump by any player, the dealer has the right to exchange the turned up card for another card in his hand. In practice, the dealer does not take the turned up card into his hand, but leaves it on the pack until it is played; the dealer signifies this exchange by placing his discard face down underneath the pack.

Making the Trump

Beginning with the player to the left of the dealer, each player passes or accepts the turn-up as trump. An opponent of the dealer accepts by saying “I order it up.” The partner of the dealer accepts by saying, “I assist.” The dealer accepts by making his discard, called “taking it up.”

The dealer signifies refusal of the turn-up by removing the card from the top and placing it (face up) partially underneath the pack; this is called “turning it down.”

If all four players pass in the first round, each player in turn, starting with the player to the dealer’s left, has the option of passing again or of naming the trump suit. The rejected suit may not be named. Declaring the other suit of the same color as the reject is called “making it next”; declaring a suit of opposite color is called “crossing it.”

If all four players pass in the second round, the cards are gathered and shuffled, and the next dealer deals. Once the trump is fixed, either by acceptance of the turn-up or by the naming of another suit, the turn-up is rejected, the bidding ends and play begins.

Playing Alone

If the player who fixes the trump suit believes it will be to his side’s advantage to play without the help of his partner’s cards, the player exercises this option by declaring “alone” distinctly at the time of making the trump. This player’s partner then turns his cards face down and does not participate in the play.

The Play

The opening lead is made by the player to the dealer’s left, or if this player’s partner is playing alone, it is made by the player across from the dealer. If he can, each player must follow suit to a lead. If unable to follow suit, the player may trump or discard any card. A trick is won by the highest card of the suit led, or, if it contains trumps, by the highest trump. The winner of a trick leads next.

Matlab Euchre Introduction

Welcome to Matlab Euchre!  Euchre is a team game where two teams compete to gain points utilizing cards to beat the other team.  Euchre is a simple game with many different rules that can vary by region of the United States that the game is played in.  The game is broken up into rounds where the players will compete to take the majority of the “tricks” thus guaranteeing them points.

 

Euchre is a great 4 player party game that doesn’t require much skill to play but is a ton of fun!