重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package xly04;
成都创新互联公司长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为兴国企业提供专业的成都网站建设、网站建设,兴国网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
import java.util.Random;
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=random.nextInt(10);//[0,10) %10
//System.out.println(index);
//String card=cards[random.nextInt(cards.length)];//长度为8,随机下标[0,8)
//System.out.println(card);//随机的某张牌
for(int i=cards.length-1;i=1;i--){
int j=new Random().nextInt(i);//或者(1)不注销用int j=random.nextInt(i);
String t=cards[i];
cards[i]=cards[j];
cards[j]=t;
}
for(int i=0;icards.length;i++){
System.out.print(cards[i]+",");
}
System.out.println();//输出回车字符
//顺序发牌:顺序的将每个牌发给每个人
String[] players=new String[]{"马伊琍","文章","六六"};
//int i=0;
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
for(int i=0;icards.length;i++){
//i=0,1,2,3,4,5,6,7
String card=cards[i];//card 代表每张扑克牌
String player=players[i%players.length];
//player代表每个玩游戏的人
System.out.println(player+":"+card);
}
}
}
package zhidao;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
* 发牌游戏
*/
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 ();
/**
* 发到哪张牌 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 = 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[] 每张花色的扑克牌数组
*/
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 ("输入人数:");
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 Card {
private int face; //牌的面值
private int suit; //牌的花色
public Card(int face, int suit) {
super();
this.face = face;
this.suit = suit;
}
public int getFace() {
return face;
}
public void setFace(int face) {
this.face = face;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
}
//模拟洗牌和发牌
public class Test {
public static void main(String[] args) {
Card[] deck = new Card[52];
fillDeck(deck);
deal(deck);
System.out.println();
}
private static void fillDeck(Card[] deck) {
int i, j=0;
for (i = 0; i 52; i++) {
j = (int) (Math.random() * 52);
deck[i]=new Card(j%13,j/13);
}
}
private static void deal(Card[] deck) {
int k1, k2;
System.out.println();
for (k1 = 0, k2 = k1 + 26; k1 26; k1++, k2++) {
if (k1 % 13 == 0)
System.out.println("\n第" + (k1 / 13 + 1) +
"组牌:\t\t\t第"+ (k1 / 13 + 3) + "组牌:");
System.out.print("\nCard: " + deck[k1].getFace() +
" Suit: "+ deck[k1].getSuit());
System.out.print("\t\t\tCard: " + deck[k2].getFace() +
" Suit: "+ deck[k2].getSuit());
}
}
}
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("发牌后: ");
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;
}
}