日期:2014-05-20 浏览次数:21086 次
import org.apache.commons.lang3.ArrayUtils; import java.util.*; //import org.apache.commons.lang.ArrayUtils; public class ShowHand{ private String[] types={"\4","\5","\3","\6"}; private String[] values={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; private final int PLNUM=5; private LinkedList<String> cards=new LinkedList<String>(); private String[] players=new String[PLNUM]; private List<String>[] playersCards=new LinkedList[PLNUM]; void initCards(){ for(int i=0;i<types.length;i++) { for(int j=0;j<values.length;j++) { cards.add(types[i]+values[j]+" "); } } Collections.shuffle(cards); } boolean initPlayers(String... names){ if(names.length<2||names.length>PLNUM) { System.out.println("players nubember error."); return false; } for(int i=0;i<names.length;i++) { players[i]=names[i]; } return true; } void initPlayersCards() { for(int i=0;i<players.length&&players[i]!=null;i++) { playersCards[i]=new LinkedList<String>(); } } boolean deleverCards(String first){ boolean find=false; for(int i=0;i<players.length && (players[i]!=null);i++){ if(first==players[i]){ find=true; break; } } if(find==false){ System.out.println("name error."); return false; } // int firstPos= ArrayUtils.search(players,first); //下面是common-lang3的API调用 int firstPos= ArrayUtils.indexOf(players,first); for(int i=firstPos;i<players.length && (players[i]!=null);i++){ playersCards[i].add(cards.pollFirst()); } for(int i=0;i<firstPos;i++){ playersCards[i].add(cards.pollFirst()); } return find; } ShowHand(String... names){ initCards(); initPlayers(names); initPlayersCards(); } void showCards(){ for(String c:cards) System.out.print(c+" "); System.out.println(); } void showPlayersCards(){ for(int i=0;i<players.length&&(players[i]!=null);i++) { System.out.print(players[i]+": "); for(String c:playersCards[i]) System.out.print(c+" "); System.out.println(); } } public static void main(String[] args) { ShowHand showHand=new ShowHand("li","liu","zhang","bai"); for(int i=0;i<5;i++) showHand.deleverCards("bai"); showHand.showPlayersCards(); } }