JAVA斗地主
实现:扑克随机发给三名玩家,并按默认大小的牌序显示。
public class Poke {
private String huase;
private String shuzi;
private int key; //排列序号
public String getHuase() {
return huase;
}
public void setHuase(String huase) {
this.huase = huase;
}
public String getShuzi() {
return shuzi;
}
public void setShuzi(String shuzi) {
this.shuzi = shuzi;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
@Override
public String toString() {
return "poke{" +
"huase='" + huase + '\'' +
", shuzi='" + shuzi + '\'' +
'}';
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class DouDiZhu {
List<Poke> PokeList = new ArrayList<>();
//生成牌
public void Make(){
int key = 0;
String[] colors = {"♡", "♠", "♢", "♣", "小", "大"};
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "王"};
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 14; j++) {
if (i < 4 && j < 13) {
Poke poke = new Poke();
poke.setHuase(colors[i]);
poke.setShuzi(numbers[j]);
PokeList.add(poke);
}
if (i >= 4 && j >= 13) {
Poke poke = new Poke();
poke.setHuase(colors[i]);
poke.setShuzi(numbers[j]);
PokeList.add(poke);
}
}
}
System.out.println("生成牌:");
for (Poke x : PokeList) {
if (x.getShuzi() == "A")
key = 11;
if (x.getShuzi() == "2")
key = 12;
if (x.getHuase() == "小")
key = 13;
if (x.getHuase() == "大")
key = 14;
x.setKey(key);
System.out.print(x.getHuase() + x.getShuzi() + " ");
key++;
if(key >= 11){
key = 0;
}
}
}
//洗牌
public void Wish(){
System.out.println("\n洗 牌:");
Collections.shuffle(PokeList);
for (Poke x : PokeList) {
System.out.print(x.getHuase() + x.getShuzi() + " ");
}
}
//随机发牌
public void Give(){
List<Poke> Player1 = new ArrayList<>();
List<Poke> Player2 = new ArrayList<>();
List<Poke> Player3 = new ArrayList<>();
List<Poke> LastPoke = new ArrayList<>();
for (int i = 0; i < PokeList.size(); i++) {
Poke poke = PokeList.get(i);
if (i >= 51){
LastPoke.add(poke);
}else{
if(i % 3 ==0){
Player1.add(poke);
}else if (i % 3==1){
Player2.add(poke);
}else{
Player3.add(poke);
}
}
}
System.out.println("\n底牌:");
for (Poke x : SelectSort(LastPoke)) {
System.out.print(x.getHuase() + x.getShuzi() + " ");
}
System.out.println("\n玩家1:");
for (Poke x : SelectSort(Player1)) {
System.out.print(x.getHuase() + x.getShuzi() + " ");
}
System.out.println("\n玩家2:");
for (Poke x : SelectSort(Player2)) {
System.out.print(x.getHuase() + x.getShuzi() + " ");
}
System.out.println("\n玩家3:");
for (Poke x : SelectSort(Player3)) {
System.out.print(x.getHuase() + x.getShuzi() + " ");
}
}
//选择排序进行大小整理
public List<Poke> SelectSort(List<Poke> pokes){
int MinSub;
for (int i = 0; i < pokes.size(); i++) {
MinSub = i;
for (int j = i + 1; j < pokes.size(); j++) {
if (pokes.get(j).getKey() < pokes.get(MinSub).getKey())
MinSub = j;
}
Collections.swap(pokes, i, MinSub);
}
return pokes;
}
}
public class Start {
public static void main(String[] args) {
DouDiZhu doudizhu = new DouDiZhu();
doudizhu.Make();
doudizhu.Wish();
doudizhu.Give();
}
}