首页 > 编程语言 > 详细

基于Java----斗地主游戏的开发

时间:2018-06-05 23:59:16      阅读:257      评论:0      收藏:0      [点我收藏+]

//这是无序的手牌 用到ArrayList集合(无排序功能)

public class PokerDemo {

public static void main(String[] args) {
//创建牌盒
ArrayList<String> array = new ArrayList<String>();

//定义一个花色数组
String[] colors = {"?","?","?","?"};
//定义一个点数数组
String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//装牌
for(String color : colors){
for(String number : numbers){
array.add(color.concat(number));
}
}
array.add("小王");
array.add("大王");

//洗牌
Collections.shuffle(array);

//发牌
ArrayList<String> s1 = new ArrayList<String>();
ArrayList<String> s2 = new ArrayList<String>();
ArrayList<String> s3 = new ArrayList<String>();
ArrayList<String> diPai = new ArrayList<String>();

for(int i = 0; i < array.size();i++){
if(i >= array.size() - 3){
diPai.add(array.get(i));
}else if(i % 3 ==0){
s1.add(array.get(i));
}else if(i % 3 ==1){
s2.add(array.get(i));
}else if(i % 3 ==2){
s3.add(array.get(i));
}
}
//看牌
lookPoker("张三",s1);
lookPoker("李四",s2);
lookPoker("王五",s3);
lookPoker("底牌",diPai);

}

public static void lookPoker(String name,ArrayList<String> array){
System.out.print(name + "的底牌是:");
for(String s : array){
System.out.print(s+" ");
}
System.out.println();
}

}

 

 

 

 

//这是有序的手牌 用到TreeSet集合(有排序功能)

import java.util.*;

public class PokerDemo {

public static void main(String[] args) {
// 创建一个HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();

// 创建一个ArrayList集合
ArrayList<Integer> array = new ArrayList<Integer>();

// 定义一个花色数组
String[] colors = { "?", "?", "?", "?" };
// 定义一个点数数组
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
// 从0开始往HashMap中里面存储编号,并存对应的牌,同时往ArrayList里面存储编号即可
int index = 0;
// 装牌
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;

}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);

// 洗牌(洗的是编号)
Collections.shuffle(array);

// 发牌
TreeSet<Integer> s1 = new TreeSet<Integer>();
TreeSet<Integer> s2 = new TreeSet<Integer>();
TreeSet<Integer> s3 = new TreeSet<Integer>();
TreeSet<Integer> diPai = new TreeSet<Integer>();

for (int i = 0; i < array.size(); i++) {
if (i >= array.size() - 3) {
diPai.add(array.get(i));
} else if (i % 3 == 0) {
s1.add(array.get(i));
} else if (i % 3 == 1) {
s2.add(array.get(i));
} else if (i % 3 == 2) {
s3.add(array.get(i));
}
}

// 看牌(遍历TreeSet集合,获取编号,到HashMap集合中找对应的牌)
lookPoker("张三", s1, hm);
lookPoker("李四", s2, hm);
lookPoker("王五", s3, hm);
lookPoker("底牌", diPai, hm);

}

public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}

}

基于Java----斗地主游戏的开发

原文:https://www.cnblogs.com/Tangxiaolin/p/9142458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!