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

集合、异常综合应用之简易扑克牌游戏

慕粉1617316290
关注TA
已关注
手记 5
粉丝 5
获赞 2

package imooc.poker;

public class Card implements Comparable<Card> {
private String shape;
private String number;

public Card(){
    this("","");
}

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

@Override
public String toString() {
    return  shape + number;
}

public String getShape() {
    return shape;
}

public void setShape(String shape) {
    this.shape = shape;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

@Override
public int compareTo(Card o) {
    if(this.number.compareTo(o.getNumber()) > 0){
        return 1;
    }
    else if(this.number.compareTo(o.getNumber()) == 0){
        if(this.shape.compareTo(o.getShape()) > 0){
            return 1;
        }
        else if(this.shape.compareTo(o.getShape()) > 0){
            return -1;
        }
        else{
            return 0;   
        }
    }
    else
        return -1;
}

}
package imooc.poker;

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

public class Player {
private int ID;
private String name;
private List<Card> listcard;

public Player(){
    this(0, "",null);
}

public Player(int id,String name,List<Card> listcard){
    this.ID = id;
    this.name = name;
    this.listcard = new ArrayList<Card>();
}

public String  getName(){
    return name;
}

public List<Card> getListCard(){
    return listcard;
}

public void takeCard(Card card){
    listcard.add(card);

}

}
package imooc.poker;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class SimplePoker {

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    Random random = new Random();
    System.out.println("--------------创建扑克牌---------------");
    List<Card> ListPoker = new ArrayList<Card>();
    Card []cards = {new Card("方片","2"),new Card("方片","3"),new Card("方片","4"),new Card("方片","5"),
            new Card("方片","6"),new Card("方片","7"),new Card("方片","8"),new Card("方片","9"),new Card("方片","10"),
            new Card("方片","J"),new Card("方片","Q"),new Card("方片","K"),new Card("方片","A"),new Card("梅花","2"),
            new Card("梅花","3"),new Card("梅花","4"),new Card("梅花","5"),new Card("梅花","6"),new Card("梅花","7"),
            new Card("梅花","8"),new Card("梅花","9"),new Card("梅花","10"),new Card("梅花","J"),new Card("梅花","Q"),
            new Card("梅花","K"),new Card("梅花","A"),new Card("黑桃","2"),new Card("黑桃","3"),new Card("黑桃","4"),
            new Card("黑桃","5"),new Card("黑桃","6"),new Card("黑桃","7"),new Card("黑桃","8"),new Card("黑桃","9"),
            new Card("黑桃","10"),new Card("黑桃","J"),new Card("黑桃","Q"),new Card("黑桃","K"),new Card("黑桃","A"),
            new Card("红桃","2"),new Card("红桃","3"),new Card("红桃","4"),new Card("红桃","5"),new Card("红桃","6"),
            new Card("红桃","7"),new Card("红桃","8"),new Card("红桃","9"),new Card("红桃","10"),new Card("红桃","J"),
            new Card("红桃","Q"),new Card("红桃","K"),new Card("红桃","A")}; 
    ListPoker.addAll(Arrays.asList(cards));

    System.out.println("-------------扑克牌创建成功--------------");
    System.out.print("扑克牌为:"+"[");
    int i = 0;
    for (Card card : cards) {
        i++;
        System.out.print(card.toString());
        if(i < cards.length){
            System.out.print(",");
        }
    }
    System.out.print("]");

    System.out.println("---------------开始洗牌-----------------");
    System.out.println("---------------洗牌结束-----------------");
    System.out.println("---------------创建玩家-----------------");

    System.out.println("请输入第一位玩家的ID和姓名:");
    int id = 0;
    while(true){
        try{
            System.out.println("输入ID:");
            id = console.nextInt();
        }catch (Exception e) {
            System.out.println("请输入数字来表示ID!");
            String buffer = console.next();
            continue;
        } 
        break;
    }
    System.out.println("输入姓名:");
    String name = console.next();
    Player player1 = new Player(id,name,new ArrayList<Card>());

    System.out.println("请输入第二位玩家的ID和姓名:");
    int id2 = 0;
    while(true){
        try{
            System.out.println("输入ID:");
            id2 = console.nextInt();
            while(id2 == id){
                System.out.println("ID被占用,请重新输入:");
                id2 = console.nextInt();
            }

        }catch (Exception e) {
            System.out.println("请输入数字来表示ID!");
            String buffer = console.next();
            continue;
        } 
        break;
    }
    System.out.println("输入姓名:");
    String name2 = console.next();
    while(name2.equals(name)){
        System.out.println("该姓名已被占用,请重新输入:");
        name2 = console.next();
    }
    Player player2 = new Player(id2,name2,new ArrayList<Card>());

    System.out.println("-------欢迎玩家"+player1.getName());
    System.out.println("-------欢迎玩家"+player2.getName());

    System.out.println("---------------开始发牌--------------");
    System.out.println("-------玩家"+player1.getName()+"拿牌");
    int index1 = random.nextInt(ListPoker.size());
    Card card1 = ListPoker.get(index1);
    player1.takeCard(card1);
    ListPoker.remove(index1);

    System.out.println("-------玩家"+player2.getName()+"拿牌");
    int index2 = random.nextInt(ListPoker.size());
    Card card2 = ListPoker.get(index2);
    player2.takeCard(card2);
    ListPoker.remove(index2);

    System.out.println("-------玩家"+player1.getName()+"拿牌");
    int index3 = random.nextInt(ListPoker.size());
    Card card3 = ListPoker.get(index3);
    player1.takeCard(card3);
    ListPoker.remove(index3);

    System.out.println("-------玩家"+player2.getName()+"拿牌");
    int index4 = random.nextInt(ListPoker.size());
    Card card4 = ListPoker.get(index4);
    player2.takeCard(card4);

    System.out.println("---------------发牌结束--------------");
    System.out.println("---------------开始游戏--------------");

    Collections.sort(player1.getListCard());
    Card c1 = player1.getListCard().get(player1.getListCard().size()-1);
    System.out.println("玩家:"+player1.getName()+"最大的手牌为:"+c1.toString());

    Collections.sort(player2.getListCard());
    Card c2 = player2.getListCard().get(player2.getListCard().size()-1);
    System.out.println("玩家:"+player2.getName()+"最大的手牌为:"+c2.toString());

    if(c2.compareTo(c1) > 0){
        System.out.println("玩家 "+player2.getName()+"获胜!");
    }
    else {
        System.out.println("玩家 "+player1.getName()+"获胜!");
    }

    System.out.println("玩家各自的手牌为:");
    System.out.print(player1.getName()+":"+"[");

    Object []cards0 =  player1.getListCard().toArray();
    int k = 0;
    for (Object card : cards0) {
        k++;
        System.out.print(card.toString());
        if(k < cards0.length){
            System.out.print(",");
        }
    }
    System.out.print("]");
    System.out.println();

    System.out.print(player2.getName()+":"+"[");
    Object []cards1 = player2.getListCard().toArray();

    int j = 0;
    for (Object card : cards1) {
        j++;
        System.out.print(card.toString());
        if(j < cards1.length){
            System.out.print(",");
        }
    }
    System.out.print("]");

}

}
测试效果:
--------------创建扑克牌---------------
-------------扑克牌创建成功--------------
扑克牌为:[方片2,方片3,方片4,方片5,方片6,方片7,方片8,方片9,方片10,方片J,方片Q,方片K,方片A,梅花2,梅花3,梅花4,梅花5,梅花6,梅花7,梅花8,梅花9,梅花10,梅花J,梅花Q,梅花K,梅花A,黑桃2,黑桃3,黑桃4,黑桃5,黑桃6,黑桃7,黑桃8,黑桃9,黑桃10,黑桃J,黑桃Q,黑桃K,黑桃A,红桃2,红桃3,红桃4,红桃5,红桃6,红桃7,红桃8,红桃9,红桃10,红桃J,红桃Q,红桃K,红桃A]---------------开始洗牌-----------------
---------------洗牌结束-----------------
---------------创建玩家-----------------
请输入第一位玩家的ID和姓名:
输入ID:
1
输入姓名:
Jack
请输入第二位玩家的ID和姓名:
输入ID:
2
输入姓名:
Tom
-------欢迎玩家Jack
-------欢迎玩家Tom
---------------开始发牌--------------
-------玩家Jack拿牌
-------玩家Tom拿牌
-------玩家Jack拿牌
-------玩家Tom拿牌
---------------发牌结束--------------
---------------开始游戏--------------
玩家:Jack最大的手牌为:红桃Q
玩家:Tom最大的手牌为:方片8
玩家 Jack获胜!
玩家各自的手牌为:
Jack:[梅花6,红桃Q]
Tom:[黑桃4,方片8]

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP