poker編程
一: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;i<54;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;i<haoma.length;i++){
for(int j = 0;j<huase.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();
}
}
㈡ C語言編程——發牌洗牌模擬,求幫助
實現了2副牌的發牌,和每個人的牌和底牌
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct CARD //牌
{
char suit[10]; /*花色*/
char face[10]; /*牌面*/
};
enum { posA, posB, posC, posD};//定義好每個人的位置
struct Postion
{
struct CARD getcard[25];//每人獲得的牌
};
struct Postion postion[4];//分配四個位置
struct CARD leftCard[8]; //底牌
struct CARD card[54]; //54張牌
char *suit[]={"Spades","Hearts","Clubs","Diamonds"};
char *face[] = {"A","2","3","4","5","6","7","8","9",
"10","jack","Queen","King"};
/* 函數功能:將52張牌的順序打亂,
函數參數:結構體數組wCard,表示52張牌
函數返回值:無
*/
void Shuffle(struct CARD *wCard)
{
int i,j;
struct CARD temp;
for (i=0; i<54; i++)
{
j=rand()%54;
temp=wCard[i];
wCard[i]=wCard[j];
wCard[j]=temp;
}
}
/*函數功能:發牌結果
函數參數:結構體數組wCard,表示有54張牌
函數返回值:無
*/
void Deal(struct CARD *wCard)
{
int i,aidx=0,bidx=0,cidx=0,didx=0;
Shuffle(card);//將牌打亂
/*************發第一副牌,只發50張,分別分給A,B,C,D四個位置 4張留底**************/
// 第一次發完50張後,A,B多一張,所以下面第二次讓C,D排在前面,兩次發完剛好各40張 */
for (i=0; i<50; i++)//發牌數
{
// printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posA].getcard[aidx++]=wCard[i];
else if(i%4==1)
postion[posB].getcard[bidx++]=wCard[i];
else if(i%4==2)
postion[posC].getcard[cidx++]=wCard[i];
else if(i%4==3)
postion[posD].getcard[didx++]=wCard[i];
}
/**********剩下的四張作為底牌*********/
leftCard[0]=wCard[i++];
leftCard[1]=wCard[i++];
leftCard[2]=wCard[i++];
leftCard[3]=wCard[i++];
Shuffle(card);//再次將牌打亂
/*************發第二副牌,也只發50張,分別分給A,B,C,D四個位置,4張留底,一共8張底**************/
for (i=0; i<50; i++)//發牌數
{
// printf("%10s %5s\n", wCard[i].suit, wCard[i].face);
if(i%4==0)
postion[posC].getcard[cidx++]=wCard[i];
else if(i%4==1)
postion[posD].getcard[didx++]=wCard[i];
else if(i%4==2)
postion[posA].getcard[aidx++]=wCard[i];
else if(i%4==3)
postion[posB].getcard[bidx++]=wCard[i];
}
/**********剩下的四張作為底牌,這樣就一共為8張底牌*********/
leftCard[4]=wCard[i++];
leftCard[5]=wCard[i++];
leftCard[6]=wCard[i++];
leftCard[7]=wCard[i++];
}
/* 函數功能:將52張牌按黑桃、紅桃、草花、方塊花色順序,面值按A~K順序排列
函數參數:結構體數組wCard,表示不同花色和面值的52張牌
指針數組wFace,指向面值字元串
指針數組wSuit,指向花色字元串
函數返回值:無
*/
void FillCard(struct CARD wCard[],char *wSuit[], char *wFace[])
{
int i;
for (i=0; i<52; i++)
{
strcpy(wCard[i].suit, wSuit[i/13]);
strcpy(wCard[i].face, wFace[i%13]);
}
// wCard[53].face="Big"; //大小王
strcpy(wCard[52].suit, "Small");
strcpy(wCard[52].face, "ghost");
strcpy(wCard[53].suit, "Big");
strcpy(wCard[53].face, "ghost");
}
void print(char ch)//輸出牌
{
int i;
switch(ch)
{
case 'A': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posA].getcard[i].suit, postion[posA].getcard[i].face);
}
break;
case 'B': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posB].getcard[i].suit, postion[posB].getcard[i].face);
}
break;
case 'C': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posC].getcard[i].suit, postion[posC].getcard[i].face);
}
break;
case 'D': for(i=0; i<25; i++)
{
printf("%10s %5s\n", postion[posD].getcard[i].suit, postion[posD].getcard[i].face);
}
break;
}
}
void outputLeftCard()//輸出底牌
{
int i;
for(i=0; i<8; i++)
printf("%10s %5s\n", leftCard[i].suit, leftCard[i].face);
}
int main()
{
char pos;
srand(time(NULL));
FillCard(card,suit,face);
//Shuffle(card);
Deal(card);
printf("Please choose your position(A、B、C、D):");
scanf("%c", &pos);
print(pos);//輸出你所在位置的牌
/**********下面輸出的是,除了你之外其他人的牌**********/
if(pos !='A')
{
printf("A:\n");
print('A');
}
if(pos !='B')
{
printf("B:\n");
print('B');
}
if(pos !='C')
{
printf("C:\n");
print('C');
}
if(pos !='D')
{
printf("D:\n");
print('D');
}
printf("底牌為:\n");
outputLeftCard();//輸出底牌
return 0;
}