今天下午上班的时候脑海里无意间闪过了家里长辈们逢年过节时在桌前的牌局娱乐行为,有麻将、扑克等。小时候不以为然,长大后才慢慢意识到这种娱乐行为给家庭带来的危害【指输赢比较大的牌局】。我花了1个小时的时间模拟了“斗牛”扑克牌游戏的实现,尽管是非常简陋的程序,但我任有操控后台的手段使玩家的胜率为0,更何况是复杂的人心。
常规的斗牛规则相信大家了解一二,由于本人水平和时间的关系,我并没有将细节全部实现。程序设计方面也有所欠缺,望大家海涵。
庄家与玩家各发5张牌,2 - 10 为牌面值,A 代表 1,J、Q、K代表10,计算5张牌总和3张牌总和需满足 sum % 10 == 0
sum <= 10 :五小牛 (输赢翻5倍)
sum % 10 == 0 :牛牛 (输赢翻4倍)
sum % 10 == 9 :牛9 (输赢翻3倍)
sum % 10 == 8 :牛8 (输赢翻2倍)
1 <= sum % 10 < 8 :(输赢翻1倍)
每局固定押注10000¥
程序运行效果如下
下面来看看实现程序的三个类
@Data
public class Person {
// 昵称
private String name;
// 账户余额
private int money;
public Person(String name, int money) {
this.name = name;
this.money = money;
}
}
public class Card {
/* 构造cards所需条件 */
private static final String[] color = {"♣", "♦", "♠", "♥"};
private static final String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
// 所有的cards
private static final List cards = new ArrayList<>(52);
// 维护所有的翻倍条件
private static final Map ruleMap = new HashMap<>();
static {
// 初始化所有卡片
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < color.length; j++) {
cards.add(color[j] + numbers[i]);
}
}
// 初始化所有翻倍条件
ruleMap.put("五小牛", 5);
ruleMap.put("牛牛", 4);
ruleMap.put("牛9", 3);
ruleMap.put("牛8", 2);
ruleMap.put("牛7", 1);
ruleMap.put("牛6", 0);
ruleMap.put("牛5", -1);
ruleMap.put("牛4", -2);
ruleMap.put("牛3", -3);
ruleMap.put("牛2", -4);
ruleMap.put("牛1", -5);
ruleMap.put("无牛", -6);
}
/**
* 得到两组 5 张卡片的数组
* @param cards 卡片集合
* @return
*/
public String[][] getFiveCard(List cards) {
// 洗牌
Collections.shuffle(cards);
String[][] res = new String[2][5];
int index = 0;
for (int i = 0; i < res.length; i++) {
for (int j = 0; j < res[0].length; j++) {
res[i][j] = cards.get(index);
index++;
}
}
return res;
}
/**
* 获取牌面对应的【牛】
* @param nums 5张卡片的数组
* @return
*/
public String getSkill(String[] nums) {
// ["♠5", "♣10"]
int sum = 0;
List integerList = new ArrayList<>();
for (String num : nums) {
String indexNum = num.substring(1);
// JQKA对应数字转换
String number = convertJQKA(indexNum);
// 求和
sum += Integer.parseInt(number);
integerList.add(Integer.parseInt(number));
}
String nForSum = computeNForSum(sum);
boolean hasN = hasN(integerList);
// 若有牛则返回
if (hasN || nForSum.equals("五小牛")) {
return nForSum;
}
return "无牛";
}
/**
* 判断三张牌是否满足有牛需求 sum % 10 == 0
* @param numbers
* @return
*/
private static boolean hasN(List numbers) {
int size = numbers.size();
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
for (int k = j + 1; k < size; k++) {
int sum = numbers.get(i) + numbers.get(j) + numbers.get(k);
if (sum % 10 == 0) {
return true;
}
}
}
}
return false;
}
/**
* JQKA对应数字转换
* @param num
* @return
*/
private String convertJQKA(String num) {
String res = num;
if (num.equals("J") || num.equals("Q") || num.equals("K")) {
res = "10";
} else if (num.equals("A")) {
res = "1";
}
return res;
}
/**
* 计算牌面【牛】
* @param sum
* @return
*/
private String computeNForSum(int sum) {
String res = "";
if (sum <= 10) {
res = "五小牛";
} else if (sum % 10 == 0) {
res = "牛牛";
} else {
res = "牛" + (sum % 10);
}
return res;
}
public static List getCards() {
return cards;
}
/**
* 比较boss与person间的牌面
* @param skillBoss boss的牌面
* @param boss
* @param skillPerson person的牌面
* @param person
*/
public static void compare(String skillBoss, Person boss, String skillPerson, Person person) {
// 获取牌面对应倍数
Integer intBoss = ruleMap.get(skillBoss);
Integer intPerson = ruleMap.get(skillPerson);
if (intBoss >= intPerson) {
// 如果倍数小于等于1 则置为1方便计算输赢
if (intBoss <= 1) {
intBoss = 1;
}
System.out.println("本局 " + boss.getName() + " 获胜~");
System.out.println(person.getName() + "在本局输掉了 " + 10000 * intBoss + "¥");
person.setMoney(person.getMoney() - 10000 * intBoss);
System.out.println(person.getName() + "当前账户余额: " + person.getMoney() + "¥");
boss.setMoney(boss.getMoney() + 10000 * intBoss);
System.out.println(boss.getName() + "当前账户余额: " + boss.getMoney() + "¥");
System.out.println("赌博违法,十赌九输!");
} else {
if (intPerson <= 1) {
intPerson = 1;
}
System.out.println("本局 " + person.getName() + " 获胜~");
System.out.println(person.getName() + "在本局赢了 " + 10000 * intPerson + "¥");
person.setMoney(person.getMoney() + 10000 * intPerson);
System.out.println(person.getName() + "当前账户余额: " + person.getMoney() + "¥");
boss.setMoney(boss.getMoney() - 10000 * intPerson);
System.out.println(boss.getName() + "当前账户余额: " + boss.getMoney() + "¥");
System.out.println("赌博违法,十赌九输!");
}
}
}
public class DN_App {
public static void main(String[] args) {
Person boss = new Person("黄老板", 20000000);
Person lly = new Person("卢姥爷", 200000);
System.out.println("经典语录【doge】");
System.out.println("你们可能不知道只用20万赢到578万是什么概念,我们一般只会用两个字来形容这种人:赌怪。\n" +
"我经常说一句话,当年陈刀仔他能用20块赢到3700万,我卢本伟用20万赢到500万,不是问题。");
System.out.println();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("===========欢迎来到斗牛game v0.1 ===========");
System.out.println("=========== 请输入你的选择 ===========");
System.out.println("1:开始游戏");
System.out.println("2:退出游戏");
int choose = scanner.nextInt();
if (choose == 1) {
Card card = new Card();
String[][] fiveCard = card.getFiveCard(Card.getCards());
System.out.println(boss.getName() + "的牌如下: ");
System.out.println(Arrays.toString(fiveCard[0]));
String skillBoss = card.getSkill(fiveCard[0]);
System.out.println("\t" + skillBoss);
System.out.println(lly.getName() + "的牌如下: ");
System.out.println(Arrays.toString(fiveCard[1]));
String skillLLY = card.getSkill(fiveCard[1]);
System.out.println("\t" + skillLLY);
Card.compare(skillBoss, boss, skillLLY, lly);
if (lly.getMoney() <= 0) {
System.out.println(lly.getName() + "已经输光了所有钱...game over!");
break;
}
} else if (choose == 2) {
System.out.println("珍爱生命,远离赌博~");
System.out.println("小赌怡情,大赌伤财,强赌灰飞烟灭~");
break;
}
}
}
}