十点半游戏
十点半是一种流行的纸牌游戏,可以说是一种变体的二十一点游戏。游戏的规则是,每个玩家根据所拿到的牌点数的总和来决定是否继续要牌。目标是尽量接近但不超过十点半的点数,超过十点半即为爆牌。如果两名玩家都未爆牌,则点数更接近十点半的人获胜。这个游戏非常简单且容易上手,适合多人一起娱乐。
代码实现
java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Poker {
private static final List<String> suits = Arrays.asList("♠", "♥", "♦", "♣"); // 花色
private static final List<String> ranks = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "小王", "大王"); // 点数
private List<String> deck;
private Boolean isComplete;
public Poker() {
this(true);
}
public Poker(boolean isComplete) {
deck = new ArrayList<>(); // 扑克牌
// 向扑克牌列表中储存元素
for (String suit : suits) {
for (String rank : ranks.subList(0, 13)) {
String card = suit + rank;
deck.add(card);
}
}
if (isComplete) {
deck.add("大王");
deck.add("小王");
}
}
public List<String> getDeck() {
return deck;
}
}
java
import java.util.*;
/**
* @author BXB
*/
public class Game {
public static void main(String[] args) {
List<String> poker = new Poker(false).getDeck();
shuffle(poker);
gameing(poker);
}
// 进行游戏
public static void gameing(List<String> poker){
ArrayList<String> player = new ArrayList<>();
ArrayList<String> bot = new ArrayList<>();
boolean isTermination = true;
Scanner input = new Scanner(System.in);
// 玩家发牌
do {
player.add(poker.get(0)); // 向玩家发牌
System.out.println(player);
poker.remove(0); // 去除已经发出去的牌
if (countPoints(player) > 10.5) {
break;
}
System.out.println("还要继续取牌吗?(Y or N)");
if ("N".equals(input.next())) {
isTermination = false;
}
} while (isTermination);
if (isWin(player)) {
System.out.println("你赢了,恭喜恭喜");
System.out.println(bot);
} else if (countPoints2(player) <= 10.5) {
// 机器人取牌
while (countPoints2(bot) <= countPoints2(player) && countPoints2(bot) != 10.5) {
bot.add(poker.get(0));
poker.remove(0);
}
// 判断机器人是否赢了
if (isLost(bot)) {
System.out.println("机器人输了\n" + bot);
} else if (isWin(bot) || isWin(bot, player)) {
System.out.println("机器人赢了\n" + bot);
} else {
System.out.println("你赢了,恭喜恭喜\n" + bot);
}
} else {
System.out.println("你输了");
}
}
// 洗牌
public static void shuffle(List<String> list) {
for (int i = 0; i < 3; i++) {
// System.currentTimeMillis() 来设置随机种子。每一次运行程序时都会使用不同的随机种子,从而产生更随机的结果。
Collections.shuffle(list, new Random(System.currentTimeMillis()));
}
}
// 计算点数和2
public static double countPoints2(List<String> list) {
List<String> ranks = Arrays.asList("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"); // 点数
double count = 0;
// 匿名函数中局部变量 count 必须声明为 final 或者实际上是 final 的(即该变量值不可更改)。
for (String str : list) {
str = str.replaceAll("[♠♥♦♣]", ""); // 去除花色
switch (str) {
case "A" -> count += 1;
case "J","Q","K" -> count += 0.5;
default -> count += Double.parseDouble(str);
}
}
return count;
}
// 判断输赢
public static boolean isLost(List<String> list) {
if (countPoints2(list) > 10.5) {
return true;
}
return false;
}
public static boolean isWin(List<String> list) {
if (countPoints2(list) > 10.5) {
return false;
} else if (list.size() == 5) {
return true;
}
return false;
}
public static boolean isWin(List<String> list, List<String> botList) {
return countPoints2(list) > countPoints2(botList);
}
}