手记

简易扑克牌游戏,请多多指教!

这个小项目是看了又看别人的代码才写出来的。有不足的地方请指教!
还想请教小伙伴们是怎样把一个项目从无到写出来的,因为我每次写项目的时候都不知道如何入手,都要先看看别人的例子才能写出来。。。。。。

package com.imooc;

//扑克类
public class Pokers implements Comparable<Pokers>{

    static final String[] colors = {"方片","梅花","红桃","黑桃"};
    static final String[] points = {"2","3","4","5","6","7","8","9","J","Q","K","A"};
    private int prefix;//前缀
    private int postfix;//后缀

    //定义有参的构造方法
    public Pokers(int prefix, int postfix) {
        super();
        this.prefix = prefix;
        this.postfix = postfix;
    }

    public int getPrefix() {
        return prefix;
    }

    public int getPostfix() {
        return postfix;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + postfix;
        result = prime * result + prefix;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Pokers other = (Pokers) obj;
        if (postfix != other.postfix)
            return false;
        if (prefix != other.prefix)
            return false;
        return true;
    }

    @Override
    public String toString() {
        return colors[this.prefix] + points[this.postfix];
    }

    @Override
    public int compareTo(Pokers o) {
        if(this.getPostfix() > o.getPostfix()) {
            return 1;
        } else if (this.getPostfix() < o.getPostfix()) {
            return -1;
        }else {
            if (this.getPrefix() > o.getPrefix()) {
                return 1;
            }else if (this.getPrefix() < o.getPrefix()) {
                return -1;
            }else 
                return 0;
        }

    }

}
package com.imooc;

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

//Players玩家类
public class Players {

    private String playerId;//玩家ID
    private String playerName;//玩家姓名
    private List<Pokers> pokersOfPlayer; //玩家手牌

    //定义有参的构造方法对变量进行初始化
    public Players(String playerId, String playerName) {
        super();
        this.playerId = playerId;
        this.playerName = playerName;
        this.pokersOfPlayer = new ArrayList<>();
    }

    public String getPlayerId() {
        return playerId;
    }
    public void setPlayerId(String playerId) {
        this.playerId = playerId;
    }
    public String getPlayerName() {
        return playerName;
    }
    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public List<Pokers> getPokersOfPlayer() {
        return pokersOfPlayer;
    }

    public void setPokersOfPlayer(List<Pokers> pokersOfPlayer) {
        this.pokersOfPlayer = pokersOfPlayer;
    }

    //玩家添加一张扑克牌
    public void add(Pokers p) {
        this.pokersOfPlayer.add(p);
    }

    //判断玩家ID不能重复
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((playerId == null) ? 0 : playerId.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;
        Players other = (Players) obj;
        if (playerId == null) {
            if (other.playerId != null)
                return false;
        } else if (!playerId.equals(other.playerId))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "ID是" + playerId + "的玩家" + playerName ;
    }

    //对玩家的手牌进行排序,调用sort()方法时自动调用compareto()方法
    public void sort() {
        Collections.sort(this.pokersOfPlayer);
    }
}
package com.imooc;

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

public class PlayPoker {
    public static void main(String[] args) {

        PlayPoker playPoker = new PlayPoker();
        Scanner sc = new Scanner(System.in);
        System.out.println("------------------欢迎来到简易扑克牌游戏系统!------------------");
        System.out.println("输入1进入游戏,输入0退出系统");
        int choice = sc.nextInt();
        if (choice == 1) {
            playPoker.rule();
            playPoker.addPlayers();
            playPoker.newPokers();
            playPoker.shuffle();
            playPoker.show();
            playPoker.deal();
            playPoker.result();
            playPoker.showPokersOfPlayers();
        }else {
            System.out.println("已退出系统!");
                 System.exit(0);
        }
    }

    private List<Players> players;//存放玩家
    private List<Pokers> newPokers;//存放新扑克牌
    private List<Pokers> reNewPokers;//存放洗好的扑克牌

