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

简易扑克牌游戏的写法,肯定有很多重复代码,可以修改

greenhandc
关注TA
已关注
手记 1
粉丝 3
获赞 1
对于简单扑克牌游戏,实现了对子的比较,为了测试的时候方便,顺便加上了重新玩这个部分;
一共是三个类,扑克牌、玩家、还有游戏。
扑克牌类首先有花色和点数属性,为了比较大小,重写了equals方法和使用了系统自动生成的Hashcode方法,对于不用HashSet的类来说,可以不要。
玩家类有名字 id和手牌三个属性,记住是private,通过系统生成的get和set方法进行获取和修改。

package com.poker;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Gameplay {
static Boolean restart=true;
List<Poker>pokerlist=new ArrayList<Poker>();
List<Player>playerlist=new ArrayList<Player>();
List<Poker>player1handpoker=new ArrayList<Poker>();
List<Poker>player2handpoker=new ArrayList<Poker>();
String[]type={"方块","梅花","红桃","黑桃"};
String[]point={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
/**

  • 首先创建的是一副扑克牌
    */
    public void creatPoker(){
    System.out.println("开始创建扑克牌");
    for(int i=0;i<type.length;i++){
    for(int j=0;j<point.length;j++){
    pokerlist.add(new Poker(type[i],point[j]));
    }

        }
    System.out.println("扑克牌创建成功");

    }
    /**

    • 检验扑克牌是否完成
      */
      public void testCreatPoker(){
      for(Poker pk:pokerlist){
      System.out.println("有如下牌:"+pk.getType()+pk.getNum()+" ");}
      }

    /**

    • 然后是创建2个玩家
      */
      public void creatPlayer(){
      for(int n=0;n<2;n++){
      System.out.println("请输入玩家ID");
      String playerID=new Scanner(System.in).next();
      System.out.println("请输入玩家姓名");
      String playerName=new Scanner(System.in).next();
      playerlist.add(new Player(playerID,playerName));
      }
      System.out.println("玩家添加成功!"+"现有玩家是:");
      for(Player player:playerlist){
      System.out.println(player.getID()+":"+player.getName());
      }

    }
    /**

    • 接下来的步骤是洗牌
      */
      public void shufflepoker(){
      System.out.println("开始洗牌咯~~"+" .....");
      Collections.shuffle(pokerlist);
      System.out.println("洗牌完成!");
      }
      /**
    • 下面是发牌步骤
      */
      public void deliverPoker(){
      System.out.println("开始发牌!");
      for(int i=0;i<4;i++){
      Poker newpoker=pokerlist.get(i);
      if(i%2==0){
      player1handpoker.add(newpoker);
      System.out.println("玩家1抓了一张牌");
      }else
      {
      player2handpoker.add(newpoker);
      System.out.println("玩家2抓了一张牌");

      }

      }

      for(Poker Poker1:player1handpoker){
      System.out.println(playerlist.get(0).getName()+"手里的牌是"
      +Poker1.getType()+Poker1.getNum());
      }
      for(Poker Poker2:player2handpoker){
      System.out.println(playerlist.get(1).getName()+"手里的牌是"
      +Poker2.getType()+Poker2.getNum());
      }

    }

    /**

    • 这是比较牌面大小
      */
      public void comparenum(){
      Poker pk11=player1handpoker.get(0);
      Poker pk12=player1handpoker.get(1);
      Poker pk21=player2handpoker.get(0);
      Poker pk22=player2handpoker.get(1);
      int level1=0;
      int level2=0;
      if(pk11.getNum()==pk12.getNum())level1++;
      if(pk21.getNum()==pk22.getNum()) level2++;
      if(level1>level2){
      System.out.println(playerlist.get(0).getName()+"手里的牌是对子,所以"+playerlist.get(0).getName()+"获胜!");
      }
      else if(level1<level2){
      System.out.println(playerlist.get(1).getName()+"手里的牌是对子,所以"+playerlist.get(1).getName()+"获胜!");
      }
      else{
      System.out.println("两个人的牌型是一样的,所以比较点数!");
      //获得玩家1手中最大的牌
      Collections.sort(player1handpoker);
      Poker pk1Max=player1handpoker.get(player1handpoker.size()-1);
      System.out.println(playerlist.get(0).getName()+"手里最大的牌是:"+pk1Max.getType()+pk1Max.getNum());

      //获得玩家2手中最大的牌
      Collections.sort(player2handpoker);
      Poker pk2Max=player2handpoker.get(player2handpoker.size()-1);
      System.out.println(playerlist.get(1).getName()+"手里最大的牌是:"+pk2Max.getType()+pk2Max.getNum());

      if(pk1Max.compareTo(pk2Max)>0){
          System.out.println(playerlist.get(0).getName()+"获胜!");
      }else{
          System.out.println(playerlist.get(1).getName()+"获胜!");
      }

      }

    }

    public void restart(){
    int door=0; //保证输入没问题的时候才开门
    do{
    System.out.println("是否重新开始游戏?Y/N");
    Scanner sc=new Scanner(System.in);
    String regame=sc.next();
    if(regame.equals("N")||regame.equals("n")){
    restart=false;
    System.out.println("欢迎下次再玩!");
    door=1;
    }else if(regame.equals("Y")||regame.equals("y")){
    door=1;
    }else{
    System.out.println("您的输入有误,请重新输入!");
    }}while(door==0);

    }

    /**

    • @param args
      */

    public static void main(String [] args){

    do{
    Gameplay t1=new Gameplay();
    t1.creatPlayer();
    t1.creatPoker();
    t1.testCreatPoker();
    t1.shufflepoker();
    t1.deliverPoker();
    t1.comparenum();
    t1.restart();
    }while(restart==true);

    }

}

package com.poker;

public class Poker implements Comparable<Poker> {
private String type;
private String num;
public Poker(String type ,String num){

this.setType(type);
this.setNum(num);
}

public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime result + ((num == null) ? 0 : num.hashCode());
result = prime
result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Poker other = (Poker) obj;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
@Override
public int compareTo(Poker o) {
String pokernum="2,3,4,5,6,7,8,9,10,J,Q,K,A";
String pokertype="方块,梅花,红桃,黑桃";
int result;
result=pokernum.indexOf(this.getNum())-pokernum.indexOf(o.getNum());
if(result==0){
result=pokertype.indexOf(this.getType())-pokertype.indexOf(o.getType());
}
return result;
}

}

package com.poker;

public class Player {
private String ID;
private String name;
public Poker handpoker;
public Player(String ID,String name){
this.ID=ID;
this.name=name;

}
public String getID() {
    return ID;
}
public void setID(String iD) {
    ID = iD;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

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