基于Java----斗地主游戏的开发
Posted tangxiaolin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Java----斗地主游戏的开发相关的知识,希望对你有一定的参考价值。
//这是无序的手牌 用到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----斗地主游戏的开发的主要内容,如果未能解决你的问题,请参考以下文章