Java實戰(zhàn)項目之斗地主和斗牛游戲的實現(xiàn)
一、前言
練一個斗地主小游戲,只能發(fā)看牌
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運行結(jié)果:
2.斗地主升級版
增加了對牌的排序和地主牌的加入,后續(xù)可能會增加玩牌的功能
2.1原理:
斗地主升級版的原理就是,創(chuàng)建HashMap用來后續(xù)鍵找值,然后創(chuàng)建ArrayList
集合(創(chuàng)建其他集合也是可以的)ArrayList集合的作用是用來存儲和HashMap
對應(yīng)的鍵值,通過兩個for進(jìn)行組合每產(chǎn)生一個就將鍵值加一,產(chǎn)生不同的鍵值,
所對應(yīng)的牌是唯一, 這樣為后續(xù)排列打下了堅實的前提,然后再創(chuàng)建TreeSet集
合(TreeSet集合可以進(jìn)行自然排序),然后將ArrayList集合中的元素(其實就是
一些數(shù)子,這些數(shù)字是HashMap中的鍵值),分配到三個人上,里面的已經(jīng)被
TreeSet排序完成,調(diào)用方法是通過HashMap將每個人的鍵值來得到對應(yīng)的牌。
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>(); //定義花色和底數(shù)數(shù)組 String []colors={"♣","♥","♠","♦"}; String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2"}; int index=0; //注意需要將數(shù)字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); //創(chuàng)建四個牌位分別用來存儲三個玩家和三張底牌,因要排序所以用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("-------游戲結(jié)束-------"); } } } //定義遍歷方法,并通過存儲的index1的鍵來獲取對應(yīng)的值 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運行結(jié)果:
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>(); //定義花色、點數(shù)數(shù)組 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;//定義局?jǐn)?shù) 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>(); // 為了防止玩家崩潰,隨機(jī)數(shù)給莊家無敵牌或普通牌 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); }//使莊家的牌正常,可以設(shè)置多少一個輪回 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("-------游戲結(jié)束--------"); } } //定義一個看牌的動作 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運行結(jié)果:
到此這篇關(guān)于Java實戰(zhàn)項目之斗地主和斗牛游戲的實現(xiàn)的文章就介紹到這了,更多相關(guān)Java 斗地主內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java如何將若干時間區(qū)間進(jìn)行合并的方法步驟
這篇文章主要介紹了Java如何將若干時間區(qū)間進(jìn)行合并的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Springboot整合logback多節(jié)點日志文件加端口號區(qū)分的操作方法
這篇文章主要介紹了Springboot整合logback多節(jié)點日志文件加端口號區(qū)分的操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09