This site requires JavaScript, please enable it in your browser!
Greenfoot back
drummerboy619
drummerboy619 wrote ...

2015/3/16

BlackJack

drummerboy619 drummerboy619

2015/3/16

#
I am creating black jack and I need to input an array list to do something in this project can someone please help I am in desperate need.
danpost danpost

2015/3/16

#
drummerboy619 wrote...
I am creating black jack and I need to input an array list to do something in this project can someone please help I am in desperate need.
'import' -- how? 'list' -- what kind? 'need' -- are you sure? Please be specific as to what you are trying to accomplish. Being vague tends to make the solution evade you for much longer than necessary. Show what code you have tried. That will give an idea of what you are trying to do as well and fixing code is much easier than producing code that may not be what you really need.
drummerboy619 drummerboy619

2015/3/16

#
I don't have any code yet? I am stuck on how to do the whole array list for this project. I need to create the array list to shuffle the cards and give me a random card when I click deal or hit.
danpost danpost

2015/3/16

#
drummerboy619 wrote...
I don't have any code yet? I am stuck on how to do the whole array list for this project. I need to create the array list to shuffle the cards and give me a random card when I click deal or hit.
Do you have a Card class yet?
drummerboy619 drummerboy619

2015/3/18

#
yes
danpost danpost

2015/3/18

#
You should have a method like 'createDeck', which clears the world of all cards, initializes (or re-initializes) the deck array and creates a new set of cards, placing them within the array. The cards can be placed in the array in order and then shuffled afterwards. Use the 'java.util.Collections.shuffle' method to shuffle. For example:
1
2
3
Card[] deck = new Card[52];
for (int n=0; n<52; n++) deck[n] = new Card(n);
java.util.Collections.shuffle(java.util.Arrays.asList(deck));
The last line shuffles the elements of your array list.
drummerboy619 drummerboy619

2015/3/18

#
So when I was doing this I originally made a math.random to select a number and then it selects that card that goes with that random number so should I take that out and then inout what you just gave me?
danpost danpost

2015/3/18

#
drummerboy619 wrote...
So when I was doing this I originally made a math.random to select a number and then it selects that card that goes with that random number so should I take that out and then inout what you just gave me?
That -- you could do. However. the names of the fields may not coincide with what you have and the Card class and constructor might need to be adjusted so it can be used with this code. Since you did not supply your Card class, a detailed explanation of what needs done cannot be given.
drummerboy619 drummerboy619

