I am trying to simulate a blackjack game
이전 댓글 표시
initially I wrote a function that takes as input the number of the card (in the sense of the columns labeled # in the table), and returns the value of the card according to the table. If the number of the card corresponds to an Ace, the function should return the number 1. the command card_value(42) should output 10 and so on.

function hand = card_value(card_number)
%card_number = [1:52];
%card_value = [1:13];
if card_number > 40
hand = 10;
end
if (card_number < 11) && (card_number > 1)
hand = card_number;
end
if (card_number < 21) && (card_number > 11)
hand = card_number - 10;
end
if (card_number < 31) && (card_number > 21)
hand = card_number - 20;
end
if (card_number < 41) && (card_number > 31)
hand = card_number - 30;
end
end
I would now like account for Ace being counted as a 1 or 11. If I would like to write a function which simulates a dealers hand(two cards). I would like a function that takes as input the vector of possbible outcomes (sum if Ace =1, sum if Ace =11) and displays 999 if its a bust, the hand if the sum is >=17 and <= 21, or -999 if the dealer would have to draw (sum of hand is <17)
my code so far is below
function p = check_possibilities(cards)
cards = [5 11]; %example hand
for i = 1:2
card = cards(i);
if card_value(card) == 1
hand = hand +1;
handbig = handbig + 11;
elseif card_value(card) == 11
hand = hand +1;
handbig = handbig +11;
elseif card_value(card) == 21
hand = hand +1;
handbig = handbig +11;
elseif card_value(card) == 31
hand = hand +1;
handbig = handbig +11;
else
cardvalue = card_value(card);
hand = hand + cardvalue;
handbig = handbig +cardvalue;
end
end
if hand == handbig
disp(hand)
else
disp(hand, hangbig)
end
if hand && handbig > 21
disp(999);
end
if (17 <= handbig) && (handbig<= 21)
disp(hand)
end
if hand && handbig < 17
disp(-999)
end
end
function hand = card_value(card_number)
%card_number = [1:52];
%card_value = [1:13];
if card_number > 40
hand = 10;
end
if (card_number < 11) && (card_number > 1)
hand = card_number;
end
if (card_number < 21) && (card_number > 11)
hand = card_number - 10;
end
if (card_number < 31) && (card_number > 21)
hand = card_number - 20;
end
if (card_number < 41) && (card_number > 31)
hand = card_number - 30;
end
end
im getting the error that hand is an unrecognizable function. I am new to matlab, plz help!
댓글 수: 4
David Hill
2022년 9월 9일
Take a look at the structure of my blackjack game. hand, playingdeck and card classdef
Geoff Hayes
2022년 9월 12일
@Jacob Wagner - I think the issue (given your comment that hand is an unrecoginizable function is originating from this code
function p = check_possibilities(cards)
cards = [5 11]; %example hand
for i = 1:2
card = cards(i);
if card_value(card) == 1
hand = hand +1;
handbig = handbig + 11;
elseif card_value(card) == 11
hand = hand +1;
handbig = handbig +11;
Note that the line
hand = hand +1;
is trying to use hand before it has been defined. Perhaps you think that this exists because of the signature for card_value
function hand = card_value(card_number)
which returns the the card value in the output parameter named hand but because you haven't assigned the output from this function to any local variable, then it doesn not exist outside of the expression of the if statement. I think that you might want to do something like the following instead
function p = check_possibilities(cards)
cards = [5 11]; %example hand
hand = 0; % <----- create local variable that will be the sum of the two cards
for i = 1:2
card = cards(i);
cardvalue = card_value(card); % <----- create a local variable for the card value
if cardvalue == 1 || cardvalue == 11 || cardvalue == 21 || cardvalue == 31
hand = hand +1;
handbig = handbig + 11;
else
hand = hand + cardvalue;
handbig = handbig +cardvalue;
end
% etc.
end
Note that since the bodies for each of the if/elseif are identical, you can combine them into one if expression for all.
(You will also want to correct hangbig which is used further in your code...I suspect that this should be handbig.)
Walter Roberson
2022년 9월 12일
Remember that you can have up to 4 aces in your hand, each one of which can be treated as 1 or 11 as desired (though in practice, if you treat more than one of them as 11 then you lose.)
Walter Roberson
2022년 9월 12일
편집: Walter Roberson
2022년 9월 12일
In the more general case of card games (such as Poker), there might be multiple ways to intepret a hand. In such cases, it might be worth considering creating a list of all ways to interpret a hand so-far, and then as each additional card is dealt, examine each of the interpretations in turn in order to determine all of the new interpretations.
In theory, for example, you could have a rule that "3s are wild if you have a 4 and a 7 in the hand.". If you had 4, 5, 7, 8 in the hand, and add a 3, that should generate interpretations {3 4 5 7 8 -> just high card(8)} and {3->4 4 5 7 8 -> pair of 4s} and {3->5 4 5 7 8 -> pair of 5s} and {3->6 4 5 7 8 -> 5 card flush} and {3->7 4 5 7 8 -> pair of 7s} and {3->8 4 5 7 8 -> pair of 8's} and {3->A 4 5 7 8 -> high card A} and probably even {3->10 4 5 7 8 -> high card 10} and the other possible interpretations for the 3.
In the case of Blackjack, this would mean that normally hands would have one interpreation, 2 to 10 intepreted as themselves, J Q K interpreted as 10, and that hands with an A in them would add the interpretation A->1 to the list and also A->11 to the list. Then you win 21 if the sum of any of the interpretations is 21.
.. as opposed to having branch trees that need to handle each card separately.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Card games에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!