一:

Player

  1. package com.player;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6. public class Player {
  7. private int id;
  8. private String name;
  9. ArrayList<String> cardInfo = new ArrayList<String>();
  10. public Player(int id, String name){
  11. this.id = id;
  12. this.name = name;
  13. }
  14. public ArrayList<String> getCardInfo() {
  15. return cardInfo;
  16. }
  17. public void setCardInfo(String cardInfo) {
  18. this.cardInfo.add(cardInfo);
  19. }
  20. public String maxCard(){
  21. Map<String, Integer> cardCompare = new HashMap<String, Integer>();
  22. cardCompare.put("梅花", 4);
  23. cardCompare.put("黑桃", 3);
  24. cardCompare.put("红心", 2);
  25. cardCompare.put("方片", 1);
  26. ArrayList<String> cards = getCardInfo();
  27. String maxCard = null;
  28. int a ;
  29. int b ;
  30. a = transfer(cards.get(0));
  31. b = transfer(cards.get(1));
  32. if(a > b)
  33. maxCard = cards.get(0);
  34. else if(a < b)
  35. maxCard = cards.get(1);
  36. else{
  37. String card1 = cards.get(0).substring(0,2);
  38. String card2 = cards.get(1).substring(0,2);
  39. if(cardCompare.get(card1) > cardCompare.get(card2))
  40. maxCard = cards.get(0);
  41. else if(cardCompare.get(card1) < cardCompare.get(card2))
  42. maxCard = cards.get(1);
  43. else
  44. maxCard = cards.get(0);
  45. }
  46. return maxCard;
  47. }
  48. public int transfer(String str){
  49. int a;
  50. if(str.substring(2).equals("J"))
  51. a = 11;
  52. else if(str.substring(2).equals("Q"))
  53. a = 12;
  54. else if(str.substring(2).equals("K"))
  55. a = 13;
  56. else if(str.substring(2).equals("A"))
  57. a = 1;
  58. else
  59. a = Integer.parseInt(str.substring(2));
  60. return a;
  61. }
  62. public String getName() {
  63. return name;
  64. }
  65. }

Game

  1. package com.game;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8. import com.card.Card;
  9. import com.player.Player;
  10. public class Game {
  11. public static void main(String[] args) {
  12. // Create new Card class
  13. Card card = new Card();
  14. Player[] player = new Player[2];
  15. Scanner input = new Scanner(System.in);
  16. System.out.println("**********开始创建扑克牌**********");
  17. System.out.println("**********扑克牌创建成功**********");
  18. System.out.println(Arrays.toString(card.getNewCards()));
  19. System.out.println("**********开始洗牌**********");
  20. String[] cardsRandom = card.RandomCard();
  21. System.out.println("**********洗牌结束**********");
  22. System.out.println("**********创建玩家**********");
  23. //Create players
  24. for(int i = 0; i < player.length;i ++){
  25. System.out.println("请输入第" + (i+1) + "玩家的ID和姓名");
  26. System.out.println("请输入ID:");
  27. int id = input.nextInt();
  28. System.out.println("请输入姓名:");
  29. String name = input.next();
  30. Player player1 = new Player(id,name);
  31. player[i] = player1;
  32. }
  33. //Show players' name
  34. for(int i = 0;i < player.length;i ++)
  35. System.out.println("*****欢迎玩家:" + player[i].getName());
  36. System.out.println("**********开始发牌**********");
  37. Random random = new Random();
  38. int index;
  39. for(int i = 0;i < 4;i ++){
  40. System.out.println("*****玩家: " + player[i % 2].getName() + " 拿牌");
  41. index = random.nextInt(cardsRandom.length);
  42. player[i % 2].setCardInfo(cardsRandom[index]);
  43. }
  44. System.out.println("**********发牌结束**********");
  45. System.out.println("**********开始游戏**********");
  46. String[] cardsComp = new String[2];
  47. for(int i = 0;i < player.length;i ++){
  48. cardsComp[i] = player[i].maxCard();
  49. System.out.println("玩家:" + player[i].getName() + "最大的手牌为: " + player[i].maxCard());
  50. }
  51. int result = maxCard(cardsComp);
  52. if(result == 1)
  53. System.out.println("********玩家 " + player[0].getName() + "获胜");
  54. else if(result == -1)
  55. System.out.println("********玩家 " + player[1].getName() + "获胜");
  56. else
  57. System.out.println("********玩家 " + player[1].getName() + "和玩家 " + "手牌一样大,没有输赢");
  58. System.out.println("**********玩家的手牌为:");
  59. for(int i = 0;i < player.length;i ++)
  60. System.out.println(player[i].getName() + ":[ " + player[i].getCardInfo().get(0) + " , "+ player[i].getCardInfo().get(1) + " ]");
  61. }
  62. public static int maxCard(String[] cards){
  63. Map<String, Integer> cardCompare = new HashMap<String, Integer>();
  64. cardCompare.put("梅花", 4);
  65. cardCompare.put("黑桃", 3);
  66. cardCompare.put("红心", 2);
  67. cardCompare.put("方片", 1);
  68. int winer;
  69. int a = 0;
  70. int b = 0;
  71. a = transfer(cards[0]);
  72. b = transfer(cards[1]);
  73. if(a > b)
  74. winer = 1;
  75. else if(a < b)
  76. winer = -1;
  77. else{
  78. String card1 = cards[0].substring(0,2);
  79. String card2 = cards[1].substring(0,2);
  80. if(cardCompare.get(card1) > cardCompare.get(card2))
  81. winer = 1;
  82. else if(cardCompare.get(card1) < cardCompare.get(card2))
  83. winer = -1;
  84. else
  85. winer = 0;
  86. }
  87. return winer;
  88. }
  89. public static int transfer(String str){
  90. int a = 0;
  91. if(str.substring(2).equals("J"))
  92. a = 11;
  93. else if(str.substring(2).equals("Q"))
  94. a = 12;
  95. else if(str.substring(2).equals("K"))
  96. a = 13;
  97. else if(str.substring(2).equals("A"))
  98. a = 1;
  99. else
  100. a = Integer.parseInt(str.substring(2));
  101. return a;
  102. }
  103. }