2015/3/18

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Card here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Card extends Actor
{
    public int cardValue;
    /**
     * Act - do whatever the Card wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        
    
     
    public void getCard()
    {
        
           int random = (int) (Math.random() * 52 + 2);
           String cardName = cardLookUp(random);
           setImage(new GreenfootImage(cardName));
            
             
        
    }
     
    public int getCardValue()
    {
        return cardValue;
    }
     
    public String cardLookUp(int value)
    {
        
        String cardName = "I don't know";
        switch (value)
        {
            case 2: cardName = "clubace.png";
                    cardValue = 11;
                    break;
            case 3: cardName = "club2.png";
                    cardValue = 2;
                    break;
            case 4: cardName = "club3.png";
                    cardValue = 3;
                    break;
            case 5: cardName = "club4.png";
                    cardValue = 4;
                    break;
            case 6: cardName = "club5.png";
                    cardValue = 5;
                    break;
            case 7: cardName = "club6.png";
                    cardValue = 6;
                    break;
            case 8: cardName = "club7.png";
                    cardValue = 7;
                    break;
            case 9: cardName = "club8.png";
                    cardValue = 8;
                    break;
            case 10: cardName = "club9.png";
                    cardValue = 9;
                    break;
            case 11: cardName = "club10.png";
                    cardValue = 10;
                    break;
            case 12: cardName = "clubjack.png";
                    cardValue = 10;
                    break;
            case 13: cardName = "clubqueen.png";
                    cardValue = 10;
                    break;
            case 14: cardName = "clubking.png";
                    cardValue = 10;
                    break;
            case 15: cardName = "diamondace.png";
                    cardValue = 11;
                    break;
            case 16: cardName = "diamond2.png";
                    cardValue = 2;
                    break;
            case 17: cardName = "diamond3.png";
                    cardValue = 3;
                    break;
            case 18: cardName = "diamond4.png";
                    cardValue = 4;
                    break;
            case 19: cardName = "diamond5.png";
                    cardValue = 5;
                    break;
            case 20: cardName = "diamond6.png";
                    cardValue = 6;
                    break;
            case 21: cardName = "diamond7.png";
                    cardValue = 7;
                    break;
            case 22: cardName = "diamond8.png";
                    cardValue = 8;
                    break;
            case 23: cardName = "diamond9.png";
                    cardValue = 9;
                    break;
            case 24: cardName = "diamond10.png";
                    cardValue = 10;
                    break;
            case 25: cardName = "diamondjack.png";
                    cardValue = 10;
                    break;
            case 26: cardName = "diamondqueen.png";
                    cardValue = 10;
                    break;
            case 27: cardName = "diamondking.png";
                    cardValue = 10;
                    break;
            case 28: cardName = "heartace.png";
                    cardValue = 11;
                    break;
            case 29: cardName = "heart2.png";
                    cardValue = 2;
                    break;
            case 30: cardName = "heart3.png";
                    cardValue = 3;
                    break;
            case 31: cardName = "heart4.png";
                    cardValue = 4;
                    break;
            case 32: cardName = "heart5.png";
                    cardValue = 5;
                    break;
            case 33: cardName = "heart6.png";
                    cardValue = 6;
                    break;
            case 34: cardName = "heart7.png";
                    cardValue = 7;
                    break;
            case 35: cardName = "heart8.png";
                    cardValue = 8;
                    break;
            case 36: cardName = "heart9.png";
                    cardValue = 9;
                    break;
            case 37: cardName = "heart10.png";
                    cardValue = 10;
                    break;
            case 38: cardName = "heartjack.png";
                    cardValue = 10;
                    break;
            case 39: cardName = "heartqueen.png";
                    cardValue = 10;
                    break;
            case 40: cardName = "heartking.png";
                    cardValue = 10;
                    break;
            case 41: cardName = "spadeace.png";
                    cardValue = 11;
                    break;
            case 42: cardName = "spade2.png";
                    cardValue = 2;
                    break;
            case 43: cardName = "spade3.png";
                    cardValue = 3;
                    break;
            case 44: cardName = "spade4.png";
                    cardValue = 4;
                    break;
            case 45: cardName = "spade5.png";
                    cardValue = 5;
                    break;
            case 46: cardName = "spade6.png";
                    cardValue = 6;
                    break;
            case 47: cardName = "spade7.png";
                    cardValue = 7;
                    break;
            case 48: cardName = "spade8.png";
                    cardValue = 8;
                    break;
            case 49: cardName = "spade9.png";
                    cardValue = 9;
                    break;
            case 50: cardName = "spade10.png";
                    cardValue = 10;
                    break;
            case 51: cardName = "spadejack.png";
                    cardValue = 10;
                    break;
            case 52: cardName = "spadequeen.png";
                    cardValue = 10;
                    break;
            case 53: cardName = "spadeking.png";
                    cardValue = 10;
                    break;
        }
        return cardName;
    }
danpost danpost

2015/3/18

#
(1) cards do not act on their own -- remove the 'act' method (it is empty anyway); (2) remove the 'getCard' method; that is something that is done outside of the class (a Card object is Card object and it already set as far as values; it should only have a 'shown' (or 'facedUp') changeable state which goes along with changing its image; (3) the 'name' and 'value' of each card can be determined by the identification number given it; you will need a constructor for the class to accept its identification number and set the value of those fields; we can make use of two 'static final String' arrays for the naming:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import greenfoot.*;
 
public class Card extends Actor
{
    private static final String[] faces = { "ace", "2", "3", "4", "5', "6", "7", "8", "9", "10", "jack", "queen", "king" };
    private static final String[] suits = { "club", "diamond", "heart", "spade" };
    private static final GreenfootImage back = new GreenfootImage("blueback.png");
 
    private int id;
    private int value;
    private String suit;
    private String face;
    private GreenfootImage front;
 
    public Card(int idNum)
    {
        id = idNum; // save identification number
        suit = suits[id/13]; // save suit name
        face = faces[id%13]; // save face name
        front = new GreenfootImage(suit+face+".png"); // save front image
        value = 1+id%13; // set base value
        if (value > 10) value = 10; // adjustment for face card
        if (value == 1) value = 11; // adjustment for ace
        hide(); // set card face down
    }
 
    public int getValue()
    {
        return value;
    }
 
    public void show()
    {
        setImage(front);
    }
 
    public void hide()
    {
        setImage(back);
    }
 
    public boolean isShowing()
    {
        return getImage() == front;
    }
 
    public void flip()
    {
        if (isShowing()) setImage(back); else setImage(front);
    }
}
The above is about all you should need for the Card class. If needed you might add some getter methods for the Strings of the suit and face of the card; but, I do not anticipate a need for that.
drummerboy619 drummerboy619

2015/3/18

#
But I need to keep the values and information about the cards from lines 21 to 201 correct?
danpost danpost

2015/3/18

#
drummerboy619 wrote...
But I need to keep the values and information about the cards from lines 21 to 201 correct?
All the information on those lines has already been taken care of in the constructor of the class I gave. The fields 'suit', 'face' and 'value' will be set to the appropriate values when the card is created. They will hold, for an example, when 'new Card(11)' is executed: id = 11; suit = "club"; face = "queen"; value = 10; and the 'front' field will hold the face up image for that card; the 'back' field holds the face down image for ALL the cards (you may need to adjust the filename used for it, however).
You need to login to post a reply.