这里只打印了第一个人person1 的牌,你也可以按照类似的方法查看其他人的牌或者剩下的底牌。
package Java学习.Java高级.集合.斗地主小游戏;
public class Main {
public static void main(String[] args) {
GameStar gameStar = new GameStar();
for (String s : gameStar.getPerson1()) {
System.out.println(s);
}
}
}
package Java学习.Java高级.集合.斗地主小游戏;
import java.util.ArrayList;
import java.util.Random;
public class GameStar {
private ArrayList<String> totalCards = new ArrayList<>();
private ArrayList<String> ghosts = new ArrayList<>();
private ArrayList<String> spade = new ArrayList<>();
private ArrayList<String> hearts = new ArrayList<>();
private ArrayList<String> clubs = new ArrayList<>();
private ArrayList<String> person1 = new ArrayList<>();
private ArrayList<String> person2 = new ArrayList<>();
private ArrayList<String> person3 = new ArrayList<>();
private ArrayList<String> leftOver = new ArrayList<>();
public ArrayList<String> getTotalCards() {
return totalCards;
}
public ArrayList<String> getGhosts() {
return ghosts;
}
public ArrayList<String> getSpade() {
return spade;
}
public ArrayList<String> getHearts() {
return hearts;
}
public ArrayList<String> getClubs() {
return clubs;
}
public ArrayList<String> getPerson1() {
return person1;
}
public ArrayList<String> getPerson2() {
return person2;
}
public ArrayList<String> getPerson3() {
return person3;
}
public ArrayList<String> getLeftOver() {
return leftOver;
}
public ArrayList<String> getDiamonds() {
return diamonds;
}
public String[] getBases() {
return bases;
}
public GameStar() {
//初始化
init();
//洗牌算法:
totalCards = shuffle(totalCards);
//发牌
deal(totalCards);
}
private ArrayList<String> diamonds = new ArrayList<>();
private String[] bases = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
public void init() {
//给牌赋值
setArrayList(ghosts);
setArrayList(spade);
setArrayList(hearts);
setArrayList(clubs);
setArrayList(diamonds);
setArrayList(totalCards);
}
public void setArrayList(ArrayList<String> arrayList) {
if (arrayList == ghosts) {
ghosts.add("大王");
ghosts.add("小王");
} else if (arrayList == spade) {
for (int i = 0; i < bases.length; i++) {
spade.add("黑桃" + bases[i]);
}
} else if (arrayList == hearts) {
for (int i = 0; i < bases.length; i++) {
hearts.add("红桃" + bases[i]);
}
} else if (arrayList == clubs) {
for (int i = 0; i < bases.length; i++) {
clubs.add("梅花" + bases[i]);
}
} else if (arrayList == diamonds) {
for (int i = 0; i < bases.length; i++) {
diamonds.add("方块" + bases[i]);
}
} else if (arrayList == totalCards) {
// totalCards.clear();
for (int i = 0; i < ghosts.size(); i++) {
totalCards.add(ghosts.get(i));
}
for (int i = 0; i < spade.size(); i++) {
totalCards.add(spade.get(i));
}
for (int i = 0; i < hearts.size(); i++) {
totalCards.add(hearts.get(i));
}
for (int i = 0; i < clubs.size(); i++) {
totalCards.add(clubs.get(i));
}
for (int i = 0; i < diamonds.size(); i++) {
totalCards.add(diamonds.get(i));
}
}
}
//洗牌算法:
public ArrayList<String> shuffle(ArrayList<String> arrayList) {
ArrayList<String> arrayList2 = new ArrayList<>();
int length = arrayList.size();
for (int i = 0; i < length; i++) {
Random random = new Random();
int x = random.nextInt(arrayList.size());
arrayList2.add(arrayList.get(x));
arrayList.remove(x);
}
return arrayList2;
}
//发牌
public void deal(ArrayList<String> totalCards) {
for (int i = 0; i < 54; i++) {
if (i < 51) {
if (i % 3 == 0) {
person1.add(totalCards.get(i));
} else if (i % 3 == 1) {
person2.add(totalCards.get(i));
} else if (i % 3 == 2) {
person3.add(totalCards.get(i));
}
} else if (i >= 51 && i < 54) {
leftOver.add(totalCards.get(i));
}
}
}
}
梅花7
黑桃2
方块9
黑桃9
红桃3
红桃A
红桃Q
红桃6
红桃4
黑桃4
黑桃5
红桃7
方块7
黑桃6
黑桃K
方块4
方块K
Process finished with exit code 0
思路新建一个集合arrayList2 ,随机取出一个 arrayList 中的元素放到 arrayList 中,然后删除 arrayList 中的该元素。
GameStar的属性用 private 私有修饰。其实赋值的一些方法也可以用私有修饰,用共有的构造器调用初始化init()和其他方法,完成赋值。只留下一些必要的公有的public ,来访问元素,操作设置等
===========================================
===================================================================手动分割
* 二:步骤
* 1,合成总牌,
* 2,总牌打乱顺序
* 3.发牌给3玩家和底牌
* 4.排序
* 5.输出
package Java学习.Java高级.集合.斗地主小游戏.斗地主小游戏2实现排序;
import java.util.*;
/**
* 一:目的:获得打印出有序的三个玩家手牌和底牌。
* 二:步骤
* 1,合成总牌,
* 2,总牌打乱顺序
* 3.发牌给3玩家和底牌
* 4.排序
* 5.输出
* 三、需要的数据定义
* 四、注意:编程思维,
* 1.init()函数初始化数据
* 2.构造函数调用init()函数和步骤函数
* 3.步骤函数分开写,低耦合
*/
public class GameStar2 {
private HashMap<Integer, String> totalCards = new HashMap<>();
private ArrayList<Integer> person1 = new ArrayList<>();
private ArrayList<Integer> person2 = new ArrayList<>();
private ArrayList<Integer> person3 = new ArrayList<>();
private ArrayList<Integer> leftOver = new ArrayList<>();
private String[] bases = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
private ArrayList<Integer> sequence = new ArrayList<>();
public ArrayList<Integer> getSequence() {
return sequence;
}
public void setSequence(ArrayList<Integer> sequence) {
this.sequence = sequence;
}
/**
* 一、构造器调用init() and the methods.
* 二:步骤
* 1,合成总牌,
* 2,总牌打乱顺序
* 3.发牌给3玩家和底牌
* 4.排序
* 5.输出
*/
public GameStar2() {
init();
//1,合成总牌,setTotalCards()
setTotalCards();
//2.洗牌 shuffleSequence(); 打乱顺序
shuffleSequence();
//3.发牌
deal();
//4.排序
sortCards();
//5.打印输出
printCards();
}
/**
* init() 数据初始化
*/
private void init() {
for (int i = 0; i < 54; i++) {
sequence.add(i);
}
}
/**
* 步骤 1,合成总牌,setTotalCards()
* 二、具体
* 1,0 1 大小王
* 2.黑桃2,红桃2,
*/
private void setTotalCards() {
int index = bases.length - 1;
for (int i = 0; i < 54; i++) {
if (i == 0) {
totalCards.put(i, "大王");
continue;
} else if (i == 1) {
totalCards.put(i, "小王");
continue;
}
//四色牌
else if (i % 4 == 2) {
totalCards.put(i, "黑桃" + bases[index]);
continue;
} else if (i % 4 == 3) {
totalCards.put(i, "红心" + bases[index]);
continue;
} else if (i % 4 == 0) {
totalCards.put(i, "梅花" + bases[index]);
continue;
} else if (i % 4 == 1) {
totalCards.put(i, "方块" + bases[index]);
index -= 1;
continue;
}
}
}
/**
* 2.洗牌 shuffleSequence()
* 问题:如何实现所有的值的类型都可打乱
* 二、步骤
* 三、错误,for()里++,for{}又 --就只有一半了。
*/
private void shuffleSequence() {
ArrayList<Integer> arrayList2 = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < sequence.size(); ) {
int x = random.nextInt(sequence.size());
arrayList2.add(sequence.get(x));
sequence.remove(x);
}
sequence = arrayList2;
}
/**
* 3.发牌
*/
private void deal() {
for (int i = 0; i < 54; i++) {
if (i < 3) {
leftOver.add(sequence.get(i));
continue;
} else if (i % 3 == 0) {
person1.add(sequence.get(i));
continue;
} else if (i % 3 == 1) {
person2.add(sequence.get(i));
continue;
} else if (i % 3 == 2) {
person3.add(sequence.get(i));
continue;
}
}
}
/**
* 4.排序 Collections.sort(arr);
*/
private void sortCards() {
//默认升序,改为降序,然后牌由小到大
Collections.sort(leftOver, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
Collections.sort(person1, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
Collections.sort(person2, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
Collections.sort(person3, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
}
/**
* 5.打印输出:
*/
private void printCards() {
System.out.println("=====开始发牌======");
System.out.print("person1: ");
for (Integer integer : person1) {
System.out.print(totalCards.get(integer) + " ");
}
System.out.println();
System.out.print("person2: ");
for (Integer integer : person2) {
System.out.print(totalCards.get(integer) + " ");
}
System.out.println();
System.out.print("person3: ");
for (Integer integer : person3) {
System.out.print(totalCards.get(integer) + " ");
}
System.out.println();
System.out.print("leftOver: ");
for (Integer integer : leftOver) {
System.out.print(totalCards.get(integer) + " ");
}
System.out.println("\n======发牌结束====");
}
}
package Java学习.Java高级.集合.斗地主小游戏.斗地主小游戏2实现排序;
/**
* 使用键值对, Collections.sort(arr,new Comparator()),对牌进行排序
*/
public class Main {
public static void main(String[] args) {
long timeStar = System.currentTimeMillis();
new GameStar2();
long timeEnd = System.currentTimeMillis();
System.out.println("the time is : " + (timeEnd - timeStar)+"毫秒");
}
}
输出:
=====开始发牌======
person1: 红心3 黑桃3 方块4 红心5 方块6 梅花6 方块7 红心8 黑桃8 红心9 梅花J 黑桃J 梅花Q 红心Q 黑桃A 方块2 大王
person2: 梅花4 黑桃4 方块5 梅花5 红心6 黑桃7 方块8 梅花8 方块10 梅花10 红心10 红心J 方块Q 方块K 梅花K 黑桃K 红心2
person3: 方块3 梅花3 红心4 黑桃5 黑桃6 梅花7 红心7 方块9 梅花9 黑桃9 黑桃10 方块J 黑桃Q 方块A 梅花A 红心A 梅花2
leftOver: 红心K 黑桃2 小王
======发牌结束====
the time is : 7毫秒
int a = 10
int sum = 0;
for(int i = 0 ;i <10;i++){
sum += i;
a --;
}