森木
2015-07-21 17:33
package pokerGame; import java.util.ArrayList; import java.util.List; public class Player { //id只能是整数 private int id; private String name; private List<Poker> cards; public Player(){ this.id=1; this.name="Not named"; this.cards=new ArrayList<Poker>(); } public Player(int id,String name){ this.id=id; this.name=name; this.cards=new ArrayList<Poker>(); } //给玩家添加手牌 public void addCard(Poker poker){ this.cards.add(poker); } //返回玩家最大的手牌 public Poker getMaxCard(){ //得到两张手牌 并比较大小 返回最大的那张 Poker poker1=this.cards.get(0); Poker poker2=this.cards.get(1); if(poker1.compareTo(poker2)==1){ return poker1; } else{ return poker2; } } public void outputCards(){ if(!this.cards.isEmpty()){ Poker poker1=this.cards.get(0); Poker poker2=this.cards.get(1); System.out.println(this.name+":["+poker1.getColor()+poker1.getValue() +","+poker2.getColor()+poker2.getValue()+"]"); } } public int getId() { return id; } public String getName() { return name; } public List<Poker> getCards(){ return this.cards; } public void setCards(List<Poker> cards) { this.cards = cards; } }
package pokerGame; import java.util.List; public class Poker implements Comparable<Poker>{ private String color; private String value; public Poker(String color,String value){ this.color=color; this.value=value; } //实现可比较接口 如果两者相等 返回0 如果本牌大于参数 返回1 否则返回-1 @Override public int compareTo(Poker o) { // TODO Auto-generated method stub //得到花色数组和点数数组 List<String> colors=globalValues.colors; List<String> values=globalValues.values; //把用字符串表示的点数转换为整型变量 注意A,J,Q,K要转换成数字 String[] tempValue={this.value,o.getValue()}; int[] value=new int[2]; for(int i=0;i<2;i++){ switch(tempValue[i]){ case "A": value[i]=1; break; case "J": value[i]=11; break; case "Q": value[i]=12; break; case "K": value[i]=13; break; default: value[i]=Integer.valueOf(tempValue[i]); } } int value1=value[0]; int value2=value[1]; if(value1>value2){ return 1; } else if(value1<value2) { return -1; } //两张牌的点数相等 则比较花色 else{ String color1=this.color; String color2=o.getColor(); int index1=colors.indexOf(color1); int index2=colors.indexOf(color2); if(index1>index2){ return 1; } else if(index1<index2){ return -1; } else{ return 0; } } } public String getColor() { return color; } public String getValue() { return value; } }
package pokerGame; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class Cards { //所有的52张牌 声明为List是为了使用Collections.shuffle(List<?> list) private List<Poker> cards; public Cards(){ System.out.println("------------创建扑克牌------------"); cards=new ArrayList<Poker>(); createCards(); } public void createCards(){ List<String> colors=globalValues.colors; List<String> values=globalValues.values; //添加52张牌 for(int i=0;i<4;i++){ for(int j=0;j<13;j++){ Poker poker=new Poker(colors.get(i),values.get(j)); cards.add(poker); } } System.out.println("------------扑克牌创建成功!------------"); } public void outputCards(){ if(!cards.isEmpty()){ Iterator<Poker> iterator=this.cards.iterator(); System.out.print("为:["); while(iterator.hasNext()){ Poker poker=iterator.next(); System.out.print(poker.getColor()+poker.getValue()+","); } System.out.println("]"); } } //洗牌 public void shuffleCards(){ System.out.println("------------开始洗牌---------"); Collections.shuffle(this.cards); System.out.println("------------洗牌结束---------"); } //给玩家发牌 public void sendCards(List<Player> players){ System.out.println("--------开始发牌-------"); //每个玩家拿两张牌 for(int j=0,index=0;j<2;j++){ for(int i=0;i<players.size();i++,index++){ System.out.println("----玩家:"+players.get(i).getName()+"--拿牌"); //交替顺序得到前4张牌 Poker poker=this.cards.get(index); players.get(i).addCard(poker); } } System.out.println("------发牌结束!---"); } }
package pokerGame; import java.util.Arrays; import java.util.HashSet; import java.util.List; public class globalValues { public static List<String> colors= Arrays.asList(new String[]{"方块","梅花","红桃","黑桃"}); public static List<String> values= Arrays.asList(new String[] {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}); }
package pokerGame; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; public class PokerGame { private Scanner console; private Cards cards; private List<Player> players; public PokerGame(){ console=new Scanner(System.in); } public void gameStart(){ createCards(); createPlayers(); dispatchCards(); startCompare(); } //创建扑克牌并进行打印输出并进行洗牌 private void createCards(){ cards=new Cards(); cards.outputCards(); cards.shuffleCards(); } private void createPlayers(){ players=new ArrayList<Player>(); //创建两个玩家 for(int i=0;i<2;i++){ try{ System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:"); System.out.println("输入ID:"); int id=console.nextInt(); System.out.println("输入姓名:"); String name=console.next(); Player player1=new Player(id,name); players.add(player1); } catch(InputMismatchException e){ System.out.print("请输入整数类型的ID!"); i--; console.next(); } } System.out.println("—————欢迎玩家:"+players.get(0).getName()); System.out.println("—————欢迎玩家:"+players.get(1).getName()); } //给玩家发牌 private void dispatchCards(){ cards.sendCards(players); } //开始游戏 private void startCompare(){ Poker[] pokers=new Poker[2]; System.out.println("-----开始游戏----"); for(int i=0;i<2;i++){ Player player=players.get(i); Poker poker=player.getMaxCard(); System.out.println("玩家"+player.getName()+"最大的手牌为:" +poker.getColor()+poker.getValue()); pokers[i]=poker; } //输出输赢结果 if(pokers[0].compareTo(pokers[1])==1){ System.out.println("----玩家"+players.get(0).getName()+"获胜----"); } else { System.out.println("玩家"+players.get(1).getName()+"获胜"); } //输出玩家的手牌 System.out.println("玩家各自的手牌为:"); players.get(0).outputCards(); players.get(1).outputCards(); } }
package pokerGame; public class testMain { public static void main(String[] args) { // TODO Auto-generated method stub PokerGame pokerGame=new PokerGame(); pokerGame.gameStart(); } }
截图 ~
Java入门第三季
409792 学习 · 4340 问题
相似问题