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

简易扑克牌游戏(Java)

标签:
Java
package lu.zhang;

/**
 * 纸牌类,单个纸牌
 * @author Administrator
 *
 */
public class Card implements Comparable<Card>{  
    public String suit;     //花色属性
    public String points;   //点数属性
    public int weight;  //大小属性
    //无参构造器
    public Card(){

    }
    //有参构造器
    public Card(String suit,String points,int weight){
        this.suit=suit;
        this.points=points;
        this.weight=weight;
    }

    @Override
    public int compareTo(Card o) {
        // TODO Auto-generated method stub
        if (this.weight>o.weight)
            return 1;
        else if (this.weight==o.weight) return 0;
        else return -1;

    }
}
package lu.zhang;

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

/**
 * 纸牌的ArrayList
 * @author Administrator
 *
 */
public class CardList {

    List<Card> cardList=new ArrayList<Card>();
    List<Card> cardListRandom=new ArrayList<Card>();
    public void creatPoker(){
        System.out.println("-------------欢迎来到纸牌游戏-------------");
        System.out.println("创建扑克牌...");
        String[] strAry1=new String[]{"黑桃","红桃","梅花","方片"}; 
        String[] strAry2=new String[]{"2","3","4","5","6","7","8","9","10","A","J","Q","K"};
        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                this.cardList.add(new Card(strAry1[i],strAry2[j],(10*(j+1)+i)));
            }
        }
        System.out.println("扑克牌创建成功!");
        System.out.print("扑克牌为:"+"[");
        for (Card card : cardList) {
            System.out.print(card.suit+card.points+" ");
        }
        System.out.println("]");
    }

    public void shuffleCards(){
        System.out.println("正在洗牌...");
        Random random=new Random();
        Integer randInt;
        List<Integer> randIntList=new ArrayList<Integer>();
        for(int i=0;i<52;i++){
            do{
                randInt=random.nextInt(52);
            }while(randIntList.contains(randInt));
            randIntList.add(randInt);
            this.cardListRandom.add(cardList.get(randInt));
        }
        System.out.println("洗牌完毕!");

    }

}
package lu.zhang;

import java.util.ArrayList;
import java.util.List;
/**
 * 玩家类
 * @author Administrator
 *
 */
public class Player {
    public int id;  //玩家id
    public String name; //玩家名字
    public List<Card> cards=new ArrayList<Card>();  //玩家的手牌List

    //有参构造器
    public Player(int id,String name){
        this.id=id;
        this.name=name;
    }
    //无参构造器
    public Player(){

    }
}
package lu.zhang;

import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;

public class PlayGame {

    public Player player1;
    public Player player2;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CardList cl=new CardList();
        //创建一副牌
        cl.creatPoker();
        //洗牌
        cl.shuffleCards();
        //输入玩家信息
        PlayGame playgame=new PlayGame();
        playgame.playerLoad();
        System.out.println("欢迎玩家:"+playgame.player1.name);
        System.out.println("欢迎玩家:"+playgame.player2.name);
        //发牌
        System.out.println("--------------开始发牌--------------");
        playgame.player1.cards.add(cl.cardListRandom.get(0));
        playgame.player2.cards.add(cl.cardListRandom.get(1));
        playgame.player1.cards.add(cl.cardListRandom.get(2));
        playgame.player2.cards.add(cl.cardListRandom.get(3));
        System.out.println("开始游戏...");
        //比牌
        playgame.compareCards();
    }

    //开始游戏,输入玩家信息
    public void playerLoad(){
        this.player1=new Player();
        this.player2=new Player();
        Scanner input=new Scanner(System.in);
        System.out.println("请输入第一位玩家的ID和姓名:");
        while(true){
            System.out.println("输入ID:");
            try{
                int tempId=input.nextInt();
                this.player1.id=tempId;
                break;
            }
            catch(InputMismatchException e){
                System.out.println("您输入的ID有误,请检查后重新输入");
            }
        }
        System.out.println("输入姓名:");
        String tempStr=input.next();
        this.player1.name=tempStr;

        System.out.println("请输入第二位玩家的ID和姓名:");
        while(true){
            System.out.println("输入ID:");
            try{
                int tempId=input.nextInt();
                this.player2.id=tempId;
                break;
            }
            catch(Exception e){
                System.out.println("您输入的ID有误,请检查后重新输入");
            }
        }
        System.out.println("输入姓名:");
        tempStr=input.next();
        this.player2.name=tempStr;

    }

    //比较玩家手中牌的大小,进行游戏
    public void compareCards(){

        Collections.sort(this.player1.cards);
        Collections.sort(this.player2.cards);
        System.out.println("玩家"+this.player1.name+"的最大牌是:"+this.player1.cards.get(1).suit+this.player1.cards.get(1).points);
        System.out.println("玩家"+this.player2.name+"的最大牌是:"+this.player2.cards.get(1).suit+this.player2.cards.get(1).points);
        System.out.println();
        if(this.player1.cards.get(1).compareTo(this.player2.cards.get(1))>0){
            System.out.println("-----------玩家:"+this.player1.name+"获胜!------------");
        }else if(this.player1.cards.get(1).compareTo(this.player2.cards.get(1))==0)
            System.out.println("-----------平局----------");
            else System.out.println("-----------玩家:"+this.player2.name+"获胜!------------");
        System.out.println(this.player1.name+"的手牌:["+this.player1.cards.get(0).suit+this.player1.cards.get(0).points+", "+this.player1.cards.get(1).suit+this.player1.cards.get(1).points+"]");
        System.out.println(this.player2.name+"的手牌:["+this.player2.cards.get(0).suit+this.player2.cards.get(0).points+", "+this.player2.cards.get(1).suit+this.player2.cards.get(1).points+"]");
        System.out.println("游戏结束");

    }

}
er1.cards.get(0).suit+this.player1.cards.get(0).points+", "+this.player1.cards.get(1).suit+this.player1.cards.get(1).points+"]");
        System.out.println(this.player2.name+"的手牌:["+this.player2.cards.get(0).suit+this.player2.cards.get(0).points+", "+this.player2.cards.get(1).suit+this.player2.cards.get(1).points+"]");
        System.out.println("游戏结束");

    }

}

控制台效果:
-------------欢迎来到纸牌游戏-------------
创建扑克牌...
扑克牌创建成功!
扑克牌为:[黑桃2 黑桃3 黑桃4 黑桃5 黑桃6 黑桃7 黑桃8 黑桃9 黑桃10 黑桃A 黑桃J 黑桃Q 黑桃K 红桃2 红桃3 红桃4 红桃5 红桃6 红桃7 红桃8 红桃9 红桃10 红桃A 红桃J 红桃Q 红桃K 梅花2 梅花3 梅花4 梅花5 梅花6 梅花7 梅花8 梅花9 梅花10 梅花A 梅花J 梅花Q 梅花K 方片2 方片3 方片4 方片5 方片6 方片7 方片8 方片9 方片10 方片A 方片J 方片Q 方片K ]
正在洗牌...
洗牌完毕!
请输入第一位玩家的ID和姓名:
输入ID:
01
输入姓名:
小兔子
请输入第二位玩家的ID和姓名:
输入ID:
02
输入姓名:
小松鼠
欢迎玩家:小兔子
欢迎玩家:小松鼠
--------------开始发牌--------------
开始游戏...
玩家小兔子的最大牌是:梅花Q
玩家小松鼠的最大牌是:方片9
-----------玩家:小兔子获胜!------------
小兔子的手牌:[梅花3, 梅花Q]
小松鼠的手牌:[梅花8, 方片9]
游戏结束

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消