Would like to create a simple blackjack game but can't assign card values and images to card number.

5 visualizzazioni (ultimi 30 giorni)
Hey there, I am creating a blackjack game. I would really appreciate some advice and teaching on how to assign images to the card numbers while assigning card values to the card numbers. Its really racking my brain.
Here is my code so far:
% A deck of cards contains 52 cards
% Shuffle the deck of cards
shuffledDeck = randperm (52);
% Set values for all the cards
cardValues = [1:10 10 10 10 1:10 10 10 10 1:10 10 10 10 1:10 10 10 10];
% Deal cards to player and dealer
player_hand = shuffledDeck(1:2:3);
dealer_hand = [shuffledDeck(2) shuffledDeck(4)];
disp(player_hand);
disp(dealer_hand);
I tried creating a function where i assign the images to each card number from 1 to 52, and so if i used the function on the hands of both the player and dealer, it would open up the image corresponding to the number the player/dealer was assigned when the deck was shuffled
function [ ] = card(hand)
1 == imread('KC.png');
2 == imread('QC.png');
3 == imread('JC.png');
4 == imread('10C.png');
5 == imread('9C.png');
6 == imread('8C.png');
7 == imread('7C.png');
8 == imread('6C.png');
9 == imread('5C.png');
10 == imread('4C.png');
11 == imread('3C.png');
12 == imread('2C.png');
13 == imread('AC.png');
14 == imread('KD.png');
15 == imread('QD.png');
16 == imread('JD.png');
17 == imread('10D.png');
18 == imread('9D.png');
19 == imread('8D.png');
20 == imread('7D.png');
21 == imread('6D.png');
22 == imread('5D.png');
23 == imread('4D.png');
24 == imread('3D.png');
25 == imread('2D.png');
26 == imread('AD.png');
27 == imread('KH.png');
28 == imread('QH.png');
29 == imread('JH.png');
30 == imread('10H.png');
31 == imread('9H.png');
32 == imread('8H.png');
33 == imread('7H.png');
34 == imread('6H.png');
35 == imread('5H.png');
36 == imread('4H.png');
37 == imread('3H.png');
38 == imread('2H.png');
39 == imread('AH.png');
40 == imread('KS.png');
41 == imread('QS.png');
42 == imread('JS.png');
43 == imread('10S.png');
44 == imread('9S.png');
45 == imread('8S.png');
46 == imread('7S.png');
47 == imread('6S.png');
48 == imread('5S.png');
49 == imread('4S.png');
50 == imread('3S.png');
51 == imread('2S.png');
52 == imread('AS.png');
for i = 1:52
if hand == i
imshow(i);
end
end
end
% this function code doesn't work coz its wrong
I would really appreciate someone teaching me and showing me if it works.
  2 Commenti
Jeffrey Keough
Jeffrey Keough il 28 Apr 2021
Could you please elaborate?
Do you mean, I would create a (4X13) array but instead of numbers, i put in the image file?
Is this what you mean?
deck = [ image1.png, image2.png, image3.png, ......]
so to get the card i want, i just say deck(1)?
I'm sorry, but this is all i can think of.

Accedi per commentare.

Risposta accettata

DGM
DGM il 28 Apr 2021
Modificato: DGM il 28 Apr 2021
I should note first off that the conventions you're using to associate the faces with an overarching linear index for the whole deck varies. On one hand, your card value array cycles ascending from ace to king:
cardValues = [1:10 10 10 10 1:10 10 10 10 1:10 10 10 10 1:10 10 10 10];
But your image display function cycles descending from king to ace. You'll have to pick one convention or the other.
If you've figured out some convention for how the faces and suits correspond to a linear index of 1:52, then you can sort out your image handling function something like this. (I chose to keep the descending arrangement)
function [ ] = showcards(hand)
% don't waste a bunch of time and memory reading images that aren't needed
face = {'K','Q','J','10','9','8','7','6','5','4','3','2','A'};
suit = {'C','D','H','S'};
handpict = [];
for c=1:numel(hand)
thiscard = hand(c);
% programmatically build the filename for this card
filename = [face{mod(thiscard-1,13)+1} suit{ceil(thiscard/13)} '.png'];
% edge-cat the images into one as a simple way of viewing multiple cards
% this relies on the images having the same height, so beware of that
handpict = cat(2,handpict,imread(filename));
end
imshow(handpict);
end
If you wanted to display the dealer and player hand, you could do that as simply as using subplot:
subplot(2,1,1); showcards(dealer_hand);
subplot(2,1,2); showcards(player_hand);
  2 Commenti
DGM
DGM il 28 Apr 2021
Modificato: DGM il 28 Apr 2021
If you prefer to touch the disk less often, you could combine the approaches something like this:
function [ ] = showcards(hand)
% build a persistent cell array of all card images
persistent allcardpicts
if isempty(allcardpicts)
face = {'K','Q','J','10','9','8','7','6','5','4','3','2','A'};
suit = {'C','D','H','S'};
allcardpicts = cell(52,1);
for c=1:52
thiscard = hand(c);
% programmatically build the filename for this card
filename = [face{mod(thiscard-1,13)+1} suit{ceil(thiscard/13)} '.png']
% concatenate the images into a cell array
allcardpicts{c} = imread(filename);
end
end
handpict = [];
for c=1:numel(hand)
thiscard = hand(c);
handpict = cat(2,handpict,allcardpicts{c});
end
imshow(handpict);
end
It would mean that all card images are in memory, but it would only have to read them once.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Card games in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by