package xly04;
import javautilRandom;
public class ArrayDemo3 {
/
数组应用:
1 随机洗牌
2 顺序发牌
/
public static void main(String[] args) {
String[] cards=new String[]{"黑桃A","黑桃2","黑桃3","黑桃4",
"黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃K",
"红桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9",
"红桃10","红桃J","红桃Q","红桃K","方块A","方块2","方块3","方块4","方块5",
"方块6","方块7","方块8","方块9","方块10","方块J","方块Q","方块K","梅花A",
"梅花2","梅花3","梅花4","梅花5","梅花6","梅花7","梅花8","梅花9","梅花10",
"梅花J","梅花Q","梅花K","大王","小王"};
//Random random=new Random();(1)
//int index=randomnextInt(10);//[0,10) %10
//Systemoutprintln(index);
//String card=cards[randomnextInt(cardslength)];//长度为8,随机下标[0,8)
//Systemoutprintln(card);//随机的某张牌
for(int i=cardslength-1;i>=1;i--){
int j=new Random()nextInt(i);//或者(1)不注销用int j=randomnextInt(i);
String t=cards[i];
cards[i]=cards[j];
cards[j]=t;
}
for(int i=0;i<cardslength;i++){
Systemoutprint(cards[i]+",");
}
Systemoutprintln();//输出回车字符
//顺序发牌:顺序的将每个牌发给每个人
String[] players=new String[]{"马伊琍","文章","六六"};
//int i=0;
//Systemoutprintln(players[i++%playerslength]);
//Systemoutprintln(players[i++%playerslength]);
//Systemoutprintln(players[i++%playerslength]);
//Systemoutprintln(players[i++%playerslength]);
//Systemoutprintln(players[i++%playerslength]);
//Systemoutprintln(players[i++%playerslength]);
for(int i=0;i<cardslength;i++){
//i=0,1,2,3,4,5,6,7
String card=cards[i];//card 代表每张扑克牌
String player=players[i%playerslength];
//player代表每个玩游戏的人
Systemoutprintln(player+":"+card);
}
}
}
#include <stdioh>
#include <stdlibh>
#include <timeh>
const int TOTAL = 52;
const int PLAYERS = 4;
const int MAXSIZE = TOTAL/PLAYERS;
const int TYPES = 4;
typedef struct card {
int type; // type 0:梅花,1:方片,2:红桃,3:黑桃
int point; // point = 2 -- 14
}CARD;
void sort(int a[][TYPES][MAXSIZE]) {
int i,j,k,m,n,t,size;
for(m = 0; m < PLAYERS; ++m) {
for(n = 0; n < TYPES; ++n) {
for(size = 0; a[m][n][size]; ++size);
for(i = 0; i < size - 1; ++i) {
k = i;
for(j = i + 1; j < size; ++j) {
if(a[m][n][k] < a[m][n][j]) k = j;
}
if(k != i) {
t = a[m][n][k];
a[m][n][k] = a[m][n][i];
a[m][n][i] = t;
}
}
}
}
}
void show(int a[][TYPES][MAXSIZE]) {
int i,j,k;
char type[TYPES] = {'\5','\4','\3','\6'};
char player[PLAYERS][6] = {"EAST","SOUTH","WEST","NORTH"};
char point[] = "JQKA";
for(k = 0; k < PLAYERS; ++k) {
printf("<%s>\n",player[k]);
for(i = 0; i < TYPES; ++i) {
printf(" %c ",type[i]);
for(j = 0; j < MAXSIZE && a[k][i][j];++j) {
if(a[k][i][j] < 11) printf("%d ",a[k][i][j]);
else printf("%c ",point[a[k][i][j] - 11]);
}
printf("\n");
}
printf("\n");
}
}
int Has(int a[][TYPES][MAXSIZE],CARD Card) {
int i,j;
for(i = 0; i < PLAYERS; ++i) {
for(j = 0; j < MAXSIZE && a[i][Cardtype][j]; ++j)
if(a[i][Cardtype][j] == Cardpoint)
return 1;
}
return 0;
}
int isFull(int a[][MAXSIZE]) {
int n = 0,i,j;
for(i = 0; i < TYPES; ++i) {
for(j = 0; a[i][j] && j < MAXSIZE; ++i)
++n;
}
return n == MAXSIZE;
}
int addCard(int a[PLAYERS][TYPES][MAXSIZE],CARD Card) {
int i = 0;
if(Has(a,Card)) return 0;
int who = rand()%PLAYERS;
while(isFull(a[who])) who = (who + 1)%PLAYERS;
while(a[who][Cardtype][i]) ++i;
a[who][Cardtype][i] = Cardpoint;
return 1;
}
int main() {
int i,playingCard[PLAYERS][4][MAXSIZE] = {0};
CARD t;
srand((unsigned)time(NULL));
for(i = 0; i < TOTAL; ++i) {
ttype = rand()%4;
tpoint = rand()%13 + 2;
if(addCard(playingCard,t) == 0) --i;
}
sort(playingCard);
show(playingCard);
return 0;
}
//牌类
public class Card {private int face; //牌的面值
private int suit; //牌的花色
public Card(int face, int suit) {
super();
thisface = face;
thissuit = suit;
}
public int getFace() {
return face;
}
public void setFace(int face) {
thisface = face;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
thissuit = suit;
}
}
//模拟洗牌和发牌
public class Test {public static void main(String[] args) {
Card[] deck = new Card[52];
fillDeck(deck);
deal(deck);
Systemoutprintln();
}
private static void fillDeck(Card[] deck) {
int i, j=0;
for (i = 0; i < 52; i++) {
j = (int) (Mathrandom() 52);
deck[i]=new Card(j%13,j/13);
}
}
private static void deal(Card[] deck) {
int k1, k2;
Systemoutprintln();
for (k1 = 0, k2 = k1 + 26; k1 < 26; k1++, k2++) {
if (k1 % 13 == 0)
Systemoutprintln("\n第" + (k1 / 13 + 1) +
"组牌:\t\t\t第"+ (k1 / 13 + 3) + "组牌:");
Systemoutprint("\nCard: " + deck[k1]getFace() +
" Suit: "+ deck[k1]getSuit());
Systemoutprint("\t\t\tCard: " + deck[k2]getFace() +
" Suit: "+ deck[k2]getSuit());
}
}
}
package zhidao;
import javautilArrays;
import javautilCollections;
import javautilComparator;
import javautilScanner;
/
发牌游戏
/
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 = POKERSclone ();
/
发到哪张牌 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 = currentsplit ("\\" + T)[0]trim ();
int col = IntegerparseInt (currentsplit ("\\" + T )[1]trim ());
if (null == results[m][col])
{
results[m][col] = pk + " ";
}
else
{
results[m][col] += pk + " ";
}
number--;
}
for ( int i = 0; i < resultslength; 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 )
{
Arrayssort (temp, new Comparator<String> ()
{
@Override
public int compare ( String o1, String o2 )
{
int index1 = orialPokersindexOf (o1 + T);
int index2 = orialPokersindexOf (o2 + T);
if (index1 > index2)
{
return -1;
}
else if (index1 < index2)
{
return 1;
}
else
{
return 0;
}
}
});
}
/
洗牌
/
private static void shufflePokers ( String[] POKERS )
{
Collectionsshuffle (ArraysasList (POKERS));
}
public static void main ( String[] args )
{
Systemoutprint ("输入人数:");
Scanner scanner = new Scanner (Systemin);
int n = scannernextInt ();
scannerclose ();
String[] pColors = POKER_COLORsplit ("\\" + T );
String[][] results = new String[n][pColorslength];
shufflePokers (POKERS);
drawPoker (n, results);
final String poker = ArraystoString (ORIAL_POKERS);
Systemoutprintln ("\n输出如下:\n");
for (int i = 0; i < resultslength; i++)
{
Systemoutprintln ("第" + ( i + 1 ) + "个人:");
for ( int j = 0; j < results[i]length; j++ )
{
String[] temp = results[i][j]trim ()split ("\\s+");
sortPokers (poker, temp);
Systemoutprintln (pColors[j] + ":" + ArraystoString (temp)replaceAll ("[\\,\\[\\]]", ""));
}
Systemoutprintln ();
}
}
}
首先实现洗牌,最简单的方法是选用不记录顺序的集合,这种集合不会记录实体插入顺序,你只需要把实体都插进去再遍历取出来就已经是乱序的了。HashMap是个不错的选择,记得使用entry进行遍历。
#include<stdlibh>
int pokers[53];
int array[502];
int shiwei;
int gewei;
int rseed;
int i=0;
int j=0;
void fun(int n) //用于将编号对应为牌面输出
{ switch(n%100 )
{
case 1: cout<< ‘A’<<’\t’;break ;
case 2: cout<< 2<<’\t’;break ;
case 3: cout<< 3<<’\t’;break ;
case 4: cout<< 4<<’\t’;break ;
case 5: cout<< 5<<’\t’;break ;
case 6: cout<< 6<<’\t’;break ;
case 7: cout<< 7<<’\t’;break ;
case 8: cout<< 8<<’\t’;break ;
case 9: cout<< 9<<’\t’;break ;
case 10: cout<< 10<<’\t’;break ;
case 11: cout<< ‘J’<<’\t’;break ;
case 12: cout<< ‘Q’<<’\t’;break ;
case 13: cout<< ‘K’<<’\t’;break ;
}
}
int main(){
//随机产生一副牌
while(true)
{
shiwei = rand()% (4) + 1;//产生十位数
gewei = rand()%(12)+1;//产生个位数
if(shiwei==5){
gewei = rand()%(1)+1;
}
rseed= shiwei100+gewei;//用于产生一个101 ~ 502 间的机选整数
if(array[rseed]==1)
continue;
else{
array[rseed]==1;
pokers[rseed]==rseed;
i++;
}
if(i==54)
break;
}
for(j=0;j<=53;j++){
switch(pokers[j]/100)
{
case 5 :if(pokers[i]%500==1)
cout<<(char)1<<’\t’;
else cout<<(char)2<<’\t’
break ;
case 1: cout<<(char)6; fun( pokers[ i ]);break;
case 2: cout<<(char)3; fun( pokers[ i ]);break;
case 3: cout<<(char)5; fun( pokers[ i ]);break;
case 4: cout<<(char)4; fun( pokers[ i ]);break;
}
}
}
以上就是关于请问用java数组编写洗牌程序怎么写我刚学java,请多指教,谢谢!!!全部的内容,包括:请问用java数组编写洗牌程序怎么写我刚学java,请多指教,谢谢!!!、扑克牌洗牌发牌过程模拟、用java撰写一个简单的洗牌程序,可在文本模式下显示洗牌结果等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)