Card

  1. package com.card;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Random;
  5. public class Card {
  6. private String number;
  7. private String color;
  8. /**
  9. * Create a pair of new cards
  10. * @return
  11. */
  12. public ArrayList<Card> createCard(){
  13. String[] carNumber = new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
  14. String[] carColor = new String[]{"方块","红心","梅花","黑桃"};
  15. ArrayList<Card> newCards = new ArrayList<Card>();
  16. //Create a pair of new cards
  17. for(int i = 0;i < carColor.length;i ++){
  18. for(int j = 0;j < carNumber.length;j ++){
  19. // card.id = i;
  20. Card card = new Card();
  21. card.color = carColor[i];
  22. card.number = carNumber[j];
  23. newCards.add(card);
  24. }
  25. }
  26. return newCards;
  27. }
  28. /**
  29. * show the new cards
  30. * @return
  31. */
  32. public String[] getNewCards(){
  33. String[] newCardGroup = new String[createCard().size()];
  34. for(int i = 0;i < createCard().size();i ++){
  35. newCardGroup[i] = createCard().get(i).color +createCard().get(i).number ;
  36. }
  37. return newCardGroup;
  38. }
  39. /**
  40. * Create random cards
  41. * @return
  42. */
  43. public String[] RandomCard(){
  44. Random random = new Random();
  45. String[] cardsArray = getNewCards();
  46. String swapCard = null;
  47. for(int i = 0;i < cardsArray.length;i ++){
  48. int index = random.nextInt(cardsArray.length);
  49. swapCard = cardsArray[i];
  50. cardsArray[i] = cardsArray[index];
  51. cardsArray[index] = swapCard;
  52. }
  53. return cardsArray;
  54. }
  55. }

二:

Poker.zip

