Java扑克牌小程序

慕课网第三季作业

要求
1. 创建一副扑克牌,包括2-10,J,Q,K,A,黑桃,红桃,梅花,方片,不包括大小王。
2. 创建两名玩家,有name属性。
3. 洗牌,利用Collections.shuffle方法打乱顺序。
4. 发牌,从第一张开始,按照一人一张的方式,每人发两张牌。
5. 比牌,两人分别拿出手中最大牌比较,点数大者赢,若点数相同,则比较花色。

设计思路:
创建Cards类,Play类,Game类,Test类。
其中Game类中的Compare(Caed x,Card y)方法用来比较两张牌的大小

运行结果:
Java扑克牌小程序_第1张图片

Cards类

package crad;

public class Cards {
    /**
     * 卡牌类
     */
    private String color;//花色
    private String number;//数字

    public Cards(String color,String number){
        this.color = color;
        this.number = number;
    }

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    @Override
    public String toString() {
        return getColor()+getNumber();
    }



}

Play类

package crad;

public class Play {
    /**
     * 玩家类
     */
    private Integer id;//玩家ID
    private String name;//玩家姓名

    public Play(Integer id,String name){
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Game类

package crad;
/**
 * 游戏主程序
 */

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

//创建牌       CreateCards()
//创建玩家  CreatePlays()
//洗牌    ChangeCards()
//发牌    SendCards()
//判断输赢  GameKo()

public class Game {
    Scanner input = new Scanner(System.in);


    List CardsList = new ArrayList();
    List PlayList = new ArrayList();
    List nowCardsList = new ArrayList();  

    String[] color =new String[] {"黑桃","红桃","梅花","方片"};
    String[] number = new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

    public void CreatePlays(){

        System.out.println("说明:本游戏为2人游戏,双方玩家各发2张牌,取出最大的一张牌与对手比较,点数大者赢,若点数相同,按'黑,红,梅,方'比较大小");
        for(int i=0;i<2;i++){
            System.out.println("请输入玩家"+(i+1)+"的姓名");
            PlayList.add(new Play(i, input.next()));
        }
    }

    public void CreateCards(){
        System.out.println("正在创建扑克牌......");

        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                CardsList.add(new Cards(color[i], number[j]));

            }
        }
        System.out.println("扑克牌有:"+CardsList.toString());
        System.out.println("创建完成!");
    }

    public void ChangeCards(){
        System.out.println("开始洗牌......");
        Random rm = new Random();
        int x = rm.nextInt(10);
        for(int i=0;iout.println("第"+(i+1)+"次洗牌");
            //System.out.println("打乱前:"+CardsList.toString());
            Collections.shuffle(CardsList);
            //System.out.println("打乱后:"+CardsList.toString());
        }
        System.out.println("洗牌结束!");

    }

    public void SendCards(){
        System.out.println("开始发牌......");
        for(int i=0;i<4;i++){
            //nowCardsList.add(new Cards(color[i], number[i]));
            nowCardsList.add(CardsList.get(i));
            System.out.println("玩家"+PlayList.get(i%2).getName()+"拿牌"+nowCardsList.get(i).toString());
        }
        System.out.println("发牌结束!");
    }

    public int Comparison(Cards x,Cards y){
        int Comparison1=0,Comparison2=0;//比较数字
        int Comparison3=0,Comparison4=0;//比较花色

        //玩家的手牌1
        for(int i=0;i<13;i++){
            if(x.getNumber().equals(number[i])){
                Comparison1 = i;
            }

        }
        for(int i=0;i<4;i++){
            if(x.getColor().equals(color[i])){
                Comparison3 = i;
            }
        }
        //玩家的手牌2
        for(int i=0;i<13;i++){
            if(y.getNumber().equals(number[i])){
                Comparison2 = i;
            }

        }
        for(int i=0;i<4;i++){
            if(y.getColor().equals(color[i])){
                Comparison4 = i;
            }
        }

        //选出玩家2张牌中较大的一张
        if(Comparison1return 2;
        }else if(Comparison1>Comparison2){
            return 1; 
        }else if(Comparison3return 2;
        }else{
            return 1;
        }


    }

    public void GameKo(){
        List maxCard = new ArrayList();
        int last1=0,last2=0,last3=0;
        //玩家1最大手牌
        last1 = Comparison(nowCardsList.get(0), nowCardsList.get(2));
        if(last1==1){
            maxCard.add(nowCardsList.get(0));
        }else if(last1==2){
            maxCard.add(nowCardsList.get(2));
        }
        System.out.println("玩家"+PlayList.get(0).getName()+"的最大手牌是"+maxCard.get(0).toString());
        //玩家2最大手牌
        last2 = Comparison(nowCardsList.get(1), nowCardsList.get(3));
        if(last2==1){
            maxCard.add(nowCardsList.get(1));
        }else if(last2==2){
            maxCard.add(nowCardsList.get(3));
        }
        System.out.println("玩家"+PlayList.get(1).getName()+"的最大手牌是"+maxCard.get(1).toString());
        //双方比较
        last3 = Comparison(maxCard.get(0), maxCard.get(1));
        if(last3==1){
            System.out.println("玩家"+PlayList.get(0).getName()+"获胜。");
        }else if(last3==2){
            System.out.println("玩家"+PlayList.get(1).getName()+"获胜。");
        }
    }

}

Test类

package crad;

public class test {
    public static void main(String[] args) {
        Game gm  =new Game();
        gm.CreatePlays();
        gm.CreateCards();
        gm.ChangeCards();
        gm.SendCards();
        gm.GameKo();
    }
}

你可能感兴趣的:(java学习记录)