成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java封裝撲克牌代碼 java封裝撲克牌代碼是多少

JAVA撲克牌編程

1.定義撲克的對(duì)象 有兩個(gè)參數(shù)a?;ㄉ琤數(shù)字Poke{ int number, String color, Poker(String color,int number) { this.color=color; this.number=number; }} 2.每一張牌都實(shí)例化一個(gè)撲克對(duì)象。 poke1= new Poke("紅桃","10");poke2=new Poke("黑桃","9"); 3.定義游戲規(guī)則類(lèi),類(lèi)里定義一個(gè)方法,用來(lái)比較大小。public comparePoke(Poke poke1,Poke poke2){ if(花色比較){ return 結(jié)果; } if(數(shù)字比較){ return 結(jié)果; }}。調(diào)用方法就能比較

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、雅安服務(wù)器托管、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、陜州網(wǎng)站維護(hù)、網(wǎng)站推廣。

JAVA 撲克牌

package justforjoke.pkGame;

//花色

public class CardsType {

private static final String [] ct={

"黑桃",

"紅桃",

"梅花",

"方塊"

};

public static String getType(int i){

return ct[i];

}

public static int com(String s,String c){

int s1=find(s);

int s2=find(c);

if(s1==-1)return 100;

if(s2==-1)return -100;

return s2-s1;

}

private static int find(String s){

int i=-1;

for(String st:ct ){

i++;

if(st.equals(s))return i;

}

return i;

}

}

package justforjoke.pkGame;

//牌碼

public class Num {

private static final String[]n={

"0","2","3","4","5","6","7","8","9","10","J","Q","K","A"

};

public static String getN(int i){

if(i1||i13)return null;

return n[i];

}

}

package justforjoke.pkGame;

//每張牌

public class Cards implements Comparable{

private String type;

private int num;

public Cards(){}

public Cards(String s,int n){

this.type=s;

this.num=n;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public boolean equals(Object o){

if(!(o instanceof Cards))return false;

Cards c=(Cards)o;

if(this.num==c.getNum()this.type.equals(c.getType()))return true;

return false;

}

public int compareTo(Object o) {

if(!(o instanceof Cards))return 1;

Cards c=(Cards)o;

int bjjg;

if((bjjg=CardsType.com(this.type,c.getType()))!=0)return bjjg;

return c.getNum()-this.num;

}

public String toString(){

return type+" : "+Num.getN(num);

}

}

package justforjoke.pkGame;

//一副新?lián)淇?/p>

public class PkC {

public static Cards[] getNewCards(){

Cards[] nc=new Cards[52];

int k=0;

for(int i=0;i4;i++){

String t=CardsType.getType(i);

for(int j=1;j14;j++){

nc[k++]=new Cards(t,j);

}

}

return nc;

}

}

package justforjoke.pkGame;

import java.util.Arrays;

import java.util.Random;

//游戲,包含兩幅撲克

public class Game {

private Cards[]gc=new Cards[104];

private int[] num=new int[104];

private int length=103;

public Game(){

int i=0;

for(Cards c:PkC.getNewCards()){

gc[i++]=c;

}

for(Cards c:PkC.getNewCards()){

gc[i++]=c;

}

for(int j=0;j104;j++)num[j]=j;

}

//發(fā)牌

public Cards[] fp(){

Cards[] pf=new Cards[5];

Random r=new Random();

for(int i=0;i5;i++){

if(length1)break;

int t=r.nextInt(length);

int tem=num[t];

num[t]=num[length];

num[length--]=tem;

pf[i]=gc[tem];

}

return pf;

}

//去掉函數(shù)中的注釋會(huì)打印游戲的中間過(guò)程

public static String cp(Cards[] c){

String result1="同花";

String result2="順子";

String result3="";

for(int i=0;i4;i++){

if(!c[i].getType().equals(c[i+1].getType()))result1="";

if(c[i+1].getNum()-c[i].getNum()!=1)result2="";

if(c[i].equals(c[i+1]))result3="對(duì)子";

//System.out.print(c[i]+" || ");

}

//System.out.println(c[4]);

return result1+result2+result3;

}

public static void main(String []args){

Game g=new Game();

while(true){

Cards[] pf=g.fp();

if(pf[4]==null){

System.out.println("游戲失?。?!");

break;

}

Arrays.sort(pf);

String r=Game.cp(pf);

if(!r.equals("")){

System.out.println(r);

for(Cards cs:pf)System.out.print(cs+" || ");

break;

}

}

}

}

····上面有5個(gè)類(lèi)。Came類(lèi)就是用來(lái)游戲的。它會(huì)打印游戲結(jié)果。

題目所要求的都解決了,不過(guò)有一點(diǎn),那就是排序時(shí)又大到小排的。

這個(gè)在寫(xiě)代碼的時(shí)候沒(méi)有注意到。

代碼只會(huì)打印游戲的結(jié)果,如果你想打印其他的可以在代碼中添加。也可以去掉一些注釋查看中間過(guò)程

java寫(xiě)一個(gè)撲克牌程序

使用JAVA編寫(xiě)以下程序:

1、請(qǐng)定義一個(gè)名為Card的撲克牌類(lèi),該類(lèi)有兩個(gè)private訪(fǎng)問(wèn)權(quán)限的字符串變量face和suit;分別描述一張牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“紅桃”、“梅花”、“方塊”);定義Card類(lèi)中的public訪(fǎng)問(wèn)權(quán)限的構(gòu)造方法,為類(lèi)中的變量賦值;定義protected訪(fǎng)問(wèn)權(quán)限的方法getFace(),得到撲克牌的牌面值;定義protected訪(fǎng)問(wèn)權(quán)限的方法getSuit(),得到撲克牌的花色;定義方法toString(),返回表示撲克牌的花色和牌面值字符串(如“紅桃A”、“梅花10”等)。

