亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消