-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
73 lines (52 loc) · 2.36 KB
/
Game.java
File metadata and controls
73 lines (52 loc) · 2.36 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
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
package game;
import java.util.Scanner;
public class Game {
private Deck deck;
private int playerTotal = 0;
private int dealerTotal = 0;
public Game() {
deck = new Deck(); //Game() はクラスのコンストラクタ
//デッキを初期化して、ゲーム開始準備をします
}
public void start() {
Scanner sc = new Scanner(System.in);
System.out.println("=== Blackjack Start ===");
playerTotal += drawCard("あなた");//2回カードを引く playerTotal = playerTotal + drawCard
playerTotal += drawCard("あなた");
dealerTotal += drawCard("dealer");//2回カードを引く
dealerTotal += drawCard("dealer");
while(true) { //無限ループ while(true) でプレイヤーの行動を処理
System.out.println("あなたの得点は " + playerTotal);
System.out.print("もう一回引きたいですか? (y/n): ");
String choice = sc.nextLine();
if (choice.equalsIgnoreCase("y")) { //もう一回引くと答えたら
playerTotal += drawCard("あなた");
if (playerTotal > 21) {
System.out.println("21オーバー! あなたの負け!");
return;
}
}else {
break; //playerターン終了
}
}
while (dealerTotal < 17 ) {
dealerTotal += drawCard("dealer");//合計点数が17未満の間は自動でカードを引く
}
System.out.println("得点は" + dealerTotal);
judge();//judgeメソッド
}
private int drawCard(String who) {//drawcardメソッドを作る、カードを1枚引く処理
Card c = deck.draw();//deck.draw() でカードを取得
System.out.println(who + " draw " + c);//誰がどのカードを引いたか」を表示
return c.getValue(); //カードの 点数 (getValue()) を返す → 合計に加算
}
private void judge() {
if (dealerTotal > 21 || playerTotal > dealerTotal ) {
System.out.println("プレイヤーの勝ち!");
}else if (playerTotal == dealerTotal) {
System.out.println("引き分け");
}else {
System.out.println("プレイヤー負け");
}
}
}