一:import java.util.Scanner;
成都創(chuàng)新互聯(lián)公司作為成都網(wǎng)站建設(shè)公司,專注重慶網(wǎng)站建設(shè)公司、網(wǎng)站設(shè)計(jì),有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問題,行業(yè)涉及火鍋店設(shè)計(jì)等多個(gè)領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認(rèn)可。
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;i54;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;jhuase.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);
}
//剩余牌數(shù)
public int getSize(){
return pages.size();
}
}
public?class?PokerTest?{
public?static?void?main(String[]?args)?{
Poker.show();
}
}
class?Poker?{
public?int?num;
public?String?breed;
public?Poker()?{
}
public?static?void?show()?{
int?i?=?(int)?(Math.random()?*?13)?+?1;//?生成1-13的隨機(jī)數(shù)
String[]?s?=?new?String[]?{?"紅心",?"黑桃",?"方塊",?"黑花"?};
int?j?=?(int)?(Math.random()?*?4);
Poker?襲慧模p?=?new?Poker();
p.num?=?i;
p.breed?=?拍緩s[j];
System.out.println(p.toString());
}
@Override
public?String?toString()?{
return?"碧蔽Poker?[num="?+?num?+?",?breed="?+?breed?+?"]";
}
}
java內(nèi)的Collections類自侍畝帶了一個(gè)shuffle洗牌算法。
static void shuffle(List? list)
使用默認(rèn)隨機(jī)老知森源對(duì)指定列表進(jìn)行置換。
static void shuffle(List? list, Random rnd)
使用指定的隨機(jī)源對(duì)指定列表進(jìn)猛簡(jiǎn)行置換。
現(xiàn)在你可以把52張牌放進(jìn)一個(gè)List里,調(diào)用他的shuffle算法打亂順序。
我不知道電腦出牌為什么要進(jìn)行大小比較以及刪牌操作? 我的是52張牌若干玩家,隨機(jī)發(fā)牌給玩家,玩家收到電腦發(fā)的牌。
這個(gè)是定義52張牌:Card.java
package com.etc.week4.day03;
public class Card {
// 點(diǎn)數(shù) 3 ---0 4 1 12 2 11 A 13 小王 14 大王磨扒
public int rank;
public final static int THREE = 0;
public final static int FOUR = 1;
public final static int FIVE = 2;
public final static int SIX = 3;
public final static int SEVEN = 4;
public final static int EIGHT = 5;
public final static int NINE = 6;
public final static int TEN = 7;
public final static int JACK = 8;//J
public final static int QUEEN = 9;//Q
public final static int KING = 10;//K
public final static int ACE = 11;//A
public final static int DEUCE = 12; //2
public final static int BLACK = 13; //小王
public final static int COLOR = 14;//大王
//花色(suit=0suit=4)
private int suit;
public final static int CLUB = 0; //梅花
public static final int DIAMOND = 1; //方塊(鉆石)
public static final int HEART = 2; //紅桃(紅此歲心)
public static final int SPADE = 3; //黑桃(花鋤)
public static final int JOKER = 4; //王
public Card(int suit,int rank){
this.setSuit(suit);
this.setRank(rank);
}
public Card(){}
public int getRank() {
return rank;
}
public void setRank(int rank) {
if(rankCard.THREE||rankCard.COLOR){
throw new RuntimeException();
}
this.rank = rank;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
if(suitCard.CLUB || suitCard.JOKER){
throw new RuntimeException();
}
this.suit = suit;
}
// 點(diǎn)數(shù)對(duì)應(yīng) 名稱
public final static String[] RANK_NAMES={
"3","4","5","6","7","8","9","10","J","Q",
"K","A","2","小王"瞎扒昌,"大王"};
public final static String[] SUIT_NAMES={
"梅花","方塊","紅桃","黑桃",""};
@Override
public String toString() {
return SUIT_NAMES[this.suit]+RANK_NAMES[this.rank];
}
public static void main(String[] args) {
Card c =new Card(HEART, ACE);
System.out.println(c);
}
}
這個(gè)是定義若干玩家Player.java
package com.etc.week4.day03;
import java.util.Arrays;
public class Player {
String id;
String name;
Card cards[] ={};
public void addCards(Card card ){
// 每個(gè)玩家獲取的牌
cards = Arrays.copyOf(cards, cards.length+1);
cards[cards.length-1] = card;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card[] getCards() {
return cards;
}
public void setCards(Card[] cards) {
this.cards = cards;
}
public Player(String id, String name) {
this.id = id;
this.name = name;
}
public Player() {
super();
}
@Override
public String toString() {
return "Player:"+name+" cards="
+ Arrays.toString(cards) + "]";
}
}
這個(gè)是電腦隨機(jī)發(fā)牌給玩家,玩家收到電腦發(fā)的牌PlayDemo.java
package com.etc.week4.day03;
import java.util.Arrays;
import java.util.Random;
public class PlayerDemo {
public static void main(String[] args) {
// 創(chuàng)建一副撲克牌
// 方塊:13張(THREE~DEUCE)
// 梅花:13張(THREE~DEUCE)
// 黑桃:13張(THREE~DEUCE)
// 紅桃:13張(THREE~DEUCE)
// 王: BLACK COLOR
Card [] cards = new Card[54];
int i=0;
// 構(gòu)造52張普通的牌
for(int rank=Card.THREE;rank=Card.DEUCE;rank++){
cards[i++] = new Card(Card.DIAMOND, rank);
cards[i++] = new Card(Card.CLUB, rank);
cards[i++] = new Card(Card.SPADE, rank);
cards[i++] = new Card(Card.HEART, rank);
}
// 添加 大小王
cards[i++] = new Card(Card.JOKER, Card.BLACK);
cards[i++] = new Card(Card.JOKER, Card.COLOR);
// 將數(shù)組 變成集合 List
// List arr= Arrays.asList(cards);
// 將集合里面的元素 打亂... 洗牌
//Collections.shuffle(arr);
// 洗牌//
for(int n=cards.length-1;n=1;n--){
Random rd = new Random();
int index = rd.nextInt(n); //1 0
Card t = cards[n];
cards[n] = cards[index];
cards[index] = t;
}
// System.out.println("=====測(cè)試洗牌.....\n"+Arrays.toString(cards));
// 組建玩家團(tuán)隊(duì)
Player[] players = new Player[4];
players[0] = new Player("1010", "張柏芝");
players[1] = new Player("1011", "曾志偉");
players[2] = new Player("1012", "譚詠麟");
// players[3] = new Player("1013", "郭德綱");
// 發(fā)牌.....
for(int j =0;jcards.length;j++){
players[j%players.length].addCards(cards[j]);
}
// 顯示各個(gè)玩家的牌
for(int m=0;mplayers.length;m++){
System.out.println(players[m]);
}
}
}