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

Java入门第三季课程——综合练习

标签:
Java

主函数:

package test;

public class PokerGame {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("--------欢迎来到扑克牌游戏--------");
    System.out.println("-----------创建扑克牌-----------");
    CardList cl = new CardList();
    cl.addCards();
    System.out.println("----------扑克牌创建成功----------");
    System.out.println("------------开始洗牌------------");
    System.out.println("------------洗牌结束------------");

    System.out.println("------------创建玩家------------"); 
    PlayerList pl = new PlayerList();
    pl.addPlayers();
    Player player1 = pl.playersList.get(0);
    Player player2 = pl.playersList.get(1);
    System.out.println("欢迎玩家:"+player1.name);
    System.out.println("欢迎玩家:"+player2.name);

    System.out.println("------------开始发牌------------");
    int[] num = cl.randomCards();
    Card player1Card1 = new Card(cl.cardsList.get(num[0]).kind, cl.cardsList.get(num[0]).number);
    Card player1Card2 = new Card(cl.cardsList.get(num[1]).kind, cl.cardsList.get(num[1]).number);
    Card player2Card1 = new Card(cl.cardsList.get(num[2]).kind, cl.cardsList.get(num[2]).number);
    Card player2Card2 = new Card(cl.cardsList.get(num[3]).kind, cl.cardsList.get(num[3]).number);

    //假装真的在拿牌哈哈蛤
    System.out.println("-----玩家:"+player1.name+"拿牌----");
    System.out.println("-----玩家:"+player2.name+"拿牌----");
    System.out.println("-----玩家:"+player1.name+"拿牌----");
    System.out.println("-----玩家:"+player2.name+"拿牌----");
    System.out.println("------------发牌结束------------");

    System.out.println("------------开始游戏------------");
    //根据已经排好的顺序,比较牌的大小,
    if(num[0] > num[1]){
        System.out.println("玩家"+player1.name+"的最大手牌是"+player1Card1.kind+player1Card1.number);
        if(num[2] > num[3]){
            System.out.println("玩家"+player2.name+"的最大手牌是"+player2Card1.kind+player2Card1.number);
            if(num[0] > num[2]){
                System.out.println("------"+"玩家"+player1.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }else{
                System.out.println("------"+"玩家"+player2.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }
        }else{
            System.out.println("玩家"+player2.name+"的最大手牌是"+player2Card2.kind+player2Card2.number);
            if(num[0] > num[3]){
                System.out.println("------"+"玩家"+player1.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }else{
                System.out.println("------"+"玩家"+player2.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }
        }
    }else{
        System.out.println("玩家"+player1.name+"的最大手牌是"+player1Card2.kind+player1Card2.number);
        if(num[2] > num[3]){
            System.out.println("玩家"+player2.name+"的最大手牌是"+player2Card1.kind+player2Card1.number);
            if(num[1] > num[2]){
                System.out.println("------"+"玩家"+player1.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }else{
                System.out.println("------"+"玩家"+player2.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }
        }else{
            System.out.println("玩家"+player2.name+"的最大手牌是"+player2Card2.kind+player2Card2.number);
            if(num[1] > num[3]){
                System.out.println("------"+"玩家"+player1.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }else{
                System.out.println("------"+"玩家"+player2.name+"获胜"+"------");
                System.out.println("玩家各自的手牌为:");
                System.out.println("玩家"+player1.name+":"+player1Card1.kind+player1Card1.number
                        +"  "+player1Card2.kind+player1Card2.number);
                System.out.println("玩家"+player2.name+":"+player2Card1.kind+player2Card1.number
                        +"  "+player2Card2.kind+player2Card2.number);
            }
        }
    }
}

}


Card类:
package test;

public class Card{

public String kind;
public String number;
public Card(String kind, String number){
    this.kind = kind;
    this.number = number;
}

}


Player类:
package test;

public class Player {

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

}


扑克牌的List类:
package test;

import java.util.*;

public class CardList {

public List<Card> cardsList;

public CardList(){
    this.cardsList = new ArrayList<Card>();
}

public void addCards(){
    //十三种数值 * 四种花色
    String num[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    for(int i=0; i<13; i++){
        cardsList.add(new Card("方块",num[i]));
        cardsList.add(new Card("梅花",num[i]));
        cardsList.add(new Card("红桃",num[i]));
        cardsList.add(new Card("黑桃",num[i]));
    }
}

public void cardsForEach(){
    System.out.println("成功添加如下卡片:");
    for(Card card:cardsList){
        System.out.print(card.kind+card.number+" ");
    }
    System.out.println();
}

//随机抽牌函数
public int[] randomCards(){
    Random random = new Random();
    Set<Integer> cardSet = new HashSet<Integer>();
    int[] num = new int[4];
    for(int i=0; i<4; i++){
        do{
            num[i] = random.nextInt(51);
        }while(cardSet.contains(num[i]));
        cardSet.add(num[i]);
    }
    return num;
}

}


玩家的List类:
package test;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class PlayerList {

public List<Player> playersList;

public PlayerList(){
    this.playersList = new ArrayList<Player>();
}

public void addPlayers(){
    Scanner console = new Scanner(System.in);
    for(int i=1; i<=2; i++){
        boolean x = false;
        do{
            try{
                System.out.println("请输入第"+i+"位玩家ID:");
                Integer id = console.nextInt();
                System.out.println("请输入第"+i+"位玩家称呼:");
                String name = console.next();
                playersList.add(new Player(id,name));
                x = false;
            }catch(InputMismatchException e){
                System.out.println("请输入正整数ID:");
                x = true;
            }
        }while(x);
    }
    console.close();
}

}

点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消