手记

JAVA入门第三季:简易扑克牌

Card类

public class Card {

public String color;

public String number;

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

public Card(){

}

@Override
public String toString() {
    return "Card [color=" + color + ", number=" + number + "]";
}

}

GamePlayer类

import java.util.ArrayList;

public class GamePlayer {

public String id ; 

public String name;

public ArrayList<Card> handCard ;

public GamePlayer(String id, String name){

    this.id = id;

    this.name = name;

    this.handCard = new ArrayList<Card>();
}

}

比较类

import java.util.Comparator;

public class CardComparator implements Comparator<Card> {
@Override
public int compare(Card o1, Card o2) {

if(o1.number==o2.number){
return o1.color.compareTo(o2.color);
}else {
return o1.number.compareTo(o2.number); }
}
}

测试类

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

public class GameTest {

public List<Card> cardList;
public List<GamePlayer> playersList;
public List<Card> player1;
public List<Card> player2;
Scanner console = new Scanner(System.in);

public GameTest(){
    this.cardList = new ArrayList<Card>();
    this.playersList = new ArrayList<GamePlayer>();
    this.player1 = new ArrayList<Card>();
    this.player2 = new ArrayList<Card>();
}

/**
 * 创建扑克牌集合
 * @param args
 */

public void creatCard(){
    String[] color = {"红桃","黑桃","方片","梅花"};
    String[] number = {"1","2","3","4","5","6","7","8","9","J","Q","K","A"};
    //创建扑克牌
    System.out.println("------创建扑克牌--------");
    for(int i = 0 ; i< 4 ; i++){
        for(int j=0; j<13 ; j++){
            cardList.add(new Card(color[i],number[j]));
        }
    }
    System.out.println("--------扑克牌创建成功-----------");
    System.out.println("添加了扑克牌:");
    for (Card string : cardList) {
        System.out.println(string);
    }
    Collections.shuffle(cardList);
    System.out.println("------洗牌成功-------");
}

/**
 * 创建用户
 * @param args
 */

public void creatPlayer(){
    System.out.println("-------创建玩家-------");
    System.out.println("请输入第一位玩家的ID和姓名:");
    System.out.println("输入ID:");
    String id1 = console.next();
    System.out.println("输入姓名:");
    String name1 = console.next();
    GamePlayer gp1 = new GamePlayer(id1,name1);
    System.out.println("请输入第二"
            + "位玩家的ID和姓名:");
    System.out.println("输入ID:");
    String id2 = console.next();
    System.out.println("输入姓名:");
    String name2 = console.next();
    GamePlayer gp2 = new GamePlayer(id2,name2);
    System.out.println("------欢迎玩家:"+gp1.name);
    System.out.println("------欢迎玩家:"+gp2.name);
    playersList.add(gp1);
    playersList.add(gp2);
}

/**
 * 发牌
 * @param args
 */
public void dealCard(){
    System.out.println("-------开始发牌------");
    int j = 0;
    for(int i = 1;i<3; i++){
        System.out.println("----玩家:"+playersList.get(0).name+"拿牌");
        playersList.get(0).handCard.add(cardList.get(j));
        j++;
        System.out.println("----玩家:"+playersList.get(1).name+"拿牌");
        playersList.get(1).handCard.add(cardList.get(j));
        j++;
    }
    System.out.println("-------发牌结束-------");
}

/**
 * 开始比较
 * @param args
 */
public void Compare(){
    CardComparator cardComparator =new CardComparator();
    player1.add(playersList.get(0).handCard.get(0));
    player1.add(playersList.get(0).handCard.get(1));
    player2.add(playersList.get(1).handCard.get(0));
    player2.add(playersList.get(1).handCard.get(1));
    //找出第一个玩家最大的牌
    int a = cardComparator.compare(player1.get(0), player1.get(1));
    Card aCard = null;
    if(a == 1){
        aCard = player1.get(0);
    }else {
        aCard = player1.get(1);
    }
    System.out.println("玩家:"+playersList.get(0).name+"最大的手牌是:"+aCard);
    //找出第二个玩家最大的牌
    int b = cardComparator.compare(player2.get(0), player2.get(1));
    Card bCard = null;
    if(b == 1){
        bCard = player2.get(0);
    }else {
        bCard = player2.get(1);
    }
    System.out.println("玩家:"+playersList.get(1).name+"最大的手牌是:"+bCard);
    //比较两个玩家最大的牌的大小
    int result = cardComparator.compare(aCard, bCard);
    if(result == 1){
        System.out.println("------玩家:"+playersList.get(0).name+"获胜-------");
    }
    if(result == 0){
        System.out.println(playersList.get(0).name+"和"+playersList.get(1).name+"大小相同");
    }else{
        System.out.println("------玩家:"+playersList.get(1).name+"获胜-------");
    }
}

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    GameTest gt = new GameTest();
    gt.creatCard();
    gt.creatPlayer();
    gt.dealCard();
    gt.Compare();

}

}

0人推荐
随时随地看视频
慕课网APP