3.7 综合实战(常用工具类)

简易扑克牌游戏

功能描述
1、创建一副扑克牌
包括四种花色
十三个点数,不考虑大小王
2、创建两名玩家
玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
3、洗牌
将之前创建的一副扑克牌打乱顺序
4、发牌
将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,没人发两张
5、游戏
比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,点数大的赢,若两人各自的点数最大的牌相等,则按花色比较

package poker;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Player {
    public  int id;
    public String name;
    public List myPo = new ArrayList();
    public Player(int id,String name)
    {
        this.id = id;
        this.name = name;
    }
}
package poker;

public class Poker implements Comparable {
    public String flower;
    public  String num;
    public Integer k;//定义点数大小
    public Integer f;//定义花色大小
    public Poker(String flower,String num){
        this.flower = flower;
        this.num = num;
        if(num.equals("J"))
        {
            this.k = 11;
        }
        else if(num.equals("Q"))
        {
            this.k = 12;
        }
        else if(num.equals("K"))
        {
            this.k = 13;
        }
        else if(num.equals("A"))
        {
            this.k = 14;
        }
        else
        {
            this.k = Integer.parseInt(num);
        }
        if(flower.equals("方块"))
        {
            this.f = 1;
        }
        else if(flower.equals("梅花"))
        {
            this.f = 2;
        }
        else if(flower.equals("红心"))
        {
            this.f = 3;
        }
        else
        {
            this.f = 4;
        }
    }
    
     public int compareTo(Poker arg0){
         if(!(this.k==arg0.k))
         {
             return this.k.compareTo(arg0.k);
         }
         else
         {
             return this.f.compareTo(arg0.f);
         }
         
     }
     public String toString(){
         return (this.flower+this.num);
     }
}
package poker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class PlayGame {
    List allPokers = new ArrayList();
    List players = new ArrayList();
    public void createPokers(){
        System.out.println("----------创建扑克牌----------");
        String[] flowers = {"方块","梅花","红心","黑桃"};
        String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<13;j++)
            {
                allPokers.add(new Poker(flowers[i],nums[j]));
            }
        }
        System.out.println("----------扑克牌创建成功!----------");
        System.out.print("为:"+allPokers.toString());

    }
    
    public void shuf(){
        System.out.println("----------开始洗牌...----------");
        Collections.shuffle(allPokers);
        System.out.println("----------洗牌结束!----------");
    }
    
    public void createPlayers(int num){
        System.out.println("----------创建玩家...----------");
        Scanner console = new Scanner(System.in);
        int id = 0;
        boolean isError = true;
        for(int i=0;i1)
            {
                winner = players.get(i);
            }
        }
        System.out.println("----------玩家:"+winner.name+"获胜!----------");
    }
    public void pokersOfPlayer(){
        System.out.println("玩家各自的手牌为:");
        for(Player p:players)
        {
            System.out.println(p.name+":"+p.myPo.toString());
        }
    }
    
    public static void main(String[] args) {
        PlayGame pg = new PlayGame();
        pg.createPokers();
        pg.shuf();
        pg.createPlayers(3);
        pg.deal(3);
        pg.startGame();
        pg.winOrLose();
        pg.pokersOfPlayer();
    }

}

你可能感兴趣的:(3.7 综合实战(常用工具类))