简单的Java代码实现斗地主
斗地主综合分析:
1.准备牌:54张牌存储到一个集合中
特殊牌:大王,小王
其他52张牌:
定义一个数组/集合,存储4种花色;
定义一个数组/集合,存储13个序号
循环嵌套遍历两个数组/集合,组装52张牌
2.洗牌
使用集合工具Collections的方法
static void shuffle(List<?> list)
随机打乱集合中元素的顺序
3.发牌
1人17张牌为底牌,轮流发牌,集合的索引(0-53)%3
定义4个集合,存储3个玩家的牌和底牌
4.看牌
直接打印集合,遍历存储玩家和底牌的集合
代码
import java.util.ArrayList;
import java.util.Collections;
public class DouDiZhu {
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//1.准备牌</span>
ArrayList<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> poker <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</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">"A"</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">"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 punctuation">;</span>
<span class="token comment">//存储大小王</span>
poker<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>
poker<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>
<span class="token comment">//循环遍历两个数组,组装52张牌</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>
poker<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>
<span class="token comment">/*
2.洗牌
使用集合的工具类Collections中的方法
static void shuffle(List<?> list) 使用默认随机源对指定列表进行置换
*/</span>
Collections<span class="token punctuation">.</span><span class="token function">shuffle</span><span class="token punctuation">(</span>poker<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">/*
3.发牌
*/</span>
<span class="token comment">//定义4个集合,存储玩家的牌和底牌</span>
ArrayList<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> player01 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></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> player02 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></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> player03 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></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 operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">/*
遍历poker集合,获取每一张牌
使用poker集合的索引%3给3个玩家轮流发牌
剩余3张牌给底牌
注意:
先判断底牌(i >=51)
*/</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>
String p <span class="token operator">=</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 punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>i <span class="token operator">>=</span><span class="token number">51</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>p<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>
player01<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>p<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>
player02<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>p<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>
player03<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">/*
4.发牌
*/</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 string">"张曼玉"</span> <span class="token operator">+</span> player01<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 string">"王祖贤"</span> <span class="token operator">+</span> player02<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 string">"林青霞"</span> <span class="token operator">+</span> player03<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 string">"底牌"</span> <span class="token operator">+</span> dipai<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
如果你是刚开始学习java,或者刚开始从事java行业,有很多的问题都可以关注微信公众号: java学长 ,一个致力于打造免费指导学习java高薪就业的公益平台!点赞、关注 哦,不定期分享程序员的骚操作和强势浪漫!