本文主要是介绍扑克牌炸金花,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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);
}
}
这篇关于扑克牌炸金花的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!