手记

简易 poker比大小游戏

Player 类

package com.imooc.model;

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

public class Player {
    public int id;//玩家id
    public String name;//玩家名
    public List<Card> twoCard;//玩家手中卡牌
    public Player(int i,String n){
        id=i;
        name=n;
        twoCard=new ArrayList<Card>();

    }
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Player other = (Player) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}

Card类

package com.imooc.model;

import java.util.Arrays;
import java.util.List;

public class Card implements Comparable<Card>{
    public String color;//卡牌花色
    public String point;//卡牌点数
    public static final List<String> colors= Arrays.asList("方块","梅花","红桃","黑桃");
    public static final List<String> points=Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");
    public Card(String c,String p){
        color=c;
        point=p;
    }
    //用于返回花色及大小
    public String toString(){
        return color+point;
    }
    //利用点数及花色在List中的index值,比较牌面大小,同大小比较花色index
    @Override
    public int compareTo(Card o) {
        if(o.point.equals(point)){
            return Integer.valueOf(colors.indexOf(o.color)).compareTo(Integer.valueOf(colors.indexOf(color))); 
        }
        return Integer.valueOf(points.indexOf(o.point)).compareTo(Integer.valueOf(points.indexOf(point)));
    }

}

Game类

package com.imooc.view;

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

import com.imooc.model.Card;
import com.imooc.model.Player;

public class Game {
    private  int numPlayer;//玩家数量
    private  int numCard;//玩家卡牌数量
    private Scanner in;
    private String[] colors={"方块","梅花","红桃","黑桃"};//花色
    private String[] points={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};//卡牌值
    private List<Card> cardList;//用于存储卡牌
    private List<Player> playerList;//用于存储玩家组
    private Game(){
        cardList=new ArrayList<Card>();
        playerList=new ArrayList<Player>();
    }
    /**
     * @param numPlayer the numPlayer to set
     */
    public void setNumPlayer(int numPlayer) {
        this.numPlayer = numPlayer;
    }
    /**
     * @param numCard the numCard to set
     */
    public void setNumCard(int numCard) {
        this.numCard = numCard;
    }
    /**
     * 创建卡牌
     */
    private void createCards(){
        Card card;
        System.out.println("------开始创建卡牌组------");
        for(int i=0;i<colors.length;i++){
            for(int j=0;j<points.length;j++){
                card=new Card(colors[i],points[j]);
                cardList.add(card);
            }
        }
        System.out.println("--------创建卡牌组成功-----------");
        System.out.println(cardList.toString());
        System.out.println("--------开始洗牌-----------");
        Collections.shuffle(cardList);
        System.out.println("-----洗牌结束------");
    }
    /**
     * 创建玩家
     */
    private void createPlayer(){

        int id;
        for(int i=0;i<numPlayer;i++){
        System.out.println("-----开始创建"+(i+1)+"玩家,请输入玩家id及用户名-----");
        while(true){
            try{
                System.out.println("请输入玩家id");
                id=in.nextInt();

                break;
            }catch(Exception e){
                in.next();
                System.out.println("输入有误,请输入数字");
                continue;
            }
        }
        System.out.println("------请输入玩家用户名---------");
        String name=in.next();
        Player player=new Player(id,name);
        //遍历playerList,判断是否有重复的玩家,如果重复重新输入
        for (Player p : playerList) {
            if (p.equals(player)) {
                i--;
                System.out.println("玩家已存在,请重新输入");
                continue;
            }
        }
        playerList.add(player);
        }
        for (Player p : playerList) {
            System.out.println("欢迎玩家"+p.name+"游戏");
        }
    }
    /**
     * 用于给多位玩家发牌
     */
    private void deal(){
        System.out.println("-------开始发牌-------");
        for(int i=0;i<numPlayer*numCard;i++){
            for(int j=0;j<numPlayer;j++){
                if(i%numPlayer == j){
                    System.out.println("----给"+playerList.get(j).name+"发牌-----");
                    playerList.get(j).twoCard.add(cardList.get(i));
                    break;
            }
            }
        }
        System.out.println("------发牌结束----");
    }
    /**
     * 比较手牌大小
     */
    private void compareCard(){
        int maxNum=0;//存储最大卡牌
        List<Card> maxCardList=new ArrayList<Card>(); //用于存储每位玩家的最大卡牌
        for(int i=0;i<numPlayer;i++){
                Collections.sort(playerList.get(i).twoCard);//对手牌有大到小进行排序
                maxCardList.add(playerList.get(i).twoCard.get(0));//获取最大的手牌添加到最大手牌数组
                System.out.println("玩家"+playerList.get(i).name+"最大手牌为"+playerList.get(i).twoCard.get(0));
        }
        //假设maxCardList中的第一张牌是最大的
        Card max=maxCardList.get(0);
        //循环比较,如果比第一张牌大的,存在max中,记录下序号
        for(int z=1;z<maxCardList.size();z++){
            if(max.compareTo(maxCardList.get(z))>0){
                max=maxCardList.get(z);
                maxNum=z;
            };
        }
        System.out.println("获胜者为"+playerList.get(maxNum).name+"其id为"+playerList.get(maxNum).id);
        //输出每位玩家的卡牌
        for(int a=0;a<numPlayer;a++){
            System.out.println("id为"+playerList.get(a).id+"的"+playerList.get(a).name+"的手牌为");
            System.out.println(playerList.get(a).twoCard.toString());
        }
        System.out.println("游戏结束");
    }
    /**
     * 初始设置
     */
    public void initialGame(){
        System.out.println("游戏开始,请设置玩家数量及每位玩家卡牌数");
        setNum("玩家数",5);
        setNum("卡牌数",5);
        createCards();
        createPlayer();
        deal();
        compareCard();
    }
    /**
     * 用于设置输入玩家数或者卡牌数
     */
    private void setNum(String s,int n){
        in=new Scanner(System.in);
        while(true){
            System.out.println("请输入"+s+"数量,且数量不大于"+n);
            try{
                int num=in.nextInt();
                if(num >5 && num <=0){
                    in.next();
                    System.out.println("输入有误,请输入数字大于0小于"+(n+1));
                }
                if(s.equals("玩家数")){
                    setNumPlayer(num);
                }else{
                    setNumCard(num);
                }
                System.out.println("设置"+s+"成功");
                break;
            }catch(Exception e){
                in.next();
                System.out.println("输入有误,请输入数字");
                continue;
            }
        }
    }
    public static void main(String[] args){
        Game game=new Game();
        game.initialGame();
    }
}
0人推荐
随时随地看视频
慕课网APP