cloudddd_
2016-02-03 12:15
/* * 创建扑克类 */ public class Card { private String color; private String num; public Card(String color, String num) { this.color = color; this.num = num; } public Card() { } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } } /* * 创建玩家类 */ public class Player { private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } /* * */ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class CardList { public List<Card> cards; public List<Player> players; public CardList() { this.cards = new ArrayList<Card>(); this.players = new ArrayList<Player>(); } /* * 创建玩家p1、p2并加入到 players List中去 */ public void addPlayer() { System.out.println("------开始创建玩家-------"); Scanner sc = new Scanner(System.in); try { System.out.println("请输入第一位玩家ID:"); int id1 = sc.nextInt(); System.out.println("请输入第一位玩家姓名:"); String name1 = sc.next(); Player p1 = new Player(); p1.setId(id1); p1.setName(name1); players.add(p1); System.out.println("第一位玩家:" + p1.getName() + "创建完成!"); System.out.println("请输入第二位玩家ID:"); int id2 = sc.nextInt(); System.out.println("请输入第二位玩家姓名:"); String name2 = sc.next(); Player p2 = new Player(); p2.setId(id2); p2.setName(name2); players.add(p2); System.out.println("第二位玩家" + p2.getName() + "创建完成!"); } catch (Exception e) { System.err.println("请输入整数!"); addPlayer(); } } /* * 通过双for循环遍历实现两位玩家依次拿两张牌 */ public void deal() { System.out.println("-------开始发牌--------"); for (int j = 0; j < 2; j++) { for (int i = 0; i < 2; i++) { Player p = (Player) players.get(i); System.out.println("玩家:" + p.getName() + "拿牌"); } } System.out.println("-------发牌结束--------"); } /* * 创建扑克牌的cards List; * */ public void addCard() { String[] colors = { "黑桃", "红桃", "梅花", "方块" }; String[] nums = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; for (int i = 0; i < colors.length; i++) { for (int j = 0; j < nums.length; j++) { cards.add(new Card(colors[i], nums[j])); } } } /* * for循环遍历cards获得其中的每一个元素 */ public void forGet() { System.out.println("------扑克牌创建成功------"); for (int i = 0; i < cards.size(); i++) { Card c = (Card) cards.get(i); System.out.print(c.getColor() + c.getNum() + " "); } System.out.println(""); } /* * 通过Collections中的shuffle方法打乱cards中的元素顺序实现洗牌 */ public void shuffle() { System.out.println("-------开始洗牌--------"); Collections.shuffle(cards); System.out.println("-------洗牌结束--------"); } /* * 首先获取打乱后的牌库中的前四张牌,玩家1获得第一和第三张牌,玩家2获得第二和第四张牌; * 创建花色数组和牌的点数数组; * 通过获得玩家手牌中的花色和点数分别在花色数组和点数数组中的索引位置来判断牌与牌之间的大小 */ public void get() { Card temp1 = new Card(); Card temp2 = new Card(); Player p1 = (Player) players.get(0); Player p2 = (Player) players.get(1); int k1 = 0, k2 = 0, k3 = 0, k4 = 0; int l1 = 0, l2 = 0, l3 = 0, l4 = 0; Card card1 = (Card) cards.get(0); Card card2 = (Card) cards.get(1); Card card3 = (Card) cards.get(2); Card card4 = (Card) cards.get(3); String[] colors = { "黑桃", "红桃", "梅花", "方块" }; String[] nums = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; for (int i1 = 0; i1 < colors.length; i1++) { if (card1.getColor().equals(colors[i1])) { k1 = i1; break; } } for (int j1 = 0; j1 < nums.length; j1++) { if (card1.getNum().equals(nums[j1])) { l1 = j1; break; } } for (int i2 = 0; i2 < colors.length; i2++) { if (card2.getColor().equals(colors[i2])) { k2 = i2; break; } } for (int j2 = 0; j2 < nums.length; j2++) { if (card2.getNum().equals(nums[j2])) { l2 = j2; break; } } for (int i3 = 0; i3 < colors.length; i3++) { if (card3.getColor().equals(colors[i3])) { k3 = i3; break; } } for (int j3 = 0; j3 < nums.length; j3++) { if (card3.getNum().equals(nums[j3])) { l3 = j3; break; } } for (int i4 = 0; i4 < colors.length; i4++) { if (card4.getColor().equals(colors[i4])) { k4 = i4; break; } } for (int j4 = 0; j4 < nums.length; j4++) { if (card4.getNum().equals(nums[j4])) { l4 = j4; break; } } if (k1 == k3) { if (l1 > l3) { System.out.println("玩家:" + p1.getName() + "最大手牌为" + card1.getColor() + card1.getNum() ); temp1 = card1; } else if(l1<l3) { System.out.println("玩家:" + p1.getName() + "最大手牌为" + card3.getColor() + card3.getNum()); temp1 = card3; }else if(l1 == l3){ System.out.println("玩家:" + p1.getName() + "最大手牌为" + card1.getColor() + card1.getNum() ); temp1 = card1; } } else if (k1 > k3) { System.out.println("玩家:" + p1.getName() + "最大手牌为" + card3.getColor() + card3.getNum()); temp1 = card3; } else if(k1 < k3){ System.out.println("玩家:" + p1.getName() + "最大手牌为" + card1.getColor() + card1.getNum()); temp1 = card1; } if (k2 == k4) { if (l2 > l4) { System.out.println("玩家:" + p2.getName() + "最大手牌为" + card2.getColor() + card2.getNum()); temp2 = card2; } else if(l2< l4) { System.out.println("玩家:" + p2.getName() + "最大手牌为" + card4.getColor() + card4.getNum()); temp2 = card4; }else if(l2 == l4){ System.out.println("玩家:" + p2.getName() + "最大手牌为" + card2.getColor() + card2.getNum()); temp2 = card2; } } else if (k2 > k4) { System.out.println("玩家:" + p2.getName() + "最大手牌为" + card4.getColor() + card4.getNum()); temp2 = card4; } else if(k2<k4){ System.out.println("玩家:" + p2.getName() + "最大手牌为" + card2.getColor() + card2.getNum()); temp2 = card2; } /* * 花色m 點數n; */ int m1 = 0, n1 = 0; int m2 = 0, n2 = 0; for (int i1 = 0; i1 < colors.length; i1++) { if (temp1.getColor().equals(colors[i1])) { m1 = i1; break; } } for (int j1 = 0; j1 < nums.length; j1++) { if (temp1.getNum().equals(nums[j1])) { n1 = j1; break; } } for (int i2 = 0; i2 < colors.length; i2++) { if (temp2.getColor().equals(colors[i2])) { m2 = i2; break; } } for (int j2 = 0; j2 < nums.length; j2++) { if (temp2.getNum().equals(nums[j2])) { n2 = j2; break; } } if (m1 == m2) { if (n1 > n2) { System.out.println(" ------玩家" + p1.getName() + "获胜!------" ); } else if(n1 < n2) { System.out.println(" ------玩家" + p2.getName() + "获胜!------" ); } else if(n1 == n2){ System.out.println(" 太巧了两位玩家最大牌一样大!" +n1+" "+n2); } } else if (m1 < m2) { System.out.println("------玩家" + p1.getName() + "获胜!------" ); } else if (m1 > m2) { System.out.println("------玩家" + p2.getName() + "获胜!------" ); } System.out.println("两位玩家各自手牌为:"); System.out.println(p1.getName()+":["+card1.getColor()+card1.getNum()+"," +card3.getColor()+card3.getNum()+"]"); System.out.println(p2.getName()+":["+card2.getColor()+card2.getNum()+"," +card4.getColor()+card4.getNum()+"]"); } } /* * 游戏测试类 */ public class GameTest { public static void main(String[] args) { CardList cl = new CardList(); Player p = new Player(); cl.addCard(); cl.forGet(); cl.shuffle(); cl.addPlayer(); cl.deal(); cl.get(); } }
贴一段我的吧,看了下大家写的,其实思路都一样的,用comparator重写compare去实现比较方式。
import java.util.Comparator; public class CardComparator implements Comparator<Card> { public CardComparator() { // TODO Auto-generated constructor stub } @Override public int compare(Card o1, Card o2) { // TODO Auto-generated method stub String[] color = { "黑桃", "红桃", "梅花", "方片" }; String[] value = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "2", "A" }; int colorIndexByo1 = 0; int colorIndexByo2 = 0; int valueIndexByo1 = 0; int valueIndexByo2 = 0; for (int i = 0; i < color.length; i++) { if (color[i].equals(o1.getColor())) { colorIndexByo1 = i; } if (color[i].equals(o2.getColor())) { colorIndexByo2 = i; } } for (int j = 0; j < value.length; j++) { if (value[j].equals(o1.getValue())) { valueIndexByo1 = j; } if (value[j].equals(o2.getValue())) { valueIndexByo2 = j; } } if (valueIndexByo1 != valueIndexByo2) { //System.out.println(value[valueIndexByo1]+"与"+value[valueIndexByo2]+"比较 "+valueIndexByo1+" "+valueIndexByo2); return valueIndexByo1 - valueIndexByo2; } else { //System.out.println(color[colorIndexByo1]+"与"+color[colorIndexByo2]+"比较 "+colorIndexByo1+" "+colorIndexByo2); return colorIndexByo2-colorIndexByo1; } } }
有错。。。不是先比较花色在比较数字,而是比较数字再比较花色
public class Card implements Comparable<Card>{ public int compareTo(Card o){ String color[] = {"黑桃","红桃","梅花","方片"}; String num[] = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; int this_num = 0,o_num = 0,this_color = 0,o_color = 0; for(int index = 0;index < num.length;index ++){ if(num[index].equals(this.num)) this_num = index; if(num[index].equals(o.num)) o_num = index; } for(int index = 0;index < color.length;index ++){ if(color[index].equals(this.color)) this_color = index; if(color[index].equals(o.color)) o_color = index; } if(this_num > o_num){ return 1; }else if(this_num < o_num){ return -1; }else{ if(this_color < o_color){ return 1; }else if(this_color > o_color){ return -1; }else{ return 0; } } } }
import java.util.Comparator; public class PokerComparable implements Comparator<Poker> { @Override public int compare(Poker o1, Poker o2) { // TODO Auto-generated method stub String temp1 = "Spade"; String temp2 = "Heart"; String temp3 = "Club"; String temp4 = "Diamond"; PokerComparable pc = new PokerComparable(); if (o1.getColorPoker().equals(o2.getColorPoker())){ pc.comparePoint(o1, o2); }else if((o1.getColorPoker().equals(temp1))&&(o2.getColorPoker()!=temp1)){ return 1; }else if((o1.getColorPoker().equals(temp2))&&(o2.getColorPoker()==temp3||o2.getColorPoker()==temp4)){ return 1; }else if((o1.getColorPoker().equals(temp3))&&(o2.getColorPoker()==temp4)){ return 1; }else{ // ((o1.getColorPoker().equals(temp4))&&(o2.getColorPoker()!=temp4)){ return -1; } return 0; } public int comparePoint(Poker o1, Poker o2){ String[] str = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; int i,j; String c1 = o1.getPointPoker(); String c2 = o2.getPointPoker(); for(i=0;i<str.length;i++){ if(c1==str[i]){ for(j=0;j<str.length;j++){ if (c2==str[j]){ if(i<j){ return -1; }else if(i==j){ return 1; }else{ return 1; } } } }else{ continue; } } return 0; } }
比较方法好复杂啊!建议尝试使用comparable接口或者comparator接口实现比较方法
我的比较方法感觉很繁杂,但是不知道怎样可以简化
Java入门第三季
409792 学习 · 4340 问题
相似问题