Java实现简单的抽牌游戏
作者:Testarossa118 时间:2022-10-06 11:55:01
本文实例为大家分享了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[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 < n ; i++) c[i] = m_card[i];
return c;
}
public void sortOut(Card[] c)
{
//理牌
Arrays.sort(c);
}
}
Card类
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";
}
}
来源:https://blog.csdn.net/Testarossa118/article/details/53008736
标签:java,抽牌
0
投稿
猜你喜欢
SpringBoot自定义注解实现Token校验的方法
2023-11-13 23:17:52
Java 实现Excel文档添加超链接的代码
2021-09-05 20:06:52
Android开发悬浮按钮 Floating ActionButton的实现方法
2023-05-02 10:44:07
Android图片三级缓存策略(网络、本地、内存缓存)
2022-08-06 01:28:19
spring基础概念AOP与动态代理理解
2022-01-29 20:55:13
Android 6.0区别U盘和SD卡设备的方法详解
2022-09-09 13:10:58
Android app第三方支付宝支付接入教程
2022-06-05 20:02:19
java实现文件重命名
2023-08-26 09:27:46
java读取properties文件的方法
2021-12-25 11:30:36
SpringBoot2 实现JPA分页和排序分页的案例
2022-02-23 13:26:08
Java实现的两个线程同时运行案例
2023-05-14 09:10:40
Android中TextView动态设置缩进距离的方法
2023-08-07 09:52:00
Java中的stream流的概念解析及实际运用总结
2022-06-10 23:19:10
Java面向对象之抽象类,接口的那些事
2022-08-25 19:16:30
浅谈Java中复制数组的方式
2022-04-14 23:30:27
功能强大的TraceId 搭配 ELK使用详解
2021-09-16 02:26:43
Java 关于String字符串原理上的问题
2021-05-26 12:48:44
C# XmlDocument操作XML案例详解
2021-11-23 04:00:01
Java 网络爬虫基础知识入门解析
2022-05-16 02:39:06
关于IDEA配置文件字符集的问题
2022-11-01 13:06:44