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

JAVA入门第三季项目(简易扑克牌游戏)

标签:
Java
package com.imooc;

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

public class Player {
    private int id;
    private String name;
    private List<Card> handCards;
    public Player(int id,String name){
        this.id=id;
        this.name=name;
        this.handCards=new ArrayList<Card>();
    }   
    public Player(){    
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public List<Card> getHandCards() {
        return handCards;
    }
}

package com.imooc;

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

public class Card implements Comparable<Card>{
  private String color;
  private String value;
  private static final List<String> Colors=Arrays.asList("方片","梅花","红桃","黑桃");
  private static final List<String> Values=Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");
  public Card(String color,String value){
      this.color=color;
      this.value=value;
  }
  public Card(){
      this.color="";
      this.value="";
  }
public String getColor() {
    return color;
}
public String getValue() {
    return value;
}
public int compareTo(Card o) {
    if(Values.indexOf(this.value)==Values.indexOf(o.value))
    return Integer.valueOf(Colors.indexOf(this.color)).compareTo(Integer.valueOf(Colors.indexOf(o.color)));
else
    return Integer.valueOf(Values.indexOf(this.value)).compareTo(Integer.valueOf(Values.indexOf(o.value)));
}
public static List<Card> generateCard(){
    List<Card> poker=new ArrayList<Card>();
    for(int i=0;i<3;i++){
        String color=Colors.get(i);
        for(int j=0;j<13;j++){
            String value=Values.get(j);
            poker.add(new Card(color,value));
        }
    }
    return poker;
}
@Override
public String toString() {
    return color+value;
}

}

package com.imooc;
import java.util.*;

public class PokerGame {
    private List<Card> cards;
    private List<Player> players;
    Scanner scanner;
    private static final int HandCardNum=2;
    private static final int PlayerNum=2;
    public PokerGame(){
        this.cards=Card.generateCard();
        scanner=new Scanner(System.in);
        this.players=new ArrayList<Player>();
    }

public void showCards(){
    System.out.println(cards);
        }

public void createPlayers(){
    int id;
    String name;
    for(int i=0;i<PlayerNum;){
        try{
            System.out.println("创建第"+(i+1)+"位玩家");
            System.out.println("请输入玩家编号:");
            id=scanner.nextInt();
        }

        catch(Exception e){
            System.out.println("请输入正整数:");
            scanner.next();  //吸收缓冲区
            continue;
        }
        System.out.println("请输入玩家姓名:");
        name=scanner.next();
        players.add(new Player(id,name));
        i++;
    }
}

    public void shuffleCards(){
        Collections.shuffle(cards);
    }
    public void showPlayers(){
        for(Player p:players)
        System.out.println("欢迎玩家"+p.getName());
    }
    public void play(){
        System.out.println("-----------------开始发牌----------------");
        for(int i=0;i<HandCardNum*PlayerNum;i++){         
            players.get(i%PlayerNum).getHandCards().add(cards.get(i));
            System.out.println("玩家:"+players.get(i%PlayerNum).getName()+"拿牌");
        }
        System.out.println("-----------------发牌结束----------------");
        System.out.println("-----------------开始游戏----------------");
        int winner=0;
        Card max=new Card();
        for(int i=0;i<PlayerNum;i++){
            Collections.sort(players.get(i).getHandCards());
            Collections.reverse(players.get(i).getHandCards());
            Card maxCard=players.get(i).getHandCards().get(0);
            System.out.println("玩家:"+players.get(i).getName()+"的最大手牌为:"+maxCard);       
        if (max.compareTo(maxCard) < 0) {
            max=maxCard;
            winner = i;
        }
        }
        System.out.println("-------------玩家"+players.get(winner).getName()+"获胜---------");
        System.out.println("玩家各自的手牌为:");
      for (Player p:players){
          System.out.println(p.getName()+":"+p.getHandCards());
      }

    }

    public static void main(String[] args) {
        System.out.println("---------------创建扑克牌------------------");
        PokerGame pg=new PokerGame();
        System.out.println("--------------创建扑克牌成功----------------------");
        pg.showCards();
        System.out.println("--------------开始洗牌----------------------");
        pg.shuffleCards();
        System.out.println("--------------洗牌结束----------------------");
        System.out.println("--------------创建玩家----------------------");
        pg.createPlayers();
        pg.showPlayers();
        pg.play();    
    }

}

参考了很多慕友的代码,希望对大家有帮助
对于初学者来说,每一次写程序真的很痛苦,坚持下来就好,一定要多练,大家一起加油!

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消