ITPub博客

首页 > IT职业 > IT生活 > 简易 poker比大小游戏

简易 poker比大小游戏

IT生活 作者:nighteagle 时间:2021-09-09 08:57:48 0 删除 编辑

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 twoCard;//玩家手中卡牌
    public Player(int i,String n){
        id=i;
        name=n;
        twoCard=new ArrayList();

    }
    /* (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{
    public String color;//卡牌花色
    public String point;//卡牌点数
    public static final List colors= Arrays.asList("方块","梅花","红桃","黑桃");
    public static final List 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 cardList;//用于存储卡牌
    private List playerList;//用于存储玩家组
    private Game(){
        cardList=new ArrayList();
        playerList=new ArrayList();
    }
    /**
     * @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 maxCardList=new ArrayList(); //用于存储每位玩家的最大卡牌
        for(int i=0;i0){
                max=maxCardList.get(z);
                maxNum=z;
            };
        }
        System.out.println("获胜者为"+playerList.get(maxNum).name+"其id为"+playerList.get(maxNum).id);
        //输出每位玩家的卡牌
        for(int a=0;a5 && num 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4363/viewspace-2798221/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    97
  • 访问量
    740