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

简易扑克牌游戏改进版

Etan_
关注TA
已关注
手记 1
粉丝 2
获赞 11

---------玩家类,定义了三个属性

package com.poker;

import java.util.*;

public class Player {
    private int id;
    private String name;
    private List<Playingcard> handCard;
    public Player(){

    }
    public Player(int id,String name){
        setId(id);
        setName(name);
        handCard =new ArrayList<Playingcard>();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Playingcard> getHandCard() {
        return handCard;
    }
}

---------扑克牌类,定义了两个属性

package com.poker;

public class Playingcard {
    private String type;
    private String num;
    public Playingcard(String type,String num){
        setType(type);
        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;
    }
}

---------先取牌 用for循环add,用StringBuilder来打印的

package com.poker;

import java.util.*;
import java.util.List;

public class Creatcards {
    private List<Playingcard>poker = new ArrayList<Playingcard>();
    public List<Playingcard> createcard(){
        System.out.println("取出一副扑克牌......");
        String type[]={"方片","梅花","红桃","黑桃"};
        String num[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        for(int i =0;i<type.length;i++){
            for(int j=0;j<num.length;j++){
                Playingcard card =new Playingcard(type[i],num[j]);
                poker.add(card);
            }
        }
        StringBuilder card =new StringBuilder();
        for (Playingcard playingcard : poker) {         
            if(poker.indexOf(playingcard)==51){
                card.append(playingcard.getType()+playingcard.getNum());
            }else{
            card.append(playingcard.getType()+playingcard.getNum()+",");}
        }
        System.out.println("["+card.toString()+"]");
        return poker;
    }
    public List<Playingcard> getPoker() {
        return poker;
    }
    public void setPoker(List<Playingcard> poker) {
        this.poker = poker;
    }
}

---------用Collections.shuffle()洗牌

package com.poker;

import java.util.Collections;
import java.util.List;

public class Washcard {
    StringBuilder card2 =new StringBuilder();
    public void Wash(List<Playingcard>poker){
        Collections.shuffle(poker);
        for (Playingcard playingcard : poker) {         
            if(poker.indexOf(playingcard)==51){
                card2.append(playingcard.getType()+playingcard.getNum());
            }else{
                card2.append(playingcard.getType()+playingcard.getNum()+",");}
        }
        System.out.println("正在洗牌......");
        System.out.println("洗牌成功!......");
            //打印洗后的牌
            //System.out.println("扑克牌");
            //System.out.println("["+card2.toString()+"]");

        }
}

---------用户登录

package com.poker;
import java.util.*;
public class UserRegister {
    private int i=0;
    private int playerCount =4;//更改玩家的个数
    Map<Integer,Player>playerList;
    public UserRegister(){
        playerList =new HashMap<Integer,Player>();
    }
    public void register(){
        System.out.println("欢迎来到"+playerCount+"人扑克游戏!");
        for(;i<playerCount;){
            System.out.println("请输入第"+(i+1)+"个玩家的ID与姓名");
            System.out.println("请输入ID:");
            try{
                Scanner in =new Scanner(System.in);
                int id =in.nextInt();
                System.out.println("请输入姓名:");
                String Name =in.next();
                Player user =new Player(id,Name);
                playerList.put(i, user);
                i++;
            }catch(InputMismatchException e){
                OrderException Exc =new OrderException("你输入的ID有误!请重新输入数字ID!......");
                System.out.println(Exc.getMessage());
            }
        }
        Set<Integer>playList =playerList.keySet();
        for (Integer integer : playList) {
            Player user =playerList.get(integer);
                System.out.println("欢迎玩家:"+user.getName()+"  ID:"+user.getId());

        }
    }
    public int getI() {
        return i;
    }
    public void setI(int i) {
        this.i = i;
    }
}

---------定义了命令异常类

package com.poker;

public class OrderException extends Exception {
    public OrderException(){

    }
    public OrderException(String message){
        super(message);
    }
}

---------发牌,这里handCardCount可以随意调整你要发的牌数,并且每次都打印了你得到的牌

package com.poker;

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

public class HandOutPoker {
    List<Player> py = new ArrayList<Player>();
    private int pyNum;//玩家个数
    private int handCardCount =3;//更改每位玩家的手牌个数
    public HandOutPoker(Map<Integer,Player>playerList,int pyNum){
        for(int i=0;i<pyNum;i++){
        py.add(playerList.get(i));
        }
        this.pyNum=pyNum;
    }
    public void handOut(List<Playingcard>poker){
        System.out.println("开始发牌......");       
        int pokerNum=0;//这个值是整副扑克牌的牌的位置 
        int cardNum=0;//这个值是你手牌的牌的位置
        for(;pokerNum<(pyNum*handCardCount);){
            for(int o=0;o<pyNum;o++){
                py.get(o).getHandCard().add(poker.get(pokerNum));
                System.out.println("玩家"+py.get(o).getName()+"取牌......"+py.get(o).getHandCard().get(cardNum).getType()+py.get(o).getHandCard().get(cardNum).getNum());
                pokerNum++;
            }
            cardNum++;
        }
        System.out.println("发牌结束......");
    }
    public int getHandCardCount() {
        return handCardCount;
    }
}

---------定义了比较类的比较方法,这里返回牌的时候其实把花色数字转了两遍 有点繁琐

package com.poker;

import java.util.*;

public class PokerComparator implements Comparator<Playingcard> {

