在javaSE加强学习的第二天,对一个斗地主案例进行了实现,主要让大家对集合的操作有了更综合的体验,现在我将集合属性封装在Person这个对象里面,具体实现过程如下(其中为了简便,Person类和Poker类写在了一个java文件里):
package ketang.basic_enhance.day02;
import java.util.ArrayList;
import java.util.Collections;
public class Poker {
public static void main(String[] args) {
System.out.println("现在准备牌:");
ArrayList<String> poker = ready();
System.out.println(poker);
System.out.println("开始洗牌:");
Collections.shuffle(poker);
System.out.println(poker);
System.out.println("现在开始发牌:");
Person p1 = new Person("小明");
Person p2 = new Person("小红");
Person p3 = new Person("小方");
p3.isIsLandlord(true);
deal(poker,p1,p2,p3);
System.out.println("小明的牌:"+p1.getPoker()+" 共有:"+p1.getPoker().size()+"张");
System.out.println("小红的牌:"+p2.getPoker()+" 共有:"+p2.getPoker().size()+"张");
System.out.println("小方的牌:"+p3.getPoker()+" 共有:"+p3.getPoker().size()+"张");
}
public static void deal(ArrayList<String> poker,Person p1,Person p2,Person p3){
for (int i = 0; i < poker.size()-3; i++) {
switch (i%3) {
case 0:
p1.getPoker().add(poker.get(i));
break;
case 1:
p2.getPoker().add(poker.get(i));
break;
case 2:
p3.getPoker().add(poker.get(i));
break;
}
}
if(p1.getIsLandlord() == true) {
p1.getPoker().add(poker.get(poker.size()-1));
p1.getPoker().add(poker.get(poker.size()-2));
p1.getPoker().add(poker.get(poker.size()-3));
}else if(p2.getIsLandlord() == true) {
p2.getPoker().add(poker.get(poker.size() - 1));
p2.getPoker().add(poker.get(poker.size() - 2));
p2.getPoker().add(poker.get(poker.size() - 3));
}else {
p3.getPoker().add(poker.get(poker.size() - 1));
p3.getPoker().add(poker.get(poker.size() - 2));
p3.getPoker().add(poker.get(poker.size() - 3));
}
}
public static ArrayList<String> ready() {
String[] color = {"♠","♥","♣","♦"};
String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
ArrayList<String> arrayList = new ArrayList<>();
for (String s : color) {
for (String s1 : num) {
arrayList.add(s+s1);
}
}
arrayList.add("大王");
arrayList.add("小王");
return arrayList;
}
}
class Person {
private String name;
private ArrayList<String> poker = new ArrayList<>();
private boolean isLandlord;
public Person(String name) {
this.name = name;
}
public Person() {
}
public boolean getIsLandlord() {
return isLandlord;
}
public void isIsLandlord(boolean isLandlord) {
this.isLandlord = isLandlord;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getPoker() {
return poker;
}
public void setPoker(ArrayList<String> poker) {
this.poker = poker;
}
}
运行后的结果如下:
现在准备牌:
[♠A, ♠2, ♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♠J, ♠Q, ♠K, ♥A, ♥2, ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9, ♥10, ♥J, ♥Q, ♥K, ♣A, ♣2, ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♣J, ♣Q, ♣K, ♦A, ♦2, ♦3, ♦4, ♦5, ♦6, ♦7, ♦8, ♦9, ♦10, ♦J, ♦Q, ♦K, 大王, 小王]
开始洗牌:
[♣6, ♥4, ♣8, ♥A, ♦Q, ♣4, ♦5, ♠10, ♦4, ♦6, ♠8, ♣J, ♠5, ♥8, ♥9, 大王, ♣5, ♥10, ♠K, ♣2, ♥3, ♠A, ♠7, ♥J, ♥2, ♠J, ♦7, ♦10, ♣K, ♠9, ♣9, ♦J, ♠4, ♥Q, ♦3, ♦A, ♠6, ♥7, ♣10, ♥6, ♦K, ♣7, 小王, ♣3, ♠3, ♣A, ♥5, ♦2, ♠Q, ♦9, ♠2, ♣Q, ♥K, ♦8]
现在开始发牌:
小明的牌:[♣6, ♥A, ♦5, ♦6, ♠5, 大王, ♠K, ♠A, ♥2, ♦10, ♣9, ♥Q, ♠6, ♥6, 小王, ♣A, ♠Q] 共有:17张
小红的牌:[♥4, ♦Q, ♠10, ♠8, ♥8, ♣5, ♣2, ♠7, ♠J, ♣K, ♦J, ♦3, ♥7, ♦K, ♣3, ♥5, ♦9] 共有:17张
小方的牌:[♣8, ♣4, ♦4, ♣J, ♥9, ♥10, ♥3, ♥J, ♦7, ♠9, ♠4, ♦A, ♣10, ♣7, ♠3, ♦2, ♠2, ♦8, ♥K, ♣Q] 共有:20张 |
|