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

java程序:简易扑克牌游戏

qq__9987
关注TA
已关注
手记 4
粉丝 5
获赞 12
package lun.cardgame;
//单张扑克牌模板
public class Card implements Comparable<Card>{
      String cardColor;
      String cardPoints;
      String color="'方块''梅花''黑桃''红桃'";
      String points="345678910JQKA2";
      public Card(String cardColor,String cardPoints){
          this.cardColor=cardColor;
          this.cardPoints=cardPoints;
      }
      public Card(){

      }
    @Override
    public int compareTo(Card o) {
        // TODO Auto-generated method stub
        if(points.indexOf(this.cardPoints)==points.indexOf(o.cardPoints)){
            if(color.indexOf(this.cardColor)==color.indexOf(o.cardColor))
               return 0;
            else
                return color.indexOf(this.cardColor)-color.indexOf(o.cardColor);
        }
        else
            return points.indexOf(this.cardPoints)-points.indexOf(o.cardPoints);
    }//比较Card类型大小
}

package lun.cardgame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
//存放扑克牌
public class Cards {
       public List<Card> cards;
       String[] colorAll={"黑桃","红桃","梅花","方块"};
       String[] pointsAll={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
       public Cards(){
           this.cards=new ArrayList<Card>();
       }
       //创建扑克牌
       public void build(){
           System.out.println("开始创建扑克牌");
           for(String s1:colorAll){
               for(String s2:pointsAll ){
                   Card c=new Card();
                  c.cardColor=s1;c.cardPoints=s2;
                  cards.add(c);
               }
           }
           System.out.println("********");
           System.out.println("********");
           System.out.println("创建完毕");
       }
       //输出创建好的扑克牌
       public void display(){
           System.out.println("打印输出创建好的扑克牌");
           for(Card c:cards){
               System.out.print("["+c.cardColor+c.cardPoints+"]"+" ");
           }
       }
       //洗牌
       public void shuffle(){
           System.out.println();
           System.out.println("开始洗牌!");
         Collections.shuffle(cards);
         System.out.println("*******");
        System.out.println("洗牌结束!");

}
}

package lun.cardgame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
//玩家模板构建
public class Player {
       int playerId;      
       String playerName;
       List<Card> handCard;
       Scanner input=new Scanner(System.in);
       public Player(int playerId,String playerName){
           this.playerId=playerId;
           this.playerName=playerName;
           this.handCard=new ArrayList<Card>();
       }
       public Player(){
           this.handCard=new ArrayList<Card>();
       }

       //输入玩家id和姓名
       public void inputPlayer(){
       while(true){
       try{    

           System.out.println("输入玩家id");
           int id=input.nextInt();
           System.out.println("输入玩家姓名");
           String name=input.next();
           playerId=id;
           playerName=name;
           break;
       }catch(Exception e){

           String s=input.nextLine();//清楚缓存区非int型数据
           System.out.println("请输入整型数据!");
       }
       }
       }

       //发一张到玩家手上
       public void addCard(Card e){
           handCard.add(e);
       }
       //展示玩家手上的牌
       public void showHandCard(){
           System.out.println(playerName+"手中的牌:");
           for(Card c:handCard){
               System.out.print("["+c.cardColor+c.cardPoints+"]"+" ");
           }
       }
       //将玩家手上的牌从小到大排序,方便比较大小
       public void handCardSort(){
           Collections.sort(handCard);
       }

}

package lun.cardgame;

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

import org.omg.CORBA.SystemException;

public class Startplay {

    public static void main(String[] args){
        Cards c=new Cards();
        c.build();//创建扑克牌
        c.display();//打印输出整幅扑克牌
        c.shuffle();//洗牌
        Player p1=new Player();
        Player p2=new Player();
        p1.inputPlayer();//输入一号玩家ID和姓名
        p2.inputPlayer();//输入二号玩家ID和姓名
        System.out.println("开始发牌");//开始发牌
        for(int i=0;i<4;i++){
            p1.addCard(c.cards.get(i));
            System.out.println("一号玩家"+p1.playerName+"拿牌!");
            i++;
            p2.addCard(c.cards.get(i));
            System.out.println("二号玩家"+p2.playerName+"拿牌");
        }
        System.out.println("发牌结束");//发牌完毕
        p1.handCardSort();//调整一号玩家手牌顺序,从小到大
        p2.handCardSort();//调整二号玩家手牌顺序,从小到大       
        p1.showHandCard();//亮出一号玩家手中的牌
        System.out.println();//回车
        p2.showHandCard();//亮出二号玩家手中的牌
        System.out.println();//回车
        Card p1max=p1.handCard.get(1);
        Card p2max=p2.handCard.get(1);
        List<Card> cardCompare=new ArrayList<Card>();
        cardCompare.add(p1max);
        cardCompare.add(p2max);
        Collections.sort(cardCompare);//比较大小,调整顺序,从小到大
        if(cardCompare.get(1).equals(p1max))
            System.out.println("一号玩家:"+p1.playerName+"胜出");
        else
            System.out.println("二号玩家:"+p2.playerName+"胜出");
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP