-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
47 lines (32 loc) · 1.49 KB
/
Deck.java
File metadata and controls
47 lines (32 loc) · 1.49 KB
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
package game;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Deck { //山札カード52枚を管理
public List<Card> cards = new ArrayList<>();//cardsというListを作って、ArrayListに格納
public Deck()
{
String [] types = {"heart","diamond", "spade", "clover" };
String[] rank = { "A", "1", "2", "3", "4", "5", "6", "7", "8", "9","10", "J", "Q", "K" };
//カードを作成してdeckに追加
for (String type : types ) {
for (int i = 0; i < rank.length; i++) {
int value;
if (i >= 10) { //i >= 10 の場合 → value = 10(J, Q, K を 10 と扱う)それ以外 → value = i + 1(数字カードは 1~10 の値
value = 10;
} else {
value = i + 1;
}
cards.add(new Card(type, rank[i], value)); //Card クラスの新しいインスタンスを作って cards リストに追加しています
}
}
Collections.shuffle(cards);
//Collections は Javaのユーティリティクラス で、リストやセットなどのコレクションを操作する便利なメソッドがたくさん入ってる
//shuffle() はその中の リストをシャッフル(順番をランダムに入れ替える) メソッド
}
public Card draw() {
Card firstCard = cards.get(0); // 一枚目のカードを取得
cards.remove(0); // リストから削除
return firstCard; // 取得したカードを返す
}
}