    //游戏规则
    public void rule () {
        System.out.println("-------------------------游戏规则---------------------------------");
        System.out.println("创建两名玩家,分别输入其ID和姓名,随机给两名玩家分配两张扑克牌,比较大小,然后得出胜负。");
        System.out.println("----------------------------------------------------------");
    }

    //创建玩家
    public void addPlayers() {
        Scanner sc = new Scanner(System.in);
        String playerId;
        String playerName;
        boolean flag = false;
        this.players =new ArrayList<Players>(); //存储玩家的list
            for(int i = 0 ; i < 2 ; i ++) {
                while (true) {
                System.out.println("请输入第" + (i + 1) + "名玩家的ID:");
                playerId = sc.next();
                //遍历players
                for(Players player:players) {
                    //ID是否存在
                    if (player.getPlayerId().equals(playerId)) {
                        flag = true;
                        break;
                    }
                }   
                    if (flag) {
                        flag = false;
                        System.out.println("玩家ID为" + playerId + "已经存在,请重新输入新ID。");
                        continue;
                    }else
                        break;
                }
                    System.out.println("请输入第" + (i + 1) + "名玩家的姓名:");
                    playerName =sc.next();
                    //创建玩家并添加到players中
                    Players player = new Players(playerId,playerName);
                    this.players.add(player);
                    System.out.println("ID为" + playerId + "姓名为" + playerName + "的玩家已经添加成功!");
            }

    }

    //创建一副扑克牌
    public void newPokers() {
        //创建一个list用于存放新扑克牌
        this.newPokers = new ArrayList<Pokers>();

        for(int i = 0 ; i < Pokers.colors.length ; i ++ ) {
            for (int j = 0 ; j <Pokers.points.length ; j ++ ) {
                this.newPokers.add(new Pokers(i,j));
            }
        }
    }

    //洗牌
    public void shuffle() {
        System.out.println("----------------------开始洗牌----------------------");
        Random random = new Random();
        this.reNewPokers = new ArrayList<Pokers>();
        for (int i = 0 ; i < this.newPokers.size() ; i ++ ) {
            while (true) {
            int index = random.nextInt(newPokers.size());
            Pokers poker = this.newPokers.get(index);//poker是随机产生的牌
            if (this.reNewPokers.contains(poker)) {
                continue;
            }else {
                this.reNewPokers.add(poker);//把随机产生的扑克牌添加到存放洗好的扑克牌list
                break;
            }
            }
        }
        System.out.println("----------------------结束洗牌----------------------");
    }

    //洗牌后的情况
    public void show() {
        for(Pokers poker:reNewPokers) {
            System.out.println(poker.toString());
        }
    }

    //发牌
    public void deal () {
        System.out.println("----------------------开始发牌----------------------");
        //发第一张牌的索引
        int k = 1 ;
        //遍历两位玩家的list
        for (int i = 0 ; i < 2 ; i ++) {
            //遍历两位玩家手牌的list
            for (int j = 0 ; j < 2 ; j ++) {
                this.players.get(j).add(this.reNewPokers.get(k-1));
                k ++;
            }
        }
        System.out.println("----------------------发牌结束----------------------");
    }

    //显示赢家
    public void result() {
        Pokers maxPoker = new Pokers(0,0);
        Players winner = null;
        for ( Players player : players) {
            player.sort();
            //排序后最后一张牌为最大的扑克牌
            if (player.getPokersOfPlayer().get(1).compareTo(maxPoker) == 1) {
                maxPoker = player.getPokersOfPlayer().get(1);
                winner = player;
            }
        }
        System.out.println("赢家是" + winner + "!");
        System.out.println("最大的扑克牌是" + maxPoker);
    }

    //显示玩家的手牌
    public void showPokersOfPlayers() {
        for (Players player : players) {
            //打印出玩家信息
            System.out.println(player.toString() + "手牌为:");
            for (Pokers poker : player.getPokersOfPlayer()) {
                //打印出玩家手牌
                System.out.println( poker.toString());
            }
        }
    }
}
0人推荐
随时随地看视频
慕课网APP