ITPub博客

首页 > IT职业 > IT生活 > JAVA入门第三季结课作业,简易扑克牌游戏

JAVA入门第三季结课作业,简易扑克牌游戏

IT生活 作者:penny_liang 时间:2021-09-09 08:57:58 0 删除 编辑
Brand类 (定义卡牌的属性)
package cn.edu.nuc;

import java.util.Comparator;

/**
 * Created by asus on 2016/11/7.
 */
/*
                功能描述
   1.创建一副扑克牌
   包括4种花色:黑桃,红桃,梅花,方块
   十三中点数:2-10,J,Q,K,A
   2.创建两名玩家
   玩家至少要有ID,姓名,手牌等属性,手牌为扑克牌的集合
   3.洗牌
   将之前创建的“一副扑克牌”打乱顺序
   4.发牌
   讲洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张
   5.游戏
   比较两个人手中的扑克牌,规则为:取两人各自手中点数最大牌进行比较,点数大的赢;
   如果两个人各自的点数相等,则按花色比较
 */
public class Brand implements Comparable{
        private  String  color;
        private  String  point ;

        public String getColor() {
                return color;
        }

        public void setColor(String color) {
                this.color = color;
        }

        public String getPoint() {
                return point;
        }

        public void setPoint(String point) {
                this.point = point;
        }

        public Brand(String color , String point ){
                this.color=color;
                this.point=point;
        }

        @Override
        public int compareTo(Brand o) {
                if (this.getPoint() == o.getPoint()) {
                        if (this.getColor().equals("黑桃")) {
                                return 1;
                        } else if (this.getColor().equals("红桃")) {
                                if (o.getColor().equals("黑桃")) {
                                        return -1;
                                } else {
                                        return 1;
                                }
                        } else if (this.getColor().equals("梅花")) {
                                if (o.getColor().equals("黑桃") || o.getColor().equals("红桃")) {
                                        return -1;
                                } else {
                                        return 1;
                                }
                        } else {
                                return -1;
                        }
                }else {
                        if(this.getPoint().equals("A")){
                                return 1;
                        }else if(this.getPoint().equals("K")){
                                if(o.getPoint().equals("A")){
                                        return -1 ;
                                }else{
                                        return 1 ;
                                }
                        }else if(this.getPoint().equals("Q")){
                                if(o.getPoint().equals("A")||o.getPoint().equals("K")){
                                        return -1;
                                }else{
                                        return 1 ;
                                }
                        }else if(this.getPoint().equals("J")){
                                if(o.getPoint().equals("A")||o.getPoint().equals("K")
                                        ||o.getPoint().equals("Q")){
                                        return -1 ;
                                }else{
                                        return 1 ;
                                }
                        }else if(this.getPoint().equals("10")){
                                if(o.getPoint().equals("A")||o.getPoint().equals("K")
                                        ||o.getPoint().equals("Q")||o.getPoint().equals("J")){
                                        return -1 ;
                                }else{
                                        return 1 ;
                                }
                        }else{
                                return this.getPoint().compareTo(o.getPoint());
                        }
                }

        }
}
Plays类(定义玩家的属性)
 * Created by asus on 2016/11/8.
 */
public class Plays {
    private int  ID ;
    private String name ;
    public  List cards;

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Plays(){

    }
    public Plays(int  ID , String name ){
        this.ID= ID;
        this.name =name ;
        this.cards= new ArrayList();
    }
}
测试类
package cn.edu.nuc;

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

/**
 * Created by asus on 2016/11/8.
 */
public class Test {
    //保存扑克牌的集合
    List cards =new ArrayList();
    //保存玩家的集合
    List playsList = new ArrayList();

    public Test(){

    }
    //创建扑克牌
    private void foundCard(){
        System.out.println("-----------创建扑克牌-------------");
        for (int i= 1 ; i  gameCards = new ArrayList();
        gameCards.addAll(playsList.get(0).cards);
        gameCards.addAll(playsList.get(1).cards);
        Collections.sort(gameCards);
        if(playsList.get(0).cards.contains(gameCards.get(gameCards.size()-1))){
            System.out.println("玩家:"+playsList.get(0).getName()+"获胜");
        }else{
            System.out.println("玩家:"+playsList.get(1).getName()+"获胜");
        }
    }
    public void LookPlaysCards(){
        System.out.println("玩家各自的手牌为:");
        System.out.println(playsList.get(0).getName()+":"+"["+playsList.get(0).cards.get(0).getColor()+playsList.get(0).cards.get(0).getPoint()+"]"
        +"["+playsList.get(0).cards.get(1).getColor()+playsList.get(0).cards.get(1).getPoint()+"]");
        System.out.println(playsList.get(1).getName()+":"+"["+playsList.get(1).cards.get(0).getColor()+playsList.get(1).cards.get(0).getPoint()+"]"
                +"["+playsList.get(1).cards.get(1).getColor()+playsList.get(1).cards.get(1).getPoint()+"]");
    }
    public static void main(String[] args){
            Test cr =new Test();
        cr.foundCard();
        cr.testForEach();
        cr.foundPlays();
        cr.shuffle();
        cr.getCard();
        cr.gameStart();
        cr.LookPlaysCards();
    }
}
  • 结果会不一样
    • 本代码的难点是重写compareTo()方法,我们按照自己的想要的规则定义后就可以进行排序了
    • 尽量以private的形式定义变量,再以Get()andSet()函数调用变量

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

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

注册时间:2019-04-12

  • 博文量
    189
  • 访问量
    1736