发牌、看牌
需求: 斗地主中实现发牌和看牌
public class PokerDemo { public static void main(String[] args) { //创建一个空牌盒,ArrayList实现 ArrayList<String> array = new ArrayList<String>();
<span class="token comment">//装牌(A到K四种花色,大王小王)</span> <span class="token comment">//定义四种花色</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> colors <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"♦"</span><span class="token punctuation">,</span> <span class="token string">"♣"</span><span class="token punctuation">,</span> <span class="token string">"♥"</span><span class="token punctuation">,</span> <span class="token string">"♠"</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">//定义十三张点</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> numbers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"2"</span><span class="token punctuation">,</span> <span class="token string">"3"</span><span class="token punctuation">,</span> <span class="token string">"4"</span><span class="token punctuation">,</span> <span class="token string">"5"</span><span class="token punctuation">,</span> <span class="token string">"6"</span><span class="token punctuation">,</span> <span class="token string">"7"</span><span class="token punctuation">,</span> <span class="token string">"8"</span><span class="token punctuation">,</span> <span class="token string">"9"</span><span class="token punctuation">,</span> <span class="token string">"10"</span><span class="token punctuation">,</span> <span class="token string">"J"</span><span class="token punctuation">,</span> <span class="token string">"Q"</span><span class="token punctuation">,</span> <span class="token string">"k"</span><span class="token punctuation">,</span> <span class="token string">"A"</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String color <span class="token operator">:</span> colors<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String number <span class="token operator">:</span> numbers<span class="token punctuation">)</span><span class="token punctuation">{</span> array<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>color <span class="token operator">+</span> number<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> array<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"大王"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> array<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"小王"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
// System.out.println(array);
<span class="token comment">//洗牌</span>
Collections<span class="token punctuation">.</span><span class="token function">shuffle</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//发牌</span>
<span class="token comment">//定义三个对象存放三个人的牌,以及三张底牌</span>
ArrayList<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> benben <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ArrayList<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> doudou <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ArrayList<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> qiqi <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ArrayList<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> dipai <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> array<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
String poker <span class="token operator">=</span> array<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">>=</span> array<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
dipai<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>poker<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">3</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
benben<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>poker<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">3</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
doudou<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>poker<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">3</span> <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
qiqi<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>poker<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
// System.out.print(dipai);
<span class="token comment">//看牌</span>
<span class="token function">lookPoker</span><span class="token punctuation">(</span><span class="token string">"笨笨"</span><span class="token punctuation">,</span> benben<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">lookPoker</span><span class="token punctuation">(</span><span class="token string">"兜兜"</span><span class="token punctuation">,</span> doudou<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">lookPoker</span><span class="token punctuation">(</span><span class="token string">"琪琪"</span><span class="token punctuation">,</span> qiqi<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">lookPoker</span><span class="token punctuation">(</span><span class="token string">"底牌"</span><span class="token punctuation">,</span> dipai<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">lookPoker</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> ArrayList poker<span class="token punctuation">)</span><span class="token punctuation">{</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>name <span class="token operator">+</span> <span class="token string">"的牌:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> poker<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>poker<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
//显示结果
笨笨的牌:♣A ♥10 ♥J ♥8 ♦9 大王 ♠4 ♥4 ♦8 ♠6 ♠3 ♣8 ♦k 小王 ♣6 ♥2 ♥5
兜兜的牌:♣9 ♣Q ♠9 ♦7 ♥7 ♥9 ♣J ♥3 ♠8 ♦Q ♣7 ♠A ♦5 ♦A ♠Q ♦10 ♥A
琪琪的牌:♣10 ♠10 ♦4 ♣4 ♠J ♦2 ♣k ♠k ♣5 ♦J ♠2 ♣3 ♣2 ♥k ♥Q ♦6 ♥6
底牌的牌:♠7 ♦3 ♠5
模拟斗地主
需求: 通过程序实现模拟斗地主过长中的发牌、看牌、要求进行排序
public class pokerTest { public static void main(String[] args) { //创建HashMap HashMap<Integer, String> hm = new HashMap<Integer, String>();
<span class="token comment">//创建ArrayList</span> ArrayList<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span> al <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//创建花色和点数数组</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> colors <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"♦"</span><span class="token punctuation">,</span> <span class="token string">"♣"</span><span class="token punctuation">,</span> <span class="token string">"♠"</span><span class="token punctuation">,</span> <span class="token string">"♥"</span><span class="token punctuation">}</span><span class="token punctuation">;</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> numbers <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"3"</span><span class="token punctuation">,</span> <span class="token string">"4"</span><span class="token punctuation">,</span> <span class="token string">"5"</span><span class="token punctuation">,</span> <span class="token string">"6"</span><span class="token punctuation">,</span> <span class="token string">"7"</span><span class="token punctuation">,</span> <span class="token string">"8"</span><span class="token punctuation">,</span> <span class="token string">"9"</span><span class="token punctuation">,</span> <span class="token string">"10"</span><span class="token punctuation">,</span> <span class="token string">"J"</span><span class="token punctuation">,</span> <span class="token string">"Q"</span><span class="token punctuation">,</span> <span class="token string">"K"</span><span class="token punctuation">,</span> <span class="token string">"A"</span><span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">//往HashMap中存储编号和牌,往ArrayList中存储编号</span> <span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String number <span class="token operator">:</span> numbers<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String color <span class="token operator">:</span> colors<span class="token punctuation">)</span><span class="token punctuation">{</span> hm<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>index<span class="token punctuation">,</span> color <span class="token operator">+</span> number<span class="token punctuation">)</span><span class="token punctuation">;</span> al<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> hm<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>index<span class="token punctuation">,</span><span class="token string">"小王"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> al<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span> index<span class="token operator">++</span><span class="token punctuation">;</span> hm<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>index<span class="token punctuation">,</span><span class="token string">"大王"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> al<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//洗牌</span> Collections<span class="token punctuation">.</span><span class="token function">shuffle</span><span class="token punctuation">(</span>al<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//创建三个TreeSet对象,发牌</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span> xiaoming <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span> xiaohong <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span> xiaoli <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span> dipai <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> al<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> x <span class="token operator">=</span> al<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">>=</span> al<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">{</span> dipai<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">3</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> xiaoming<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">3</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> xiaohong<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">%</span> <span class="token number">3</span> <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">{</span> xiaoli<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">//看牌</span> <span class="token function">lp</span><span class="token punctuation">(</span><span class="token string">"小明"</span><span class="token punctuation">,</span> xiaoming<span class="token punctuation">,</span> hm<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">lp</span><span class="token punctuation">(</span><span class="token string">"小红"</span><span class="token punctuation">,</span> xiaoming<span class="token punctuation">,</span> hm<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">lp</span><span class="token punctuation">(</span><span class="token string">"小李"</span><span class="token punctuation">,</span> xiaoli<span class="token punctuation">,</span> hm<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">lp</span><span class="token punctuation">(</span><span class="token string">"底牌"</span><span class="token punctuation">,</span> dipai<span class="token punctuation">,</span> hm<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//看牌的方法</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">lp</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span> treeSet<span class="token punctuation">,</span> HashMap<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">,</span> String<span class="token punctuation">></span></span> hm<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>name <span class="token operator">+</span> <span class="token string">"的牌:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>Integer ts <span class="token operator">:</span> treeSet<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>hm<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>ts<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
//显示结果
小明的牌:♣3 ♠4 ♣5 ♠5 ♥6 ♠6 ♦9 ♥9 ♦J ♦Q ♠Q ♦K ♣K ♠A ♣2 ♥2 大王
小红的牌:♣3 ♠4 ♣5 ♠5 ♥6 ♠6 ♦9 ♥9 ♦J ♦Q ♠Q ♦K ♣K ♠A ♣2 ♥2 大王
小李的牌:♦4 ♥4 ♦6 ♣6 ♦7 ♥7 ♠7 ♦8 ♥8 ♠8 ♠9 ♥Q ♠K ♦A ♣A ♥A ♦2
底牌的牌:♣8 ♥J ♥K
公众号:发哥讲
这是一个稍偏基础和偏技术的公众号,甚至其中包括一些可能阅读量很低的包含代码的技术文,不知道你是不是喜欢,期待你的关注。
代码分享
微信公众号 点击关于我,加入QQ群,即可获取到代码以及高级进阶视频和电子书!!
本文仅仅作为个人转载以及分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题, 请通过 [博客园消息]联系作者处理(删除),如需商业授权请联系原作者/原网站。
参考链接: https://blog.csdn.net/joker_Ling/article/details/108416699