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

扑克牌游戏(Java课后作业)

标签:
Java

Poker.java

package com.imooc.pokergame;

public class Poker {
    public String colors;//扑克花色
    public String points;//扑克点数

    public Poker(String colors,String points){
        this.colors = colors;
        this.points = points;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((colors == null) ? 0 : colors.hashCode());
        result = prime * result + ((points == null) ? 0 : points.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Poker))
            return false;
        Poker other = (Poker) obj;
        if (colors == null) {
            if (other.colors != null)
                return false;
        } else if (!colors.equals(other.colors))
            return false;
        if (points == null) {
            if (other.points != null)
                return false;
        } else if (!points.equals(other.points))
            return false;
        return true;
    }
}

Player.java

package com.imooc.pokergame;

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

public class Player {
    public int id;
    public String name;
    public List<Poker> pokers = new ArrayList<Poker>();

    public Player(int id,String name){
        this.id = id;
        this.name = name;
    }
}

PokerComparator.java

package com.imooc.pokergame;

import java.util.Comparator;

public class PokerComparator implements Comparator<Poker> {

    @Override
    public int compare(Poker o1, Poker o2) {
        // TODO Auto-generated method stub
        String[] color = {"方片","梅花","红桃","黑桃"}; 
        String[] point = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        int temp1 = 0;
        int temp2 = 0;
        for(int i=0;i<point.length;i++){
            if(o1.points.equals(point[i])){
                temp1 = i + 1;
            }
            if(o2.points.equals(point[i])){
                temp2 = i + 1;
            }
        }
        if(temp1 > temp2){//牌值左边大返回-1
            return -1;
        }
        else if(temp1 == temp2){//牌值大小如果相同,比较花色
            for(int i=0;i<color.length;i++){
                if(o1.colors.equals(color[i])){
                    temp1 += i;
                }
                if(o2.colors.equals(color[i])){
                    temp2 += i;
                }
            }
            if(temp1 > temp2){//花色左边大返回-1
                return -1;
            }else{//花色右边大返回1
                return 1;
            }
        }
        else{//牌值右边大返回1
            return 1;
        }
    }
}

GameTest.java

package com.imooc.pokergame;

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

