Java案例斗地主游戏
目录
一案例要求:
(由于暂时没有学到通信知识,所以只会发牌,不会设计打牌游戏)
二具体代码:
Ⅰ:主函数
package three;
public class test {
public static void main(String[] args) {
Room room=new Room();
room.start();
}
}
Ⅱ:实体类:
package three;
public class card {
private String number;
private String color;
private int size;
public card() {
}
public card(String number, String color, int size) {
this.number = number;
this.color = color;
this.size = size;
}
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 getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Override
public String toString() {
return color+number;
}
}
Ⅲ操作类:
package three;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Room {
private List<card> allcards=new ArrayList<>();
public Room(){
String []numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String []color={"♠","♣","■","♥"};
int size=0;
for (String number : numbers) {
size++;
for (String s : color) {
card a=new card(number,s,size);
allcards.add(a);
}
}
card a1=new card("","🃏",++size);
card a2=new card("","🃏",++size);
Collections.addAll(allcards,a1,a2);
System.out.println("新牌"+allcards);
}
public void start() {
Collections.shuffle(allcards);
System.out.println("洗牌后"+allcards);
List<card>xiaozhang=new ArrayList<>();
List<card>xiaowang=new ArrayList<>();
List<card>xiaochong=new ArrayList<>();
for (int i = 0; i < allcards.size()-3; i++) {
if(i%3==0){
xiaozhang.add(allcards.get(i));
}
if(i%3==1){
xiaowang.add(allcards.get(i));
}
if(i%3==2){
xiaochong.add(allcards.get(i));
}
}
sort(xiaozhang);
sort(xiaowang);
sort(xiaochong);
System.out.println("小张"+xiaozhang);
System.out.println("小王"+xiaowang);
System.out.println("小冲"+xiaochong);
List<card> left=allcards.subList(allcards.size()-3,allcards.size());
xiaozhang.addAll(left);
sort(xiaozhang);
System.out.println("小张抢到地主牌后"+xiaozhang);
}
public void sort(List<card> a){
Collections.sort(a, new Comparator<card>() {
@Override
public int compare(card o1, card o2) {
return o1.getSize()-o2.getSize();
}
});
}
}
原文地址:https://blog.csdn.net/mrjieke6/article/details/140496261
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!