import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
/**
* @author suny
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
new Player().strategy();
}
}
/**
* 花色的常量
*/
interface Decor {
int Spade = 1;
int Heart = 2;
int Club = 4;
int Diamond = 3;
}
/**
* 牌的种类
*/
class Card implements Decor {
int decor;//花色
String value;//值
public Card(String decor, String value) {
this.decor = getDecor(decor);
this.value = value;
}
@Override
public String toString() {
return decor_(decor) + value;
}
/**
* 将花色转换为字符串,用于输出
* @param decor:花色
* @return:花色的字符串
*/
public String decor_(int decor) {
switch (decor) {
case 1:
return "Spade";
case 2:
return "Heart";
case 4:
return "Club";
case 3:
return "Diamond";
default:
return "error";
}
}
/**
* 将牌的值转换为数字,根据A的值不同,返回不同的值
* A的值为11时,返回A的值为11,其他值为10
* A的值为1时,返回A的值为1,其他值为按照排列顺序的值
* @param value
* @param A
* @return
*/
public int value_(String value, int A) {
switch (value) {
case "A":
return A;
case "K":
return A == 11 ? 10 : 13;
case "Q":
return A == 11 ? 10 : 12;
case "J":
return A == 11 ? 10 : 11;
default:
return Integer.parseInt(value);
}
}
//将牌的值转换为数字
public int getValue() {
return value_(value, 11);
}
public int getValue_print() {
return value_(value, 1);
}
//将花色转换为数字
public int getDecor(String decor) {
switch (decor) {
case "Spade":
return Decor.Spade;
case "Heart":
return Decor.Heart;
case "Club":
return Decor.Club;
case "Diamond":
return Decor.Diamond;
default:
return 0;
}
}
}
class Hand {
ArrayList<Card> allCard = new ArrayList<>();//记录所有的牌
//有多少A牌
int A = 0;
public void add(boolean before2) {
String str = Tool.str();
Card ca;
String[] split = str.split(" ");
ca = new Card(split[0], split[1]);
allCard.add(ca);
//如果是A牌,则A的数量加1
if (ca.getValue() == 11) {
A++;
if (!before2) {
System.out.println(ca.decor_(ca.decor) + " 1 11");
return;
}
}
if (!before2) {
System.out.println(ca.decor_(ca.decor) + " " + ca.getValue());
}
}
//返回当前的总值,指定A的规则:如果总值大于21,则A的值为1,否则为11
public int getTotal() {
int total = 0;
//A==11,如果总和大于21,则A的值为1,否则为11
for (Card card : allCard) {
total += card.getValue();
}
for (int i = 0; i < A; i++) {
if (total > 21) {
total -= 10;
} else {
break;
}
}
return total;
}
//点数之和小于17点则继续要下一张牌,直到大于等于17点为止
public Boolean isAdd() {
return getTotal() < 17;
}
//总点数超过21点
public Boolean isLose() {
return getTotal() > 21;
}
//等于21点
public Boolean isBlackjack() {
return getTotal() == 21 && allCard.size() == 2;
}
}
class Player {
Hand myCards = new Hand();
public void strategy() {
//前两张牌不输出
myCards.add(true);
myCards.add(true);
while (true) {
if (myCards.isLose()) {
System.out.println("Stand");
print();
System.out.println("Bust");
break;
} else if (myCards.isBlackjack()) {
System.out.println("Stand");
print();
System.out.println("Blackjack");
break;
} else if (myCards.isAdd()) {
System.out.println("Hit");
myCards.add(false);
} else if (!myCards.isAdd()) {
System.out.println("Stand");
print();
System.out.println(myCards.getTotal());
break;
}
}
}
//输出当前的牌:牌与牌之间用一个空格分隔。
// 牌输出的顺序为先看牌面,牌面小的在前(牌面由小到大的顺序为A,2,3....J,Q,K),
// 当牌面相同时看花色,输出顺序从前到后为Spade, Heart, Diamond, Club。
public void print() {
myCards.allCard.sort(new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
if (o1.getValue_print() == o2.getValue_print()) {
return o1.decor - o2.decor;
} else {
return o1.getValue_print() - o2.getValue_print();
}
}
});
for (Card card : myCards.allCard) {
System.out.print(card + " ");
}
System.out.println();
}
}
class Tool {
private static Scanner scanner = new Scanner(System.in);
public static String str() {
return scanner.nextLine();
}
}
01-30
3394
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-25
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交