鬥地主發牌ArrayList
阿新 • • 發佈:2021-02-08
技術標籤:筆記
鬥地主
用arraylist 和 for each 以及for
實現
♠♣♦❤
1.準備牌
2.將牌組合
3.給玩家分牌
import java.util.*;
public class Main
{
public static void main(String[] args)
{
ArrayList <String>pkp =new ArrayList<String>();
String [] color ={"黑桃","♡","♧","♢" };
String [] num={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
pkp.add("大王"); pkp.add("小王");
for(String num1:num){
for(String color1: color){
pkp.add(color1+num1);}}
System.out.print("原本的牌: ");
System.out.println(pkp);
Collections.shuffle(pkp);
System.out.print("打亂後的牌: ");
System.out.println(pkp);
ArrayList <String> wj1=new ArrayList<String>();
ArrayList <String> wj2=new ArrayList <String>();
ArrayList <String> wj3=new ArrayList<String>();
ArrayList <String> dipan=new ArrayList<String>();
for(int i=1;i<pkp.size();i++){
String pkp1=pkp.get(i);
if(i>=51){
dipan.add(pkp1);
}else if(i%3==0){
wj1.add(pkp.get(i));
}else if(i%3==1){
wj2.add(pkp1);
}else if(i%3==2){
wj3.add(pkp1);}}
System.out.println("玩家1的牌: "+wj1);
System.out.println("玩家2的牌: "+wj2);
System.out.println("玩家3的牌: "+wj3);
System.out.println("底牌: "+dipan);
效果圖