成都网站建设设计

将想法与焦点和您一起共享

纸牌比大小java代码 扑克牌比大小java编程

JAVA中随机生成各有13张扑克牌(每张牌从“A”到“K”)的两组,比较两组数对应位置上牌点数大小的程序?

package card;

创新互联主营达茂旗网站建设的网络公司,主营网站建设方案,成都App制作,达茂旗h5小程序设计搭建,达茂旗网站营销推广欢迎达茂旗等地区企业咨询

import java.util.Hashtable;

public class Card {

/**

* 0:方块 3:黑桃 4:王

*/

public static final int DIAMODE=0;

/**1:梅花*/

public static final int CLUB=1;

/**2:红桃*/

public static final int HEART=2;

/**3:黑桃*/

public static final int SPADE=3;

/**4:王*/

static final int WANG=4;

/**0:方块 3:黑桃 4:王*/

private static String[] SUIT_NAMES=new String[]{"方块","梅花","红桃","黑桃","王"};

public static final int THREE=0;

public static final int FOUR=1;

public static final int FIVE=2;

public static final int SIX=3;

public static final int SEVEN=4;

public static final int EIGHT=5;

public static final int NINE=6;

public static final int TEN=7;

public static final int JACK=8;

public static final int QUEEN=9;

public static final int KING=10;

public static final int ACE=11;

public static final int DUCK=12;

public static final int BALCK=13;

public static final int COLOR=14;

public static String[] RANK_NAMES=new String[]{"3","4","5","6","7"

,"8","9","10","J","Q","K","A","2","小","大"};

private int suit,rank;

// private static class Entry{

// private String name;

// private int value;

// public Entry(String name,int value) {

// this.name=name;

// this.value=value;

// }

// public int getValue() {

// return value;

// }

// public void setValue(int value) {

// this.value = value;

// }

// @SuppressWarnings("unused")

// public String getName() {

// return name;

// }

// @SuppressWarnings("unused")

// public void setName(String name) {

// this.name = name;

// }

// }

//public static Entry[] table = new Entry[120];

static HashtableString, Integer table = new HashtableString,Integer();

static{

for (int i = 0; i SUIT_NAMES.length; i++) {

// Entry entry = new Entry(SUIT_NAMES[i],i);

// table[SUIT_NAMES[i].hashCode()%120]=entry;

table.put(SUIT_NAMES[i], i);

}

for (int i = 0; i RANK_NAMES.length; i++) {

//Entry entry = new Entry(RANK_NAMES[i],i);

//table[RANK_NAMES[i].hashCode()%120]=entry;

table.put(RANK_NAMES[i], i);

}

}

public int getSuit() {

return suit;

}

public void setSuit(int suit) {

this.suit = suit;

}

public int getRank() {

return rank;

}

public void setRank(int rank) {

this.rank = rank;

}

public Card(int suit,int rank) {

setSuit(suit);

setRank(rank);

}

public Card(String strName){

String suitName;

String rankName;

if(strName.endsWith("王")){

suitName=strName.substring(1);

rankName=strName.substring(0,1);

}else{

suitName=strName.substring(0,2);

rankName=strName.substring(2);

}

setSuit(findSuit(suitName));

setRank(findRank(rankName));

}

private int findSuit(String suitName) {

//return table[suitName.hashCode()%120].getValue();

return table.get(suitName);

}

private int findRank(String rankName) {

return table.get(rankName);

}

@Override

public String toString() {

if(suit==Card.WANG)

return RANK_NAMES[rank]+SUIT_NAMES[suit];

return SUIT_NAMES[suit]+RANK_NAMES[rank];

}

@Override

public boolean equals(Object obj) {

if(obj==null)

return false;

if(obj==this)

return true;

if(obj instanceof Card){

Card other=(Card)obj;

return other.rank==rank other.suit==suit;

}

return false;

}

@Override

public int hashCode() {

return rank*10+suit;

}

}

/*************************************************************************************/

package card;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.List;

public class CardDemo {

/**

* @param args

*/

public static void main(String[] args) {

// Card c=new Card(Card.HEART,Card.THREE);

// System.out.println(c.toString());

// c=new Card("黑桃A");

// System.out.println(c.toString());

ListCard cards = new ArrayListCard();

for (int i = 0; i = Card.DUCK; i++) {

for (int j = 0; j = Card.SPADE; j++) {

cards.add(new Card(j, i));

}

}

cards.add(new Card("小王"));

cards.add(new Card("大王"));

// Random r = new Random();

// for (int i =1; i cards.size(); i++) {

// int j=r.nextInt(i);

// Card temp=cards.get(i);

// cards.set(i, cards.get(j));

// cards.set(j, temp);

// }

Collections.shuffle(cards);

System.out.println(cards.toString());

Player[] players = new Player[3];

players[0] = new Player(0, "萧炎");

players[1] = new Player(1, "罗峰");

players[2] = new Player(2, "小胖");

// for (int i = 0; i cards.size();i++) {

// players[i%players.length].add(cards.get(i));

// }

// for (int i = 0; i players.length; i++) {

// System.out.println(players[i].toString());

// }

IteratorCard ite = cards.iterator();

int c=0;

while(ite.hasNext()){

players[(c++)%players.length].add(ite.next());

ite.remove();

}

for (int i = 0; i players.length; i++) {

System.out.println(players[i]);

}

}

}

