java实现--简易扑克牌游戏

时间:2022-12-13 16:18:03

第三季听课后的综合实战,要求如下:
1.创建一副扑克牌,不考虑大小王
2.创建两名玩家,玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
3.洗牌,将之前创建的扑克牌顺序打乱
4.发牌,将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张
5.开始游戏,比大小,大小相同比花色(黑红梅方)

实现代码如下:

扑克类

public class Poker {
public Integer color;
public Integer number;
public Integer dengji;//等级属性,用于之后的比较大小

public Poker(int color,int number){
this.color = color;
this.number = number;
this.dengji = color + number;
}

@Override
public String toString() {
String colorString = "";
String numberString = "";
switch(color){
case 0 : colorString = "方片";break;
case 13 : colorString = "梅花";break;
case 26 : colorString = "红桃";break;
case 39 : colorString = "黑桃";break;
}
switch(number){
case 11 : numberString = "J";break;
case 12 : numberString = "Q";break;
case 13 : numberString = "K";break;
case 14 : numberString = "A";break;
default :numberString = String.valueOf(number);break;
}
return colorString + numberString ;
}
}

玩家类

public class Player {
public Integer id;
public String name;
public List<Poker> playerlist;

public Player(Integer id,String name){
this.id = id;
this.name = name;
this.playerlist = new ArrayList<Poker>();
}
}

游戏具体实现

public class PokerList {
public List<Poker> pokerlist;
private Scanner console;
private Player player1;
private Player player2;

public PokerList(){
this.pokerlist = new ArrayList<Poker>(52);
this.console = new Scanner(System.in);
}

//创建一副扑克牌
public void newPoker(){
for(int i = 0 ; i < 4 ; i++){
int color = i * 13;
for(int j = 0 ; j < 13 ; j++){
int number = j + 2;
Poker poker = new Poker(color,number);
pokerlist.add(poker);
}
}
}

//遍历输出
public void eachPoker(){
for(Poker po : pokerlist){
System.out.print(po.toString() + ",");
}
System.out.println();
}

//洗牌
public void shufflePoker(){
Collections.shuffle(pokerlist);
}

//创建玩家并发牌
public void dealPoker(){
System.out.println("请输入第一位玩家的id和姓名");
System.out.println("输入id:");
Integer id1 = console.nextInt();
System.out.println("输入姓名:");
String name1 = console.next();
System.out.println("请输入第二位玩家的id和姓名");
System.out.println("输入id:");
Integer id2 = console.nextInt();
System.out.println("输入姓名:");
String name2 = console.next();
System.out.println("******欢迎玩家:" + name1);
System.out.println("******欢迎玩家:" + name2);

System.out.println("******开始发牌******");
player1 = new Player(id1,name1);
player2 = new Player(id2,name2);
player1.playerlist.add(pokerlist.get(0));
System.out.println("***玩家:" + name1 + " 拿牌***");
player2.playerlist.add(pokerlist.get(1));
System.out.println("***玩家:" + name2 + " 拿牌***");
player1.playerlist.add(pokerlist.get(2));
System.out.println("***玩家:" + name1 + " 拿牌***");
player2.playerlist.add(pokerlist.get(3));
System.out.println("***玩家:" + name2 + " 拿牌***");
System.out.println("******发牌结束******");
}

//比较牌的大小
public void comparePoker(){
System.out.println("******游戏开始******");
Collections.sort(player1.playerlist);
Poker p1 = player1.playerlist.get(1);
System.out.println("玩家:" + player1.name + "最大的手牌是:" + p1);
Collections.sort(player2.playerlist);
Poker p2 = player2.playerlist.get(1);
System.out.println("玩家:" + player2.name + "最大的手牌是:" + p2);
if(p1.number > p2.number)
System.out.println("******玩家:" + player1.name + "获胜!!******");
else if(p1.number < p2.number)
System.out.println("******玩家:" + player2.name + "获胜!!******");
else{
PokerComparator result = new PokerComparator();
if(result.compare(p1, p2) == 1)
System.out.println("******玩家:" + player1.name + "获胜!!******");
else
System.out.println("******玩家:" + player2.name + "获胜!!******");
}
}

//输出各个玩家的手牌
public void showPoker(){
System.out.println("玩家各自的手牌为:");
for(Poker po1 : player1.playerlist){
System.out.print(player1.name + ":" + po1.toString() + " ");
}
System.out.println();
for(Poker po2 : player2.playerlist){
System.out.print(player2.name + ":" + po2.toString() + " ");
}
}
}

这里的sort,使用了Comparable接口,需要重写compareTo方法,根据数值比大小

//在Poker类里
@Override
public int compareTo(Poker o) {
// TODO Auto-generated method stub
return this.number.compareTo(o.number);
}

还是sort,使用了Comparator接口,需要重写compare方法,当数值相同时根据花色比大小
之前的等级属性派上用场了

public class PokerComparator implements Comparator<Poker> {
@Override
public int compare(Poker o1, Poker o2) {
// TODO Auto-generated method stub
return o1.dengji.compareTo(o2.dengji);
}
}

结果:
java实现--简易扑克牌游戏
java实现--简易扑克牌游戏

总结:
难的地方在于比较牌的大小,这里重写了compare方法,并给扑克牌设置了等级属性,这里借鉴了http://www.ituring.com.cn/article/18401