JAVA 单张牌出牌逻辑 实现斗地主

news/2024/11/17 0:19:38/

JAVA 单张牌出牌逻辑 实现斗地主

  1. Card类
public class Card {Integer degree;String color;public Card(Integer degree, String color) {this.degree = degree;this.color = color;}public Card() {}
}
  1. Player类
import java.util.ArrayList;
public class Player {String name;ArrayList<Card> cards;
}
  1. Landlord类 (main)
import java.util.*;public class Landlord {ArrayList<Player> Players = new ArrayList<Player>();HashMap<Integer, ArrayList<Card>> cards = new HashMap<Integer, ArrayList<Card>>();String[] colors = "♥-♠-♦-♣".split("-");String[] nums = "3-4-5-6-7-8-9-10-J-Q-K-A-2".split("-");public Landlord() {Player player1 = new Player();player1.name = "农民1";player1.cards = new ArrayList<>();Players.add(player1);Player player2 = new Player();player2.name = "农民2";player2.cards = new ArrayList<>();Players.add(player2);Player player3 = new Player();player3.name = "农民3";player3.cards = new ArrayList<>();Players.add(player3);Player surplus = new Player();surplus.name = "底牌";surplus.cards = new ArrayList<>();Players.add(surplus);}public void setCards() {Integer index = 1;for (String num : nums) {ArrayList<Card> colours = new ArrayList<Card>();for (String color : colors) {colours.add(new Card(index, num + color));}cards.put(index++, colours);}ArrayList<Card> colours = new ArrayList<Card>();colours.add(new Card(index, "小王"));cards.put(index++, colours);colours = new ArrayList<Card>();colours.add(new Card(index, "大王"));cards.put(index, colours);int i = 1;}//    根据手牌等级插入排序public void sortCards() {for (int i = 0; i < 4; i++) {ArrayList<Card> cards = new ArrayList<>();for (Card c : Players.get(i).cards) {insertSort(cards, c);}Players.get(i).cards = cards;}}public void insertSort(ArrayList<Card> cards, Card card) {if (cards.isEmpty()) {cards.add(card);return;}for (int i = 0; i < cards.size(); i++) {if (card.degree < cards.get(i).degree) {  //这张卡的等级比遍历到的卡的等级低,则插入当前位置cards.add(i, card);return;}}//遍历完都没找到插入位置,就要放到最后cards.add(card);return;}//    随机发牌,先随机选择卡牌等级,再选择随机花色public void distributeCards() {Random random = new Random();HashMap<Integer, ArrayList<Card>> cardsClone = (HashMap<Integer, ArrayList<Card>>) cards.clone();int p;for (p = 0; p < 3; p++) {   // 三个玩家for (int i = 1; i <= 17; i++) {List<Integer> keysAsArray = new ArrayList<Integer>(cardsClone.keySet());//获取卡组的键的数组int i1 = keysAsArray.get(random.nextInt(keysAsArray.size()));  //左闭右开 获取现有卡组中的键ArrayList<Card> colorClone = cardsClone.get(i1);  //获取一个数字的所有花色牌int i2 = random.nextInt(colorClone.size());  //下标 0~牌数-1Players.get(p).cards.add(colorClone.get(i2));  //将此牌加入底牌colorClone.remove(i2);  //将此牌移除对应数字的所有花牌if (colorClone.isEmpty()) {  //如果此数字的花牌数为0cardsClone.remove(i1); //删除此数字(键值对)}}}for (int i = 1; i <= 3; i++) {  //一副底牌List<Integer> keysAsArray = new ArrayList<Integer>(cardsClone.keySet());//获取卡组的键的数组int i1 = keysAsArray.get(random.nextInt(keysAsArray.size()));  //左闭右开 获取现有卡组中的键ArrayList<Card> colorClone = cardsClone.get(i1);  //获取一个数字的所有花色牌int i2 = random.nextInt(colorClone.size());  //下标 0~牌数-1Players.get(p).cards.add(colorClone.get(i2));  //将此牌加入底牌colorClone.remove(i2);  //将此牌移除对应数字的所有花牌if (colorClone.isEmpty()) {  //如果此数字的花牌数为0cardsClone.remove(i1); //删除此数字(键值对)}}int test = 0;}public void showCards() {System.out.println("\n**********手牌展示**********");int i;for (i = 0; i < 3; i++) {System.out.print("玩家" + (i + 1) + ":");for (Card c : Players.get(i).cards) {System.out.print(c.color + " ");}System.out.println();}System.out.print("底牌" + ":");for (Card c : Players.get(i).cards) {System.out.print(c.color + " ");}System.out.println("\n*************************");}//    地主随机出一张牌,其他玩家跟public void dealOneCard() {Random random = new Random();Player lord = Players.get(0);Player player2 = Players.get(1);Player player3 = Players.get(2);for (int i = 0; i < 3; i++) {lord.cards.add(Players.get(3).cards.get(i));   // 玩家1作地主}Players.get(3).cards.clear(); //地主拿走所有底牌showCards();int flag = 0;Card tempCard = new Card();while (true) {if (flag == 0) {  //农民没有足够大的牌int k = random.nextInt(lord.cards.size());tempCard = lord.cards.get(k);System.out.println("地主:一个" + tempCard.color);lord.cards.remove(k);} else {  //地主需要出足够大的牌for (int j = 0; j < lord.cards.size(); j++) {if (lord.cards.get(j).degree > tempCard.degree) {System.out.println("地主:看招,一个" + lord.cards.get(j).color);tempCard = lord.cards.get(j);lord.cards.remove(j);flag = 0;break;}}}if (flag != 0) { //地主没有足够大的牌System.out.println("地主:要不起");switch (flag) {case 2:int t = random.nextInt(player3.cards.size());tempCard = player3.cards.get(t);System.out.println(player3.name + ":一个" + tempCard.color);player3.cards.remove(t);break;case 3:int s = random.nextInt(player2.cards.size());tempCard = player2.cards.get(s);System.out.println(player2.name + ":一个" + tempCard.color);player2.cards.remove(s);break;}} else {  //农民需要出足够大的牌for (int j = 0; j < player2.cards.size(); j++) {if (player2.cards.get(j).degree > tempCard.degree) {System.out.println(player2.name + ":管上! " + player2.cards.get(j).color);tempCard = player2.cards.get(j);player2.cards.remove(j);flag = 2;break;}}for (int j = 0; j < player3.cards.size() && flag == 0; j++) {if (player3.cards.get(j).degree > tempCard.degree) {System.out.println(player3.name + ":大你! " + player3.cards.get(j).color);tempCard = player3.cards.get(j);player3.cards.remove(j);flag = 3;break;}}}if (flag == 0) {System.out.println("农民们:pass...");}if (lord.cards.isEmpty()) {System.out.println("地主获胜!");showCards();return;}if (player2.cards.isEmpty() || player3.cards.isEmpty()) {System.out.println("农民获胜!");showCards();return;}}}public static void main(String[] args) {Landlord landlord = new Landlord();landlord.setCards();landlord.distributeCards();landlord.sortCards();landlord.showCards();landlord.dealOneCard();}
}

