扑克牌炸金花

1.创建类

使用权限修饰符定义所需要参数,使用this关键字生成方法

public class gamejinhua {
private String suit;//花色

private int rank;//数字

public gamejinhua(String suit, int rank) {
this.suit = suit;

this.rank = rank;

}

2.使用快捷键生成get和set方法,作用是调取方法

public gamejinhua() {
// TODO Auto-generated constructor stub

}

public String getSuit() {
return suit;

}

public void setSuit(String suit) {
this.suit = suit;

}

public int getRank() {
return rank;

}

public void setRank(int rank) {
this.rank = rank;

}

3.传入花色

@Override

public String toString() {
return "{ "+suit+" "+rank+"}"; //重写toString方法,调用时产生花色与牌号

}

private static final String[] suits = {"♥","♣","♦","♠"};

4.创建list集合

public List<gamejinhua> takePoker(){
List<gamejinhua> pokers = new ArrayList<>();

for (int i = 0; i < 4; i++) { //外循环为4种花色

for (int j = 1; j <= 13; j++) { //内循环为A~K13张牌

gamejinhua poker = new gamejinhua(suits[i], j);

pokers.add(poker); //通过循环产生52张牌并放入集合中

}

}

return pokers;

5.使用random随机数生成打乱顺序

public void shuffle(List<gamejinhua> pokers){
for (int i = pokers.size()-1; i > 0; i--) {
Random random = new Random(); //用random随机函数打乱牌序

int index = random.nextInt(i);

swap(pokers,i,index);

}

}

6.调用方法

private void swap(List<gamejinhua> pokers, int i, int j){
gamejinhua tmp = pokers.get(i);

pokers.set(i,pokers.get(j)); //调用时,产生随机的花色和数字

pokers.set(j,tmp);

}

7.创建人的对象摸牌规则

public List<List<gamejinhua>> game(List<gamejinhua> pokers){
List<List<gamejinhua>> hand = new ArrayList<>();

List<gamejinhua> hand1 = new ArrayList<>();

List<gamejinhua> hand2 = new ArrayList<>();

List<gamejinhua> hand3 = new ArrayList<>();

hand.add(hand1);

hand.add(hand2); //三只手代表游戏的三个人

hand.add(hand3);

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
gamejinhua removePoker = pokers.remove(0); //循环控制三个人轮流摸牌,一人摸五张,一共15张

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

}

}

return hand;

}

8.创建主函数并运行

public static void main(String[] args) {
gamejinhua game = new gamejinhua();

List<gamejinhua> pokers = game.takePoker();

System.out.println(pokers);

//洗牌

game.shuffle(pokers);

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

System.out.println(pokers);

//揭牌

List<List<gamejinhua>> hand = game.game(pokers);

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

for (int i = 0; i < hand.size(); i++) {
System.out.println("第 "+(i+1)+"个人的牌:"+hand.get(i));

}

System.out.println("剩下的牌");

System.out.println(pokers);

}

}

package twlveth;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class gamejinhua {
    private String suit;//花色
    private int rank;//数字
 
    public gamejinhua(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }
 
   

package twlveth;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class gamejinhua {
    private String suit;//花色
    private int rank;//数字
 
    public gamejinhua(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }
 
    public gamejinhua() {
        // TODO Auto-generated constructor stub
    }
 
    public String getSuit() {
        return suit;
    }
 
    public void setSuit(String suit) {
        this.suit = suit;
    }
 
    public int getRank() {
        return rank;
    }
 
    public void setRank(int rank) {
        this.rank = rank;
    }
 
    @Override
    public String toString() {
        return "{ "+suit+" "+rank+"}";        //重写toString方法,调用时产生花色与牌号
    }
    private static final String[] suits = {"♥","♣","♦","♠"};
    
    public List<gamejinhua> takePoker(){
        List<gamejinhua> pokers = new ArrayList<>();
        for (int i = 0; i < 4; i++) {                    //外循环为4种花色
            for (int j = 1; j <= 13; j++) {                //内循环为A~K13张牌
                gamejinhua poker = new gamejinhua(suits[i], j);
                pokers.add(poker);                        //通过循环产生52张牌并放入集合中
            }
        }
        return pokers;
    }
    public void shuffle(List<gamejinhua> pokers){
        for (int i = pokers.size()-1; i > 0; i--) {
            Random random = new Random();                //用random随机函数打乱牌序
            int index = random.nextInt(i);
            swap(pokers,i,index);
        }
    }
 
    private void swap(List<gamejinhua> pokers, int i, int j){
        gamejinhua tmp = pokers.get(i);
        pokers.set(i,pokers.get(j));                    //调用时,产生随机的花色和数字
        pokers.set(j,tmp);
    }
    public List<List<gamejinhua>> game(List<gamejinhua> pokers){
        List<List<gamejinhua>> hand = new ArrayList<>();
        List<gamejinhua> hand1 = new ArrayList<>();
        List<gamejinhua> hand2 = new ArrayList<>();
        List<gamejinhua> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);                //三只手代表游戏的三个人
        hand.add(hand3);
 
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                gamejinhua removePoker = pokers.remove(0);                //循环控制三个人轮流摸牌,一人摸五张,一共15张
                hand.get(j).add(removePoker);
            }
        }
        return hand;
        
    }
    public static void main(String[] args) {
        gamejinhua game = new gamejinhua();
        List<gamejinhua> pokers = game.takePoker();
        System.out.println(pokers);
 
        //洗牌
        game.shuffle(pokers);
        System.out.println("洗牌:");
        System.out.println(pokers);
 
        //揭牌
        List<List<gamejinhua>> hand = game.game(pokers);
        System.out.println("揭牌:");
        for (int i = 0; i < hand.size(); i++) {
            System.out.println("第 "+(i+1)+"个人的牌:"+hand.get(i));
        }
        System.out.println("剩下的牌");
        System.out.println(pokers);
    }
    
}
 

50543891cebb411b9320dbe6c6c8e042.png

相关推荐

  1. 扑克牌排序

    2023-12-20 07:44:02       36 阅读
  2. 设计一套扑克牌

    2023-12-20 07:44:02       25 阅读
  3. ChatGPT裂了

    2023-12-20 07:44:02       28 阅读
  4. python 敌人。

    2023-12-20 07:44:02       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-20 07:44:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-20 07:44:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-20 07:44:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-20 07:44:02       20 阅读

热门阅读

  1. MySQL安装

    2023-12-20 07:44:02       46 阅读
  2. TensorFlow 的基本概念和使用场景

    2023-12-20 07:44:02       38 阅读
  3. bash数组的用法

    2023-12-20 07:44:02       40 阅读
  4. Python装饰器

    2023-12-20 07:44:02       38 阅读
  5. 正则表达式

    2023-12-20 07:44:02       46 阅读