import java.util.Scanner;
class card {
/*
* 用于保存一张牌
*/
private final String color;
private final String value;
private int trueValue;
public card(String color, String value) {
this.color = color;
this.value = value;
}
public String getColor() {
return this.color;
}
public String getValue() {
return this.value;
}
public void setTrueValue(int trueValue) {
this.trueValue = trueValue;
}
public int getTrueValue() {
return this.trueValue;
}
@Override
public String toString() {
if (this.value.equals("A")) {
return this.color + " " + "1 11";
}
return this.color + " " + (this.trueValue);
}
}
class hand {
/*
* 用于保存一手牌
*/
private final card[] cardList = new card[20];
public hand() {
}
public void addACard(card newCard, int index) {
this.cardList[index] = newCard;
}
public card getACard(int index) {
return this.cardList[index];
}
public void getString(int cardSum) {
// 输出一手牌
// 构造一个数组
String[] sortList = new String[52];
String[] mathNumber = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
int j = 0;
int m = 0;
while(m < 52) {
sortList[m] = ("Spade" + mathNumber[j]);
m = m + 1;
sortList[m] = ("Heart" + mathNumber[j]);
m = m + 1;
sortList[m] = ("Diamond" + mathNumber[j]);
m = m + 1;
sortList[m] = ("Club" + mathNumber[j]);
m = m + 1;
j = j + 1;
}
// 构造一个全为0的数组
int[] indexList = new int[52];
for (int n = 0; n < 52; n ++) {
indexList[n] = 0;
}
for (int i = 0; i < cardSum; i++) {
// 获取一张卡牌
card aCard = this.getACard(i);
String cardString = aCard.getColor() + aCard.getValue();
// 获取索引值
int aCardIndex = 0;
for (int k = 0; k < 52; k++) {
if (cardString.equals(sortList[k])) {
aCardIndex = k;
}
}
// 再将全0数组对应位置置1
indexList[aCardIndex] = 1;
}
int sum = 0;
for (int i = 0; i < indexList.length; i++) {
if (indexList[i] == 1) {
sum += 1;
System.out.print(sortList[i] + " ");
}
}
System.out.println();
}
}
class player {
/*
* 基于指定策略完成一次游戏过程
*/
private hand hand;
public player() {
}
public int getAllCardsValue(int cardSum) {
int allValue = 0;
int AFlag = 0;
int ASum = 0;
card[] aCardArray = new card[cardSum];
int aIndex = 0;
// 将除了A的所有卡牌的数值相加
for (int i = 0; i < cardSum; i++) {
int value = 0;
String[] specialString = {"J", "Q", "K"};
for (int s = 0; s < 3; s ++) {
if (specialString[s].equals(this.hand.getACard(i).getValue())) {
value = 10;
this.hand.getACard(i).setTrueValue(10);
}
}
String[] mathNumber = {"2", "3", "4", "5", "6", "7", "8", "9", "10"};
for (int n = 0; n < 9; n++) {
if (mathNumber[n].equals(this.hand.getACard(i).getValue())) {
value = Integer.parseInt(this.hand.getACard(i).getValue());
this.hand.getACard(i).setTrueValue(value);
}
}
allValue += value;
if ("A".equals(this.hand.getACard(i).getValue())) {
AFlag = 1;
ASum += 1;
// 将所有卡牌A放入一个数组中
aCardArray[aIndex] = this.hand.getACard(i);
aIndex += 1;
}
}
int AIsEleven = 0;
int AIsElevenIndex = 0;
if (AFlag == 1) {
int AValue;
for (int i = 0; i < ASum; i++) {
if (allValue + 11 <= 21) {
AValue = 11;
AIsEleven = 1;
AIsElevenIndex = i;
} else {
AValue = 1;
}
allValue += AValue;
aCardArray[i].setTrueValue(AValue);
}
if (allValue > 21 && AIsEleven == 1) {
aCardArray[AIsElevenIndex].setTrueValue(1);
}
}
allValue = 0;
for (int i = 0; i < cardSum; i++) {
allValue += this.hand.getACard(i).getTrueValue();
}
return allValue;
}
public void playFun() {
// 获取输入的牌保存到hand中
Scanner scanner = new Scanner(System.in);
this.hand = new hand();
int cardSum = 0;
// 先获取两张牌
for (int i = 0; i < 2; i++) {
String s1 = scanner.next();
String s2 = scanner.next();
card newCard = new card(s1, s2);
this.hand.addACard(newCard, i);
cardSum += 1;
}
int allCardValue = this.getAllCardsValue(cardSum);
// 如果只有两张牌且两张牌加起来为21
if (allCardValue == 21) {
System.out.println("Stand");
this.hand.getString(cardSum);
System.out.println("Blackjack");
return;
}
if (allCardValue >= 17) {
System.out.println("Stand");
this.hand.getString(cardSum);
System.out.println(allCardValue);
return;
}
while (true) {
// 获取一张牌
System.out.println("Hit");
String s1 = scanner.next();
String s2 = scanner.next();
card newCard = new card(s1, s2);
this.hand.addACard(newCard, cardSum);
cardSum += 1;
// 重新计算此时的牌面
allCardValue = this.getAllCardsValue(cardSum);
// 输出刚才要的这张牌
System.out.println(newCard);
// 判断
if (allCardValue >= 17) {
System.out.println("Stand");
this.hand.getString(cardSum);
if (allCardValue > 21) {
System.out.println("Bust");
break;
}
System.out.println(allCardValue);
break;
}
}
}
}
public class Main {
public static void main(String[] args) {
player player = new player();
player.playFun();
}
}
java课程作业+北邮+OJ+已通过+黑杰克
版权申诉
5星 · 超过95%的资源 8 浏览量
2022-06-02
10:08:11
上传
评论 2
收藏 2KB 7Z 举报
稽函数
- 粉丝: 6
- 资源: 33
最新资源
- 基于matlab实现对表面肌电信号进行归一化处理,并对归一化后的图形显示 .rar
- 基于matlab实现单级倒立摆的 T-S 模型 包括 LMI 程序源码
- 图书管理系统(struts+hibernate+spring+ext).rar
- 基于matlab实现此压缩包包含语音信号处理中的语音变声代码加音频.rar
- STM32使用PWM驱动舵机并通过OLED显示
- 基于matlab实现车辆路径规划;遗传算法;matlab代码.rar
- 图书管理系统(struts+hibernate+spring)130225.rar
- 基于matlab实现采用标量衍射理论,实现菲涅尔衍射和夫琅禾费衍射,对光波的波前传播和数字全息的应用有帮助.rar
- JavaScript版去除链表重复元素
- 微信小程序项目-功德木鱼(带设置面板-自定义文字、可选字体颜色、可选木鱼样式)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页