斗地主
准备52+大小王共54张牌 存放于数组或者集合当中 用花色和牌面两个数组 嵌套循环组装牌 洗牌 shuffle 使用指定的随机元对指定列表进行置换 打乱集合中元素的顺序 发牌 定义四个集合 三个玩家 和底牌 索引%3 看牌 遍历输出储存玩家和底牌的集合
package DemoDoudizhu; import java. util. ArrayList;
import java. util. Collections;
public class Doudizhu { public static void main ( String[ ] args) { ArrayList< String> poker = new ArrayList < > ( ) ; String [ ] color= { "♥" , "♣" , "♦" , "♠" } ; String [ ] numbers= { "2" , "A" , "K" , "Q" , "J" , "10" , "9" , "8" , "7" , "6" , "5" , "4" , "3" } ; poker. add ( "大王" ) ; poker. add ( "小王" ) ; for ( String s : color) { for ( String number : numbers) {
poker. add ( s+ number) ; } } Collections. shuffle ( poker) ; ArrayList< String> play1 = new ArrayList < > ( ) ; ArrayList< String> play2 = new ArrayList < > ( ) ; ArrayList< String> play3 = new ArrayList < > ( ) ; ArrayList< String> Dipai = new ArrayList < > ( ) ; for ( int i = 0 ; i < poker. size ( ) ; i++ ) { String s = poker. get ( i) ; if ( i>= 51 ) Dipai. add ( s) ; else if ( i% 3 == 0 ) play1. add ( s) ; else if ( i% 3 == 1 ) play2. add ( s) ; else if ( i% 3 == 2 ) play3. add ( s) ; } System. out. println ( "zqh" + play1) ; System. out. println ( "wxy" + play2) ; System. out. println ( "caohe" + play3) ; System. out. println ( "底牌" + Dipai) ; }
}