手记

第三季综合实战(简易扑克牌)

package poker_game;

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

public class Cards implements Comparable<Cards> {

public String color;
public String point;
List<String> colors=Arrays.asList("方片","梅花","红桃","黑桃");
List<String> points=Arrays.asList("2","3","4","5","6","7","8","9",
         "10","J","Q","K","A");
public Cards(String color,String point){
    this.color=color;
    this.point=point;
}

/**

  • @param args
    */
    public Cards(){

    }
    @Override
    public int compareTo(Cards o) {
    // TODO Auto-generated method stub
    if (points.indexOf(this.point) == points.indexOf(o.point))
    return Integer.valueOf(colors.indexOf(this.color)).compareTo
    (Integer.valueOf(colors.indexOf(o.color)));
    else
    return Integer.valueOf(points.indexOf(this.point)).compareTo
    (Integer.valueOf(points.indexOf(o.point)));
    }

}

package poker_game;

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

public class Player {
public int id;
public String name;

List<Cards> FirstplayerCards=new ArrayList<Cards>();
List<Cards> SecondplayerCards=new ArrayList<Cards>();
Scanner scanner;

public Player(int id,String name){
    this.id=id;
    this.name=name;

}

public String getName(){
    return name;
}
public Player() {
    // TODO Auto-generated constructor stub
}

}

package poker_game;

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

public class PokerCards {
Player player=new Player();
private static List<Player> playerList=new ArrayList<Player>();
static List<Cards> cardsList=new ArrayList<Cards>();
String[] colors={"方片","梅花","红桃","黑桃"};
String[] points={"2","3","4","5","6","7","8","9",
"10","J","Q","K","A"};

//创建扑克牌
public void createCards() {
    System.out.println("----------------创建扑克牌----------------");
    for(int i = 0;i < colors.length;i++){
         for(int j = 0;j < points.length;j++){
             cardsList.add(new Cards(colors[i],points[j]));                 
             System.out.print(colors[i]+points[j]+" ");
         }

     }
        System.out.println("\n"+"-------------扑克牌创建成功!--------------");   
        //洗牌
        Collections.shuffle(cardsList);
        System.out.println("--------------洗牌完成--------------");
}

//创建两个玩家
public void creatPlayer(){
    int ID=0;
    String Name;
    Scanner sc=new Scanner(System.in);
        for(int k=1;k<=2;k++){
            System.out.println("请输入第"+k+"位玩家ID:");
            while(true){
                try{
                    sc=new Scanner(System.in);
                    ID=sc.nextInt();
                    break;
                }catch(Exception e){
                    System.out.println("请输入整数");
                    continue;              
                }  
                        }
                System.out.println("请输入第"+k+"位玩家姓名:");
                Name=sc.next();
                playerList.add(new Player(ID,Name));

    }
        //遍历playerlist查看此时系统中有哪些玩家
        for(int i=0;i<playerList.size();i++){
        System.out.println("目前存在的玩家有:"+playerList.get(i).id+
                "  "+playerList.get(i).name);
        }
}

//发牌
public void dealCard(Player p1, Player p2, List<Cards> cardList) {
    int i = 1;

    Iterator<Cards> iterator = cardList.iterator();
    while (iterator.hasNext()) {
        if (i % 2 == 1) {
            System.out.println("玩家" + p1.getName() + "拿牌");
            player.FirstplayerCards.add(iterator.next());
        }
        if (i % 2 == 0) {
            System.out.println("玩家" + p2.getName() + "拿牌");
            player.SecondplayerCards.add(iterator.next());
        }
        if (i >= 4) {
            break;
        }
        i++;
    }

   System.out.println("玩家"+playerList.get(0).name+"的牌有:"); 
  for(Cards player1:player.FirstplayerCards){
  System.out.println(player1.color+player1.point);
}
  System.out.println("玩家"+playerList.get(1).name+"的牌有:");
  for(Cards player2:player.SecondplayerCards){
      System.out.println(player2.color+player2.point);
}  
}

//比较大小
public void compare(){
    List<Cards> MaxCardList=new ArrayList<Cards>();
    Collections.sort(player.FirstplayerCards);
    Collections.sort(player.SecondplayerCards);

    System.out.println("玩家"+playerList.get(0).name+"的最大牌:"+player.FirstplayerCards.get(1).color+
            player.FirstplayerCards.get(1).point);
    System.out.println("玩家"+playerList.get(1).name+"的最大牌:"+player.SecondplayerCards.get(1).color+
            player.SecondplayerCards.get(1).point);

    MaxCardList.add(player.FirstplayerCards.get(1));
    MaxCardList.add(player.SecondplayerCards.get(1));
    Collections.sort(MaxCardList);

    if(player.FirstplayerCards.contains(MaxCardList.get(1)))
        System.out.println("Winner is:"+playerList.get(0).name);
    if(player.SecondplayerCards.contains(MaxCardList.get(1)))
        System.out.println("Winner is:"+playerList.get(1).name);
}

public static void main(String[] args) {
    PokerCards pc=new PokerCards();
    pc.createCards();
    pc.creatPlayer();
    pc.dealCard(playerList.get(0), playerList.get(1), cardsList);
    pc.compare();
}

}

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