手记

JAVA入门第三季 7-1 简单扑克牌

输入代码
主函数代码
package yf;
import java.util.*;
public class Gaming {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CardList cc=new CardList();
        Scanner IN=new Scanner(System.in);
        cc.CreatCard();
        System.out.println("\n洗牌中......");
        cc.WashCard();
        System.out.println("\n+++++++++++++++++++++++++++++++++\n\n洗牌完成!\n");
        int a;
        int b;
        String aa= null;
        String bb = null;
        System.out.println("请输入玩家A的ID:");
        a=cc.ID();
        System.out.println("请输入玩家A的姓名:");
        aa=IN.next();

        System.out.println("请输入玩家B的ID:");
        b=cc.ID();
        System.out.println("请输入玩家B的姓名:");
        bb=IN.next();

        System.out.println("玩家"+aa+"拿牌:");
        System.out.println("玩家"+bb+"拿牌:");
        System.out.println("玩家"+aa+"拿牌:");
        System.out.println("玩家"+bb+"拿牌:\n");
        Data c11=cc.deal();
        Data c21=cc.deal();
        Data c12=cc.deal();
        Data c22=cc.deal();

        int i ;
        Data c13,c23;
        i=cc.Compare(c11, c12);
        if(i!=1) {
            System.out.println("玩家"+aa+"最大的手牌为:"+c12.HuaSe +c12.Card );
            c13=c12;
        }else {
            System.out.println("玩家"+aa+"最大的手牌为:"+c11.HuaSe +c11.Card);
            c13=c11;
        }   
        ///////////////////////

        i=cc.Compare(c21, c22);
        cc.Compare(c21, c22);
        if(i!=1) {
            System.out.println("玩家"+bb+"最大的手牌为:"+c22.HuaSe +c22.Card );
            c23=c22;
        }else {
            System.out.println("玩家"+bb+"最大的手牌为:"+c21.HuaSe +c21.Card );
            c23=c21;
        }
        ////////////////////////
        i=cc.Compare(c13, c23);
        if(i==1)
            System.out.println("玩家"+aa+"赢了!\n\n++++++++++++++++++++++++++++++++++");
        else
            System.out.println("玩家"+bb+"赢了!\n\n++++++++++++++++++++++++++++++++++");
        System.out.println("玩家"+aa+"的牌为:"+c11.HuaSe+c11.Card+" "
        +c12.HuaSe + c12.Card);
        System.out.println("玩家"+bb+"的牌为:"+c21.HuaSe+c21.Card+" "
        +c22.HuaSe + c22.Card);
    }

}

``

输入代码
卡牌处理类
package yf;
import java.util.*;

public class CardList {

List<Data> Cards;
public CardList() {
    Cards=new ArrayList<Data>();
}
public void CreatCard() {

    HuaSe H=new HuaSe();
    Card C=new Card();
    for(int i=0;i<13;i++) {
        for(int j=0;j<4;j++) {
            Data d=new Data(C.CardList.get(i),H.HuaSeList.get(j));
            Cards.add(d);
        }   
    }
    int i=0;
    for(Data data:Cards) {
        if(i==4) {
            System.out.println(" ");
            i=0;
        }
        if(i!=4){
            System.out.print("<"+data.HuaSe+data.Card+"> ");
            i++;
        }
    }
    System.out.println("\n++++++++++++ 洗 牌 前 ++++++++++++++");
}

//洗牌
List<Data> WASH=new ArrayList<Data>();
public void WashCard(){

    Random RM=new Random();
    int i=0;

    while(i<52) {
        Data d=Cards.get(RM.nextInt(52));
        if(!WASH.contains(d)) {
            WASH.add(d);
            i++;
        }
    }

    System.out.println("\n++++++++++++ 洗 牌 后 ++++++++++++++");
    i=0;
    for(Data data:WASH) {
        if(i==4) {
            System.out.println(" ");
            i=0;
        }
        if(i!=4){
            System.out.print("<"+data.HuaSe+data.Card+"> ");
            i++;
        }
    }
}

//输入玩家信息
public int ID() {
    int i=-1;
    Scanner IN=new Scanner(System.in);
    try {
        System.out.println("请输入ID:");
        i=IN.nextInt();
    }catch(InputMismatchException a) {
        System.out.println("请输入整数!");
    }
    if(i==-1)
        ID();
    return i;
}

//发牌
int deal=0;
public Data deal() {
    Data c=WASH.get(deal);
    deal++;
    return c;
}

//比较牌数大小
public int Compare(Data A,Data B) {
    Card C=new Card();
    HuaSe H=new HuaSe();
    if(C.CardList.indexOf(A.Card )>C.CardList.indexOf(B.Card )) {
        return 1;
    }else if(H.HuaSeList.indexOf(A.HuaSe)>H.HuaSeList.indexOf(B.HuaSe)) {
            return 1;
    }else {
        return 0;
    }
}

}

输入代码
卡牌点数类
package yf;

import java.util.ArrayList;
import java.util.List;

public class Card  {
    public List<String> CardList;

    public Card() {
        CardList=new ArrayList<String>();
        for(int i=1;i<10;i++) {

            CardList.add((i+1)+"");
    }
        CardList.add("J");
        CardList.add("Q");
        CardList.add("K");
        CardList.add("A");

    }

}

``

输入代码
卡牌花色类
package yf;
import java.util.*;
public class HuaSe {

public List<String> HuaSeList;

public HuaSe() {
    HuaSeList=new ArrayList<String>();
    HuaSeList.add("方块");
    HuaSeList.add("梅花");
    HuaSeList.add("红桃");
    HuaSeList.add("黑桃");
}

}

输入代码
卡牌数据处理类
package yf;

public class Data {
public String Card;
public String HuaSe;
public Data(String Card,String HuaSe) {
this.Card =Card;
this.HuaSe=HuaSe;
}
}

1人推荐
随时随地看视频
慕课网APP