继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java第三季的扑克牌游戏练习

我是叶霖
关注TA
已关注
手记 2
粉丝 3
获赞 4

功能描述:
1.创建一副扑克牌(包括4种花色,和13种点数)
2.创建2名玩家(含有姓名,id,手牌等属性,手牌是扑克牌集合)
3.洗牌(将扑克牌无序打乱)
4.发牌(玩家每人从牌的顶端依次拿取2张牌)
5.游戏(比较玩家手中点数最大的手牌,若是点数相同则比较花色,最后决出胜者)
第一部分 Puke类
public class Puke implements Comparable<Puke> { //扑克类
private String ID; //点数
private Integer Inum; //点数大小
private String color; //花色
private Integer Cnum;
public Puke(String ID, Integer Inum,String color,Integer Cnum) {
this.ID = ID;
this.Inum = Inum;
this.color = color;
this.Cnum =Cnum;
}
public Integer getInum() {
return Inum;
}
public void setInum(Integer inum) {
Inum = inum;
}
public Integer getCnum() {
return Cnum;
}
public void setCnum(Integer cnum) {
Cnum = cnum;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return ID+color;
}
@Override
public int compareTo(Puke o) {
if(Inum!=Inum)
return o.Inum.compareTo(Inum);
else return o.Cnum.compareTo(Cnum);
}
}

第二部分 Peopel类
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Peopel { //玩家类
private Integer id; //玩家id
private String name; //玩家姓名
public List<Puke>pukers;

public Peopel(Integer id,String name) {
    super();
    this.id = id;
    this.name = name;
    this.pukers = new ArrayList<Puke>();
}
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;
}
public Set<Puke> getPuke() {
    return getPuke();
}
public List<Puke> getPokers() {
    return pukers;
}
public void setPokers(List<Puke> pokers) {
    this.pukers = pokers;
}   

}
第三部分 PlayGame类
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;

import com.play.People;
import com.play.Poker;

public class PlayGame { //游戏类
private List<Puke>pukelist = new ArrayList<Puke>();
public List<Peopel>peopleList = new ArrayList<Peopel>();

//创建一副扑克牌
private void createPoker() { System.out.println("------创建扑克牌------"); 
String[] colorlist= new String[] {"黑桃","红桃","梅花","方块"}; //权重由小到大 
String[] IDlist = new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};//权重由小到大
for (int i = 0; i < colorlist.length; i++) { 
    for (int j = 0; j < IDlist.length; j++) {
        pukelist.add(new Puke(colorlist[i],i,IDlist[j],j)); 
        } 
    }
}

//提示创建成功并答应
public void creatsuccess() {
    System.out.println("创建扑克牌成功");
  //  pukelist.toString();
   System.out.println("为"+pukelist.toString());

// for (Puke puke : pukelist) {
// System.out.println("为:"+puke.toString());
// }
}

//提示洗牌--------洗牌结束
public void xipai() {
    System.out.println("----开始洗牌----");
    Collections.shuffle(pukelist);
    System.out.println("----洗牌结束----");
}

//创建2名玩家 
public void createPeopel() {
    System.out.println("开始创建玩家");
    Integer id;
    String name;
for(int i = 0;i<2;i++) {
    while(true) {
    try {
    System.out.println("输入"+(i+1)+"玩家ID");
    Scanner console = new Scanner(System.in);
     id = console.nextInt();

    if(peopleList.contains(new Peopel(id,null))) {
        System.out.println("此id已经存在");
        continue;
    }
    break;
    }catch(InputMismatchException e) {
        System.out.println("请输入整数类型的id");
     }
    }

    System.out.println("请输入玩家姓名");
     Scanner console = new Scanner(System.in);
     name = console.next();
    peopleList.add(new Peopel(id,name));
   }
System.out.println("添加成功!");
for (Peopel peopel : peopleList) {
    System.out.println("欢迎玩家:"+peopel);
}
}        

//提示发牌,并给玩家发牌
public void fapai() {
    System.out.println("开始发牌!");
    int i =2;
    int j =0;
    while(i>0) {
    for (Peopel peopel : peopleList) {
        System.out.println("玩家"+peopel.getName()+"得到手牌");
        peopel.pukers.add(pukelist.get(j));
        j++;
        }
    i--;
    }
    System.out.println("发牌结束");
}

//提示开始游戏
public void  play() {
    System.out .println("开始游戏");
    for (Peopel peopel : peopleList) {
        Collections.sort(peopel.pukers);
        System.out.println("玩家"+peopel.getName()+"的最大手牌是:"+peopel.pukers.get(0));
    }

}

//提示获胜
private void gameOver() { 
    List<Puke> peopleBigPokerList = new ArrayList<Puke>();
    //所有玩家最大牌List 
    for (Peopel people : peopleList) { 
        peopleBigPokerList.add(people.pukers.get(0)); 
        } 
        Collections.sort(peopleBigPokerList); 
        Puke bigPoker = peopleBigPokerList.get(0);
        //所有玩家最大牌
        for (Peopel people : peopleList) { 
            if(people.pukers.contains(bigPoker)) { 
                System.out.println("------玩家:"+people.getName()+"获胜!------"); 
                } 
            } 
            System.out.println("玩家各自的手牌为:"); 
            for (Peopel people : peopleList) {
                System.out.println(people.getName()+":"+people.pukers.toString()); }
            } 

public static void main(String[] args) {
    PlayGame p1 = new PlayGame();
    p1.createPoker();
    p1.creatsuccess();
    p1.xipai();
    p1.createPeopel();
    p1.fapai();
    p1.play();
    p1.gameOver();
}

}

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP