一共创建了三个类,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("]");
}
}
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