做完了作业,但是感觉异常处理和MAP类还是不熟,程序里没怎么用到。
Game.java主程序:
package SimplePoker; import java.util.InputMismatchException; import java.util.Scanner; public class Game { static int ID1,ID2; static String name1,name2; public static void main(String[] args) { Game game = new Game(); Pokercards poker = new Pokercards(); game.createplayer(); Player player1 = new Player(ID1,name1); Player player2 = new Player(ID2,name2); poker.disorder(); game.deal(poker, player1, player2); System.out.println("----------开始游戏...----------"); int flag = player1.compareTo(player2); if(flag==1)System.out.println("----------玩家:"+player1.name+"获胜!----------"); else System.out.println("----------玩家:"+player2.name+"获胜!----------"); System.out.println("玩家各自的手牌为:"); System.out.println(player1.name+":["+player1.cards[0]+","+player1.cards[1]+"]"); System.out.println(player2.name+":["+player2.cards[0]+","+player2.cards[1]+"]"); } //发牌 public void deal(Pokercards poker,Player player1,Player player2) { System.out.println("----------开始发牌...----------"); player1.cards[0] = poker.cards.get(0); System.out.println("----玩家:"+player1.name+"-拿牌"); player2.cards[0] = poker.cards.get(1); System.out.println("----玩家:"+player2.name+"-拿牌"); player1.cards[1] = poker.cards.get(2); System.out.println("----玩家:"+player1.name+"-拿牌"); player2.cards[1] = poker.cards.get(3); System.out.println("----玩家:"+player2.name+"-拿牌"); System.out.println("----------发牌结束!----------"); } public void createplayer() { try{Scanner in = new Scanner(System.in); System.out.println("----------创建玩家...----------"); System.out.println("请输入第1位玩家的ID和姓名:"); System.out.println("输入ID:"); ID1 = in.nextInt(); System.out.println("输入姓名:"); name1 = in.next(); System.out.println("请输入第2位玩家的ID和姓名:"); System.out.println("输入ID:"); ID2 = in.nextInt(); System.out.println("输入姓名:"); name2 = in.next(); }catch(InputMismatchException e){ System.out.println("请输入数字!"); createplayer(); }catch(Exception e) { System.out.println("发现未知错误,请重新创建玩家!"); createplayer(); } } }
Pokercards.java:
package SimplePoker; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; public class Pokercards { public List<String> cards = new ArrayList(); public Pokercards() { System.out.println("----------创建扑克牌----------"); String[] str1 = new String[]{"黑桃","红桃","梅花","方片"}; String[] str2 = new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; for(int i=0;i<str1.length;i++) { for(int j=0;j<str2.length;j++) { cards.add(str1[i]+str2[j]); } } System.out.println("----------扑克牌创建成功----------"); this.show(); } //打乱牌序 public void disorder() { System.out.println("----------开始洗牌...----------"); Random random = new Random(); int k = random.nextInt(100); for(int i=0;i<k;i++) { int t1 = random.nextInt(52); int t2 = random.nextInt(52); if(t1 == t2)continue; String temp = cards.get(t1); cards.set(t1,cards.get(t2)); cards.set(t2,temp); } System.out.println("----------洗牌结束!----------"); } public void show() { Iterator i = cards.iterator(); System.out.print("为:["+i.next()); while(i.hasNext()) { System.out.print(","+i.next()); } System.out.println("]"); } }
Player.java:
package SimplePoker; public class Player implements Comparable<Player> { public int ID; public String name; public String[] cards = new String[2]; public Player(int id, String name) { this.ID=id; this.name=name; System.out.println("----欢迎玩家:"+this.name); } @Override public int compareTo(Player o) { String str1 = this.cards[0]; String str2 = this.cards[1]; String str3 = o.cards[0]; String str4 = o.cards[1]; System.out.println("玩家:"+this.name+"最大的手牌为:"+this.compare(str1, str2)); System.out.println("玩家:"+o.name+"最大的手牌为:"+this.compare(str3, str4)); String result = this.compare(this.compare(str1, str2),this.compare(str3, str4)); if(result.contentEquals(str1)||result.contentEquals(str2))return 1; else return -1; } public String compare(String str1,String str2) { String data1 = new String("2345678910JQKA"); String data2 = new String("黑桃红桃梅花方片"); String str1_1 = str1.substring(2); String str2_1 = str2.substring(2); String str1_2 = str1.substring(0,2); String str2_2 = str2.substring(0,2); if(!str1_1.contentEquals(str2_1)) { if(data1.indexOf(str1_1)>data1.indexOf(str2_1))return str1; else return str2; }else { if(data2.indexOf(str1_2)<data2.indexOf(str2_2))return str1; else return str2; } } }