JAVA编程判断扑克牌大小

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

ListString list = new ArrayListString();

while (scanner.hasNextLine()) {

String string = scanner.nextLine();

list.add(string);

if (list.size() == 5) {

int r = getCard(list);

System.out.println(r);

list.clear();

}

}

}

private static int getCard(ListString list) {

int temp = 7;

int size = list.size();

ListString numList = new ArrayListString();

SetString numSet = new TreeSetString();

ListString colourList = new ArrayListString();

SetString colourSet = new TreeSetString();

for (int i = 0; i  list.size(); i++) {

String num = list.get(i).split(" ")[0];

String colour = list.get(i).split(" ")[1];

if (num.equals("J")) {

numList.add("11");

numSet.add("11");

} else if (num.equals("Q")) {

numList.add("12");

numSet.add("12");

} else if (num.equals("K")) {

numList.add("13");

numSet.add("13");

} else if (num.equals("A")) {

numList.add("14");

numSet.add("14");

} else {

numList.add(num);

numSet.add(num);

}

colourList.add(colour);

colourSet.add(colour);

}

// 同花顺,或者同花

if (colourSet.size() == 1) {

Collections.sort(numList);

for (int i = 0; i  numList.size() - 1; i++) {

if (Integer.parseInt(numList.get(i + 1)) - Integer.parseInt(numList.get(i)) == 1) {

if (i == numList.size() - 2) {

temp = 1;

}

continue;

} else {

if (temp  4) {

temp = 4;

}

}

}

}

// 四条或者葫芦

if (numSet.size() == 2) {

Collections.sort(numList);

// 四条

if (!numList.get(0).equals(numList.get(1)) || !numList.get(size - 1).equals(numList.get(size - 2))) {

if (temp  2) {

temp = 2;

}

} else {

if (temp  3) {

temp = 3;

}

}

}

// 三条

if (numSet.size() == 3) {

if (temp  6) {

temp = 6;

}

}

// 顺子

if (colourSet.size()  1) {

Collections.sort(numList);

for (int i = 0; i  numList.size() - 1; i++) {

if (Integer.parseInt(numList.get(i + 1)) - Integer.parseInt(numList.get(i)) == 1) {

if (i == numList.size() - 2) {

if(temp5){

temp = 5;

}

}

} else {

break;

}

}

}

return temp;

}

}

用java如何判断扑克牌同花顺

扑克牌有两个属性,花色和牌面大小,可以分别比较花色和牌面大小是否递增或递减来确定是不是同花顺.但是由于扑克牌会有JQKA,所以牌面大小最好以数组或者字符串的方式实现,通过比较下标来比较大小,以下是大概的代码

public class PuKe {

private String color;

private String num;

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public String getNum() {

return num;

}

public void setNum(String num) {

this.num = num;

}

public PuKe(String color, String num) {

super();

this.color = color;

this.num = num;

}

public static void main(String[] args) {

ListPuKecard = new ArrayListPuKe();

card.add(new PuKe("black", "2"));

card.add(new PuKe("black", "3"));

card.add(new PuKe("black", "4"));

card.add(new PuKe("black", "5"));

card.add(new PuKe("black", "6"));

//这里因为10是两位数且牌面里面用不到1,所以直接用1代替10

String check = "234567891JQKA";

boolean flage =true;

//自定义比较器,用num在check里的下标大小作为比较依据排序

Collections.sort(card, new ComparatorObject() {

@Override

public int compare(Object o1, Object o2) {

PuKe er1 = (PuKe) o1;

PuKe er2 = (PuKe) o2;

Integer org1 = (int)(er1.getNum() == null ? 0.0 : check.indexOf(er1.getNum()));

Integer org2 = (int)(er1.getNum() == null ? 0.0 : check.indexOf(er2.getNum()));

return org1.compareTo(org2);

}

});

for (int i = 0; i  card.size()-1; i++) {

//遍历所有的牌,如果颜色不一样直接跳出循环,不是同花顺

if(!card.get(i).getColor().equals(card.get(i+1).getColor())){

flage =false;

break;

}

//如果上一张减去下一张的差值不是1,说明牌面不是顺子,也不是同花顺,跳出循环

if((int)check.indexOf(card.get(i+1).getNum())-(int)check.indexOf(card.get(i).getNum())!=1){

flage = false;

break;

}

}

if(flage){

System.out.println("牌面是同花顺");

}else{

System.out.println("牌面不是同花顺");

}

}

}

不过这种方法唯一不好的地方是check中不能直接用10,将来展示的时候需要判定,展示的是1时直接改成10.

其实实现方法很多,我这么写是因为最近在复习,这么写用到的知识点相对会多一点.


当前文章:纸牌比大小java代码 扑克牌比大小java编程
本文地址:http://chengdu.cdxwcx.cn/article/doeiijo.html