java随机发扑克牌程序,java_扑克牌⼩程序洗牌:数组重新排序 Collections.shuffle(cards);
Card:牌的属性
TestCard:测试
Player:玩家,发牌
import java.util.Objects;
public class Card {
public static final String[] SUITE_NAME = {"红桃","⽅块","梅花",
"⿊桃",""};
public static final String[] RANK_NAME = {"3","4","5","6","7",
"8","9","10","J","Q","K","A","2","⼩王","⼤王"};
public static final int HEART = 0;
public static final int DIAMOND = 1;
public static final int CLUB = 2;
public static final int SPADE = 3;
入门的java游戏小程序public static final int JOKER = 4;
public static final int THREE = 0;
public static final int FOUR = 1;
public static final int FIVE = 2;
public static final int SIX = 3;
public static final int SEVEN = 4;
public static final int EIGHT = 5;
public static final int NINE = 6;
public static final int TEN = 7;
public static final int JACK = 8;
public static final int QUEUE = 9;
public static final int KING = 10;
public static final int ACE = 11;
public static final int TWO = 12;
public static final int BLACK = 13;
public static final int COLOR = 14;
private int suit; //花⾊
private int rank; //点数
public Card() {}
public Card(int suit, int rank) {//构造⽅法
this.suit = suit;
this.rank = rank;
}
@Override
public String toString() {
return SUITE_NAME[suit]+RANK_NAME[rank];
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; Card card = (Card) o;
return suit == card.suit &&
rank == card.rank;
}
@Override
public int hashCode() {
return Objects.hash(suit, rank);
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}
//Player 玩家类
import java.util.Arrays;
public class Player {
private int id;//玩家id属性
private String name;//玩家姓名
private Card[] cards;//玩家⼿牌数组//构造⽅法,花⾊点数
public Player(int id, String name) {
this.id = id;
this.name = name;
this.cards = new Card[]{};
}
public void addCard(Card c) {
cards = pyOf(cards, cards.length + 1);//数组扩容,⼿牌长度=⼿牌长度+1
cards[cards.length - 1] = c;//把牌放进扩容后的数组中
}
@Override
public String toString() {
return id+","+name+":"+String(cards);
}
}
package cn.besttest.day4_homework;
import java.util.*;
public class TestCard {
public static void main(String[] args) {
List cards = new ArrayList<>();//创建⼀个cards数组,数组类型Crad,(花⾊,点数)
for (int rank = Card.THREE; rank <= Card.TWO; rank++) { //创建⼀副扑克牌,THREE=0,TWO=12 ⼩3-2的位数cards.add(new Card(Card.HEART, rank));//添加颜⾊和对应点数 红桃
cards.add(new Card(Card.DIAMOND, rank));//添加颜⾊和对应点数 ⽅块
cards.add(new Card(Card.CLUB, rank));//添加颜⾊和对应点数 梅花
cards.add(new Card(Card.SPADE, rank));//添加颜⾊和对应点数 ⿊桃
}
cards.add(new Card(Card.JOKER,Card.BLACK));//添加⼩王
cards.add(new Card(Card.JOKER,Card.COLOR));//添加⼤王
System.out.println(cards);
//洗牌
// Random ran=new Random();
// for (int i=cards.size()-1;i>0;i--){//到0的话就没有换的了,所以要留⼀个
// int Int(i);
// Card (index);
// Card (i);
// cards.set(i,c);
// cards.set(index,temp);
// }
Collections.shuffle(cards);// 把⼀个数组中数据重写排列
System.out.println(cards);
Player[] players={new Player(1,"碧瑶"),new Player(2,"陆雪琪"),new Player(3,"张⼩凡")}; for (int i=0;i
Card (i);
players[i%players.length].addCard(c);
}
System.out.println(players[0]);
System.out.println(players[1]);
System.out.println(players[2]);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论