实现:生成五十四张牌,然后进行洗牌,输入两名玩家,然后分别发两张牌,进行比较大小,两张牌中拥有较大一张牌的玩家获胜
Card.java代码如下:
package com.cardgame; public class Card { String suit= new String(); String num = new String(); public Card(String suit ,String num){ this.suit=suit; this.num=num; } }
Player.java代码如下:
package com.cardgame; import java.awt.List; import java.util.ArrayList; public class Player { String name= new String(); String id = new String(); java.util.List<Card> cards= new ArrayList<Card>(); public Player(String name,String id){ this.id=id; this.name=name; } }
MainClass.java代码如下:
package com.cardgame; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Scanner; public class MainClass { public List<Card> cards= new ArrayList<Card>(); public List<String> suitList= new ArrayList<String>(); public List<Card> upsetCards= new ArrayList<Card>(); /* * * 添加54张扑克牌 * */ public void add54Cards() { for (int i = 0; i < suitList.size(); i++) { String suit=suitList.get(i); String num; for (int j = 0; j < 13; j++) { if (j==0) { num="A"; } else if (j==10) { num="J"; } else if (j==11) { num="Q"; } else if (j==12) { num="K"; }else { Integer jInteger=(Integer)j+1; num=jInteger.toString(); } cards.add(new Card(suit, num)); } } cards.add(new Card(null, "小王")); cards.add(new Card(null, "大王")); System.out.println("*洗牌前*"); for (int i = 0; i < cards.size(); i++) { Card card= cards.get(i); System.out.print(card.suit+card.num+","); } System.out.println(); } /* * * 打乱顺序 * */ public void breakOrder() { int total = cards.size(); Random random = new Random(); int index; for (int i = 0; i < total; i++) { index= random.nextInt(cards.size()); upsetCards.add(cards.get(index)); cards.remove(cards.get(index)); } System.out.println("*开始洗牌*"); for (int i = 0; i < upsetCards.size(); i++) { Card card= upsetCards.get(i); System.out.print(card.suit+card.num+","); } System.out.println(); System.out.println("*洗牌完成*"); } /* * * 创建玩家,并发牌 * */ public void createPlayer(){ Scanner scanner = new Scanner(System.in); System.out.println("请输入一号玩家ID:"); String id1= scanner.next(); System.out.println("请输入一号玩家昵称:"); String name1= scanner.next(); Player first = new Player(name1, id1); System.out.println("请输入二号玩家ID:"); String id2= scanner.next(); System.out.println("请输入二号玩家昵称:"); String name2= scanner.next(); Player second = new Player(name2, id2); //发牌 for (int i = 0; i <2; i++) { Random random = new Random(); // int index1=random.nextInt(upsetCards.size()); first.cards.add(upsetCards.get(index1)); upsetCards.remove(index1); // int index2=random.nextInt(upsetCards.size()); second.cards.add(upsetCards.get(index2)); upsetCards.remove(index2); } System.out.println("*发牌成功*"); System.out.println("玩家:"+first.name+" 获得的牌为:"); for (Card card : first.cards) { System.out.print(card.suit+card.num+" "); } System.out.println(); System.out.println("玩家:"+second.name+"获得的牌为:"); for (Card card : second.cards) { System.out.print(card.suit+card.num+" "); } System.out.println(); int card11=this.getNum(first.cards.get(0)); int card12=this.getNum(first.cards.get(1)); int card21=this.getNum(second.cards.get(0)); int card22=this.getNum(second.cards.get(1)); if ((card11>card12?card11:card12)>(card21>card22?card21:card22)) { System.out.println("一号玩家获胜"); }else if ((card11>card12?card11:card12)<(card21>card22?card21:card22)) { System.out.println("二号玩家获胜"); }else { System.out.println("平局!"); } } /* * * 获得牌权值: * */ public int getNum(Card card) { int num; if (card.num=="A") { num=14; }else if (card.num=="J") { num=11; }else if (card.num=="Q") { num=12; }else if(card.num=="K") { num=13; }else if (card.num=="大王") { num=1000; }else if (card.num=="小王") { num=888; }else { num=Integer.valueOf(card.num); } /* * * 获取花色权值 * */ int suitC=0; if (card.suit=="黑桃") { suitC=4; }else if (card.suit=="红桃") { suitC=3; }else if (card.suit=="梅花") { suitC=2; }else if (card.suit=="方块") { suitC=1; } int result = num*10+suitC; return result; } public static void main(String[] args) { // TODO Auto-generated method stub MainClass mClass=new MainClass(); mClass.suitList.add("黑桃"); mClass.suitList.add("红桃"); mClass.suitList.add("梅花"); mClass.suitList.add("方块"); mClass.add54Cards(); mClass.breakOrder(); mClass.createPlayer(); } }
运行结果
时间: 2024-10-22 05:41:18