应付下作业还是可以的,【狗头】


http://www.ppmy.cn/news/583574.html

相关文章

JAVA GUI创作简易记牌器

Picture1&#xff1a; 展示出一副扑克牌中的52张 Picture2&#xff1a;在文本框内输入扑克 Picture3&#xff1a;显示出除输入之外其他的扑克牌 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;public cl…

斗地主小游戏(JAVA实现)

hello&#xff0c;我是忘鱼。 目录 前言 一、案例所需要具备知识 二、代码 2.运行结果 总结 前言 斗地主小游戏&#xff0c;属于Collection体系综合案例&#xff0c;学习帮助我们加深理解。 一、案例所需要具备知识 常用Arraylist常用操作&#xff0c;和一些基础知识。代码注释…

斗地主AI算法——第十二章の主动出牌(1)

本章开始&#xff0c;我们介绍主动出牌的算法&#xff0c;和被动出牌类似&#xff0c;我们第一步把主要架子搭起来。 首先清空出牌序列 clsHandCardData.ClearPutCardList(); 主动出牌的策略按照优先级大体可以分为三类&#xff1a; 【一】能直接一手牌出去&#xff0c;优先出…

我赢职场c语言数组斗地主,职场就行斗地主,你看懂了么?

1、小王都会被大王拍死。说明副职没有实权&#xff01; 2、没有一张大牌开路&#xff0c;再顺的小牌都出不去。说明领导很重要&#xff01; 3、无论你多会记牌、打牌&#xff0c;都抵不过人家手中的一把好牌。说明实力比能力重要&#xff01; 4、如果一堆小牌连不起来&#xff…

斗地主AI算法——第十七章の总结整理

2.0版本的斗地主AI算法在这里就算告一段落了。 **********************完结撒花********************** 不过后续应该还会开发更智能的版本&#xff0c;毕竟当前版本还有很多策略没有加入。 比如说角色位置&#xff08;地主上家下家打法&#xff09;、比如说记牌算牌、又比如…

天天QQ记牌器2.37 去广告绿色版

帮一个朋友忙 把这款软件的广告nop掉 听说是挺好的一款记牌器 已经上传到资源里 简单说下步骤 1.脱壳.. 用看雪大牛的 Aspr2.XX_unpacker_v1.0SC.osc 脚本脱壳 具体用法就不说了 gg下就知道了 2.最简单的方法是 搜索 tntn.cn 把字符串都替换成 about:blank 就OK了 不过还…

斗地主笔记

很喜欢斗地主&#xff0c;一起交流下。 文章目录 牌力分析王牌双鬼炸弹 一流强牌飞机大鬼22 强牌小鬼2AA&#xff0c;KK顶A顺子(长顺子)3连对(或以上) 大牌一般顺子三张A、KQQ 中牌(小牌)送命牌 叫牌哪些情况可以叫牌 对局 牌力分析 王牌 顶级牌&#xff0c;使用几乎无限制&…

js实现斗地主计分器

文章目录 起因实现1、页面html2、js操作--全局变量3、js操作--数据初始化4、js操作--每局结算5、js操作--撤销 源码获取 起因 现在大家斗地主的时候往往喜欢在每局结束后进行微信转账&#xff0c;然而这样的转帐过程往往比较浪费时间&#xff0c;所以咱们可以自己写一个简单的…