为了账号安全,请及时绑定邮箱和手机立即绑定

简易扑克牌游戏,对各位前辈的代码进行了改进,有不足请指出

标签:
Java

Poker类

package 扑克牌;

public class Poker {
private String colors;
    private String numbers;
    public String getColors(){
        return colors;
    }
    public void setColors(String colors){
        this.colors=colors;
    }
    public String getNumbers(){
        return numbers;
    }
    public void setNumbers(String numbers){
        this.numbers=numbers;
    }
}

Player类

package 扑克牌;

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

public class Player {
private int id;
    private String name;
    private List<Poker> handCards = new ArrayList<Poker>();
    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<Poker> getHandCards(){
        return handCards;
    }
    public void setHandCards(List<Poker> handCards){
        this.handCards=handCards;
    }
}

GameStart类

package 扑克牌;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class GameStart {
String[] color = {"","","",""};
    String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; 
    Scanner input = new Scanner(System.in);
    List<Poker> pokerList = new ArrayList<Poker>();
    List<Player> playerList = new ArrayList<Player>();
    private void setPlayer(){
        for(int i=1;i<3;i++){
            System.out.println("请输入第"+i+"个玩家id");
            int id=0;
            try{
                id=input.nextInt();
            }catch (Exception e){
                System.out.println("id不合理,请重新输入");
                i--; 
            }
            if(i!=0)
            System.out.println("请输入第"+i+"个玩家姓名");
            String name;
            name=input.next();
            Player player = new Player();
            player.setId(id);
            player.setName(name);
            playerList.add(player);
        }
        System.out.println("玩家创建完毕");
    }
    private void initializeCard(){
        System.out.println("\n-------创建扑克牌--------");
        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                Poker p = new Poker();
                p.setColors(color[i]);
                p.setNumbers(number[i]);
                pokerList.add(p);
            }
        }

        for(Poker poker:pokerList){
            System.out.println(poker.getColors()+poker.getNumbers());
        }
        System.out.println("\n------扑克牌创建完毕-------");
    }
    private void shuffleCard() { 
        System.out.println("\n---------开始洗牌--------");
        Collections.shuffle(pokerList);//List shuffle随机排序
        System.out.println("---------洗牌完毕--------"); 
    }
    private void getCard() { 
        System.out.println("\n---------开始发牌--------"); 
        System.out.println("玩家"+playerList.get(0).getName()+"拿牌...");
        playerList.get(0).getHandCards().add(pokerList.get(0));
        System.out.println("玩家"+playerList.get(1).getName()+"拿牌..."); 
        playerList.get(1).getHandCards().add(pokerList.get(1)); 
        System.out.println("玩家"+playerList.get(0).getName()+"拿牌..."); 
        playerList.get(0).getHandCards().add(pokerList.get(2)); 
        System.out.println("玩家"+playerList.get(1).getName()+"拿牌...");
        playerList.get(1).getHandCards().add(pokerList.get(3)); 
        System.out.println("--------发牌结束--------\n"); 
        }
    private void compare(){
        System.out.println("---------获胜方---------"); 
        ComparePoker comparePoker = new ComparePoker();
        List<Poker> Poker1 = new ArrayList<Poker>(); 
        List<Poker> Poker2 = new ArrayList<Poker>(); 
        //获取玩家1的第一张手牌 
        Poker1.add(0, playerList.get(0).getHandCards().get(0));
        //获取玩家1的第二张手牌 
        Poker1.add(1, playerList.get(0).getHandCards().get(1)); 
        //获取玩家2的第一张手牌 
        Poker2.add(0, playerList.get(1).getHandCards().get(0)); 
        //获取玩家2的第二张手牌
        Poker2.add(1, playerList.get(1).getHandCards().get(1));
        int a = comparePoker.compare(Poker1.get(0),Poker1.get(1));
        int b = comparePoker.compare(Poker2.get(0),Poker2.get(1));
        if(a>0){
            if(b>0){
                int ab1=comparePoker.compare(Poker1.get(0), Poker2.get(0));
                if(ab1>0)
                    System.out.println("玩家"+ playerList.get(0).getName() + "获胜!");
                if(ab1<0)
                    System.out.println("玩家"+ playerList.get(1).getName() + "获胜!");
                else
                    System.out.println("平局");
            }
            else{
                int ab2=comparePoker.compare(Poker1.get(0), Poker2.get(1));
                if(ab2>0)
                    System.out.println("玩家"+ playerList.get(0).getName() + "获胜!");
                if(ab2<0)
                    System.out.println("玩家"+ playerList.get(1).getName()+ "获胜!");
                else
                    System.out.println("平局");
            }
        }
        else{
            if(b>0){
                int ba1=comparePoker.compare(Poker1.get(1), Poker2.get(0));
                if(ba1>0)
                    System.out.println("玩家"+ playerList.get(0).getName() + "获胜!");
                if(ba1<0)
                    System.out.println("玩家"+ playerList.get(1).getName() + "获胜!");
                else
                    System.out.println("平局");
            }
            else{
                int ba2=comparePoker.compare(Poker1.get(1), Poker2.get(1));
                if(ba2>0)
                    System.out.println("玩家"+ playerList.get(0).getName() + "获胜!");
                if(ba2<0)
                    System.out.println("玩家"+ playerList.get(1).getName() + "获胜!");
                else
                    System.out.println("平局");
            }
        }
    }
    private void showCard() { 
        System.out.print("\n-------玩家各自手牌-------");
        for(Player player : playerList) { 
            System.out.print("\n玩家"+player.getName()+":");
            for(Poker Poker : player.getHandCards()) {
                System.out.print(Poker.getColors()+Poker.getNumbers()+" ");
                } 
            } 
        }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        GameStart gs=new GameStart();
        gs.setPlayer();
        gs.initializeCard();
        gs.shuffleCard();
        gs.getCard();
        gs.compare();
        gs.showCard();
    }
}

ComparePoker类

package 扑克牌;

import java.util.Comparator;

public class ComparePoker implements Comparator<Poker>{
    public int compare(Poker p1, Poker p2) { 
        String[] color = {"","","",""}; 
        String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        int n1=0,n2=0;
        for(int i=0;i<number.length;i++){
            if(p1.getNumbers()==number[i])
                n1=i;
            if(p2.getNumbers()==number[i])
                n2=i;
        }
        if(n1==n2){
            int c1=0,c2=0;
            for(int j=0;j<color.length;j++){
                if(p1.getColors()==color[j])
                    c1=j;
                if(p2.getColors()==color[j])
                    c2=j;   
            }
            if(c1>c2)
                return -1;
            if(c1<c2)
                return 1;
            else
                return 0;
        }
        if(n1>n2)
            return 1;
        if(n1<n2)
            return -1;
        else
            return 0;
    }
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消