ITPub博客

首页 > IT职业 > IT生活 > JAVA入门第三季综合实战-简易扑克游戏

JAVA入门第三季综合实战-简易扑克游戏

IT生活 作者:sealee 时间:2021-09-09 09:00:08 0 删除 编辑

自己写加参考,解决了抛异常只抛一次的问题 ,使程序更加完整

卡片类

package practice;

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

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

    public String kInd;
    public String count;

    public Card(String kInd,String count){
        this.kInd = kInd;
        this.count = count;
    }

    @Override
    public String toString() {
        return kInd + count;
    }
}

玩家类

package practice;

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

public class Player {
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Player))
            return false;
        Player other = (Player) obj;
        if (id != other.id)
            return false;
        return true;
    }

    public int id;
    public String name;
    List pcard = new ArrayList();

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

    public Player() {
        // TODO Auto-generated constructor stub
    }
}

扑克比较大小

package practice;

import java.util.Comparator;

public class CardCompatator implements Comparator {

    @Override
    public int compare(Card o1, Card o2) {
        // TODO Auto-generated method stub
        //创建从小到大的花色与字符串
        String[] a = {"方片","梅花","红桃","黑桃"};
        String b = "2345678910JQKA";

        //查找字符在字符串中的位置
        int b1 = b.indexOf(o1.count);
        int b2 = b.indexOf(o2.count);
        int a1 = 0;
        int a2 = 0;

        for(int i = 0; i b2){
            return 1;
        }else if(b1 == b2){
            if(a1 > a2){
                return 1;
            }else if(a1 == a2){
                return 0;
            }else{
                return -1;
            }
        }else{
            return -1;
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

主类:

package practice;

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

import com.imooc.Person;

public class GameTest {

    //构造两个数组,用于存放花色与数值
    String[] kind = {"方片","梅花","红桃","黑桃"};
    String[] count = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    //创建一个List存放扑克
    List cards = new ArrayList();
    //创建一个用于存放玩家信息的对象
    List player = new ArrayList();

    //导入Scanner用于输入玩家信息
    Scanner console = new Scanner(System.in);

    /**
     * 通过循环将值存放入List中
     */
    public void CreateCard(){
        for(int i=0;i

图片描述

图片描述

图片描述

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

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

注册时间:2019-08-30

  • 博文量
    82
  • 访问量
    620