StartGame

  1. import java.util.Scanner;
  2. public class StatGame {
  3. public static void main(String[] args) {
  4. //1.创建扑克牌
  5. //2.洗牌
  6. //3.接收两个玩家
  7. //4.给两个玩家发牌
  8. //5.输出结果
  9. //6.打印两个玩家手中的牌
  10. //7.游戏结束
  11. System.out.println("创建扑克牌");
  12. PokerGame pokerGame = new PokerGame();
  13. //遍历当前扑克牌
  14. pokerGame.testForEach();
  15. System.out.println("洗牌");
  16. pokerGame.shuffleCards();
  17. System.out.println("接收两个玩家");
  18. Player player[] = {new Player(),new Player()};
  19. //获取两个玩家的信息
  20. for(int i=0;i<2;i++){
  21. System.out.println("请输入玩家ID:");
  22. try{
  23. Scanner scanner = new Scanner(System.in);
  24. int n = scanner.nextInt();
  25. player[i].setId(n);
  26. }catch (Exception e) {
  27. System.out.println("玩家ID不支持字符串");
  28. i--;
  29. continue;
  30. }
  31. System.out.println("请输入玩家名称");
  32. try {
  33. Scanner scanner = new Scanner(System.in);
  34. String name = scanner.next();
  35. player[i].setName(name);
  36. }catch (Exception e){
  37. e.printStackTrace();
  38. System.out.println("玩家Name不支持数字!");
  39. i--;
  40. continue;
  41. }
  42. }
  43. System.out.println("给两个玩家发牌");
  44. for (int i=0;i<2;i++){
  45. player[i].push(pokerGame.deadCard());
  46. player[i].push(pokerGame.deadCard());
  47. player[i].push(pokerGame.deadCard());
  48. }
  49. System.out.println("输出结果");
  50. Player winner = pokerGame.runGame(player[0],player[1]);
  51. System.out.println(winner.getName()+"胜出"+"获得一个macbook pro!");
  52. System.out.println("打印两个玩家手中的牌");
  53. player[0].Traversal();
  54. player[1].Traversal();
  55. System.out.println("游戏结束");
  56. }
  57. }

Poker

  1. public class Poker implements Comparable<Object>{
  2. public static final String[] pokers={
  3. "黑桃2","红桃2","梅花2","方块2",
  4. "黑桃3","红桃3","梅花3","方块3",
  5. "黑桃4","红桃4","梅花4","方块4",
  6. "黑桃5","红桃5","梅花5","方块5",
  7. "黑桃6","红桃6","梅花6","方块6",
  8. "黑桃7","红桃7","梅花7","方块7",
  9. "黑桃8","红桃8","梅花8","方块8",
  10. "黑桃9","红桃9","梅花9","方块9",
  11. "黑桃10","红桃10","梅花10","方块10",
  12. "黑桃J","红桃J","梅花J","方块J",
  13. "黑桃Q","红桃Q","梅花Q","方块Q",
  14. "黑桃K","红桃K","梅花K","方块K",
  15. "黑桃A","红桃A","梅花A","方块A",
  16. "大王","小王"
  17. };
  18. public String getPokerDesign(){
  19. return pokerDesign;
  20. }
  21. public void setPokerDesign(String pokerDesign){this.pokerDesign=pokerDesign;}
  22. public String getPokerNum(){
  23. return pokerNum;
  24. }
  25. public void setPokerNum(String pokerNum){
  26. this.pokerNum=pokerNum;
  27. }
  28. private String pokerDesign;//扑克牌花色
  29. private String pokerNum;//扑克牌数字
  30. private int iIndex;
  31. public int getiIndex(){
  32. return iIndex;
  33. }
  34. public void setiIndex(int iIndex){ this.iIndex = iIndex;}
  35. public Poker(String pokerDesign,String pokerNum){
  36. super();
  37. this.pokerDesign = pokerDesign;
  38. this.pokerNum = pokerNum;
  39. String pokerString = this.pokerDesign+this.pokerNum;
  40. for(int i=0;i< pokers.length;i++){
  41. if(pokers[i].equals(pokerString)){
  42. iIndex=i;
  43. }
  44. }
  45. }
  46. public int compareTo(Object obj){
  47. //判断是否等于自己
  48. if(this==obj){
  49. return 0;
  50. }
  51. //判断是否能转换成Poker,不能返回
  52. if(obj instanceof Poker){
  53. Poker poker = (Poker) obj;
  54. if(this.iIndex > poker.iIndex){
  55. return 1;
  56. }else if(this.iIndex < poker.iIndex){
  57. return -1;
  58. }else if(this.iIndex == poker.iIndex){
  59. return 0;
  60. }
  61. }
  62. return -1;
  63. }
  64. }

