继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第三季easy卡牌游戏代码

小星星来也
关注TA
已关注
手记 2
粉丝 6
获赞 48

/
我自己写的有些不完善,但是运行结果并未发现错误之处,希望大家给点意见0.0
/
创建Card类并执行Compable接口,改写compareTo方法

package com.cards;

public class Card implements Comparable<Card>{
    String type;
    String num;
    public Card(String type,String num){
        this.type=type;
        this.num=num;
    }
    public Card(){

    }

    @Override
    public int compareTo(Card o) {
        // TODO Auto-generated method stub
        if(this.num!=o.num&&this.num!="A"&&o.num!="A")
            return this.num.compareTo(o.num);
        if(this.num.toCharArray()[0]=='A'&&o.num.toCharArray()[0]!='A')
            return 1;
        if(this.num.toCharArray()[0]!='A'&&o.num.toCharArray()[0]=='A')
            return -1;
        if(this.num==o.num){
            //我不知道怎么判断花型的大小,但是程序运行结果显示大小顺序没有错:方块<梅花<红桃<黑桃
            return this.type.compareTo(o.type);
        }
        return 0;   
    }
}

创建Player类

package com.cards;

public class Player {
    int ID;
    String name;
    public Player(int ID,String name){
        this.ID=ID;
        this.name=name;
    }
    public Player(){

    }
}

创建StartGame类

package com.cards;

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

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StartGame sg = new StartGame();
        sg.createCards();
        sg.shuffle();
        sg.addPlayer();
        sg.getCardsAndPlay();
    }

}

在StartGame类中创建了如下方法:

public void createCards(){
        System.out.println("----------创建扑克牌----------");

        for(int i=0;i<4;i++){

            for(int j=2;j<11;j++){
                cardsList.add(new Card(type[i],j+""));
            }
            cardsList.add(new Card(type[i],"J"));
            cardsList.add(new Card(type[i],"Q"));
            cardsList.add(new Card(type[i],"K"));
            cardsList.add(new Card(type[i],"A"));
        }

        System.out.println("--------扑克牌创建成功!---------");
        for(Card card:cardsList){
            System.out.print(card.type+card.num+",");
        }
    }
public void shuffle(){
        System.out.println("----------开始洗牌----------");
        Random random = new Random();
        for(int i=0;i<52;i++){
        int a = random.nextInt(52);
        Card carda = cardsList.get(a);
        Card card0 = cardsList.get(0);
        //Card temp = card0;
        //card0 =carda;
        //carda = temp;
        cardsList.set(a, card0);
        cardsList.set(0, carda);
        }
        System.out.println("----------洗牌完成----------");
    }
public void addPlayer(){
        System.out.println("----------创建玩家----------");
        System.out.println("请输入第一位玩家的ID和姓名:");
        int id = 0;
        while(true){
        try{System.out.println("输入ID:");
            id = input.nextInt();
            break;
        }catch(Exception e){
            System.out.println("请输入一个整数类型的数字作为ID");
            input = new Scanner(System.in); //防止死循环!    
            }
        }
        System.out.println("输入姓名:");
        String name = input.next();
        Player A = new Player(id,name);
        AA = A;
        System.out.println("请输入第二位玩家的ID和姓名:");
        int id2 = 0;
        while(true){
        try{System.out.println("输入ID:");
            id2 = input.nextInt();
            break;
        }catch(Exception e){
            System.out.println("请输入一个整数类型的数字作为ID");
            input = new Scanner(System.in);//防止死循环!
        }
        }
        System.out.println("输入姓名:");
        String name2 = input.next();
        Player B = new Player(id2,name2);
        BB =B;
    }
public void getCardsAndPlay(){

        System.out.println("欢迎玩家:"+AA.name);
        System.out.println("欢迎玩家:"+BB.name);
        System.out.println("----------开始发牌----------");
        System.out.println("玩家:"+AA.name+"拿牌");
        Card A1 = cardsList.get(0);

        System.out.println("玩家:"+BB.name+"拿牌");
        Card B1 = cardsList.get(1);
        System.out.println("玩家:"+AA.name+"拿牌");
        Card A2 = cardsList.get(2);

        System.out.println("玩家:"+BB.name+"拿牌");
        Card B2 = cardsList.get(3);
        System.out.println("----------发牌结束----------");
        System.out.println("----------开始游戏----------");
        System.out.println("玩家:"+AA.name+"的手牌为:"+A1.type+A1.num+";"+A2.type+A2.num);
        System.out.println("玩家:"+BB.name+"的手牌为:"+B1.type+B1.num+";"+B2.type+B2.num);

        Card biggerA = new Card();
        Card biggerB = new Card();
        if(A1.compareTo(A2)>0){
            biggerA = A1;
        }else{
            biggerA = A2;
        }
        if(B1.compareTo(B2)>0){
            biggerB = B1;
        }else{
            biggerB = B2;
        }
        if(biggerA.compareTo(biggerB)>0){
            System.out.println("获胜者为:"+AA.name+"其最大手牌是:"+biggerA.type+biggerA.num);
        }else{
            System.out.println("获胜者为:"+BB.name+"其最大手牌是:"+biggerB.type+biggerB.num);
        }
    }
打开App,阅读手记
8人推荐
发表评论
随时随地看视频慕课网APP