2、若應(yīng)用程序的main方法中,定義字符串?dāng)?shù)組f和s;分別表示撲克牌的牌面值和花色;定義52個(gè)元素的Card類(lèi)型數(shù)組deck,用來(lái)存放4個(gè)花色的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類(lèi)的構(gòu)造方法給deck數(shù)組的52張牌賦值,要求數(shù)組中先存放黑桃花色的A、2、3、……、K;然后是紅桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方塊花色的A、2、3、……、K。請(qǐng)寫(xiě)出實(shí)現(xiàn)上述功能的程序段。

(2)請(qǐng)編寫(xiě)模擬洗牌的完整程序,即把數(shù)組deck中的撲克牌隨機(jī)打亂存放順序。

原題如下:

引用內(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的縮寫(xiě),源的意思 HREF是點(diǎn)擊后連接的目標(biāo):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) {

//第一問(wèn)解答

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張一回合

}

}

//第二問(wèn)解答

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 的偽隨機(jī) double 值。

Card.shuffle(deck, 0, deck.length, rnd);

}

// Test

/*

for (int i=0; ideck.length; i++)

{

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

} */

}

}

java的問(wèn)題(撲克牌)

代碼僅供參考,如有疑問(wèn),歡迎追問(wèn):

import?java.util.ArrayList;

import?java.util.List;

import?java.util.Scanner;

/**

*?隨機(jī)撲克牌

*?@author?STONE

*?@version?2015-10-28

*/

public?class?Poker?{

//用于記錄丟失的手牌數(shù)

private?static?Integer?lostTotal?=?0;

//用于存放已下發(fā)的撲克

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

//用于存放當(dāng)前的撲克

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)開(kāi)始發(fā)牌...");

givePoker(5);

System.out.println("發(fā)牌完畢,當(dāng)前手牌為:");

printPoker(its2);

Integer?gameTotal?=?0;

while(gameTotal10){//循環(huán)跑10次,請(qǐng)注意此處發(fā)牌沒(méi)做限制,如果循環(huán)次數(shù)過(guò)大,會(huì)導(dǎo)致死循環(huán)

System.out.println("請(qǐng)輸入需要丟棄的手牌(多張牌格式如下:黑桃1,黑桃2,黑桃3):");

String?lost?=?scn.nextLine();

checkLost(lost);

System.out.println("丟失手牌完畢,當(dāng)前手牌:");

printPoker(its2);

System.out.println("系統(tǒng)開(kāi)始重發(fā)手牌...");

givePoker(lostTotal);

System.out.println("重發(fā)發(fā)牌完畢,當(dāng)前手牌為:");

printPoker(its2);

gameTotal++;

}

scn.close();

}

public?static?void?givePoker(Integer?num){//隨機(jī)分發(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("輸入手牌錯(cuò)誤,系統(tǒng)無(wú)法處理...");

return?false;

}

if(lost.indexOf("黑桃")0??lost.indexOf("紅心")0??lost.indexOf("梅花")0??lost.indexOf("方塊")0){

System.out.println("輸入手牌錯(cuò)誤,系統(tǒng)無(wú)法處理...");

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){//獲取手牌點(diǎn)數(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){//打印當(dāng)前手牌

String?result?=?"";

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

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

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

}

}

System.out.println(result);

}

public?static?String?pukerCheck(Integer?itr){//判斷撲克類(lèi)型

/**

?*?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?"";

}

}

測(cè)試運(yùn)行結(jié)果:

用java ArrayList集合編寫(xiě)撲克牌洗牌與發(fā)牌

import java.util.ArrayList;

import java.util.Random;

public class PokerDemo {

ArrayListCard poker = new ArrayListCard();

Player[] players;

int n = 4;

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

public PokerDemo() {

players = new Player[n];

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

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

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

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

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

System.out.println("洗牌前: " + poker);

xipai();

System.out.println("洗牌后: " + poker);

fapai();

System.out.println("發(fā)牌后: ");

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

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

}

public void xipai() {

Random r = new Random();

ArrayListCard tmp = new ArrayListCard();

while(!poker.isEmpty()) {

int index = r.nextInt(poker.size());

Card card = poker.get(index);

tmp.add(card);

poker.remove(index);

}

poker = new ArrayListCard(tmp);

}

public void fapai() {

int p = 0;

while(!poker.isEmpty()) {

Card card = poker.get(0);

players[p].add(card);

p = (p + 1) % n;

poker.remove(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 {

ArrayListCard cards = new ArrayListCard();

String name;

public Player(String n) {

name = n;

}

public void add(Card card) {

cards.add(card);

}

public String toString() {

return name + ":\t" + cards;

}

}

網(wǎng)站欄目:java封裝撲克牌代碼 java封裝撲克牌代碼是多少
當(dāng)前URL:http://jinyejixie.com/article0/doseooo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站排名網(wǎng)站導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
营山县| 莎车县| 阳原县| 池州市| 盐亭县| 嘉祥县| 密云县| 泰来县| 东港市| 瑞金市| 铁力市| 乌兰察布市| 武安市| 久治县| 邵阳县| 武邑县| 衡阳县| 井陉县| 麻城市| 新绛县| 长乐市| 江孜县| 博湖县| 丰都县| 新竹县| 都昌县| 会理县| 板桥市| 井陉县| 和林格尔县| 连山| 永和县| 盱眙县| 鱼台县| 龙山县| 西和县| 宣威市| 临朐县| 衡阳县| 平和县| 道真|