PokerGame

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. public class Player {
  5. private int id;
  6. public int getId(){
  7. return id;
  8. }
  9. public void setId(int id){this.id=id; }
  10. private String name;
  11. public String getName() {return name;}
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. private List<Poker> pokerList;
  16. public void setPokerList(List<Poker>pokerList){this.pokerList=pokerList;}
  17. public Player() {
  18. this.id = -1;
  19. this.name = "";
  20. this.pokerList = new ArrayList<Poker>();
  21. }
  22. public Player(int id, String name) {
  23. this.id = id;
  24. this.name = name;
  25. this.pokerList= new ArrayList<Poker>();
  26. }
  27. public void push(Poker poker){
  28. this.pokerList.add(poker);
  29. }
  30. //出最大牌
  31. public Poker popMax(){
  32. //出牌,默认选择最大的开始
  33. //排序
  34. Collections.sort(pokerList,new SortByIndex());
  35. Poker max = pokerList.get(pokerList.size()-1);
  36. return max;
  37. }
  38. public void Traversal(){
  39. System.out.println("玩家"+name+"接到的扑克牌:");
  40. for(Poker poker:pokerList){
  41. System.out.println(""+poker.getPokerDesign()+poker.getPokerNum());
  42. }
  43. System.out.println();
  44. }
  45. }

Player

  1. import java.util.Comparator;
  2. public class SortByIndex implements Comparator<Poker> {
  3. @Override
  4. public int compare(Poker o1, Poker o2) {
  5. if(o1.getiIndex()>o2.getiIndex()){
  6. return 1;
  7. }
  8. else if(o1.getiIndex()<o2.getiIndex()){
  9. return -1;
  10. }
  11. return 0;
  12. }
  13. }

SortByIndex

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. public class PokerGame {
  5. public List<Poker> pokerList;
  6. private static String[] numList = {
  7. "2","3","4","5","6","7","8","9","10","J","Q","K","A"
  8. };
  9. public PokerGame() {
  10. super();
  11. this.pokerList = new ArrayList<Poker>();
  12. InitPokers(this.pokerList);
  13. }
  14. public static void InitPokers(List<Poker> pokerList){
  15. for(int i =0;i<numList.length;i++){
  16. pokerList.add(new Poker("黑桃",numList[i]));
  17. pokerList.add(new Poker("红桃",numList[i]));
  18. pokerList.add(new Poker("梅花",numList[i]));
  19. pokerList.add(new Poker("方块",numList[i]));
  20. }
  21. pokerList.add(new Poker("大王",""));
  22. pokerList.add(new Poker("小王",""));
  23. }
  24. public void testForEach(){
  25. System.out.println("生成的扑克牌:");
  26. for(Poker poker:pokerList){
  27. System.out.println(""+poker.getPokerDesign()+poker.getPokerNum());
  28. }
  29. System.out.println();
  30. }
  31. public void shuffleCards(){
  32. Random rd = new Random();
  33. for(int i=0;i<pokerList.size();i++){
  34. int j = rd.nextInt(pokerList.size());//生成随机数
  35. Poker poker = pokerList.get(i);//取出当前的牌
  36. pokerList.set(i,pokerList.get(j));//当前牌与随机牌换位置
  37. pokerList.set(j,poker);
  38. }
  39. }
  40. public Poker deadCard() {
  41. Random rd = new Random();
  42. int i = rd.nextInt(pokerList.size());//生成随机数
  43. Poker poker = pokerList.get(i);//取出随机的牌
  44. pokerList.remove(i);
  45. return poker;
  46. }
  47. public Player runGame(Player p1,Player p2){
  48. Poker poker1 = p1.popMax();
  49. Poker poker2 = p2.popMax();
  50. System.out.println("玩家"+p1.getName()+"出牌"+poker1.getPokerDesign()+ poker1.getPokerNum());
  51. System.out.println("玩家"+p2.getName()+"出牌"+poker2.getPokerDesign()+ poker2.getPokerNum());
  52. if(poker1.compareTo(poker2)>0){
  53. return p1;
  54. }
  55. else {
  56. return p2;
  57. }
  58. }
  59. }