斗地主游戏思想
创建新牌
要创建一个牌组数组
public class Cards {
private String number;
private String color;
private int index;
public Cards() {
}
public Cards(String number, String color, int index) {
this.number = number;
this.color = color;
this.index = index;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String toString() {
return number + color;
}
}
牌组加入新牌
public static List<Cards> All_Cards = new ArrayList<>();
static{
// 1.定义一个数组存储牌的点数,类型确定,个数确定请用数组存储!
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
// 2.定义一个数组存储牌的花色,类型确定,个数确定请用数组存储!
int index = 0;
String[] colors = { "♠", "♥", "♣", "♦" };
for (String number : numbers) {
for (String color : colors) {
Cards card = new Cards(number,color,index ++);
All_Cards.add(card);
}
}
Collections.addAll(All_Cards,new Cards("","🃏",index ++),new Cards("","👲",index ++));
}
洗牌
/*洗牌*/
System.out.println("洗牌前" + All_Cards);
Collections.shuffle(All_Cards);
System.out.println("洗牌后" + All_Cards);
发牌
/* 定义三个玩家*/
List<Cards> zhangwuji = new ArrayList<>();
List<Cards> zhaomin = new ArrayList<>();
List<Cards> zhouzhiruo = new ArrayList<>();
/*发牌*/
for(int i = 0;i < All_Cards.size() - 3;i ++){
Cards ca = All_Cards.get(i);
switch (i % 3) {
case 0 -> zhangwuji.add(ca);
case 1 -> zhaomin.add(ca);
case 2 -> zhouzhiruo.add(ca);
}
}
发牌排序
private static void sortCards(List<Cards> cards){
cards.sort(Comparator.comparingInt(Cards::getIndex));
}
/*对牌排序*/
sortCards(zhangwuji);
sortCards(zhouzhiruo);
sortCards(zhaomin);
看牌
/*看牌*/
System.out.println("张无忌" + zhangwuji);
System.out.println("赵敏" + zhaomin);
System.out.println("周芷若" + zhouzhiruo);
List<Cards> LastCards = All_Cards.subList(All_Cards.size() - 3,All_Cards.size());
System.out.println("底牌" + LastCards);
总体代码
牌组类
public class Cards {
private String number;
private String color;
private int index;
public Cards() {
}
public Cards(String number, String color, int index) {
this.number = number;
this.color = color;
this.index = index;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String toString() {
return number + color;
}
}
牌的操作
public class Main_Demo {
public static List<Cards> All_Cards = new ArrayList<>();
static{
// 1.定义一个数组存储牌的点数,类型确定,个数确定请用数组存储!
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
// 2.定义一个数组存储牌的花色,类型确定,个数确定请用数组存储!
int index = 0;
String[] colors = { "♠", "♥", "♣", "♦" };
for (String number : numbers) {
for (String color : colors) {
Cards card = new Cards(number,color,index ++);
All_Cards.add(card);
}
}
Collections.addAll(All_Cards,new Cards("","🃏",index ++),new Cards("","👲",index ++));
}
//对分的牌排序 降序 "3","4","5","6","7","8","9","10","J","Q","K","A","2","🃏","👲"
private static void sortCards(List<Cards> cards){
cards.sort(Comparator.comparingInt(Cards::getIndex));
}
public static void main(String[] args){
/*洗牌*/
System.out.println("洗牌前" + All_Cards);
Collections.shuffle(All_Cards);
System.out.println("洗牌后" + All_Cards);
/* 定义三个玩家*/
List<Cards> zhangwuji = new ArrayList<>();
List<Cards> zhaomin = new ArrayList<>();
List<Cards> zhouzhiruo = new ArrayList<>();
/*发牌*/
for(int i = 0;i < All_Cards.size() - 3;i ++){
Cards ca = All_Cards.get(i);
switch (i % 3) {
case 0 -> zhangwuji.add(ca);
case 1 -> zhaomin.add(ca);
case 2 -> zhouzhiruo.add(ca);
}
}
/*对牌排序*/
sortCards(zhangwuji);
sortCards(zhouzhiruo);
sortCards(zhaomin);
/*看牌*/
System.out.println("张无忌" + zhangwuji);
System.out.println("赵敏" + zhaomin);
System.out.println("周芷若" + zhouzhiruo);
List<Cards> LastCards = All_Cards.subList(All_Cards.size() - 3,All_Cards.size());
System.out.println("底牌" + LastCards);
}
}