jave 简易扑克牌(炸金花)

炸金花这段代码的形成使用了  get  set方法  还有toString方法来来实现了返回花色和数字的表示

  1. package 集合类;

  2.  
  3. public class Poker {

  4. // TODO Auto-generated method stub

  5. private String suit;//花色

  6. private int rank;//数字

  7.  
  8. public Poker(String suit, int rank) {//参数为扑克牌的花色和大小的构造方法

  9. this.suit = suit;//将参数suit的值赋给suit

  10. this.rank = rank;

  11. }

  12.  
  13. public String getSuit() {//使用get方法来读取

  14. return suit;//返回

  15. }

  16.  
  17. public void setSuit(String suit) {//使用set方法重写

  18. this.suit = suit;

  19. }

  20.  
  21. public int getRank() {//使用get方法来读取

  22. return rank;

  23. }

  24.  
  25. public void setRank(int rank) {//使用set方法重写

  26. this.rank = rank;

  27. }

  28.  
  29. @Override

  30. public String toString() {//toSring方法重写

  31. return "{ "+suit+" "+rank+"}";

  32. }

  33.  
  34.  

代码图: 019c0ef27ab84683b121ebd21d4fdffc.png

 然后再是创建一个类使用for循环来 模仿买牌  之前实现类里定义了两个类型 一个int  一个String 两个类型分别是花色和数字 然后再这个类里 使用for循环来牌数字不同大小的数量和花色  然后传入两个参数来模仿洗牌   再是new三个对象使用for循环来模仿三个人一起打牌  再是使用for循环来模仿揭牌  主要代码如下
 

揭牌  主要代码如下:

3a9819fe25a742fe8cdd2984e9e96739.png

 

78e416b0be58408fa29c25c65b03106b.png

 运行结果如下:

365abb1ff4884197a5fd9891d629dda6.png 

  1. package 集合类;

  2. // TODO Auto-generated method stub

  3. import java.util.ArrayList;

  4. import java.util.List;

  5. import java.util.Random;

  6. public class Game {

  7. private static final String[] suits = {"♥","♣","♦","♠"};//花色

  8.  
  9. public List<Poker> buyPoker(){

  10. List<Poker> pokers = new ArrayList<>();//创建集合类对象

  11. for (int i = 0; i < 4; i++) {//遍历牌的花色

  12. for (int j = 1; j <= 13; j++) {//遍历拍的每张牌的大小的数量

  13. Poker poker = new Poker(suits[i], j);//创建对象

  14. pokers.add(poker);

  15. }

  16. }

  17. return pokers;//返回

  18. }

  19. public void shuffle(List<Poker> pokers){

  20. for (int i = pokers.size()-1; i > 0; i--) {//交换两个元素的位置 模仿洗牌

  21. Random random = new Random();//创建对象

  22. int index = random.nextInt(i);//判断是否有下一个

  23. swap(pokers,i,index);

  24. }

  25. }

  26.  
  27. private void swap(List<Poker> pokers, int i, int j){//传入两个参数 然后用于两个元素交换位置 然而达到洗牌的效果

  28. Poker tmp = pokers.get(i);

  29. pokers.set(i,pokers.get(j));

  30. pokers.set(j,tmp);

  31. }

  32. public List<List<Poker>> game(List<Poker> pokers){

  33. List<List<Poker>> hand = new ArrayList<>();

  34. List<Poker> hand1 = new ArrayList<>();//创建模仿三个人一起打牌 hand1

  35. List<Poker> hand2 = new ArrayList<>();//hand2

  36. List<Poker> hand3 = new ArrayList<>();//hand3

  37. hand.add(hand1);//添加

  38. hand.add(hand2);

  39. hand.add(hand3);

  40.  
  41. for (int i = 0; i < 5; i++) {//循环三个人打牌 外循环是每个人摸五张牌 内循环是三个人一起打牌

  42. for (int j = 0; j < 3; j++) {

  43. Poker removePoker = pokers.remove(0);

  44. hand.get(j).add(removePoker);

  45. }

  46. }

  47. return hand;

  48. }

  49. public static void main(String[] args) {

  50. Game game = new Game();

  51. List<Poker> pokers = game.buyPoker();

  52. System.out.println(pokers);

  53.  
  54. //洗牌

  55. game.shuffle(pokers);//洗牌

  56. System.out.println("洗牌:");

  57. System.out.println(pokers);

  58.  
  59. //揭牌

  60. List<List<Poker>> hand = game.game(pokers);//揭牌

  61. System.out.println("揭牌:");

  62. for (int i = 0; i < hand.size(); i++) {

  63. System.out.println("第 "+(i+1)+"个人的牌:"+hand.get(i));

  64. }

  65. System.out.println("剩下的牌");//然后计算剩余还没有被摸走的牌

  66. System.out.println(pokers);

  67. }

  68.  
  69. }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值