java 52张 扑克牌 问题

java 52张 扑克牌 问题,第1张

识别类:

一张牌:public class Card{};

一副牌:public class Poker{};

带main方法的类用于实现洗牌和切牌:public class Shuffle{};

识别属性:

class Card:

一张牌应该具有花色和点数这两个属性:

private color; //花色

private value; //点数

class Poker:

一副牌有54张牌,四种花色,每种花色都有2~A的点数:

static Card[] cards=new Card[54];// 每副扑克都有54张牌;

static String[] colors={"红桃","黑桃","方块","梅花"}; //每副牌都有四种花色;

static String values[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};//每个花

色的点数

定义类:

public class Card{

String color;

String value;

//定义一个构造函数用于初始化点数2~A的牌

public Card(String color,String value){

thiscolor = color;

thisvalue = value;

}

//定义一个构造函数用于初始化大王和小王

public Card(String value){

thiscolor=""; //如果不给color赋值的话,洗牌和切牌后的大小王前面会显示“null”

thisvalue = value;

}

// 取一张牌的花色

public String getcolor() {

return color;

}

// 取一张牌的值

public String getvalue() {

return value;

}

}

public class Poker{

static Card[] cards=new Card[54];

static String[] colors={"红桃","黑桃","方块","梅花"};

static String values[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

//构造方法Poker()用于初始化这副扑克

public Poker() {

for (int i = 0; i < colorslength; i++) {

for (int j = 0; j < valueslength; j++) {

cards[i 13 + j] = new Card(colors[i], values[j]);

}

}

cards[52] = new Card("小王");

cards[53] = new Card("大王");

}

//方法getCard()用于获取一张牌的花色和点数

public Card[] getCard() {

return Pokercards;

}

//方法Show()用于显示一副新的扑克

public void Show() {

……………………

}

public class Shuffle {

static Poker poker = new Poker();

static Card[] cards = new Card[54];

//方法random用于返回一个不重复的数组

public static int[] random(int length) {

int[] a = new int[length];

for (int i = 0; i < length; i++) {

a[i] = (int) (Mathrandom() length);//产生一个0到length的随机数

for (int j = 0; j < i; j++) {

if (a[j] == a[i]) {//比较是否相等,如果相等则重新产生

i--;

break;

}

}

}

return a;

}

//方法xipai()用于将所得的随机数组的值赋值给牌的下标

public static void xipai() {

int[] b = new int[54];

b = random(54);//调用random方法产生一个长度为54的不重复的随机数组

cards = pokergetCard();

Card temp = null;

for(int i = 0; i < blength; i++){ //开始遍历数组实现重新排列

int k = b[i];

temp = cards[k];

cards[i] = cards[k];

cards[k] = temp;}

//输出洗牌后的扑克

……………………

……………………

}

//方法qiepai()实现扑克的切牌 *** 作

public static void qiepai(int length) {

int number = (int) (Mathrandom() length);//产生一个随机数

Systemoutprintln("随机数为:" + number);//显示随机数是多少

Card[] temp = new Card[length];

int start = number;//设置切牌后的起点位置

for (int i = 0; i < length - number; i++) {

temp[i] = cards[start++];//将切点后面的牌暂时放入temp数组中

}

for (int j = 0; j < number; j++) {

temp[length - number + j] = cards[j];//将切点前面的牌加入temp数组中

}

for (int k = 0; k < length; k++) {

cards[k] = temp[k];}

//输出切牌后的扑克

……………………

……………………

}

//main方法

public static void main(String[] args) {

Poker newpoker = new Poker();

newpokerShow();//调用Show()方法显示一副新扑克

xipai();//调用xipai()方法显示洗牌过后的扑克

qiepai(54);//调用qiepai()方法显示切牌以后的扑克

}

}

参考资料:

>

代码仅供参考,如有疑问,欢迎追问:

import javautilArrayList;

import javautilList;

import javautilScanner;

/

  随机扑克牌

  @author STONE

  @version 2015-10-28

 /

public class Poker {

//用于记录丢失的手牌数

private static Integer lostTotal = 0;

//用于存放已下发的扑克

private static Integer[] its1 = new Integer[54];

//用于存放当前的扑克

private static Integer[] its2 = new Integer[54];

private static List<Integer> lostList = new ArrayList<Integer>();

public static void main(String[] args) {

Scanner scn = new Scanner(Systemin);

for(int i=0;i<54;i++){

its1[i] = 0;

its2[i] = 0;

}

Systemoutprintln("系统开始发牌");

givePoker(5);

Systemoutprintln("发牌完毕,当前手牌为:");

printPoker(its2);

Integer gameTotal = 0;

while(gameTotal<10){//循环跑10次,请注意此处发牌没做限制,如果循环次数过大,会导致死循环

Systemoutprintln("请输入需要丢弃的手牌(多张牌格式如下:黑桃1,黑桃2,黑桃3):");

String lost = scnnextLine();

checkLost(lost);

Systemoutprintln("丢失手牌完毕,当前手牌:");

printPoker(its2);

Systemoutprintln("系统开始重发手牌");

givePoker(lostTotal);

Systemoutprintln("重发发牌完毕,当前手牌为:");

printPoker(its2);

gameTotal++;

}

scnclose();

}

public static void givePoker(Integer num){//随机分发手牌

int total = 0;

while(total<num){

int poke = (int)(Mathrandom()54);

if(its1[poke] == 0){

its1[poke] = 1;

its2[poke] = 1;

total++;

}

}

lostTotal = 0;

}

public static Boolean checkLost(String lost){//检查需要丢失的手牌

if(lost==null || lostequals("")){

Systemoutprintln("输入手牌错误,系统无法处理");

return false;

}

if(lostindexOf("黑桃")<0 && lostindexOf("红心")<0 && lostindexOf("梅花")<0 && lostindexOf("方块")<0){

Systemoutprintln("输入手牌错误,系统无法处理");

return false;

}

String[] strs = lostsplit(",");

Boolean bol = false;

for(String str : strs){

str = strtrim();

if(str==null || strequals("")) continue;//去掉,防止空格

bol = getPokerNum(str);

}

if(bol){

losePoker();

}

return false;

}

public static void losePoker(){//丢掉手牌

lostTotal = lostListsize();

for(Integer itr : lostList){//丢掉手牌

its2[itr-1] = 0;

}

lostList = new ArrayList<Integer>();

}

public static Boolean getPokerNum(String str){//获取手牌点数并去掉

try{

Integer itr = 0;

if(strindexOf("黑桃")==0){

str = strreplace("黑桃", "");

itr = IntegerparseInt(str);

if(itr>13){

Systemoutprintln("输入手牌不存在:黑桃"+str);

return false;

}

if(its2[itr-1]==1){

lostListadd(itr);

}

} else if(strindexOf("红心")==0){

str = strreplace("红心", "");

itr = IntegerparseInt(str);

if(itr>13){

Systemoutprintln("输入手牌不存在:红心"+str);

return false;

}

if(its2[itr+12]==1){

lostListadd(itr+13);

}

} else if(strindexOf("梅花")==0){

str = strreplace("梅花", "");

itr = IntegerparseInt(str);

if(itr>13){

Systemoutprintln("输入手牌不存在:梅花"+str);

return false;

}

if(its2[itr+25]==1){

lostListadd(itr+26);

}

} else if(strindexOf("方块")==0){

str = strreplace("方块", "");

itr = IntegerparseInt(str);

if(itr>13){

Systemoutprintln("输入手牌不存在:方块"+str);

return false;

}

if(its2[itr+38]==1){

lostListadd(itr+39);

}

} else if(strindexOf("小王")==0){

if(its2[52]==1){

lostListadd(53);

}

} else if(strindexOf("大王")==0){

if(its2[53]==1){

lostListadd(54);

}

}

return true;

} catch( Exception e ){

Systemoutprintln("输入手牌有误");

return false;

}

}

public static void printPoker(Integer[] its){//打印当前手牌

String result = "";

for(int i=0;i<itslength;i++){

if(its[i]==1){//等于1表示当前手牌存在

result+=pukerCheck(i+1)+",";

}

}

Systemoutprintln(result);

}

public static String pukerCheck(Integer itr){//判断扑克类型

/

  1~13黑桃花色、14~26红心花色、27~39梅花花色

  40~52方块花色、53小王、54大王

 /

if(1<=itr && itr<=13){

return "黑桃"+itr;

} else if(14<=itr && itr<=26){

return "红心"+(itr-13);

} else if(27<=itr && itr<=39){

return "梅花"+(itr-26);

} else if(40<=itr && itr<=52){

return "方块"+(itr-39);

} else if(itr==53){

return "小王";

} else if(itr==54){

return "大王";

}

return "";

}

}

测试运行结果:

import javautilArrayList;

import javautilRandom;

public class PokerDemo {

ArrayList<Card> poker = new ArrayList<Card>();

Player[] players;

int n = 4;

String[] name = {"Wang", "Zhang", "Li", "Yuan"};

public PokerDemo() {

players = new Player[n];

for(int i = 0; i < playerslength; i++)

players[i] = new Player(name[i]);

for(int i = 0; i < 4; i++)

for(int j = 0; j < 13; j++)

pokeradd(new Card(i, j));

Systemoutprintln("洗牌前: " + poker);

xipai();

Systemoutprintln("洗牌后: " + poker);

fapai();

Systemoutprintln("发牌后: ");

for(int i = 0; i < playerslength; i++)

Systemoutprintln(players[i]);

}

public void xipai() {

Random r = new Random();

ArrayList<Card> tmp = new ArrayList<Card>();

while(!pokerisEmpty()) {

int index = rnextInt(pokersize());

Card card = pokerget(index);

tmpadd(card);

pokerremove(index);

}

poker = new ArrayList<Card>(tmp);

}

public void fapai() {

int p = 0;

while(!pokerisEmpty()) {

Card card = pokerget(0);

players[p]add(card);

p = (p + 1) % n;

pokerremove(0);

}

}

public static void main(String[] args) {

new PokerDemo();

}

}

class Card {

int val, sty;

final String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

final String[] style = {"黑桃", "红心", "梅花", "方块"};

public Card(int s, int v) {

sty = s;

val = v;

}

public String toString() {

return style[sty]+ " " + value[val];

}

}

class Player {

ArrayList<Card> cards = new ArrayList<Card>();

String name;

public Player(String n) {

name = n;

}

public void add(Card card) {

cardsadd(card);

}

public String toString() {

return name + ":\t" + cards;

}

}

不想写,只说思路吧

1,用Link当抽取一张后就删除抽取的那个节点

2用stack,stack里保存的扑克随机生机,那么抽取时也是随便也保存不会抽取到已经抽取的

还有其他的方法我就不说了,够用就行了

我不知道电脑出牌为什么要进行大小比较以及删牌 *** 作 我的是52张牌若干玩家,随机发牌给玩家,玩家收到电脑发的牌。

这个是定义52张牌:Cardjava

package cometcweek4day03;

public class Card {

// 点数 3 ---0 4 1 12 2 11 A 13 小王 14 大王

public int rank;

public final static int THREE = 0;

public final static int FOUR = 1;

public final static int FIVE = 2;

public final static int SIX = 3;

public final static int SEVEN = 4;

public final static int EIGHT = 5;

public final static int NINE = 6;

public final static int TEN = 7;

public final static int JACK = 8;//J

public final static int QUEEN = 9;//Q

public final static int KING = 10;//K

public final static int ACE = 11;//A

public final static int DEUCE = 12; //2

public final static int BLACK = 13; //小王

public final static int COLOR = 14;//大王

//花色(suit>=0&&suit<=4)

private int suit;

public final static int CLUB = 0; //梅花

public static final int DIAMOND = 1; //方块(钻石)

public static final int HEART = 2; //红桃(红心)

public static final int SPADE = 3; //黑桃(花锄)

public static final int JOKER = 4; //王

public Card(int suit,int rank){

thissetSuit(suit);

thissetRank(rank);

}

public Card(){}

public int getRank() {

return rank;

}

public void setRank(int rank) {

if(rank<CardTHREE||rank>CardCOLOR){

throw new RuntimeException();

}

thisrank = rank;

}

public int getSuit() {

return suit;

}

public void setSuit(int suit) {

if(suit<CardCLUB || suit>CardJOKER){

throw new RuntimeException();

}

thissuit = suit;

}

// 点数对应 名称

public final static String[] RANK_NAMES={

"3","4","5","6","7","8","9","10","J","Q",

"K","A","2","小王","大王"};

public final static String[] SUIT_NAMES={

"梅花","方块","红桃","黑桃",""};

@Override

public String toString() {

return SUIT_NAMES[thissuit]+RANK_NAMES[thisrank];

}

public static void main(String[] args) {

Card c =new Card(HEART, ACE);

Systemoutprintln(c);

}

}

这个是定义若干玩家Playerjava

package cometcweek4day03;

import javautilArrays;

public class Player {

String id;

String name;

Card cards[] ={};

public void addCards(Card card ){

// 每个玩家获取的牌

cards = ArrayscopyOf(cards, cardslength+1);

cards[cardslength-1] = card;

}

public String getId() {

return id;

}

public void setId(String id) {

thisid = id;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public Card[] getCards() {

return cards;

}

public void setCards(Card[] cards) {

thiscards = cards;

}

public Player(String id, String name) {

thisid = id;

thisname = name;

}

public Player() {

super();

}

@Override

public String toString() {

return "Player:"+name+" cards="

+ ArraystoString(cards) + "]";

}

}

这个是电脑随机发牌给玩家,玩家收到电脑发的牌PlayDemojava

package cometcweek4day03;

import javautilArrays;

import javautilRandom;

public class PlayerDemo {

public static void main(String[] args) {

// 创建一副扑克牌

// 方块:13张(THREE~DEUCE)

// 梅花:13张(THREE~DEUCE)

// 黑桃:13张(THREE~DEUCE)

// 红桃:13张(THREE~DEUCE)

// 王: BLACK COLOR

Card [] cards = new Card[54];

int i=0;

// 构造52张普通的牌

for(int rank=CardTHREE;rank<=CardDEUCE;rank++){

cards[i++] = new Card(CardDIAMOND, rank);

cards[i++] = new Card(CardCLUB, rank);

cards[i++] = new Card(CardSPADE, rank);

cards[i++] = new Card(CardHEART, rank);

}

// 添加 大小王

cards[i++] = new Card(CardJOKER, CardBLACK);

cards[i++] = new Card(CardJOKER, CardCOLOR);

// 将数组 变成集合 List

// List arr= ArraysasList(cards);

// 将集合里面的元素 打乱 洗牌

//Collectionsshuffle(arr);

// 洗牌//

for(int n=cardslength-1;n>=1;n--){

Random rd = new Random();

int index = rdnextInt(n); //1 0

Card t = cards[n];

cards[n] = cards[index];

cards[index] = t;

}

// Systemoutprintln("=====测试洗牌\n"+ArraystoString(cards));

// 组建玩家团队

Player[] players = new Player[4];

players[0] = new Player("1010", "张柏芝");

players[1] = new Player("1011", "曾志伟");

players[2] = new Player("1012", "谭咏麟");

// players[3] = new Player("1013", "郭德纲");

// 发牌

for(int j =0;j<cardslength;j++){

players[j%playerslength]addCards(cards[j]);

}

// 显示各个玩家的牌

for(int m=0;m<playerslength;m++){

Systemoutprintln(players[m]);

}

}

}

1定义牌类

public class Card{

public int id;//牌大小

public String number;//牌值名称

public String color;//花色

}

2定义牌组,用ArrayList存放所有牌

ArrayList<Card> cardGroup =new ArrayList<Card>();

3定义花色枚举

public static final String[] COLORS=new String[]{"方块","梅花","红桃","黑桃"};

4定义牌值大小枚举

public static fina String[] CARDVALUES=new String[]{"A","1","2","3",

"4","5","6","7","8","9","10","J","Q","K","King_Small","King_Big"};

5初始化牌组,这就是一副新牌,没切过,己经排好序的

public void initCardGroup(){

//加入A~K

for(int i=0;i<13;i++){

for (int j=0;j<4;j++){

//生成一张牌的对象

Card card=new Card();

cardid=i;

cardcolor=COLORS[j];

cardmunber=CARDVALUES[i];

//将对象加入牌组

cardGroupadd(card);

}

}

//加入大小王

Card card_k1=new Card();

card_k1id=13;

card_k1color="King_Small";

card_k1number="King_Small";

cardGroupadd(card_k1);

Card card_k2=new Card();

card_k2id=14;

card_k2color="King_big";

card_k2number="King_big";

cardGroupadd(card_k2);

}

6 洗牌,将原牌组打乱,得到新的牌组

public ArrayList<Card> flushCardsGroup(ArrayList<Card> src){

ArrayList<Card> result=new ArrayList<Card>();

while(srcsize>0){

int size=srcsize();

//以size为限,生成0~size之间的随机数

Random r=new Random();

int index=rnextInt(size);

//对index做个检查

if(index>0){index=0;}

if(index>size-1){index=size-1;}

//index就是捡出来的牌,加到新的数组中,从原来数组中删除

Card card=srcget(index);

resultadd(card);

cardremove(index);

}

return result;

}

以上就是关于java 52张 扑克牌 问题全部的内容,包括:java 52张 扑克牌 问题、java编写扑克牌游戏里,洗牌时发牌时用到了随机分配的功能,请问在那个包中哪个类哪个方法、java的问题(扑克牌)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9331605.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存