梁XX
2016-02-08 23:35
用了3个晚上写出来,在每个玩家选出最大的牌子里面糊涂了,导致自己找bug找了很久。其实名字一定要用心起好,才免于自己一直费时间找bug。代码放上来,每个类都有一定的步骤解释,各位一起交流下,一起进步!么么哒(*  ̄3)(ε ̄ *)
主要分三个大类:扑克牌、玩家、游戏(发牌、拿牌、比牌)
扑克牌创建:
package com.imooc.season3.porkDemo; import java.util.ArrayList; import java.util.List; /* * 创建扑克牌 * ①创建一个扑克牌集合,利用List集合 * ②创建一个数字数组,装2,3... * ③创建一个花色数组,装方片,梅花... */ public class CardCreation { List<String> cards = new ArrayList<String>(); String[] digits = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; String[] col ={"方片","红桃","梅花","黑桃"}; public void cardCreation(){ for(int i=0;i<digits.length;i++){ for(int y=0;y<col.length;y++){ String tmp =col[y]+digits[i]; cards.add(tmp); } } } public void cardShow(){ System.out.println("============扑克牌创建成功============"); for(String cards:cards){ System.out.print(cards+" "); } System.out.println(""); } }
//创建玩家 package com.imooc.season3.porkDemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.Map.Entry; import com.imooc.season3.CollectionDemo.Student; /* * 创建游戏玩家 * ①玩家具有ID, 昵称,手中的牌的集合三个属性 * ②游戏设定为两个玩家,并且ID具有异常捕捉 * */ public class Players { int[] playerID; String playerName; Map<Integer,String> player ; public Players(){ playerID=new int[2]; //要赋值必须初始化数组长度 player =new HashMap<Integer,String>(); } public Players(int playerID,String playerName){ player =new HashMap<Integer,String>(); player.put(playerID, playerName); } public void playerCreation(){ System.out.println(""); System.out.println("============创建玩家============"); for(int i=0;i<2;i++){ while(true){ System.out.println("请输入第"+(i+1)+"位玩家的ID"); Scanner inputID =new Scanner(System.in); try{ playerID[i]=inputID.nextInt(); }catch(Exception e){ System.out.println("请输入数字!!!"); continue; } System.out.println("请输入第"+(i+1)+"位玩家的姓名"); Scanner inputName =new Scanner(System.in); playerName=inputName.next(); player.put(playerID[i], playerName); break; } } System.out.println(""); System.out.println("============创建玩家成功============"); System.out.println(""); System.out.println("玩家:"+" "); Set<Entry<Integer,String>> playerList =player.entrySet(); //遍历map的方法--》EntrySet for(Entry<Integer,String> playerls :playerList ){ System.out.print(playerls.getKey()+":"+playerls.getValue()); System.out.println(""); } } public void pickCards(){ } }
游戏(发牌、拿牌、比牌)
package com.imooc.season3.porkDemo; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.Map.Entry; import com.imooc.season3.CollectionDemo.Student; /* * ①发牌,打乱牌的循序,然后每个玩家拿到两张牌 * ②比牌 */ public class Game { public Map<String,Players> playersCard_v1; //两位玩家的牌--姓名加ID public Map<String,Players> playersCard_v2; int playersCardHold; Scanner playerHold; CardCreation cardRule; CardCreation cardSet ; Players players; public Game(CardCreation cardSet ,Players players){ playersCard_v1 =new HashMap<String,Players>(); playersCard_v2 =new HashMap<String,Players>(); this.cardSet=cardSet; this.players=players; } public void pickCards(){ Collections.shuffle(cardSet.cards); //打乱该变量的扑克牌的顺序,Randomly permutes the specified list using a default source of randomness. while(true){ try{ System.out.println(""); System.out.println("============请输入每个玩家的持牌数============"); playerHold=new Scanner(System.in); playersCardHold =playerHold.nextInt(); //输入每个玩家可以持有的扑克牌数目,并捕捉异常 break;} catch(Exception e){ System.out.println("============输入非数字,请重新输入============"); continue; } } for(int i=0;i<playersCardHold;i++){ //玩家持牌数为2,则给每位玩家发牌2次 //两个玩家,就是总共发两轮(每轮发给两个玩家一次) String tmpCard; //创建临时存储扑克牌、玩家姓名和玩家ID的局部变量 int tmpPlayerID; String tmpPlayerName; tmpPlayerID=players.playerID[0]; //第一个玩家拿牌 tmpPlayerName =players.player.get(tmpPlayerID); System.out.println(""); System.out.println("==玩家"+tmpPlayerName+"拿牌=="); tmpCard=cardSet.cards.get(i); cardSet.cards.remove(i); //玩家拿完牌要删除这个牌,已经不存在在可抽取的牌里 playersCard_v1.put(tmpCard, new Players(tmpPlayerID,tmpPlayerName)); tmpPlayerID=players.playerID[1]; //第二个玩家拿牌 tmpPlayerName =players.player.get(tmpPlayerID); System.out.println(""); System.out.println("==玩家"+tmpPlayerName+"拿牌=="); tmpCard=cardSet.cards.get(i); cardSet.cards.remove(i); playersCard_v2.put(tmpCard, new Players(tmpPlayerID,tmpPlayerName)); } System.out.println(""); System.out.println("==玩家抽牌完毕=="); } public void showCards(){ CardCreation cardRule =new CardCreation(); //重新new一个对象,指向一个新的位置 cardRule.cardCreation(); //牌的大小排好队,需要比对位置。 String player_v1Name=players.player.get(players.playerID[0]); //玩家v1名称 String player_v2Name=players.player.get(players.playerID[1]); //玩家v2名称 String player_v1Card = null; String player_v2Card = null; int player_v1Index = 0; int player_v2Index = 0; Set<Entry<String,Players>> player_v1Set=playersCard_v1.entrySet(); //玩家1 Set<Entry<String,Players>> player_v2Set=playersCard_v2.entrySet(); //玩家2 if(playersCardHold==1){ //如果每个玩家持牌数为1,那么就只需要比对返回的Index值 for(Entry<String,Players> player_v1:player_v1Set){ player_v1Card=player_v1.getKey(); //拿到扑克牌的值 for(String cardsForRule_v1:cardRule.cards){ if(cardsForRule_v1.equals(player_v1Card)) //这个扑克牌位于哪个位置 {player_v1Index =cardRule.cards.indexOf(cardsForRule_v1); break;} } } System.out.println(""); System.out.println("玩家"+player_v1Name+"拿到"+player_v1Card); for(Entry<String,Players> player_v2:player_v2Set){ player_v2Card=player_v2.getKey(); for(String cardsForRule_v2:cardRule.cards){ if(cardsForRule_v2.equals(player_v2Card)) {player_v2Index =cardRule.cards.indexOf(cardsForRule_v2); break;} } } System.out.println(""); System.out.println("玩家"+player_v2Name+"拿到"+player_v2Card); } //玩家持牌数大于2,则需要先对自己的牌进行比对,然后选出最大的进行比对 else{ String tmpPlayer_v1Cards; //创建临时变量,作为暂时存储 String tmpPlayer_v2Cards; int tmpPlayer_v1Index; int tmpPlayer_v2Index; for(Entry<String,Players> player_v1:player_v1Set){ tmpPlayer_v1Cards=player_v1.getKey(); //拿到扑克牌的值 for(String cardsForRule_v1:cardRule.cards){ if(cardsForRule_v1.equals(tmpPlayer_v1Cards)) //这个扑克牌位于哪个位置 {tmpPlayer_v1Index =cardRule.cards.indexOf(cardsForRule_v1); if(tmpPlayer_v1Index>player_v1Index){ player_v1Card=tmpPlayer_v1Cards; player_v1Index=tmpPlayer_v1Index; } } } } System.out.println(""); System.out.println("玩家"+player_v1Name+"拿到的最大扑克牌为"+player_v1Card); for(Entry<String,Players> player_v2:player_v2Set){ tmpPlayer_v2Cards=player_v2.getKey(); for(String cardsForRule_v2:cardRule.cards){ if(cardsForRule_v2.equals(tmpPlayer_v2Cards)) { tmpPlayer_v2Index =cardRule.cards.indexOf(cardsForRule_v2); if(tmpPlayer_v2Index>player_v2Index){ player_v2Index=tmpPlayer_v2Index; player_v2Card=tmpPlayer_v2Cards; } } } } System.out.println(""); System.out.println("玩家"+player_v2Name+"拿到的最大扑克牌为"+player_v2Card); } System.out.println(""); if(player_v1Index>player_v2Index){ System.out.println("玩家"+player_v1Name+"胜"); }else{ System.out.println("玩家"+player_v2Name+"胜"); } System.out.println(""); System.out.print("玩家"+player_v1Name+"持牌:"); for(Entry<String,Players> player_v1:player_v1Set){ System.out.print(player_v1.getKey()); } System.out.println(""); System.out.print("玩家"+player_v2Name+"持牌:"); for(Entry<String,Players> player_v2:player_v2Set){ System.out.print(player_v2.getKey()); } } } //Main方法 package com.imooc.season3.porkDemo; import java.util.Collections; import java.util.List; public class GameDemo { public static void main(String[] args) { // TODO Auto-generated method stub CardCreation cards =new CardCreation(); //初始化扑克牌 cards.cardCreation(); //创建扑克牌 cards.cardShow(); Players players =new Players(); //初始化游戏玩家 players.playerCreation(); //创建游戏玩家 Game games =new Game(cards, players); //创建游戏变量games games.pickCards(); //游戏玩家拿牌 games.showCards(); } }
String player_v1Name=players.player.get(players.playerID[0]);
这个代码我有点绕晕了,能不能帮我解释一下,谢谢!
//结果如下图:
结果如图:
Java入门第三季
409792 学习 · 4340 问题
相似问题