宝安建网站多少钱,做网站备案地点,成都网页,合肥网站开发公司电话十点半游戏
十点半是一种流行的纸牌游戏#xff0c;可以说是一种变体的二十一点游戏。游戏的规则是#xff0c;每个玩家根据所拿到的牌点数的总和来决定是否继续要牌。目标是尽量接近但不超过十点半的点数#xff0c;超过十点半即为爆牌。如果两名玩家都未爆牌#xff0c;…十点半游戏
十点半是一种流行的纸牌游戏可以说是一种变体的二十一点游戏。游戏的规则是每个玩家根据所拿到的牌点数的总和来决定是否继续要牌。目标是尽量接近但不超过十点半的点数超过十点半即为爆牌。如果两名玩家都未爆牌则点数更接近十点半的人获胜。这个游戏非常简单且容易上手适合多人一起娱乐。 代码实现
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class Poker {private static final ListString suits Arrays.asList(♠, ♥, ♦, ♣); // 花色private static final ListString ranks Arrays.asList(2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A, 小王, 大王); // 点数private ListString 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 ListString getDeck() {return deck;}
}import java.util.*;/*** author BXB*/
public class Game {public static void main(String[] args) {ListString poker new Poker(false).getDeck();shuffle(poker);gameing(poker);}// 进行游戏public static void gameing(ListString poker){ArrayListString player new ArrayList();ArrayListString 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(ListString list) {for (int i 0; i 3; i) {// System.currentTimeMillis() 来设置随机种子。每一次运行程序时都会使用不同的随机种子从而产生更随机的结果。Collections.shuffle(list, new Random(System.currentTimeMillis()));}}// 计算点数和2public static double countPoints2(ListString list) {ListString 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(ListString list) {if (countPoints2(list) 10.5) {return true;}return false;}public static boolean isWin(ListString list) {if (countPoints2(list) 10.5) {return false;} else if (list.size() 5) {return true;}return false;}public static boolean isWin(ListString list, ListString botList) {return countPoints2(list) countPoints2(botList);}
}