-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson-25_BlackJack_ver3
More file actions
205 lines (159 loc) · 5.66 KB
/
Copy pathLesson-25_BlackJack_ver3
File metadata and controls
205 lines (159 loc) · 5.66 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
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
202
203
204
205
package lesson25;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// Берем 1 колоду =
import java.util.ArrayList;
import java.util.Random;
public class BlackJack {
// имя игрока
public String playerName;
// тип играка: человек или компютер
public boolean isHuman = false;
// создаем колоду карт
public static ArrayList<Integer> cardPack = new ArrayList<>();
// очки на руках у игрока
int scores = 0;
// случайное значение
Random random = new Random();
// ввод с клавиатуры
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// конструктор
public BlackJack(boolean isHuman) {
this.isHuman = isHuman;
this.playerName = "Компьютер";
}
// конструктор
public BlackJack(boolean isHuman, String name ) {
this.isHuman = isHuman;
this.playerName = name;
}
// наполняем колоду
public static ArrayList<Integer> createCardPack() {
// наполняем колоду картами от 2 до Туза
for (int i = 0; i < 4; i++) { // четыре масти
for (int j = 2; j < 12; j++) { // очки карты
cardPack.add(j); //
if (j == 10) {
cardPack.add(j); // +J Валет
cardPack.add(j); // +Q Дама
cardPack.add(j); // +K Король
}
}
}
return cardPack;
}
// метод ход
// игроку предлагается взять карту
// компютер учитывает очки, вероятность взять карту исходит от очков
public void turn() throws IOException { // исключение возможно из-за reader.readLine();
int chance10 = random.nextInt(9); // вероятность 10%
int chance50 = random.nextInt(2); // вероятность 50%
int chance100 = random.nextInt(1); // вероятность 100%
// для живого игрока
if (this.isHuman == true) {
System.out.println("Ход игрока " + this.playerName + " ...");
boolean repeatGetCard = true;
// повторное взятие карты
while (repeatGetCard == true) {
if (this.scores > 19) {
repeatGetCard = false;
System.out.println("У вас > 19!");
} else {
System.out.println("Взять карту?");
String askGetCard = reader.readLine();
if ("yes".equals(askGetCard) ) {
this.getCard();
} else if ("no".equals(askGetCard) ) {
System.out.println("Ваша сумма очков = " + this.scores);
repeatGetCard = false;
}
}
}
}
// для компютера
else {
System.out.println("Ход компютера...");
// tempscores
// this.scores = 18;
if (this.scores < 10) {
this.getCard();
} else
if (this.scores >=10 && this.scores < 15) {
if (0 == chance50)
this.getCard();
else System.out.println("Пас!");
} else
if (this.scores >=15 && this.scores < 19) {
if (0 == chance10)
this.getCard();
else System.out.println("Пас!");
} else
if (this.scores >=19) {
System.out.println("Пас!");
}
}
//System.out.println("Очок = " + this.scores);
}
// метод взять карту
public void getCard() {
System.out.println(this.playerName + " : Взята карта: ");
int card = selectCard();
scoreCount(card); // добавили карту к сумме очков
cardPack.remove(card); // удалили карту из колоды
// показать взятую карту и очки только для живого игрока
if (this.isHuman == true) {
System.out.println(card);
System.out.println("Сумма очков: " + this.scores);
}
// показать взятую карту и очки для компютера (нужно для тестировки)
// if (this.isHuman == false) {
// System.out.println(card);
// System.out.println("Сумма очков: " + this.scores);
// }
// // набор карт игрока
// ArrayList<Integer> playerCards = new ArrayList<>();
// playerCards.add(card);
}
// выбор карты из колоды
public int selectCard() {
int randomPosition = random.nextInt(cardPack.size());
int card = cardPack.get(randomPosition); // рендомная карта в колоде
return card;
}
// раздача
public static void deal(BlackJack player1, BlackJack player2) {
System.out.println("Карты розданы!");
player1.getCard();
player2.getCard();
player1.getCard();
player2.getCard();
}
// метод подсчёт очков
public int scoreCount(int card) {
scores += card;
//System.out.println("Набрано очков: " + scores);
return scores;
}
// метод вскрывает карты игроков
public static void showCards(BlackJack player1, BlackJack player2) {
System.out.println(player1.playerName + " = " + player1.scores);
System.out.println(player2.playerName + " = " + player2.scores);
System.out.println("Победитель: " + findWinner(player1, player2));
}
// метод - определение победителя
public static String findWinner(BlackJack player1, BlackJack player2) {
if (player1.scores <= 21) {
if (player1.scores > player2.scores) {
return player1.playerName;
} else if (player1.scores < player2.scores) {
return player2.playerName;
} else
return "Ничья!";
} else
if (player2.scores <= 21) {
return player2.playerName;
} else
return "Ничья!";
}
}