• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java实现简单的抽牌游戏

java 搞代码 4年前 (2022-01-05) 16次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了Java实现简单的抽牌游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现简单抽牌游戏的具体代码,供大家参考,具体内容如下

Main类

 package com.company; import java.util.*; public class Main { public static void main(String[] args) { Poke p = new Poke(); p.shuffle(); System.out.println("您想抽几张牌?"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println("抽取了"+n+"张牌,分别为:"); Card[] c = p.draw(n); for (Card g :c ) System.out.print(g); System.out.println(); p.sortOut(c); System.out.println("理牌完成!"); for (Card g :c ) System.out.print(g); } }

Poke类

 package com.company; import java.util.Arrays; /** * Created by ttc on 16-11-2. */ public class Poke { Card[] m_card = null; int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; String[] colors = {"♡", "♠", "♢", "♧"}; public Poke() { m_card = new Card[52]; for (int i = 0; i <colors.length; i++) { for (int j = 0; j <values.length; j++) { m_card[i * values.length + j] = new Card(values[j], colors[i]); } } } public void outPut() { //展示当前牌序 for (int i = 0; i <m_card.length; i++) { if (i % 13 == 0) System.out.println(); System.out.print(m_card<a style="color:transparent">来源gao($daima.com搞@代@#码网</a>[i]); } } public void shuffle() { //洗牌 Card tempC = null; for (int i = 0; i <52; i++) { tempC = m_card[i]; int j = (int) (Math.random() * 51); m_card[i] = m_card[j]; m_card[j] = tempC; } System.out.print("洗牌完成!"); } public Card[] draw(int n) { //抽N张牌 Card[] c = new Card[n]; for (int i = 0; i </div><p>Card类</p><div class="gaodaimacode"><pre class="prettyprint linenums"> package com.company; /** * Created by ttc on 16-11-2. */ public class Card implements Comparable { private int m_values; private String m_colors; public Card(int m_values, String m_colors) { this.m_values = m_values; this.m_colors = m_colors; } @Override public int compareTo(Object o) { if (this.m_values > ((Card)o).m_values) return 1; else if(this.m_values == ((Card)o).m_values) return 0; else return -1; } @Override public String toString() { String strtmp; switch (m_values) { case 1: strtmp = "A"; break; case 11: strtmp = "J"; break; case 12: strtmp = "Q"; break; case 13: strtmp = "K"; break; default: strtmp = String.valueOf(m_values); } return m_colors + strtmp + "\t"; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Java实现简单的抽牌游戏的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java实现简单的抽牌游戏
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址