炸金花代码

 

package 十二章;

public class Poker {
	private String suit;//花色
	private int rank;//数字	
	
	public Poker(String suit, int rank) {
		this.suit = suit;
		this.rank = rank;
	}
	
	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+"}";
	}
}
package 十二章;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Game {
	private static final String[] suits = {"♥","♣","♦","♠"};//花色
	
	public List buyPoker(){
		List pokers = new ArrayList<>();//创建集合类对象
		for (int i = 0; i < 4; i++) {//遍历牌的花色
			for (int j = 1; j <= 13; j++) {//遍历拍的每张牌的大小的数量
				Poker poker = new Poker(suits[i], j);//创建对象
				pokers.add(poker);
			}
		}
		return pokers;//返回
	}
	public void shuffle(List pokers){
		for (int i = pokers.size()-1; i > 0; i--) {//交换两个元素的位置  模仿洗牌
			Random random = new Random();//创建对象
			int index = random.nextInt(i);//判断是否有下一个
			swap(pokers,i,index);
		}
	}
	
	private void swap(List pokers, int i, int j){//传入两个参数   然后用于两个元素交换位置 然而达到洗牌的效果
		Poker tmp = pokers.get(i);
		pokers.set(i,pokers.get(j));	
		pokers.set(j,tmp);
	}
	public List> game(List pokers){
		List> hand = new ArrayList<>();
		List hand1 = new ArrayList<>();//创建模仿三个人一起打牌  hand1
		List hand2 = new ArrayList<>();//hand2
		List hand3 = new ArrayList<>();//hand3
		hand.add(hand1);//添加
		hand.add(hand2);
		hand.add(hand3);
		
		for (int i = 0; i < 5; i++) {//循环三个人打牌 外循环是每个人摸五张牌  内循环是三个人一起打牌
			for (int j = 0; j < 3; j++) {
				Poker removePoker = pokers.remove(0);
				hand.get(j).add(removePoker);
			}
		}
		return hand;
	}
	public static void main(String[] args) {
		Game game = new Game();
		List pokers = game.buyPoker();
		System.out.println(pokers);
		
		//洗牌
		game.shuffle(pokers);//洗牌
		System.out.println("洗牌:");
		System.out.println(pokers);
		
		//揭牌
		List> 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);
		}
}

炸金花代码_第1张图片

炸金花代码_第2张图片

炸金花代码_第3张图片 

你可能感兴趣的:(javascript,windows,开发语言)