简易扑克牌游戏源代码

释放双眼,带上耳机,听听看~!

package myEasycard;

public class Player {

public String playerID;
public String playerName;
public Card C1,C2;

public Player(String name,String id,Card C1,Card C2) {
    this.playerName = name; 
    this.playerID = id;
    this.C1 = C1;
    this.C2 = C2;
}

}


package myEasycard;

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

public class num {

public List<String> numlist;

public num(){
    numlist = new ArrayList<String>();
    for(int i=1;i<11;i++){
        String j =i+"";
        numlist.add(j);
    }
    numlist.add("J");
    numlist.add("Q");
    numlist.add("K");
}

}


package myEasycard;

import java.util.List;
import java.util.ArrayList;
public class hauSe {

public List<String> huaselist;

public hauSe() {
    // TODO Auto-generated constructor stub
    huaselist = new ArrayList<String>();

    huaselist.add("方片");
    huaselist.add("红桃");
    huaselist.add("草花");
    huaselist.add("黑桃");
}

}


package myEasycard;

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

import myEasycard.Card;
import myEasycard.CardList;

public class CardList {

public List<Card> cardlist;

public CardList() {
    cardlist= new ArrayList<Card>();        
}
/**
 * 创建扑克牌
 */

public void CreatCard(){
    hauSe h = new hauSe();
    num n = new num();

    for(int i = 0;i<13;i++)
        for(int j=0;j<4;j++){
            Card c = new Card(h.huaselist.get(j),n.numlist.get(i));
            cardlist.add(c);
        }
     int i = 0;      
     for(Card c:cardlist){
         if(i!=4){
            System.out.print("<" +c.huase +c.num +">"+" ");
            i++;
         }
         if(i==4){
             System.out.println(" ");
             i=0;
         }
     }
        System.out.println("/n---------------扑克牌创建成功-----------------/n");
}
/**
 * 洗牌操作
 */
List<Card> washCard = new ArrayList<Card>();
Random ran = new Random();
public List<Card> Wash(){       

    int i=0;
    while(i<52){
        Card c = cardlist.get(ran.nextInt(52));
        if(!washCard.contains(c)){
            washCard.add(c);
            i++;
        }
    }
    return washCard;
}

/**
 * 发牌操作
 */
public Card deal(int pos){
    return washCard.get(pos);
}
/**
 * 比较大小
 */
public Card compare(Card c1,Card c2){

    hauSe h =new hauSe();
    num n = new num();
    Integer i1=n.numlist.indexOf(c1.num);
    Integer i2=n.numlist.indexOf(c2.num);
    int i=i1.compareTo(i2);
    /*
     * 大小相同比较花色
     */
    if(i==0){
        Integer i3=h.huaselist.indexOf(c1.huase);
        Integer i4=h.huaselist.indexOf(c2.huase);
        int j=i3.compareTo(i4);
        if(j==0)
            System.out.println("平局");
        if(j<0)
            return c2;
        if(j>0)
            return c1;      
    }
    if(i>0)                 
        return c1;
    return c2;   
    }

}


package myEasycard;

public class Card {

  public String huase;
  public String num;

 public Card(String huase,String num){
     this.huase = huase;
     this.num = num;
 }

public String getHuase() {
    return huase;
}

public void setHuase(String huase) {
    this.huase = huase;
}

public String getNum() {
    return num;
}

public void setNum(String num) {
    this.num = num;
}

}


package myEasycard;

import java.util.Scanner;

import myEasycard.Card;
import myEasycard.Player;
import myEasycard.CardList;

public class Game {

public static void main(String[] args) {
    System.out.println("----------------游戏开始-------------------/n");
    System.out.println("---------------创建扑克牌------------------/n");
    CardList c1=new CardList();
    c1.CreatCard();

    System.out.println("----------------开始洗牌--------------------");
    c1.Wash();
    System.out.println("----------------洗牌完成--------------------/n");

    System.out.println("----------------创建玩家--------------------");
    Scanner console = new Scanner(System.in);
    System.out.println("创建第一位玩家:");
    System.out.println("请输入玩家ID:");
    String id1=console.next();
    System.out.println("请输入玩家姓名:/n");
    String name1=console.next();
    System.out.println("创建第二位玩家:");
    System.out.println("请输入玩家ID:");
    String id2=console.next();
    System.out.println("请输入玩家姓名:/n");
    String name2=console.next();
    System.out.println("----------------创建玩家成功--------------------/n");

    System.out.println("------------------开始发牌--------------------");
    System.out.println("-------玩家"+name1+"拿牌-------");
    System.out.println("-------玩家"+name2+"拿牌-------");
    System.out.println("-------玩家"+name1+"拿牌-------");
    System.out.println("-------玩家"+name2+"拿牌-------");

    //发牌操作
    Card cf1=c1.deal(0);                                                       
    Card cs2=c1.deal(1);
    Card cf11=c1.deal(2);
    Card cs22=c1.deal(3);
    System.out.println("------------------发牌结束--------------------/n");

    //创建玩家
    Player p1=new Player(name1,id1,cf1,cf11);
    Player p2=new Player(name2,id2,cs2,cs22);

    Card c1max=c1.compare(cf1, cf11);  //第一位玩家的最大牌
    Card c2max=c1.compare(cs2, cs22);  //第二位玩家的最大牌
    Card c=c1.compare(c1max, c2max);     //比较两位玩家的牌面
    System.out.println("------------------开始游戏--------------------");
    System.out.println("玩家"+p1.playerName+"的牌为<"+cf1.huase+cf1.num+">  <"+cf11.huase+cf11.num+">");
    System.out.println("玩家"+p2.playerName+"的牌为<"+cs2.huase+cs2.num+">  <"+cs22.huase+cs22.num+">");
    if(c.equals(c1max) && !c.equals(c2max)){
        System.out.println("-------------lalalalalala恭喜"+p1.playerName+"获胜!!!-----------------");
    }
    if(c.equals(c2max) && !c.equals(c1max)){
        System.out.println("-------------lalalalalala恭喜"+p2.playerName+"获胜!!!-----------------");
    }
    if(c.equals(c1max) && c.equals(c2max)){
        System.out.println("-------------OH MY GOD!"+p1.playerName+p2.playerName+"平局-----------------");
    }
    console.close();
}

}

【转自慕课】https://www.imooc.com

JAVA

java第一季 学习心得总结

2022-3-3 9:13:09

JAVA

一篇文章快速搞懂Java虚拟机的栈帧结构

2022-3-3 9:15:13

搜索