代碼僅供參考,如有疑問,歡迎追問:
成都創(chuàng)新互聯(lián)服務(wù)項目包括沾化網(wǎng)站建設(shè)、沾化網(wǎng)站制作、沾化網(wǎng)頁制作以及沾化網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,沾化網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到沾化省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
import?java.util.ArrayList;
import?java.util.List;
import?java.util.Scanner;
/**
*?隨機撲克牌
*?@author?STONE
*?@version?2015-10-28
*/
public?class?Poker?{
//用于記錄丟失的手牌數(shù)
private?static?Integer?lostTotal?=?0;
//用于存放已下發(fā)的撲克
private?static?Integer[]?its1?=?new?Integer[54];
//用于存放當前的撲克
private?static?Integer[]?its2?=?new?Integer[54];
private?static?ListInteger?lostList?=?new?ArrayListInteger();
public?static?void?main(String[]?args)?{
Scanner?scn?=?new?Scanner(System.in);
for(int?i=0;i54;i++){
its1[i]?=?0;
its2[i]?=?0;
}
System.out.println("系統(tǒng)開始發(fā)牌...");
givePoker(5);
System.out.println("發(fā)牌完畢,當前手牌為:");
printPoker(its2);
Integer?gameTotal?=?0;
while(gameTotal10){//循環(huán)跑10次,請注意此處發(fā)牌沒做限制,如果循環(huán)次數(shù)過大,會導(dǎo)致死循環(huán)
System.out.println("請輸入需要丟棄的手牌(多張牌格式如下:黑桃1,黑桃2,黑桃3):");
String?lost?=?scn.nextLine();
checkLost(lost);
System.out.println("丟失手牌完畢,當前手牌:");
printPoker(its2);
System.out.println("系統(tǒng)開始重發(fā)手牌...");
givePoker(lostTotal);
System.out.println("重發(fā)發(fā)牌完畢,當前手牌為:");
printPoker(its2);
gameTotal++;
}
scn.close();
}
public?static?void?givePoker(Integer?num){//隨機分發(fā)手牌
int?total?=?0;
while(totalnum){
int?poke?=?(int)(Math.random()*54);
if(its1[poke]?==?0){
its1[poke]?=?1;
its2[poke]?=?1;
total++;
}
}
lostTotal?=?0;
}
public?static?Boolean?checkLost(String?lost){//檢查需要丟失的手牌
if(lost==null?||?lost.equals("")){
System.out.println("輸入手牌錯誤,系統(tǒng)無法處理...");
return?false;
}
if(lost.indexOf("黑桃")0??lost.indexOf("紅心")0??lost.indexOf("梅花")0??lost.indexOf("方塊")0){
System.out.println("輸入手牌錯誤,系統(tǒng)無法處理...");
return?false;
}
String[]?strs?=?lost.split(",");
Boolean?bol?=?false;
for(String?str?:?strs){
str?=?str.trim();
if(str==null?||?str.equals(""))?continue;//去掉,防止空格
bol?=?getPokerNum(str);
}
if(bol){
losePoker();
}
return?false;
}
public?static?void?losePoker(){//丟掉手牌
lostTotal?=?lostList.size();
for(Integer?itr?:?lostList){//丟掉手牌
its2[itr-1]?=?0;
}
lostList?=?new?ArrayListInteger();
}
public?static?Boolean?getPokerNum(String?str){//獲取手牌點數(shù)并去掉
try{
Integer?itr?=?0;
if(str.indexOf("黑桃")==0){
str?=?str.replace("黑桃",?"");
itr?=?Integer.parseInt(str);
if(itr13){
System.out.println("輸入手牌不存在:黑桃"+str);
return?false;
}
if(its2[itr-1]==1){
lostList.add(itr);
}
}?else?if(str.indexOf("紅心")==0){
str?=?str.replace("紅心",?"");
itr?=?Integer.parseInt(str);
if(itr13){
System.out.println("輸入手牌不存在:紅心"+str);
return?false;
}
if(its2[itr+12]==1){
lostList.add(itr+13);
}
}?else?if(str.indexOf("梅花")==0){
str?=?str.replace("梅花",?"");
itr?=?Integer.parseInt(str);
if(itr13){
System.out.println("輸入手牌不存在:梅花"+str);
return?false;
}
if(its2[itr+25]==1){
lostList.add(itr+26);
}
}?else?if(str.indexOf("方塊")==0){
str?=?str.replace("方塊",?"");
itr?=?Integer.parseInt(str);
if(itr13){
System.out.println("輸入手牌不存在:方塊"+str);
return?false;
}
if(its2[itr+38]==1){
lostList.add(itr+39);
}
}?else?if(str.indexOf("小王")==0){
if(its2[52]==1){
lostList.add(53);
}
}?else?if(str.indexOf("大王")==0){
if(its2[53]==1){
lostList.add(54);
}
}
return?true;
}?catch(?Exception?e?){
System.out.println("輸入手牌有誤...");
return?false;
}
}
public?static?void?printPoker(Integer[]?its){//打印當前手牌
String?result?=?"";
for(int?i=0;iits.length;i++){
if(its[i]==1){//等于1表示當前手牌存在
result+=pukerCheck(i+1)+",";
}
}
System.out.println(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?"";
}
}
測試運行結(jié)果:
package?zhidao;
import?java.util.Arrays;
import?java.util.Collections;
import?java.util.Comparator;
import?java.util.Scanner;
/**
*?發(fā)牌游戲
*/
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?();
/**
?*?發(fā)到哪張牌?0
?*/
public?static?int?currentIndex?=?0;
/**
?*?四種花色
?*/
public?static?final?String?POKER_COLOR?=?"黑桃"?+?T?+?"紅心"?+?T?+?"草花"?+?T?+?"方塊";
/**
?*?發(fā)牌
?*?@param?n?int?一共幾個人玩牌
?*?@param?results?儲存發(fā)牌結(jié)果的數(shù)組
?*/
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[]?每張花色的撲克牌數(shù)組
?*/
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?("輸入人數(shù):");
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?();
}
}
}
public?class?PokerTest?{
public?static?void?main(String[]?args)?{
Poker.show();
}
}
class?Poker?{
public?int?num;
public?String?breed;
public?Poker()?{
}
public?static?void?show()?{
int?i?=?(int)?(Math.random()?*?13)?+?1;//?生成1-13的隨機數(shù)
String[]?s?=?new?String[]?{?"紅心",?"黑桃",?"方塊",?"黑花"?};
int?j?=?(int)?(Math.random()?*?4);
Poker?p?=?new?Poker();
p.num?=?i;
p.breed?=?s[j];
System.out.println(p.toString());
}
@Override
public?String?toString()?{
return?"Poker?[num="?+?num?+?",?breed="?+?breed?+?"]";
}
}
我不知道電腦出牌為什么要進行大小比較以及刪牌操作? 我的是52張牌若干玩家,隨機發(fā)牌給玩家,玩家收到電腦發(fā)的牌。
這個是定義52張牌:Card.java
package com.etc.week4.day03;
public class Card {
// 點數(shù) 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;
}
// 點數(shù)對應(yīng) 名稱
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) + "]";
}
}
這個是電腦隨機發(fā)牌給玩家,玩家收到電腦發(fā)的牌PlayDemo.java
package com.etc.week4.day03;
import java.util.Arrays;
import java.util.Random;
public class PlayerDemo {
public static void main(String[] args) {
// 創(chuàng)建一副撲克牌
// 方塊:13張(THREE~DEUCE)
// 梅花:13張(THREE~DEUCE)
// 黑桃:13張(THREE~DEUCE)
// 紅桃:13張(THREE~DEUCE)
// 王: BLACK COLOR
Card [] cards = new Card[54];
int i=0;
// 構(gòu)造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);
// 將數(shù)組 變成集合 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", "郭德綱");
// 發(fā)牌.....
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編寫以下程序:
1、請定義一個名為Card的撲克牌類,該類有兩個private訪問權(quán)限的字符串變量face和suit;分別描述一張牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“紅桃”、“梅花”、“方塊”);定義Card類中的public訪問權(quán)限的構(gòu)造方法,為類中的變量賦值;定義protected訪問權(quán)限的方法getFace(),得到撲克牌的牌面值;定義protected訪問權(quán)限的方法getSuit(),得到撲克牌的花色;定義方法toString(),返回表示撲克牌的花色和牌面值字符串(如“紅桃A”、“梅花10”等)。
2、若應(yīng)用程序的main方法中,定義字符串數(shù)組f和s;分別表示撲克牌的牌面值和花色;定義52個元素的Card類型數(shù)組deck,用來存放4個花色的52張牌。如下所示。
String f[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String s[] = {"黑桃","紅桃","梅花","方塊"};
Card deck = new Card[52];
(1)使用Card類的構(gòu)造方法給deck數(shù)組的52張牌賦值,要求數(shù)組中先存放黑桃花色的A、2、3、……、K;然后是紅桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方塊花色的A、2、3、……、K。請寫出實現(xiàn)上述功能的程序段。
(2)請編寫模擬洗牌的完整程序,即把數(shù)組deck中的撲克牌隨機打亂存放順序。
原題如下:
引用內(nèi)容
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
解題,完成程序解:
程序代碼
package poker;
public class Card {
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
public static void shuffle(Card[] deck, int startIndex, int size,
int splitIndex) { //shuffle洗牌
System.out.println("startIndex:" + startIndex);
if (splitIndex * 2 size) { //if rand is 30
Card.swap(deck, startIndex, splitIndex, size - splitIndex); //[52]、0、30、22
shuffle(deck, size - splitIndex, splitIndex, size - splitIndex); //[52]、22、30、22
} else if (splitIndex * 2 size) { //if rnd is 10
Card.swap(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
shuffle(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
} else {
Card.swap(deck, startIndex, splitIndex, splitIndex);
}
}
public static void swap(Card[] deck, int srcIndex, int dstIndex, int size) { //交換
//SRC是在本位置顯示:source的縮寫,源的意思 HREF是點擊后連接的目標:HyperlinkReference,超鏈接引用
String face = "";
String suit = "";
for (int i = 0; i size; i++) {
face = deck[srcIndex + i].face;
suit = deck[srcIndex + i].suit;
deck[srcIndex + i].face = deck[dstIndex + i].face;
deck[srcIndex + i].suit = deck[dstIndex + i].suit;
deck[dstIndex + i].face = face;
deck[dstIndex + i].suit = suit;
}
}
/**
* @param args
*/
public static void main(String[] args) {
//第一問解答
Card[] deck = new Card[52];
String f[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K"};
String s[] = {"Spade", "Heart", "Club", "Diamond"};
for (int i = 0; i s.length; i++) {
for (int j = 0; j f.length; j++) {
deck[i * 13 + j] = new Card(s[i], f[j]); //依次存入數(shù)組之中,每13張一回合
}
}
//第二問解答
int rnd = 0;
int numOfShuffle = 10;
for (int i = 0; i numOfShuffle; i++) {
rnd = (int) Math.abs(Math.random() * 52); //Math.random()返回大于或等于 0.0 但小于 1.0 的偽隨機 double 值。
Card.shuffle(deck, 0, deck.length, rnd);
}
// Test
/*
for (int i=0; ideck.length; i++)
{
System.out.println(deck[i]);
} */
}
}
一: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);
}
//剩余牌數(shù)
public int getSize(){
return pages.size();
}
}