一共创建了三个类,PokerCard,Player,PokerGame
第一个 PokerCard
package poker;
public class PokerCard implements Comparable <PokerCard>{
private String Name;
private Integer Id;
public PokerCard(String Name,int Id){
this.Name = Name;
this.Id = Id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
@Override
public int compareTo(PokerCard arg0) {
// TODO 自动生成的方法存根
return this.Id.compareTo(arg0.Id);
}
}
第二个 Player
package poker;
import java.util.ArrayList;
import java.util.List;
public class Player {
private String Name;
private Integer Id;
List <PokerCard> Card;
public Player(String Name,int Id){
this.Name = Name;
this.Id = Id;
this.Card = new ArrayList <PokerCard>();
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
}
第三个,主程序 PokerGame
package poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PokerGame {
private List <PokerCard> Deck;
private List <PokerCard> Deck2;
private Scanner sc;
private Player p1;
private Player p2;
public static void main(String[] args) {
//new一个PokerGame对象
PokerGame pg = new PokerGame();
//创建一副新的扑克牌
pg.establishDeck();
//提示扑克牌创建成功并且打印输出
pg.printCard();
//提示开始洗牌并洗牌
pg.shuffleTheCards();
//创建玩家
pg.establishPlayer();
//双方抽牌
pg.dealCards();
//游戏开始,对比双方最大的手牌
pg.gameStart();
//展示双方的所有手牌
pg.exhibitionPokerCard();
}
/**
* 用于创建一副新的扑克牌
* @param Deck
* @param Cardcolor
* @param CardName
*/
public void establishDeck(){
System.out.println("----------创建扑克牌---------");
String[] Cardcolor = {"方块","梅花","红心","黑桃"};
String[] CardName = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
int x = 0; //用于取出Cardcolor数组里的数据
int y = 0; //用于取出CardName数组里的数据
int z = 1; //用于给新创建的扑克牌做Id标记
//通过循环取出数组中的数据创建新的扑克牌
Deck = new ArrayList <PokerCard>();
while(true){
Deck.add(new PokerCard(Cardcolor[x] + CardName[y],z));
y++;
z = z + 4;
if(y == 13){
x++;
y = 0;
z = x + 1;
}
if(x == 4)
break;
}
}
/**
* 用于打印输出扑克牌
* @param Deck
*/
public void printCard(){
System.out.println("----------扑克牌创建成功!---------");
System.out.print("为:[");
for (PokerCard pk : Deck) {
if(Deck.indexOf(pk) != Deck.size()-1)
System.out.print(pk.getName() + ",");
else
System.out.print(pk.getName());
}
System.out.println("]");
}
/**
* 用于洗牌,从Deck中洗牌到Deck2里
* @param Deck
* @param Deck2
*/
public void shuffleTheCards(){
System.out.println("----------开始洗牌...---------");
Random rd = new Random();//创建Random对象用于生成随即数字
PokerCard pk;//用于接收Deck里的PokerCard元素
// 从Deck中随即抽取扑克牌放入Deck2中
Deck2 = new ArrayList <PokerCard>();
while(true){
do{
pk = Deck.get(rd.nextInt(Deck.size()));
}while(Deck2.contains(pk));//保证每次抽取的牌不一样
Deck2.add(pk);
if(Deck.size() == Deck2.size())
break;
}
System.out.println("----------洗牌结束!---------");
}
/**
* 用于创建玩家(两位)
*/
public void establishPlayer(){
sc = new Scanner(System.in);
for(int i = 1;i <= 2;i++){
System.out.println("请输入第"+ i +"位玩家的ID和姓名:");
while(true){
try{
System.out.println("输入ID:");
int ID = scan();
System.out.println("输入姓名:");
String name = sc.next();
if(i == 1){
p1 = new Player(name,ID);
}
if(i == 2){
p2 = new Player(name,ID);
}
break;
}catch(InputMismatchException e){
System.out.println(e.getMessage());
}
}
}
}
/**
* 用于防止输入非整数类型的ID
* @return
* @throws InputMismatchException
*/
private int scan() throws InputMismatchException{
try{
int i = sc.nextInt();
return i;
}catch(InputMismatchException e){
sc = new Scanner(System.in);
throw new InputMismatchException("请输入整数类型的ID!");
}
}
/**
* 用于发牌
* @param Deck2
* @param p1
* @param p2
*/
public void dealCards(){
System.out.println("----欢迎玩家:" + p1.getName());
System.out.println("----欢迎玩家:" + p2.getName());
System.out.println("----------开始发牌...---------");
while(true){
System.out.println("----玩家:" + p1.getName() + "-拿牌");
p1.Card.add(Deck2.get(0));
Deck2.remove(0);
System.out.println("----玩家:" + p2.getName() + "-拿牌");
p2.Card.add(Deck2.get(0));
Deck2.remove(0);
if(p2.Card.size() == 2)
break;
}
System.out.println("----------发牌结束!---------");
}
/**
* 游戏开始,比较两位最大的手牌
* @param p1
* @param p2
*/
public void gameStart(){
System.out.println("----------开始游戏...---------");
Collections.sort(p1.Card);//用Sort方法通过扑克牌的ID去排序手牌
Collections.sort(p2.Card);
PokerCard pk = p1.Card.get(p1.Card.size()-1);//取出最大的手牌
PokerCard pk2 = p2.Card.get(p2.Card.size()-1);
System.out.println(p1.getName() + "最大的手牌为:" + pk.getName());
System.out.println(p2.getName() + "最大的手牌为:" + pk2.getName());
//比较双方最大的手牌
if(pk.getId() > pk2.getId())
System.out.println("----------玩家:" + p1.getName() + "获胜!----------");
else
System.out.println("----------玩家:" + p2.getName() + "获胜!----------");
}
/**
* 展示双方手牌
* @param p1
* @param p2
*/
public void exhibitionPokerCard(){
System.out.println("玩家各自的手牌为:");
System.out.print(p1.getName() + ":[");
for (PokerCard pc : p1.Card) {//遍历玩家的手牌
if(p1.Card.indexOf(pc) != p1.Card.size()-1)//如果当前手牌index不等于最后一张手牌
System.out.print(pc.getName() + ",");
else
System.out.print(pc.getName() );
}
System.out.println("]");
System.out.print(p2.getName() + ":[");
for (PokerCard pc : p2.Card) {
if(p2.Card.indexOf(pc) != p2.Card.size()-1)
System.out.print(pc.getName() + ",");
else
System.out.print(pc.getName() );
}
System.out.println("]");
}
}
热门评论
补上运行结果:
----------创建扑克牌---------
----------扑克牌创建成功!---------
为:[方块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,黑桃2]
----------开始洗牌...---------
----------洗牌结束!---------
请输入第1位玩家的ID和姓名:
输入ID:
a
请输入整数类型的ID!
输入ID:
1
输入姓名:
Tom
请输入第2位玩家的ID和姓名:
输入ID:
b
请输入整数类型的ID!
输入ID:
2
输入姓名:
Jack
----欢迎玩家:Tom
----欢迎玩家:Jack
----------开始发牌...---------
----玩家:Tom-拿牌
----玩家:Jack-拿牌
----玩家:Tom-拿牌
----玩家:Jack-拿牌
----------发牌结束!---------
----------开始游戏...---------
Tom最大的手牌为:红心7
Jack最大的手牌为:梅花9
----------玩家:Jack获胜!----------
玩家各自的手牌为:
Tom:[黑桃4,红心7]
Jack:[黑桃8,梅花9]