为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA第三季 简易扑克牌游戏

标签:
Java

/*
花了2个小时时间写的代码,有什么错误,欢迎指出
**/

//创建卡牌类

public class Card implements Comparable<Card>{
public int id;//大小属性
public int huasesort; //花色属性的隐藏大小属性
public String huase;//花色属性
public String name;//点数属性
//无参构造器
public Card(){}
//有参构造器
public Card(int id,String huase,String name){
this.id=id;
this.huase=huase;
this.name=name;
//创建实例并给它传入花色时,分配整数型的大小顺序
switch(huase){
case "方块":huasesort=1;break;
case "梅花":huasesort=2;break;
case "红心":huasesort=3;break;
case "黑桃":huasesort=4;break;
}
}
@Override
public int compareTo(Card o) {
// TODO Auto-generated method stub
Integer int1=new Integer(this.id);
Integer int2=new Integer(o.id);
return int1.compareTo(int2);
}
}

//创建玩家类

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

public class Player {
public int id; //玩家ID
public String name; //玩家姓名
public List<Card> shoupai=new ArrayList(); //玩家手牌
public Player(int id,String name){
this.id=id;
this.name=name;
}
}

//游戏主程序类

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

public class PlayGame {
List<Card> puke1st;//一副扑克牌
Player player1;//玩家1
Player player2;//玩家2

Scanner console=new Scanner(System.in);
//创建一副扑克牌
public void creatPuke(){
    //创建方块牌
    puke1st=new ArrayList();
    for(int i=1;i<=13;i++){
        if(i>=2&&i<=10){
            puke1st.add(new Card(i,"方块",i+""));
        }else if(i==1){puke1st.add(new Card(i,"方块","A"));}
        else if(i==11)
        {puke1st.add(new Card(i,"方块","J"));}
        else if(i==12){puke1st.add(new Card(i,"方块","Q"));}
        else if(i==13){puke1st.add(new Card(i,"方块","k"));}
        }
    //创建梅花牌
    for(int i=1;i<=13;i++){
        if(i>=2&&i<=10){
            puke1st.add(new Card(i,"梅花",i+""));
        }else if(i==1){puke1st.add(new Card(i,"梅花","A"));}
        else if(i==11)
        {puke1st.add(new Card(i,"梅花","J"));}
        else if(i==12){puke1st.add(new Card(i,"梅花","Q"));}
        else if(i==13){puke1st.add(new Card(i,"梅花","k"));}
        }
    //创建红心牌
    for(int i=1;i<=13;i++){
        if(i>=2&&i<=10){
            puke1st.add(new Card(i,"红心",i+""));
        }else if(i==1){puke1st.add(new Card(i,"红心","A"));}
        else if(i==11)
        {puke1st.add(new Card(i,"红心","J"));}
        else if(i==12){puke1st.add(new Card(i,"红心","Q"));}
        else if(i==13){puke1st.add(new Card(i,"红心","k"));}
        }
    //创建黑桃牌
    for(int i=1;i<=13;i++){
        if(i>=2&&i<=10){
            puke1st.add(new Card(i,"黑桃",i+""));
        }else if(i==1){puke1st.add(new Card(i,"黑桃","A"));}
        else if(i==11)
        {puke1st.add(new Card(i,"黑桃","J"));}
        else if(i==12){puke1st.add(new Card(i,"黑桃","Q"));}
        else if(i==13){puke1st.add(new Card(i,"黑桃","k"));}
        }

System.out.println("已创建好一副牌");

    }

//对牌进行遍历打印输出
public void print(){
for(Card c:puke1st){
    System.out.print(c.huase+c.name+" ");
        }   
System.out.println();
}

//洗牌
public void xipai(){
    Collections.shuffle(puke1st);
    System.out.println("洗牌已完成!");
}

//创建2个玩家对象
public void creat2Players(){
    System.out.println("请输入第一个玩家ID:");
    int id1=console.nextInt();
    System.out.println("请输入第一个玩家姓名:");
    String name1=console.next();
     player1=new Player(id1,name1);
    System.out.println("玩家1已创建成功");

    System.out.println("请输入第二个玩家ID:");
    int id2=console.nextInt();
    System.out.println("请输入第二个玩家姓名:");
    String name2=console.next();
     player2=new Player(id2,name2);
    System.out.println("玩家2已创建成功");

}

//分别抽2张牌给玩家
public void choupai(){
    System.out.println("---开始抽牌---");
    this.player1.shoupai.add(puke1st.get(0));
    this.player2.shoupai.add(puke1st.get(1));
    this.player1.shoupai.add(puke1st.get(2));
    this.player2.shoupai.add(puke1st.get(3));
    System.out.println("---抽牌结束---");
}

//判定输赢
public void winOrLose(){
    //给玩家的手牌进行排序
    Collections.sort(player1.shoupai);
    Collections.sort(player2.shoupai);
    //抽出各玩家最大的牌
    Card player1card=player1.shoupai.get(1);
    Card player2card=player2.shoupai.get(1);
    if(player1card.id>player2card.id)
    {System.out.println("玩家"+player1.name+"获胜!");}
    else if(player1card.id<player2card.id){
        System.out.println("玩家"+player2.name+"获胜!");
    }else {
        //如果数值相等,通过花色判定输赢
        if(player1card.huasesort>player2card.huasesort){
        System.out.println("玩家"+player1.name+"获胜!");    
        }else if(player1card.huasesort<player2card.huasesort){
        System.out.println("玩家"+player2.name+"获胜!");        
        }else 
            System.out.println("平局!!!");
    }

    //对各玩家的牌进行遍历输出
System.out.println(player1.name+"的手牌如下:");
for(Card c:player1.shoupai){
    System.out.println(c.huase+" "+c.name);
}
System.out.println(player2.name+"的手牌如下:");
for(Card c:player2.shoupai){
    System.out.println(c.huase+" "+c.name);
}
}

public static void main(String[] args) {
    PlayGame play=new PlayGame();
    play.creatPuke();
    play.print();
    play.xipai();
    play.creat2Players();
    play.choupai();
    play.winOrLose();

}

}

点击查看更多内容
6人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Linux系统工程师
手记
粉丝
3
获赞与收藏
12

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消