    public String ChangeType(Playingcard poker){
        switch(poker.getType()){
            case "方片":
                poker.setType("1");break;
            case "梅花":
                poker.setType("2");break;
            case "红桃":
                poker.setType("3");break;
            case "黑桃":
                poker.setType("4");break;
        }
        return poker.getType();
    }
    public String ChangeNum(Playingcard poker){
        switch(poker.getNum()){
            case "J":
                poker.setNum("11");break;
            case "Q":
                poker.setNum("12");break;
            case "K":
                poker.setNum("13");break;
            case "A":
                poker.setNum("14");break;
        }
        return poker.getNum();
    }
    public String ChangeType2(Playingcard poker){
        switch(poker.getType()){
            case "1":
                poker.setType("方片");break;
            case "2":
                poker.setType("梅花");break;
            case "3":
                poker.setType("红桃");break;
            case "4":
                poker.setType("黑桃");break;
        }
        return poker.getType();
    }
    public String ChangeNum2(Playingcard poker){
        switch(poker.getNum()){
            case "11":
                poker.setNum("J");break;
            case "12":
                poker.setNum("Q");break;
            case "13":
                poker.setNum("K");break;
            case "14":
                poker.setNum("A");break;
        }
        return poker.getNum();
    }
    public int compare(Playingcard o1, Playingcard o2) {
        // TODO 自动生成的方法存根   
        int type1 =Integer.valueOf(ChangeType(o1));
        int type2 =Integer.valueOf(ChangeType(o2));
        if(type1>type2){
            return 1;
        }else if(type1<type2){
            return -1;
        }else{
            int num1=Integer.valueOf(ChangeNum(o1));
            int num2=Integer.valueOf(ChangeNum(o2));
            if(num1>num2){
                return 1;
            }else if(num1<num2){
                return -1;
            }               
        }
        return 0;
    }
    public Playingcard returnResult(Playingcard o1,Playingcard o2){
        int result =compare(o1,o2);
        if(result>0){
            ChangeType2(o1);
            ChangeNum2(o1);
            return o1;
        }else if(result<0){
            ChangeType2(o2);
            ChangeNum2(o2);
            return o2;
        }
        return null;
    }
}

---------开始游戏(手牌是两两比较取最大值)

package com.poker;

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

public class Match {

    public void Matching(PokerComparator ob5,HandOutPoker ob4){
        List<Playingcard>pokerMax = new ArrayList<Playingcard>();
        int pyNum=0;//玩家的位置(初始为第一位0)
        System.out.println("开始游戏......");
        for(;pyNum<ob4.py.size();pyNum++){
            Playingcard poker =ob4.py.get(pyNum).getHandCard().get(0);
            for(int j=0;j<(ob4.getHandCardCount()-1);j++){          
                poker =ob5.returnResult(poker, ob4.py.get(pyNum).getHandCard().get(j+1));
            }
            pokerMax.add(poker);
            System.out.println("玩家"+ob4.py.get(pyNum).getName()+"的最大手牌为:"+pokerMax.get(pyNum).getType()+pokerMax.get(pyNum).getNum());
        }
        Playingcard pokerMaxOne =pokerMax.get(0);
        for(int cardNum=0;cardNum<(ob4.py.size()-1);cardNum++){
            pokerMaxOne =ob5.returnResult(pokerMaxOne, pokerMax.get(cardNum+1));
        }
        int num =pokerMax.indexOf(pokerMaxOne);
        System.out.println("玩家"+ob4.py.get(num).getName()+"获胜!");
    }

}

---------打印玩家手牌(match太长了重写一个)

package com.poker;

public class PrintHandCard {
    public void Print(PokerComparator ob5,HandOutPoker ob4){
        for(int i=0;i<ob4.py.size();i++){
            StringBuilder str =new StringBuilder();
            for(int j=0;j<ob4.getHandCardCount();j++){
                if(j==(ob4.getHandCardCount()-1)){
                    str.append(ob5.ChangeType2(ob4.py.get(i).getHandCard().get(j))
                        +ob5.ChangeNum2(ob4.py.get(i).getHandCard().get(j)));
                }else{
                    str.append(ob5.ChangeType2(ob4.py.get(i).getHandCard().get(j))
                        +ob5.ChangeNum2(ob4.py.get(i).getHandCard().get(j))+",");
                }
            }
            System.out.println("玩家:"+ob4.py.get(i).getName()+"的手牌:["+str.toString()+"]");
        }
    }
}

---------main方法类

package com.poker;

import java.util.*;

public class Run {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        System.out.println("开始进行游戏......");
        Creatcards ob1 =new Creatcards();
        Washcard ob2 =new Washcard();
        UserRegister ob3 =new UserRegister();
        List<Playingcard>poker =ob1.createcard();
        ob2.Wash(poker);
        ob3.register();
        HandOutPoker ob4 =new HandOutPoker(ob3.playerList,ob3.getI());
        ob4.handOut(ob1.getPoker());    
        PokerComparator ob5= new PokerComparator();     
        Match ob6 =new Match();
        ob6.Matching(ob5, ob4);
        PrintHandCard ob7 =new PrintHandCard();
        ob7.Print(ob5,ob4);
    }
}

运行结果(玩家数3,发牌手牌5张 )
图片描述

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

热门评论

洗牌居然有函数, 还有这种操作

查看全部评论