package com.poker;
/**
* 单张扑克牌
* @author Administrator
*
*/
public class Card implements Comparable{
//花色
private String huase;
//点数
private String data;
//用来比较卡牌大小的2条属性
//根据点数
private int num;
//根据花色
private int col;
public String getHuase() {
return huase;
}
public void setHuase(String huase) {
this.huase = huase;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
//构造函数
public Card(String huase,String data,int num,int col){
this.huase=huase;
this.data=data;
this.num=num;
this.col=col;
}
public Card(){
}
@Override
public int compareTo(Card o) {
if(this.num>o.num)
return 1;
else if(this.num==o.num&&this.col>o.col)
return 1;
else
return -1;
}
}
package com.poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 一整副扑克牌的List,包含了创建扑克牌和洗牌方法
* @author Administrator
*
*/
public class Poker{
public List cardList=new ArrayList();//创建一副空扑克牌List
//添加扑克牌的值
public void createPoker(){
System.out.println("----------------创建扑克牌------------------");
String[] st1=new String[]{"黑桃","红心","梅花","方块"};
String[] st2=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<4;i++){
for(int j=0;j<13;j++){
this.cardList.add(new Card(st1[i],st2[j],j,i));
}
}
System.out.println("----------------扑克牌创建成功------------------");
System.out.print("为:[");
for (Card card : cardList) {
System.out.print(card.getHuase()+card.getData()+",");
}
System.out.println("]");
}
//洗牌
public void shufflePoker(){
Collections.shuffle(cardList);
}
}
package com.poker;
import java.util.ArrayList;
import java.util.List;
/**
* 玩家
* @author Administrator
*
*/
public class Player{
private int id;//玩家ID
private String name;//玩家姓名
public List handList=new ArrayList();;//玩家手牌
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Player(int id,String name){
this.id=id;
this.name=name;
}
public Player(){
}
}
package com.poker;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* 游戏开始
* @author Administrator
*
*/
public class Play {
public static void main(String[] args) {
Poker poker=new Poker();//创建扑克对象
Player[] pl=new Player[2];
//创建扑克牌
poker.createPoker();
//洗牌
System.out.println("----------------开始洗牌------------------");
poker.shufflePoker();
System.out.println("----------------洗牌结束------------------");
for (Card card : poker.cardList) {
System.out.print(card.getHuase()+card.getData()+",");
}
System.out.println("----------------创建玩家------------------");
while(true){
try{
Scanner s=new Scanner(System.in);
for(int j=0;j<2;j++){
System.out.println("请输入第"+(j+1)+"位玩家的ID和姓名:");
System.out.println("输入ID:");
pl[j]=new Player();
int id=s.nextInt();
pl[j].setId(id);
System.out.println("输入姓名:");
String name=s.next();
pl[j].setName(name);
}
break;
}catch(InputMismatchException e){
System.out.println("输入格式错误,请根据提示重新输入!");
}
}
System.out.println("-----欢迎玩家:"+pl[0].getName());
System.out.println("-----欢迎玩家:"+pl[1].getName());
//发牌
System.out.println("----------------开始发牌------------------");
pl[0].handList.add(poker.cardList.get(0));
System.out.println("-----玩家:"+pl[0].getName()+"-拿牌");
pl[1].handList.add(poker.cardList.get(1));
System.out.println("-----玩家:"+pl[1].getName()+"-拿牌");
pl[0].handList.add(poker.cardList.get(2));
System.out.println("-----玩家:"+pl[0].getName()+"-拿牌");
pl[1].handList.add(poker.cardList.get(3));
System.out.println("-----玩家:"+pl[1].getName()+"-拿牌");
System.out.println("----------------发牌结束------------------");
System.out.println("----------------开始游戏------------------");
Collections.sort(pl[0].handList);
Collections.sort(pl[1].handList);
//玩家1最大的手牌
System.out.println("玩家:"+pl[0].getName()+"最大的手牌为:"+pl[0].handList.get(1).getHuase()+pl[0].handList.get(1).getData());
//玩家2最大的手牌
System.out.println("玩家:"+pl[1].getName()+"最大的手牌为:"+pl[1].handList.get(1).getHuase()+pl[1].handList.get(1).getData());
if(pl[0].handList.get(1).compareTo(pl[1].handList.get(1))==1){
System.out.println("----------------玩家:"+pl[0].getName()+"获胜------------------");
}else
System.out.println("----------------玩家:"+pl[1].getName()+"获胜------------------");
System.out.println("玩家各自的手牌为:");
System.out.println(pl[0].getName()+":"+pl[0].handList.get(0).getHuase()+pl[0].handList.get(0).getData()+","
+pl[0].handList.get(1).getHuase()+pl[0].handList.get(1).getData());
System.out.println(pl[1].getName()+":"+pl[1].handList.get(0).getHuase()+pl[1].handList.get(0).getData()+","
+pl[1].handList.get(1).getHuase()+pl[1].handList.get(1).getData());
}
}