Java實戰項目之鬥地主和鬥牛遊戲的實現
一、前言
練一個鬥地主小遊戲,隻能發看牌
1.鬥地主:
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class PokerPlay2 { public static void main(String[] args) { ArrayList<String> array=new ArrayList<String>(); String []colors={"♣","♥","♠","♦"}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; for (String color:colors){ for (String number:numbers){ array.add(color+number); } }int count=1; array.add("大🃏"); array.add("小🃏"); while(true){ System.out.println("第"+count+++"局"); Collections.shuffle(array); ArrayList<String> poker1=new ArrayList<String>(); ArrayList<String> poker2=new ArrayList<String>(); ArrayList<String> poker3=new ArrayList<String>(); ArrayList<String> poker4=new ArrayList<String>(); for (int i=0;i<array.size();i++){ String poker=array.get(i); if (i>=array.size()-3){ poker4.add(poker); }else if(i%3==0){ poker1.add(poker); }else if(i%3==1){ poker2.add(poker); }else if(i%3==2){ poker3.add(poker); } } Scanner sc=new Scanner(System.in); String name1=sc.nextLine(); lookPoker("1", poker1); lookPoker("2", poker2); lookPoker("3", poker3); lookPoker("底牌", poker4); }} public static void lookPoker(String name,ArrayList<String> arrayList){ System.out.print(name+"的牌:"); for (String s:arrayList){ System.out.print(" "+s); } System.out.println(); } }
1.1運行結果:
2.鬥地主升級版
增加瞭對牌的排序和地主牌的加入,後續可能會增加玩牌的功能
2.1原理:
鬥地主升級版的原理就是,創建HashMap用來後續鍵找值,然後創建ArrayList
集合(創建其他集合也是可以的)ArrayList集合的作用是用來存儲和HashMap
對應的鍵值,通過兩個for進行組合每產生一個就將鍵值加一,產生不同的鍵值,
所對應的牌是唯一, 這樣為後續排列打下瞭堅實的前提,然後再創建TreeSet集
合(TreeSet集合可以進行自然排序),然後將ArrayList集合中的元素(其實就是
一些數子,這些數字是HashMap中的鍵值),分配到三個人上,裡面的已經被
TreeSet排序完成,調用方法是通過HashMap將每個人的鍵值來得到對應的牌。
import java.util.*; public class PokerPuls { public static void main(String[] args) { //定義HashMap集合 HashMap<Integer,String> hm=new HashMap<Integer, String>(); //定義ArrayList集合用來存儲編號 ArrayList<Integer> array=new ArrayList<Integer>(); //定義花色和底數數組 String []colors={"♣","♥","♠","♦"}; String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2"}; int index=0; //註意需要將數字for在外頭,否則是按花色排序的 for (String number:numbers){ for (String color:colors){ hm.put(index, color+number); array.add(index); index++; } } hm.put(index, "小🃏"); array.add(index); index++; hm.put(index, "大🃏"); array.add(index); int count=1; while(true){ System.out.println("**********第"+count+++"局鬥地主**********"); //洗牌 Collections.shuffle(array); //創建四個牌位分別用來存儲三個玩傢和三張底牌,因要排序所以用TreeSet集合 TreeSet<Integer> play1=new TreeSet<Integer>(); TreeSet<Integer> play2=new TreeSet<Integer>(); TreeSet<Integer> play3=new TreeSet<Integer>(); TreeSet<Integer> play4=new TreeSet<Integer>(); for (int i=0;i<array.size();i++){ Integer index1 = array.get(i); if(i>=array.size()-3){ play4.add(index1); }else if (i%3==0){ play1.add(index1); }else if (i%3==1){ play2.add(index1); }else if (i%3==2){ play3.add(index1); } } Scanner sc=new Scanner(System.in); System.out.print("第一位玩傢:"); String name1=sc.nextLine(); System.out.print("第二位玩傢:"); String name2=sc.nextLine(); System.out.print("第三位玩傢:"); String name3=sc.nextLine(); lookerPoker("1號:"+name1, play1,hm); lookerPoker("2號:"+name2, play2,hm); lookerPoker("3號:"+name3, play3,hm ); lookerPoker("底牌", play4,hm); int i=0; int num=0; while(true){ System.out.print("幾號是地主:"); num=sc.nextInt(); switch(num){ case 1:{i++; play1.addAll(play4);break; } case 2:{i++; play2.addAll(play4);break; } case 3:{i++; play3.addAll(play4);break; } default:{ System.out.println("輸入有誤,重新輸入");break; } } lookerPoker("1號:"+name1, play1,hm); lookerPoker("2號:"+name2, play2,hm); lookerPoker("3號:"+name3, play3,hm ); if (i!=0){ break; } System.out.println("-------遊戲結束-------"); } } } //定義遍歷方法,並通過存儲的index1的鍵來獲取對應的值 public static void lookerPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){ System.out.print(name+"的牌:"); for (Integer key:ts){ String value=hm.get(key); System.out.print(value+" "); } System.out.println(); }
2.2運行結果:
3.鬥牛遊戲:
import java.util.*; public class PokerPlay { public static void main(String[] args) { //定義一個ArrayList集合用來存放排盒 //分別用來給玩傢和莊傢的兩副牌 ArrayList<String> array = new ArrayList<String>(); ArrayList<String> array1 = new ArrayList<String>(); //定義花色、點數數組 String[] color = {"♣", "♦", "♠", "♥"}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; //封裝組合成牌放入集合中 for (String s1 : color) { for (String s2 : numbers) { array.add(s1 + s2); } } //定義莊傢的牌盒 String[] color1 = {"♣", "♦", "♠", "♥"}; String[] number1 = {"10", "J", "Q", "K",}; for (String s1 : color1) { for (String s2 : number1) { array1.add(s1 + s2); } } int count = 1;//定義局數 while (true) { int sum1 = 0, sum2 = 0, sum3 = 0; int sum4 = 0, sum5 = 0; //打亂牌的順序 Collections.shuffle(array); Collections.shuffle(array1); //定義五個牌位 ArrayList<String> poker1 = new ArrayList<String>(); ArrayList<String> poker2 = new ArrayList<String>(); ArrayList<String> poker3 = new ArrayList<String>(); ArrayList<String> poker4 = new ArrayList<String>(); ArrayList<String> poker5 = new ArrayList<String>(); // 為瞭防止玩傢崩潰,隨機數給莊傢無敵牌或普通牌 Random r=new Random(); int 換位=r.nextInt(100); //給莊傢的無敵牌 for (int i=0;i<array1.size();i++){ String s1 = array1.get(i); if(sum5<5&&換位<50){ poker5.add(s1); sum5++; } } for (int i = 0; i < array.size(); i++) { String poker = array.get(i);//得到每張牌 if (sum1++ < 5) { poker1.add(poker); } else if (sum2++ < 5) { poker2.add(poker); } else if (sum3++ < 5) { poker3.add(poker); } else if (sum4++< 5) { poker4.add(poker); }//使莊傢的牌正常,可以設置多少一個輪回 else if (sum5++<5) { poker5.add(poker); } } System.out.println("**************第" + (count++) + "局鬥牛遊戲開始:**************"); Scanner sc = new Scanner(System.in); System.out.print("莊傢:"); String play5 = sc.nextLine(); System.out.print("第一位玩傢:"); String play1 = sc.nextLine(); System.out.print("第二位玩傢:"); String play2 = sc.nextLine(); System.out.print("第三位玩傢:"); String play3 = sc.nextLine(); System.out.print("第四位玩傢:"); String play4 = sc.nextLine(); System.out.println("-------買定離手--------"); lookPoker("莊傢"+play5, poker5); lookPoker("玩傢"+play1, poker1); lookPoker("玩傢"+play2, poker2); lookPoker("玩傢"+play3, poker3); lookPoker("玩傢"+play4, poker4); System.out.println("-------遊戲結束--------"); } } //定義一個看牌的動作 public static void lookPoker(String name, ArrayList<String > arrayList){ //遍歷牌 System.out.print(name + "的牌:"); for (String poker : arrayList) { System.out.print(" " + poker); } System.out.println(); }}
其中 在用random,用if是控制莊傢的牌
Random r=new Random(); int 換位=r.nextInt(100); //給莊傢的無敵牌 for (int i=0;i<array1.size();i++){ String s1 = array1.get(i); if(sum5<5&&換位<50){ poker5.add(s1); sum5++; }
3.1運行結果:
到此這篇關於Java實戰項目之鬥地主和鬥牛遊戲的實現的文章就介紹到這瞭,更多相關Java 鬥地主內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!