高分急求java扑克出牌代码
我不知道电脑出牌为什么要进行大小比较以及删牌操作? 我的是52张牌若干玩家,随机发牌给玩家,玩家收到电脑发的牌。
成都创新互联公司于2013年开始,先为靖州等服务建站,靖州等地企业,进行企业商务咨询服务。为靖州企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
这个是定义52张牌:Card.java
package com.etc.week4.day03;
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=0suit=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){
this.setSuit(suit);
this.setRank(rank);
}
public Card(){}
public int getRank() {
return rank;
}
public void setRank(int rank) {
if(rankCard.THREE||rankCard.COLOR){
throw new RuntimeException();
}
this.rank = rank;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
if(suitCard.CLUB || suitCard.JOKER){
throw new RuntimeException();
}
this.suit = 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[this.suit]+RANK_NAMES[this.rank];
}
public static void main(String[] args) {
Card c =new Card(HEART, ACE);
System.out.println(c);
}
}
这个是定义若干玩家Player.java
package com.etc.week4.day03;
import java.util.Arrays;
public class Player {
String id;
String name;
Card cards[] ={};
public void addCards(Card card ){
// 每个玩家获取的牌
cards = Arrays.copyOf(cards, cards.length+1);
cards[cards.length-1] = card;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card[] getCards() {
return cards;
}
public void setCards(Card[] cards) {
this.cards = cards;
}
public Player(String id, String name) {
this.id = id;
this.name = name;
}
public Player() {
super();
}
@Override
public String toString() {
return "Player:"+name+" cards="
+ Arrays.toString(cards) + "]";
}
}
这个是电脑随机发牌给玩家,玩家收到电脑发的牌PlayDemo.java
package com.etc.week4.day03;
import java.util.Arrays;
import java.util.Random;
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=Card.THREE;rank=Card.DEUCE;rank++){
cards[i++] = new Card(Card.DIAMOND, rank);
cards[i++] = new Card(Card.CLUB, rank);
cards[i++] = new Card(Card.SPADE, rank);
cards[i++] = new Card(Card.HEART, rank);
}
// 添加 大小王
cards[i++] = new Card(Card.JOKER, Card.BLACK);
cards[i++] = new Card(Card.JOKER, Card.COLOR);
// 将数组 变成集合 List
// List arr= Arrays.asList(cards);
// 将集合里面的元素 打乱... 洗牌
//Collections.shuffle(arr);
// 洗牌//
for(int n=cards.length-1;n=1;n--){
Random rd = new Random();
int index = rd.nextInt(n); //1 0
Card t = cards[n];
cards[n] = cards[index];
cards[index] = t;
}
// System.out.println("=====测试洗牌.....\n"+Arrays.toString(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;jcards.length;j++){
players[j%players.length].addCards(cards[j]);
}
// 显示各个玩家的牌
for(int m=0;mplayers.length;m++){
System.out.println(players[m]);
}
}
}
急急急!!!那个Java大神能帮我编一下这个代码,是关于扑克牌的问题?
package zhidao;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
* 发牌游戏
*/
public class PokerGame
{
/**
* 共有1幅扑克牌,不包括大王和小王共52张牌。
*/
public static final int POKER_NUMBER = 52;
/**
* 特殊分隔符 _
*/
private static final String T = "_";
/**
* 52张牌br /
* 顺序为黑桃、红心、草花、方块
*/
public static final String[] POKERS =
{
"A" + T + "0", "A" + T + "1", "A" + T + "2", "A" + T + "3",
"2" + T + "0", "2" + T + "1", "2" + T + "2", "2" + T + "3",
"3" + T + "0", "3" + T + "1", "3" + T + "2", "3" + T + "3",
"4" + T + "0", "4" + T + "1", "4" + T + "2", "4" + T + "3",
"5" + T + "0", "5" + T + "1", "5" + T + "2", "5" + T + "3",
"6" + T + "0", "6" + T + "1", "6" + T + "2", "6" + T + "3",
"7" + T + "0", "7" + T + "1", "7" + T + "2", "7" + T + "3",
"8" + T + "0", "8" + T + "1", "8" + T + "2", "8" + T + "3",
"9" + T + "0", "9" + T + "1", "9" + T + "2", "9" + T + "3",
"10" + T + "0", "10" + T + "1", "10" + T + "2", "10" + T + "3",
"J" + T + "0", "J" + T + "1", "J" + T + "2", "J" + T + "3",
"Q" + T + "0", "Q" + T + "1", "Q" + T + "2", "Q" + T + "3",
"K" + T + "0", "K" + T + "1", "K" + T + "2", "K" + T + "3"
};
/**
* 原始的扑克牌
*/
public static final String[] ORIAL_POKERS = POKERS.clone ();
/**
* 发到哪张牌 0
*/
public static int currentIndex = 0;
/**
* 四种花色
*/
public static final String POKER_COLOR = "黑桃" + T + "红心" + T + "草花" + T + "方块";
/**
* 发牌
* @param n int 一共几个人玩牌
* @param results 储存发牌结果的数组
*/
public static void drawPoker (int n, String[][] results)
{
int m = n - 1;
if (n == 0)
{
return;
}
int number = (POKER_NUMBER - currentIndex) / n;
while (number 0)
{
String current = POKERS[currentIndex++];
String pk = current.split ("\\" + T)[0].trim ();
int col = Integer.parseInt (current.split ("\\" + T )[1].trim ());
if (null == results[m][col])
{
results[m][col] = pk + " ";
}
else
{
results[m][col] += pk + " ";
}
number--;
}
for ( int i = 0; i results.length; i++ )
{
for ( int j = 0; j results[i].length; j++ )
{
if (null == results[i][j])
{
results[i][j] = "";
}
}
}
drawPoker (--n, results);
}
/**
* 给牌排序
* @param orialPokers String 原始扑克牌
* @param temp String[] 每张花色的扑克牌数组
*/
private static void sortPokers ( final String orialPokers, String[] temp )
{
Arrays.sort (temp, new ComparatorString ()
{
@Override
public int compare ( String o1, String o2 )
{
int index1 = orialPokers.indexOf (o1 + T);
int index2 = orialPokers.indexOf (o2 + T);
if (index1 index2)
{
return -1;
}
else if (index1 index2)
{
return 1;
}
else
{
return 0;
}
}
});
}
/**
* 洗牌
*/
private static void shufflePokers ( String[] POKERS )
{
Collections.shuffle (Arrays.asList (POKERS));
}
public static void main ( String[] args )
{
System.out.print ("输入人数:");
Scanner scanner = new Scanner (System.in);
int n = scanner.nextInt ();
scanner.close ();
String[] pColors = POKER_COLOR.split ("\\" + T );
String[][] results = new String[n][pColors.length];
shufflePokers (POKERS);
drawPoker (n, results);
final String poker = Arrays.toString (ORIAL_POKERS);
System.out.println ("\n输出如下:\n");
for (int i = 0; i results.length; i++)
{
System.out.println ("第" + ( i + 1 ) + "个人:");
for ( int j = 0; j results[i].length; j++ )
{
String[] temp = results[i][j].trim ().split ("\\s+");
sortPokers (poker, temp);
System.out.println (pColors[j] + ":" + Arrays.toString (temp).replaceAll ("[\\,\\[\\]]", ""));
}
System.out.println ();
}
}
}
java语言编程扑克牌
一:import java.util.Scanner;
public class Play {
/**
* 玩牌
*/
public static void main(String[] args) {
Poker poker = new Poker();
boolean over = false;
Scanner cin=new Scanner(System.in);
while(!over){
System.out.println("打牌输入1,展示剩余牌输入其他字符:");
String c=cin.nextLine();
if("1".equals(c)){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
}else{
poker.ShowPages();
}
}
/*for(int i = 0;i54;i++){
int index = (int) (Math.random()*poker.getSize());
poker.remove(index);
poker.ShowPages();
}
*/
}
}二://牌
public class Page { private String huase = "";
private String haoma = "";
public Page(String huase,String haoma){
this.huase = huase;
this.haoma = haoma;
}
public String show(){
return this.huase+this.haoma;
}
}三:import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 一副扑克
*/
public class Poker {
private List pages = new ArrayList();
public Poker(){
String huase[] = new String[]{"黑桃","红桃","草花","方块"};
String haoma[] = new String[]{"A","2","3","4","5","6","7","8","9","10"
,"J","Q","K"};
for(int i = 0;ihaoma.length;i++){
for(int j = 0;jhuase.length;j++){
pages.add(new Page(huase[j],haoma[i]));
}
}
pages.add(new Page("","大王"));
pages.add(new Page("","小王"));
}
public void ShowPages(){
System.out.println("展示剩余牌:");
for(Iterator i = pages.iterator();i.hasNext();){
System.out.println(((Page)i.next()).show());
}
}
//打牌
public void remove(int index){
pages.remove(index);
}
//剩余牌数
public int getSize(){
return pages.size();
}
}
怎么用java代码按从大到小排序红桃8红桃8红桃Q红桃7红桃4红桃J红桃9红桃K
这样貌似不是很合理。因为按字母排序都是Q在K的后面,所以可以实现Q、K、J、9、8、7、4
这样的排序。实现很简单,代码如下:
String[] str={"红桃5","红桃Q","红桃8","红桃9","红桃K","红桃J","红桃6","红桃7"};
ListString list=new ArrayListString();
for(int i=0;istr.length;i++)
{
list.add(str[i]);
}
Collections.sort(list);//升序排序
Collections.reverse(list);//把升序倒换成倒序
for(int i=0;ilist.size();i++)
{
System.out.println(list.get(i));
}
要实现你的功能,那得再加上数组进行判断。如:
String[] str1={"K","Q","J".......};
当前文章:java红桃的代码 java代码报红
文章来源:http://lswzjz.com/article/dojepde.html