----------InitialPlayCard类------------
游戏主函数及主逻辑
package PokerCard;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/*
-
扑克牌发牌、洗牌、游戏的过程
*/
public class InitialPlayCard {
private final String[] cardPoints = {"A","K","Q","J","10","9","8","7",
"6","5","4","3","2"};
private final String[] cardSuits = {"黑桃","红桃","梅花","方片"};
private List<Card> pcs;//定义一幅扑克牌对象
private List<Player>players;//定义一群玩家
private List<Player>playersNewList;//存储调整手牌顺序后的玩家
private Scanner console;//定义输入流public InitialPlayCard(){
pcs = new ArrayList<Card>();
players = new ArrayList<Player>();
}public static void main(String[] args){
//创建一幅扑克牌
System.out.println("-------创建扑克牌----------");
InitialPlayCard initPC = new InitialPlayCard();//新建扑克牌数组
initPC.creatCard();//创建扑克牌
System.out.println("-------扑克牌创建成功------");
initPC.showCard();//显示扑克牌
System.out.println("-------开始洗牌-------------");
initPC.shuffleCard();//洗牌
System.out.println("------洗牌结束--------------");
System.out.println("------创建玩家--------------");
initPC.console = new Scanner(System.in);
for(int i =1;i<3;i++){
System.out.println("请输入第"+i+"位玩家的ID和姓名");
//这里需要实现输入整数异常处理
int ID = initPC.checkInputID();
System.out.println("输入姓名:");
String name = initPC.console.next();
initPC.addPlayer(ID,name);
}initPC.welcomePlayers();//欢迎玩家System.out.println("------开始发牌------"); initPC.dealCards();//发牌 System.out.println("------发牌结束------"); System.out.println("------开始游戏------"); initPC.play();//进行手牌比较大小 initPC.getWinner();//得到冠军 System.out.println("玩家各自的手牌为"); initPC.showHandCards();//显示玩家手中的牌
}
/*
-
创建一幅扑克牌
*/
private void creatCard(){
for(int i=cardSuits.length-1;i>0;i--){
for(int j=0;j<cardPoints.length;j++){
pcs.add(new Card(cardSuits[i],cardPoints[j]));
}
}
}/*
-
显示一幅扑克牌
*/
private void showCard(){
StringBuilder sbd = new StringBuilder();
for(Card pc:pcs){
sbd.append(pc.getInfo()+",");
}
System.out.println("为"+"["+sbd.substring(0,sbd.length()-1)+"]");
}/*
- 洗牌,打乱List顺序
/
private void shuffleCard(){
Collections.shuffle(pcs);//用shuffle方法打乱
StringBuilder sbd = new StringBuilder();
for(Card pc:pcs){
sbd.append(pc.getInfo()+",");
}
System.out.println("洗牌后为:"+"["+sbd.substring(0,sbd.length()-1)+"]");
}
/ - 判断输入的ID,必须为int类型
- Scanner在发生InputMismatchException异常时,如果不作处理,后面的next会一直异常下去
- 方法1.将当前Scanner对象重写创建 如console = new Scanner(System.in);
- 方法2.手动换行console.next();
/
private int checkInputID(){
int inputID;
do{
System.out.println("输入ID:");
try{
inputID = console.nextInt();
}catch(Exception e){
System.out.println("请输入整数类型的ID!");
inputID=-1;
//console = new Scanner(System.in);
console.next();
}
}while(inputID == -1);
return inputID;
}
/ - 添加玩家
/
private void addPlayer(int ID,String name){
Player p = new Player(ID,name);
players.add(p);
}
/ - 欢迎玩家
/
private void welcomePlayers(){
for(Player p:players){
System.out.println("-----欢迎玩家:"+p.getName());
}
}
/ - 发牌 从第一张开始,发给两名玩家,按照一人一张的方式
- 每人发两张,后面会调整手中牌的顺序,所以将刚发后的牌从这里返回
/
private void dealCards(){
for(int i=0;i<2;i++){
for(Player p:players){
Card pc = pcs.get(0);
p.getHandCards().add(pc);
System.out.println("-----玩家:"+p.getName()+"-----拿牌");
pcs.remove(0);//add一个,remove一个
}
}
}
/ - 开始游戏
/
private void play(){
playersNewList = new ArrayList<Player>();
for(Player p:players){
Player playNew = new Player(p.getID(),p.getName());
List<Card>pcNewList = new ArrayList<Card>();
for(Card pc:p.getHandCards()){
pcNewList.add(pc);
}
playNew.setHandCards(pcNewList);
playersNewList.add(playNew);
}
for(Player p:playersNewList){
Collections.sort(p.getHandCards());//Card类实现了Comparable接口,并重写compareTo方法
System.out.println("玩家:"+p.getName()+"最大的手牌为:"+p.getHandCards().get(0).getInfo());
}
}
/ - 判断获胜者
/
private void getWinner(){
List<Card>maxCards = new ArrayList<Card>();
for(Player p:playersNewList){
maxCards.add(p.getHandCards().get(0));
}
Collections.sort(maxCards);
Card maxCard = maxCards.get(0);
//System.out.println("最大的牌是:"+maxCard.getInfo());
String winner = "";
for(Player p:playersNewList){
if(p.getHandCards().contains(maxCard)){
winner = p.getName();
break;
}
}
System.out.println("--------玩家:"+winner+"获胜!------");
}
/ - 显示玩家各自的手牌
/
private void showHandCards(){
for(Player p:players){
StringBuilder sbd = new StringBuilder();
sbd.append(p.getName()+":[");
for(Card pc:p.getHandCards()){
sbd.append(pc.getInfo()+",");
}
System.out.println(sbd.substring(0,sbd.length()-1)+"]");
}
}
/ - 显示玩家排序后各自的手牌
*/
// private void showOrderHandCards(){
// for(Player p:playersNewList){
// StringBuilder sbd = new StringBuilder();
// sbd.append(p.getName()+":[");
// for(Card pc:p.getHandCards()){
// sbd.append(pc.getInfo()+",");
// }
// System.out.println(sbd.substring()-1+"]");
// }
}
- 洗牌,打乱List顺序
-
---------Card类----------
建立扑克牌
package PokerCard;
import java.util.Arrays;
import java.util.List;
/*
-
扑克牌类型
*/
public class Card implements Comparable<Card> {
private String cardSuit;//扑克牌花色黑桃红桃梅花方片
private String cardPoints;//扑克牌点数A~kpublic Card(String cardSuit,String cardPoints){
this.cardSuit=cardSuit;
this.cardPoints=cardPoints;
}
public String getCardPoints(){
return cardPoints;
}
public String getCardSuit(){
return cardSuit;
}
public String getInfo(){
return cardPoints+cardSuit;
}public int compareTo(Card o){
return this.cardPoints.compareTo(o.cardPoints);
}public boolean equals(Object o){
if(this == o)return true;
if(!(o instanceof Card))
return false;
Card card =(Card) o;
return cardSuit.equals(card.cardSuit);
}
}
--------Player类-----------
建立玩家的输入
package PokerCard;
import java.util.ArrayList;
import java.util.List;
/*
-
玩家类型
*/
public class Player {
private int ID;
private String name;
private List<Card>handCards;//这里用List不用set存放是无序的public Player(int ID,String name) {
this.ID = ID;
this.name = name;
this.handCards = new ArrayList<Card>();
}public int getID() {
return ID;
}public String getName() {
return name;
}public List<Card> getHandCards() {
return handCards;
}public void setHandCards(List<Card> handCards) {
this.handCards = handCards;
}
}