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

【作业】牌局游戏

package com.imooc.poker_game;

import com.imooc.poker_game.exception.WrongCommandException;

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

/**
 * 比较两位玩家手中的牌:
 * 1)比较两人手中点数最大的牌,点数大的赢;
 * 2)若两人点数一样大,则再按花色比较。
 *
 * 发牌规则:
 * 从扑克第一张开始,按照一人一张的顺序,每人发两张。
 */
public class Game {
    public int numOfGamer = 2;
    public int piecesPerGamer = 2;
    public List<Gamer> gamerInfo(int numOfGamer){
        List<Gamer> gamers = new ArrayList<Gamer>();
        Scanner scanner = new Scanner(System.in);
        for (int i=1; i<=numOfGamer; i++){
            System.out.println("请输入玩家"+i+"号的信息:");
            System.out.print("姓名:");
            String name = scanner.next();
            System.out.print("ID:");
            String id = scanner.next();
            try {
                Gamer gamer = new Gamer(name,id);
                gamers.add(gamer);
                System.out.println("------------------------");
            } catch (WrongCommandException e) {
                System.out.println(e.getMessage());
                i--;
            }
        }
        return gamers;
    }
    public void dealing(Poker poker, List<Gamer> gamers){
        for(int turns=1;turns<=this.piecesPerGamer;turns++){
            for (int num=0; num<this.numOfGamer;num++) {
                int index = (turns-1)*this.numOfGamer + num;
                System.out.println("-----玩家:"+gamers.get(num).name+"-拿牌");
                gamers.get(num).addPiece(poker.pokers.get(index));
            }
        }
    }

    public Gamer winner(List<Gamer> gamers){
        List<PokerPiece> bestPieces = new ArrayList<PokerPiece>();
        for(Gamer gamer:gamers){
            PokerPiece best = gamer.bestPiece();
            bestPieces.add(best);
            System.out.println("玩家:"+gamer.name+"最大的手牌为:"+best.show());
        }

        Collections.sort(bestPieces);
        PokerPiece bestOfAll = bestPieces.get(0);
        for (Gamer gamer:gamers){
            gamer.pieces.contains(bestOfAll);
            return gamer;
        }
        return null;
    }
    public static void main(String[] args) {
        //新建牌局
        Game game = new Game();
        //新建扑克
        Poker poker = new Poker();
        System.out.println("-----新建扑克-----");
        //洗牌
        poker.shuffle();
        System.out.println("-------洗牌-------");

        //新建玩家
        List<Gamer> gamers = game.gamerInfo(game.numOfGamer);
        for(Gamer gamer:gamers)
            System.out.println("欢迎玩家:"+gamer.name);

        //开始牌局
        System.out.println("-----开始发牌-----");
        game.dealing(poker,gamers);
        System.out.println("-----结束发牌-----");

        System.out.println("-----开始游戏-----");

        Gamer winner = game.winner(gamers);
        System.out.println("----玩家:"+winner.name+"获胜!----");

        //结束牌局,展示手牌
        System.out.println("玩家各自的手牌为:");
        for (Gamer gamer:gamers){
            System.out.println(gamer.name+":"+gamer.show());
        }
    }
}
package com.imooc.poker_game;

import com.imooc.poker_game.exception.WrongCommandException;

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

public class Gamer {
    String name;
    String id;
    List<PokerPiece> pieces;

    public Gamer(String name,String id) throws WrongCommandException {
        try {
            Integer.parseInt(id);
            this.name = name;
            this.id = id;
            pieces = new ArrayList<PokerPiece>();
        }catch (NumberFormatException e){
            throw new WrongCommandException("Gamer id should be numerical.");
    }
    }
    public Gamer(){

    }

    /**
     * 加牌
     */
    public void addPiece(PokerPiece piece){
        pieces.add(piece);
    }

    /**
     * 最好的牌
     */
    public PokerPiece bestPiece(){
        Collections.sort(this.pieces);
        return this.pieces.get(0);
    }

    /**
     * 展示玩家所有牌
     */
    public String show(){
        List<String> pieces_str = new ArrayList<String>();
        for (PokerPiece piece:this.pieces){
            pieces_str.add(piece.show());
        }
        return pieces_str.toString();
    }
}
package com.imooc.poker_game;

import com.imooc.collection_map.Course;

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

public class PokerPiece implements Comparable<PokerPiece>{
    Integer pattern;
    Integer figure;

    public PokerPiece(Integer pattern,Integer figure){
        this.figure = figure;
        this.pattern = pattern;
    }

    @Override
    public int compareTo(PokerPiece o) {
        int rule1 = o.figure.compareTo(this.figure);
        if(rule1 == 0){
            return this.pattern.compareTo(o.pattern);
        }else{
            return rule1;
        }
    }

    @Override
    public boolean equals(Object obj){
        if (this == obj) return true;
        if (obj == null) return false;
        if (!(obj instanceof Course)) return false;
        PokerPiece piece = (PokerPiece) obj; //以上都通过,则强转obj进一步判断。
        if (this.pattern == null || this.figure == null) {
            if (piece.pattern == null || this.figure == null) return true;
            else return false;
        }else{
            if(this.figure.equals(piece.figure)&&this.pattern.equals(piece.pattern)) return true;
            else return false;
        }
    }

    public String show(){
        String figure;
        String pattern;
        switch (this.figure){
            case 11:
                figure = "J";
                break;
            case 12:
                figure = "Q";
                break;
            case 13:
                figure = "K";
                break;
            case 14:
                figure = "A";
                break;
            default:
                figure = this.figure+"";
                break;
        }
        switch (this.pattern){
            case 1:
                pattern = "黑桃";
                break;
            case 2:
                pattern = "红心";
                break;
            case 3:
                pattern = "梅花";
                break;
            case 4:
                pattern = "方块";
                break;
            default:
                pattern = "";
                break;
        }
        return pattern+figure;
    }
}
package com.imooc.poker_game;

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

public class Poker {
    public List<PokerPiece> pokers = new ArrayList<PokerPiece>();
    public int[] patterns = new int[]{1,2,3,4};
    public int[] figures = new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14};
    /**
     * 新建扑克
     */
    public Poker(){
        for(int pattern:patterns){
            for(int figure:figures){
                pokers.add(new PokerPiece(pattern,figure));
            }
        }
    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(pokers);
    }

    /**
     * 展现牌
     */
    public void show(){
        for(PokerPiece piece:pokers){
            System.out.print(piece.show()+",");
        }
    }
}
package com.imooc.poker_game.exception;

public class WrongCommandException extends Exception{
    public WrongCommandException(String message) {
        super(message);
    }
}


正在回答

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409779    人
  • 解答问题       4339    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

【作业】牌局游戏

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信