扑克牌的实现:
首先一副扑克牌是54张,这里我们去掉大小王,也就是52张扑克牌
有♣,♠,♥,◆,四种花色,每种花色对应1到13张
定义一个Card类,来表示一张扑克牌,上面有花色suit和牌的值rank。
重写了toString方法,方便我们打印每一张牌。
重写equals()方法,如果判断两张牌是否相同,会用到。
1. public class Card { 2. public String suit; //花色 3. public int rank; //大小 4. 5. public Card(String suit, int rank) { 6. this.suit = suit; 7. this.rank = rank; 8. } 9. 10. @Override 11. public boolean equals(Object obj) { 12. if (obj == null){ 13. return false; 14. } 15. if (!(obj instanceof Card)){ 16. return false; 17. } 18. Card card = (Card)obj; 19. return this.suit.equals(card.suit) && this.rank == card.rank; 20. } 21. 22. @Override 23. public String toString() { 24. return suit+rank; 25. } 26. }
玩家定义:
每一个玩家都有手牌,我们用一个Card类型的顺序表来保存,注意这里的顺序表后面new了,也就是初始化了,这个不能少,不然后面还要专门为每个玩家的手牌初始化,会比较麻烦
1. 2. import java.util.ArrayList; 3. import java.util.List; 4. 5. 6. public class Player { 7. public String name; 8. public List<Card> cards = new ArrayList<>(); 9. 10. public Player(String name) { 11. this.name = name; 12. } 13. }
扑克牌的操作(初始化,洗牌,发牌等)
初始化:
这里使用了一个for each循环,大家也可以用两个for循环,效果是一样的
1. public static void initialiseCards(List<Card> cards){ 2. for (String s : new String[]{"♣","♠","♥","◆"}){ 3. for (int i = 1; i <= 13 ; i++) { 4. cards.add(new Card(s,i)); 5. } 6. } 7. }
洗牌:
使用了random类,遍历整个cards顺序表,对于每个元素,不大于元素个数下标的随机数,让这个随机下标对应的元素与当前元素进行交换,从而实现洗牌的效果 。
1. public static void washCards(List<Card> cards) { 2. Random random = new Random(); 3. for (int i = 0; i < cards.size(); i++) { 4. int toExchange = random.nextInt(cards.size()); 5. Card tempCard = cards.get(i); 6. cards.set(i,cards.get(toExchange)); 7. cards.set(toExchange,tempCard); 8. } 9. }
发牌:
1. public static void sendCardsToPlayers(List<Player> players, List<Card> cards) { 2. for (Player player : players) { 3. for (int i = 0; i < 13; i++) { 4. Card card = cards.remove(0); 5. player.cards.add(card); 6. } 7. } 8. } 9. 10. public static void printPlayersCards(List<Player> players) { 11. for (Player player : players) { 12. System.out.printf("玩家[%s]的手牌是:",player.name); 13. System.out.println(player.cards); 14. } 15. }
捉鬼游戏:
游戏规则:A-5 20张牌,去掉一张A
五个人依次抓牌,直到把所有的牌都抓光。抓牌期间,一旦出现两张牌点数相同(凑成一对子了)就可以扔掉。
正式游戏:
一个回合一个回合进行,每个人(甲)抽取下家(乙)的一张随机手牌。如果乙的手牌被抽完了。则乙退出游戏。甲检查手中有没有凑成一对,抽成了,还是把一对扔掉,如果扔掉一对后手牌没有了,甲也退出,只到只剩一个人。就抓到鬼了
1. 2. 3. import java.util.ArrayList; 4. import java.util.List; 5. import java.util.Random; 6. 7. public class GameCatchGhost { 8. public static void main(String[] args) { 9. List<Card> cards = new ArrayList<>(); 10. List<Player> players = new ArrayList<>(); 11. players.add(new Player("Cat")); 12. players.add(new Player("Pig")); 13. players.add(new Player("Dog")); 14. players.add(new Player("Cat")); 15. players.add(new Player("You")); 16. initialiseCards2(cards); //初始化19张牌,去掉一张A 17. washCards(cards);//洗牌 18. System.out.println("摸完牌以后,玩家中的手牌情况:"); 19. sendCardsToPlayers(players,cards); //发牌,如果有两张一样的手牌就丢弃 20. printPlayersCards(players); //依次打印所有玩家手中的手牌 21. play(players); //玩捉鬼游戏 22. 23. } 24. 25. private static void sendCardsToPlayers(List<Player> players, List<Card> cards) { 26. while(cards.size() > 0){ 27. for (int i = 0; i < players.size(); i++) { 28. if (cards.size() <= 0){ //如果牌发完了,要马上退出才行 29. break; 30. } 31. Player player = players.get(i); 32. Card nextCardToBePutIn = cards.remove(0); 33. putCardInOnePlayer(player,nextCardToBePutIn); 34. } 35. } 36. } 37. 38. private static void play(List<Player> players) { 39. int t = 1; 40. while(players.size() > 1){ 41. for (int i = 0; i < players.size(); i++) { 42. Player player = players.get(i); 43. if (player.cards.size() == 0){ 44. System.out.println("****************************************玩家"+player.name+"手牌为空,退出"); 45. players.remove(i); 46. } 47. Random random = new Random(); 48. Player nextPlayer = players.get(i == players.size()-1 ? 0 : i+1); 49. if (nextPlayer.cards.size() == 0){ //如果下一个玩家的手牌为0,不用进行随机数生成,下面的不执行 50. System.out.println("****************************************玩家"+nextPlayer.name+"手牌为空,退出"); 51. players.remove(nextPlayer); 52. continue; 53. } 54. int toDrawNextCardPos = random.nextInt(nextPlayer.cards.size()); 55. Card nextCardtoBePutIn = nextPlayer.cards.get(toDrawNextCardPos); 56. System.out.println("第"+t+"轮,"+player.name+"抽了"+nextPlayer.name+"一张手牌"+nextCardtoBePutIn.toString()); 57. putCardInOnePlayer(player,nextCardtoBePutIn); 58. printPlayersCards(players); //依次打印所有玩家手中的手牌 59. t++; 60. } 61. } 62. System.out.println("抓到鬼了!"+players.get(0).name+"是鬼,现在他的手牌是"+players.get(0).cards); 63. } 64. 65. private static void putCardInOnePlayer(Player player,Card nextCardToBePutIn){ 66. //传入一个玩家,和将要插入这个玩家的手牌,如果有重复,删除重复牌,没重复则插入 67. boolean flag = true; 68. for (int j = 0; j < player.cards.size(); j++) { 69. if (player.cards.get(j).rank == nextCardToBePutIn.rank){ 70. //如果有重复的卡,那就把那张重复的也删除 71. player.cards.remove(j); 72. flag = false; //同时标记,现在这张卡不需要add 73. break; 74. } 75. } 76. if (flag){ //flag=true表示没有重复的卡,那就add进去 77. player.cards.add(nextCardToBePutIn); 78. } 79. 80. } 81. 82. private static void initialiseCards2(List<Card> cards){ 83. for (String s : new String[]{"♠","♣","♥","◆"}){ 84. for (int i = 1; i <= 5 ; i++) { 85. cards.add(new Card(s,i)); 86. } 87. } 88. cards.remove(0); 89. } 90. public static void printPlayersCards(List<Player> players) { 91. for (Player player : players) { 92. System.out.printf("玩家[%s]的手牌是:",player.name); 93. System.out.println(player.cards); 94. } 95. } 96. public static void washCards(List<Card> cards) { 97. Random random = new Random(); 98. for (int i = 0; i < cards.size(); i++) { 99. int toExchange = random.nextInt(cards.size()); 100. Card tempCard = cards.get(i); 101. cards.set(i,cards.get(toExchange)); 102. cards.set(toExchange,tempCard); 103. } 104. } 105. }
效果图: