撲克牌生成與洗牌程序_第1頁
撲克牌生成與洗牌程序_第2頁
撲克牌生成與洗牌程序_第3頁
撲克牌生成與洗牌程序_第4頁
撲克牌生成與洗牌程序_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、撲克牌生成與洗牌程序 使用JAVA編寫以下程序: 1、請定義一個(gè)名為Card的撲克牌類,該類有兩個(gè)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)用程

2、序的main方法中,定義字符串?dāng)?shù)組f和s;分別表示撲克牌的牌面值和花色;定義52個(gè)元素的Card類型數(shù)組deck,用來存放4個(gè)花色的52張牌。如下所示。 String f ="A","2","3","4","5","6","7","8","9","10","J","Q","K" String s = "黑桃",&quo

3、t;紅桃","梅花","方塊" Card deck = new Card52; (1)使用Card類的構(gòu)造方法給deck數(shù)組的52張牌賦值,要求數(shù)組中先存放黑桃花色的A、2、3、K;然后是紅桃花色的A、2、3、K;梅花花色的A、2、3、K;方塊花色的A、2、3、K。請寫出實(shí)現(xiàn)上述功能的程序段。 (2)請編寫模擬洗牌的完整程序,即把數(shù)組deck中的撲克牌隨機(jī)打亂存放順序。 原題如下: 引用內(nèi)容 private String face; / A, K, Q, J, 10, . 2 private String suit; / Spade, Hear

4、t, 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;

5、/ 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 stati

6、c 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, s

7、ize - 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

8、swap(Card deck, int srcIndex, int dstIndex, int size) /交換 /SRC是在本位置顯示:source的縮寫,源的意思 HREF是點(diǎn)擊后連接的目標(biāo):HyperlinkReference,超鏈接引用 String face = "" String suit = "" for (int i = 0; i < size; i+) face = decksrcIndex + i.face; suit = decksrcIndex + i.suit; decksrcIndex + i.face = deckd

9、stIndex + i.face; decksrcIndex + i.suit = deckdstIndex + i.suit; deckdstIndex + i.face = face; deckdstIndex + i.suit = suit; /* * param args */ public static void main(String args) /第一問解答 Card deck = new Card52; String f = "A", "2", "3", "4", "5", &q

10、uot;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+) decki * 13 + j = new