public class GameTest {
    String[] color = new String[]{"方片","梅花","红桃","黑桃"};
    String[] point = new String[]{"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>();

    public void initpoker(){
        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                Poker poker = new Poker(color[i], point[j]);
                pokerList.add(poker);
            }
        }
        System.out.println("--------创建扑克牌--------");
        int i = 0;
        for(Poker poker:pokerList){
            System.out.print(poker.colors+poker.points+" ");
            i++;
            if(i%13 == 0){
                System.out.println("");
            }
        }
    }

    public void shuffle(){
        System.out.println("--------开始洗牌--------");
        Collections.shuffle(pokerList);//集合中的乱序方法
        int i = 0;
        for(Poker poker:pokerList){
            System.out.print(poker.colors+poker.points+" ");
            i++;
            if(i%13 == 0){
                System.out.println("");
            }
        }
        System.out.println("--------洗牌结束--------");
    }

    public void initplayer(){
        System.out.println("--------创建玩家--------");
        for(int i=1;i<=2;i++){
            System.out.println("请输入玩家"+i+"的id:");
            int id = 0;
            try{
                id = scanf();
            }catch(Exception e){
                System.out.println(e.getMessage());
                i--;
                continue;
            }
            System.out.println("请输入玩家"+i+"的姓名");
            String name = input.next();
            playerList.add(new Player(id, name));
        }
    }

    public int scanf() throws Exception{
        try{
            int in = input.nextInt();
            return in;
        }catch(Exception e){
            input = new Scanner(System.in);
            throw new Exception("输入异常,请输入整数类型的ID!");
        }
    }

    public void deal(){
        System.out.println("\n--------开始发牌--------");
        System.out.println(playerList.get(0).name+"拿牌");
        playerList.get(0).pokers.add(pokerList.get(0));
        System.out.println(playerList.get(1).name+"拿牌");
        playerList.get(1).pokers.add(pokerList.get(1));
        System.out.println(playerList.get(0).name+"拿牌");
        playerList.get(0).pokers.add(pokerList.get(2));
        System.out.println(playerList.get(1).name+"拿牌");
        playerList.get(1).pokers.add(pokerList.get(3));
        System.out.println("--------发牌结束--------");
    }

    public void sort(){
        Collections.sort(playerList.get(0).pokers,new PokerComparator());
        Collections.sort(playerList.get(1).pokers,new PokerComparator());
        System.out.println("玩家"+playerList.get(0).name+"的最大授牌为:"+playerList.get(0).pokers.get(0).colors
                +playerList.get(0).pokers.get(0).points);
        System.out.println("玩家"+playerList.get(1).name+"的最大授牌为:"+playerList.get(1).pokers.get(0).colors
                +playerList.get(1).pokers.get(0).points);
    }

    public void comparePointsColors(){
        List<Poker> maxPokers = new ArrayList<Poker>();
        maxPokers.add(new Poker(playerList.get(0).pokers.get(0).colors, playerList.get(0).pokers.get(0).points));
        maxPokers.add(new Poker(playerList.get(1).pokers.get(0).colors, playerList.get(1).pokers.get(0).points));
        Collections.sort(maxPokers,new PokerComparator());
        if(maxPokers.get(0).points.equals(playerList.get(0).pokers.get(0).points)){
            System.out.println("--------玩家"+playerList.get(0).name+"获胜!--------");
        }
        else{
            System.out.println("--------玩家"+playerList.get(1).name+"获胜!--------");
        }
    }

    public void print(){
        System.out.println("--------玩家各自手牌--------");
        for(Player player:playerList){
            System.out.println(player.name+":");
            for(Poker poker:player.pokers){
                System.out.print("["+poker.colors+poker.points+"]");
            }
            System.out.println("");
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("--------欢迎进入纸牌大战--------");
        GameTest gt = new GameTest();
        gt.initpoker();
        gt.shuffle();
        gt.initplayer();
        gt.deal();
        gt.sort();
        gt.comparePointsColors();
        gt.print();
        System.out.println("--------游戏结束--------");
    }

}

运行结果

--------欢迎进入纸牌大战--------
--------创建扑克牌--------
方片2 方片3 方片4 方片5 方片6 方片7 方片8 方片9 方片10 方片J 方片Q 方片K 方片A 
梅花2 梅花3 梅花4 梅花5 梅花6 梅花7 梅花8 梅花9 梅花10 梅花J 梅花Q 梅花K 梅花A 
红桃2 红桃3 红桃4 红桃5 红桃6 红桃7 红桃8 红桃9 红桃10 红桃J 红桃Q 红桃K 红桃A 
黑桃2 黑桃3 黑桃4 黑桃5 黑桃6 黑桃7 黑桃8 黑桃9 黑桃10 黑桃J 黑桃Q 黑桃K 黑桃A 
--------开始洗牌--------
红桃K 梅花10 黑桃6 梅花8 黑桃Q 梅花9 黑桃J 方片3 梅花3 梅花6 方片7 红桃A 黑桃3 
方片K 方片2 黑桃10 方片6 方片8 方片5 红桃Q 黑桃4 方片J 梅花7 黑桃5 梅花2 方片9 
红桃3 红桃6 梅花4 黑桃K 红桃J 梅花A 红桃8 红桃10 红桃9 黑桃8 黑桃7 红桃4 方片10 
红桃7 梅花Q 梅花K 梅花5 黑桃2 红桃5 方片A 梅花J 红桃2 黑桃9 方片4 方片Q 黑桃A 
--------洗牌结束--------
--------创建玩家--------
请输入玩家1的id:
1
请输入玩家1的姓名
tom
请输入玩家2的id:
2
请输入玩家2的姓名
jack

--------开始发牌--------
tom拿牌
jack拿牌
tom拿牌
jack拿牌
--------发牌结束--------
玩家tom的最大授牌为:红桃K
玩家jack的最大授牌为:梅花10
--------玩家tom获胜!--------
--------玩家各自手牌--------
tom:
[红桃K][黑桃6]
jack:
[梅花10][梅花8]
--------游戏结束--------
点击查看更多内容
2人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消