11、Card(si, fj); /依次存入數(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 的偽隨機(jī) double 值。 Card.shuffle(deck, 0, deck.length, rnd); / Test /* for (int i=0; i<deck.length; i+) System

12、.out.println(decki); */PS:函數(shù)shuffle是模擬洗牌的過程,這里的算法比較粗燥,隨機(jī)產(chǎn)生一個(gè)52以內(nèi)數(shù)字n,把牌分為兩份,1.n-1,和n-52,然后把兩份牌交換一下順序,這里做了十次切牌。 shuffle是一個(gè)遞歸函數(shù),startIndex 表示切牌的開始序號(hào),splitIndex表示切牌的序號(hào),即上面的n getXXX是用來得到牌的花色和數(shù)字,第二問用不到本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:public class DeckOfCards extends Applet private Card deck; private int currentCard; pr

13、ivate Button dealButton, shuffleButton; private TextField displayCard; public void init() String faces = "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack&quo

14、t;, "Queen", "King" ; String suits = "Hearts", "Diamonds", "Clubs", "Spades" ; deck = new Card 52 ; currentCard = -1; for( int i = 0; i < deck.length; i+ ) deck i = new Card( faces i % 13 , suits i / 13 ); dealButton = new Button( "

15、Deal card" ); shuffleButton = new Button( "shuffle cards" ); displayCard = new TextField( 20 ); displayCard.setEditable( false ); add( dealButton ); add( shuffleButton ); add( displayCard ); public boolean action( Event event, Object object ) if( event.target = dealButton ) Card dealt

16、 = dealCard(); if( dealt != null ) displayCard.setText( dealt.toString() ); showStatus( "Card #: " + currentCard ); else displayCard.setText( "NO MORE CARDS TO DEAL!" ); showStatus( "Shuffle cards to continue!" ); else if( event.target = shuffleButton ) displayCard.setT

17、ext( "SHUFFLE CARDS." ); showStatus( "" ); shuffle(); displayCard.setText( "DECK IS SHUFFLED!" ); return true; public void shuffle() currentCard = -1; for( int i = 0; i < deck.length; i+ ) int j = ( int )( Math.random() * 52 ); Card temp = deck i ; deck i = deck j ;

18、deck j = temp; dealButton.enable(); public Card dealCard() if( +currentCard < deck.length ) return deck currentCard ; else dealButton.disable(); return null; class Card private String face; private String suit; public Card( String f, String s ) face = f; suit = s; public String toString() return

19、face + " of " + suit ; 兩種洗牌發(fā)牌程序 收藏 用指針實(shí)現(xiàn),這種比較低效#include <stdio.h>#include <time.h>void shuffle(int 13);void deal(int 13, char *, char *);int main(void) char *suit4 = "hearts", "diamonds", "clubs", "spades" char *face13 = "one",

20、"two", "three", "four", "five", "six", "seven", "eight","nine", "ten", "eleven", "twelve", "thrteen" srand(time(NULL); int deck413 = 0; shuffle(deck); deal(deck, face, suit); retur

21、n 0;void shuffle(int deck13) int card, row, column; for (card = 1; card <= 52; card+) row = rand() % 4; column = rand() % 13; while (deckrowcolumn != 0) row = rand() % 4; column = rand() % 13; deckrowcolumn = card; void deal(int deck13, char *face, char *suit) int card, row, column; for (card = 1

22、; card <= 52; card+) for (row = 0; row <= 3; row+) for (column = 0; column <= 12; column+) if (deckrowcolumn = card) printf("%s of %sn", facecolumn, suitrow); 第二種是直接用結(jié)構(gòu)體實(shí)現(xiàn),比較高效#include <stdio.h>#include <time.h>struct card char *suit; char *face;typedef struct card Car

23、d;void fillDeck(Card *, char *, char *);void shuffle(Card *);void deal(Card *);int main(void) Card card52; char *suit4 = "hearts", "diamonds", "clubs", "spades" char *face13 = "one", "two", "three", "four", "five&qu

24、ot;, "six", "seven", "eight","nine", "ten", "eleven", "twelve", "thrteen" srand(time(NULL); fillDeck(card, face, suit); shuffle(card); deal(card); return 0;/*void shuffle(int deck13) int card, row, column; for (card = 1;

25、 card <= 52; card+) row = rand() % 4; column = rand() % 13; while (deckrowcolumn != 0) row = rand() % 4; column = rand() % 13; deckrowcolumn = card; void deal(int deck13, char *face, char *suit) int card, row, column; for (card = 1; card <= 52; card+) for (row = 0; row <= 3; row+) for (colu

26、mn = 0; column <= 12; column+) if (deckrowcolumn = card) printf("%s of %sn", facecolumn, suitrow); */void fillDeck(Card *deck, char *face, char *suit) int i; for (i = 0; i < 52; i+) decki.face = facei % 13; decki.suit = suiti / 13; void shuffle(Card *deck) int i, j; Card temp; for (i

27、 = 0; i < 52; i+) j = rand() % 52; temp = decki; decki = deckj; deckj = temp; void deal(Card *deck) int i; for (i = 0; i < 52; i+) printf("%s of %sn", decki.face, decki.suit); 用C+寫的撲克牌洗牌、發(fā)牌和檢測牌質(zhì)量的程序代碼(2010-12-12 23:00:09) 轉(zhuǎn)載標(biāo)簽: c、撲克牌it用C+寫的撲克牌洗牌、發(fā)牌和檢測牌質(zhì)量的程序代碼編譯器我用的是VS2008 。執(zhí)行結(jié)果如圖:源文件

28、:有三個(gè)文件,一個(gè)是主函數(shù)文件 撲克牌游戲.cpp,一個(gè)是類函數(shù)成員的實(shí)現(xiàn)文件dapai.cpp,還有一個(gè)是系統(tǒng)生成的stdafx.app。頭文件:有兩個(gè),一個(gè)是dapai.h,另一個(gè)是系統(tǒng)生成的stdafx.h??梢詫?shí)現(xiàn)的簡單功能:可以從一副撲克牌(不帶王牌)發(fā)出n手牌(n<4),每手牌發(fā)m張牌(n*m < 52)。每次執(zhí)行都是隨機(jī)發(fā)牌,發(fā)的牌沒有重復(fù)出現(xiàn),可以檢測每一手牌的質(zhì)量,比如有多少對牌出現(xiàn)、是否有同花出現(xiàn)、是否有連對出現(xiàn)、是否有順子出現(xiàn)。我只是憑著我的思路寫下的。里面沒有復(fù)雜的算法,只是一些簡單的語句,也有比較詳細(xì)的注釋。雖然寫得不專業(yè),但是我還會(huì)繼續(xù)努力學(xué)習(xí)下去的。下

29、面是源碼: /dapai.h文件#pragma onceclass dapaipublic:  int m;/要發(fā)幾手牌 int n;/要發(fā)牌的張數(shù) int count0;/是否有連續(xù)的五張牌出現(xiàn)標(biāo)記 int count2;/成對牌出現(xiàn)的對數(shù) int count3;/三張同號(hào)牌出現(xiàn)的個(gè)數(shù) int count4;/四張同號(hào)牌一起出現(xiàn)的標(biāo)記 int count222; void xipai();/洗牌 void fapai(int,int);/發(fā)牌 int a413;/ 發(fā)牌時(shí)統(tǒng)

30、計(jì)同號(hào)牌出現(xiàn)的次數(shù) int b44;/ 發(fā)牌時(shí)統(tǒng)計(jì)每中花色出現(xiàn)的次數(shù) dapai(void); dapai(void);private: int puke413; const char *color 4 ;/ 花色 char *face 13 ;/ 牌面;  / stdafx.h : 標(biāo)準(zhǔn)系統(tǒng)包含文件的包含文件,這個(gè)是系統(tǒng)生成的stdafx.h/ 或是經(jīng)常使用但不常更改的/ 特定于項(xiàng)目的包含文件/#pragma once#define WIN32_LEAN_AND_MEAN  / 從 Windo

31、ws 頭中排除極少使用的資料#include <stdio.h>#include <tchar.h> / TODO: 在此處引用程序需要的其他頭文件  / 撲克牌游戲.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。/主函數(shù)文件#include "stdafx.h"#include"iostream"#include "dapai.h"using namespace std;void compare( int ( *p1) 13 , int  (*p2) 4 )/檢測哪手牌比較好

32、 int max=*p2; int row1 = 0; for ( int i = 0; i < 4; i+ )   for ( int j =0; j < 4; j+ )     if ( *(*(p2 + i)+j) >max )       max = *(*(p2 + i)+j);    row1 = i;    

33、;    if ( max >=5 )   cout<<"第"<< row1+1 <<"手牌比較好!"<<endl; int _tmain(int argc, _TCHAR* argv) dapai A; do   cout<<"你要發(fā)幾手牌?"<<endl;  cin>>A.m; 

34、60;cout<<"你要發(fā)多少張牌?"<<endl;  cin>>A.n;  if ( A.m*A.n > 52 )     cout<<"輸入非法,一副撲克牌只有52張,請重新輸入!nn"    if ( A.m > 4)     cout<<"輸入非法,只允許發(fā)4手牌以內(nèi)!"<

35、<endl;    while (A.m*A.n > 52 | A.m > 4 ); A.xipai(); A.fapai(A.m,A.n); compare(A.a, A.b);  /dapai.cpp文件#include "StdAfx.h"#include "dapai.h"#include "cstdlib"#include "ctime"#include "iomanip"#include

36、 "iostream"using namespace std;/構(gòu)造函數(shù)開始dapai:dapai(void)  for ( int i = 0; i < 4; i+ )   for( int j = 0; j < 13; j+ )  aij=0;  for (int i = 0; i < 4; i+ )   for(int j = 0; j < 4; j+ )  bij=0;  color

37、0 = "黑桃" color 1 = "梅花" color 2 = "方塊" color 3 = "紅桃" face 0 ="A" face 1 = "2" face 2 = "3" face 3 = "4" face 4 = "5" face 5 ="6" face 6 = "7&quo

38、t; face 7 = "8" face 8 = "9" face 9 ="10" face 10 = "J" face 11 = "Q" face 12 = "K" for ( int row = 0; row < 4; row +)   for ( int column = 0; column < 13; column +)   &

39、#160; puke row column =0;    srand( time( 0 ) );/獲得當(dāng)前的時(shí)間作為隨機(jī)數(shù)的種子/構(gòu)造函數(shù)結(jié)束/洗牌void dapai:xipai() int row; int column; for ( int pai = 1; pai <= 52; pai +)   do     row = rand () % 4;   column = rand() % 13;

40、   while ( puke row column !=0 );  puke row column = pai; /洗牌結(jié)束/開始發(fā)牌,m手牌,每手牌n張void dapai:fapai( int m, int n) int pai = 1; int k = n; for( int j = 0; j < m; j+ )   cout<<"第"<<j+1<<"手牌為:"<<endl; 

41、; for ( ; pai <= n; pai + )     for ( int row = 0; row < 4; row + )       for ( int column = 0; column < 13; column + )         if ( puke row column = pai )    &

42、#160;      cout<<setw( 8 )<<right<<color row <<" "<<face column <<endl;      ajcolumn +;      bjrow +;          &#

43、160;     n = n+k;  count0 = 0;/是否有連續(xù)的五張牌出現(xiàn)標(biāo)記  count2 = 0;/成對牌出現(xiàn)的對數(shù)  count3 = 0;/三張同號(hào)牌出現(xiàn)的個(gè)數(shù)  count4 = 0;/四張同號(hào)牌一起出現(xiàn)的標(biāo)記    count222 = 0;/連對標(biāo)記  for (int i = 0; i < 13; i + )     if

44、( aji = 2 )/一對牌       count2+;    /cout<<"有一對"<<facei<<"在里面"<<endl;      if ( aji = 3)/三張一樣的牌       count3+;    /cou

45、t<<"有三張同號(hào)牌"<<facei<<"在里面"<<endl;      if ( aji = 4 )/四張一樣的牌       count4+;    /cout<<"有四張同號(hào)牌"<<facei<<"在里面"<<endl;  &

46、#160;      /檢測是否有順子牌(五張連續(xù)的牌)  for (int i = 0; i < 12; i+ )     if ( aji = 0 )       count0 =0;      else       count0+;  &

47、#160; if ( count0 = 5)         break;         /順子牌檢測結(jié)束  for ( int i = 0; i < 12; i+ )     if ( a j i   < 2 )       count222

48、= 0;      else       count222+;    if ( count222 =3 )         break;           /判斷對牌并輸出  if ( count2 != 0

49、 )     cout <<"tt很好!里面有"<<count2<<"對牌"<<endl;  /對牌輸出結(jié)束    if ( count3 !=0 )     cout <<"tt里面有三張同號(hào)牌"<<endl;    if ( count4 != 0 ) 

50、    cout <<"tt里面有四張同號(hào)牌"<<endl;    if ( count0 = 5 )     cout<<"tt非常棒!注意看,里面有順子牌(五張連續(xù)的牌)喔"<<endl;    if ( count222 = 3 )     cout<<"tt 不錯(cuò),里面有連對喔!"<<endl;    /判斷輸出是否有五張同花的牌  for (int  i = 0; i < 4; i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論