import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Creat_Shuffle_SendCards implements Comparator<Player>{
public List <PlayingCards> cardsAll;
Scanner input=new Scanner(System.in);
Player player1;
Player player2;
static int k=0;
//存放52张扑克
public Creat_Shuffle_SendCards(){
cardsAll=new ArrayList<PlayingCards>();
}
//创建52张扑克
public void Creatcards(){
System.out.println("----------------创建52张扑克牌中--------------------");
String []str1={"黑桃","红桃","梅花","方块"};
String []str2={"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++){
PlayingCards pc= new PlayingCards(str1[i], str2[j]);
cardsAll.add(pc);
}
}
}
//把52张扑克打乱顺序并打印出来
public void Shufflecards(){
Collections.shuffle(cardsAll);
System.out.print("----------------洗52张扑克牌中--------------------"+
"\n----------------洗牌完毕--------------------"+"\n[");
for(PlayingCards cards:cardsAll){
System.out.print(cards.getColor()+cards.getPoint()+",");
}
System.out.println("]");
}
//创建玩家
public void Creatplayers(){
System.out.print("请输入第一个玩家的ID号:");
int onep=input.nextInt();
System.out.print("请输入第一个玩家的名字:");
String onename=input.next();
player1=new Player(onep, onename, null, null);
System.out.print("请输入第二个玩家的ID号:");
int twop=input.nextInt();
System.out.print("请输入第二个玩家的名字:");
String twoname=input.next();
player2=new Player(twop, twoname,null, null);
}
//发牌
public void SendCards(){
System.out.println("----------------开始发牌--------------------");
System.out.println("玩家:"+player1.getName()+"-拿牌");
player1.setPc1(cardsAll.get(k++));
System.out.println("玩家:"+player2.getName()+"-拿牌");
player2.setPc1(cardsAll.get(k++));
System.out.println("玩家:"+player1.getName()+"-拿牌");
player1.setPc2(cardsAll.get(k++));
System.out.println("玩家:"+player2.getName()+"-拿牌");
player2.setPc2(cardsAll.get(k++));
System.out.println("----------------发牌结束--------------------");
System.out.println("----------------开始游戏--------------------");
}
//接口的compare玩家方法
public int compare(Player p1, Player p2) {
PlayingCards pc=new PlayingCards();
if(pc.compare(p1.getPc1(), p1.getPc2())==1)
p1.bigCards=p1.getPc1();
else p1.bigCards=p1.getPc2();
if(pc.compare(p2.getPc1(), p2.getPc2())==1)
p2.bigCards=p2.getPc1();
else p2.bigCards=p2.getPc2();
if(pc.compare(p1.bigCards, p2.bigCards)==1)
return 1;
else if(pc.compare(p1.bigCards, p2.bigCards)==-1)
return -1;
else return 0;
}
//玩家类的compare方法
public Player compareplayer(Player p1, Player p2) {
PlayingCards pc=new PlayingCards();
if(pc.comparecard(p1.getPc1(), p1.getPc2())==p1.getPc1())
p1.bigCards=p1.getPc1();
else p1.bigCards=p1.getPc2();
if(pc.comparecard(p2.getPc1(), p2.getPc2())==p2.getPc1())
p2.bigCards=p2.getPc1();
else p2.bigCards=p2.getPc2();
if(pc.comparecard(p1.bigCards, p2.bigCards)==p1.bigCards)
return p1;
else if(pc.comparecard(p1.bigCards, p2.bigCards)==p2.bigCards)
return p2;
else return null;
}
//打印玩家1,2最大手牌
public void printBigCard(){
System.out.println("玩家:"+player1.getName()+"最大手牌为:"+
player1.bigCards.getColor()+player1.bigCards.getPoint());
System.out.println("玩家:"+player2.getName()+"最大手牌为:"+
player2.bigCards.getColor()+player2.bigCards.getPoint());
}
// 如果使用接口的比较方法:
// public void printWiner(){
// if(compare(player1, player2)==1)
// System.out.println("----------玩家:"+player1.getName()+"获得胜利-----------");
// else if(compare(player1, player2)==-1){
// System.out.println("----------玩家:"+player2.getName()+"获得胜利-----------");
// }else System.out.println("----------玩家:"+player1.getName()+
// "玩家:"+player2.getName()+"打成平局-----------");
// System.out.println("玩家:"+player1.getName()+"手牌为:"+player1.getPc1().getColor()
// +player1.getPc1().getPoint()+" "+player1.getPc2().getColor()
// +player1.getPc2().getPoint()+"\n"+
// "玩家:"+player2.getName()+"手牌为:"+player2.getPc1().getColor()
// +player2.getPc1().getPoint()+" "+player2.getPc2().getColor()
// +player2.getPc2().getPoint());
// }
//使用自定义玩家比较方法
public void printWiner(){
if(compareplayer(player1, player2)==player1)
System.out.println("----------玩家:"+player1.getName()+"获得胜利-----------");
else if(compareplayer(player1, player2)==player2){
System.out.println("----------玩家:"+player2.getName()+"获得胜利-----------");
}else System.out.println("----------玩家:"+player1.getName()+
"玩家:"+player2.getName()+"打成平局-----------");
System.out.println("玩家:"+player1.getName()+"手牌为:"+player1.getPc1().getColor()
+player1.getPc1().getPoint()+" "+player1.getPc2().getColor()
+player1.getPc2().getPoint()+"\n"+
"玩家:"+player2.getName()+"手牌为:"+player2.getPc1().getColor()
+player2.getPc1().getPoint()+" "+player2.getPc2().getColor()
+player2.getPc2().getPoint());
}
public static void main(String []args){
String choice="t";
Scanner input=new Scanner(System.in);
Creat_Shuffle_SendCards poker=new Creat_Shuffle_SendCards();
poker.Creatcards();
poker.Shufflecards();
poker.Creatplayers();
while(51-k>=3&&choice.equalsIgnoreCase("t")){
poker.SendCards();
poker.compare(poker.player1,poker.player2);
poker.printBigCard();
poker.printWiner();
System.out.println("是否还想继续玩?(t/f):");
choice=input.next();
}
System.out.println("游戏结束!欢迎使用!");
}
}
public class Player {
private PlayingCards pc1;
private PlayingCards pc2;
public PlayingCards bigCards;
private int ID;
private String name;
public Player(){
}
public Player(int ID,String name,PlayingCards pc1,PlayingCards pc2) {
this.ID=ID;
this.name=name;
this.pc1=pc1;
this.pc2=pc2;
// bigCards=new PlayingCards();
}
public PlayingCards getPc1() {
return pc1;
}
public void setPc1(PlayingCards pc1) {
this.pc1 = pc1;
}
public PlayingCards getPc2() {
return pc2;
}
public void setPc2(PlayingCards pc2) {
this.pc2 = pc2;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.Comparator;
public class PlayingCards implements Comparator<PlayingCards>{
private String point;
private String color;
public PlayingCards(){
}
public PlayingCards(String color,String point){
this.color=color;
this.point=point;
}
// public List<PlayingCards> getCards() {
// return cardsAll;
// }
// public void setCards(List<PlayingCards> cards) {
// this.cardsAll = cards;
// }
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int compareColor(PlayingCards c){
String color=c.getColor();
if(color.equals("黑桃"))
return 4;
if(color.equals("红桃"))
return 3;
if(color.equals("梅花"))
return 2;
if(color.equals("方块"))
return 1;
return 0;
}
public int comparePoint(PlayingCards c){
String point=c.getPoint();
if(point.equals("2"))
return 2;
if(point.equals("3"))
return 3;
if(point.equals("4"))
return 4;
if(point.equals("5"))
return 5;
if(point.equals("6"))
return 6;
if(point.equals("7"))
return 7;
if(point.equals("8"))
return 8;
if(point.equals("9"))
return 9;
if(point.equals("10"))
return 10;
if(point.equals("J"))
return 11;
if(point.equals("Q"))
return 12;
if(point.equals("K"))
return 13;
if(point.equals("A"))
return 14;
return 0;
}
//扑克牌类自定义的compare方法
public PlayingCards comparecard(PlayingCards o1,PlayingCards o2){
if(o1.compareColor(o1)>o2.compareColor(o2)){
return o1;
}else if(o1.compareColor(o1)<o2.compareColor(o2)){
return o2;
}else {
if(o1.comparePoint(o1)>o2.comparePoint(o2))
return o1;
else if(o1.comparePoint(o1)<o2.comparePoint(o2))
return o2;
else return null;
}
}
@Override
//接口中扑克牌的compare方法
public int compare(PlayingCards o1, PlayingCards o2) {
if(o1.compareColor(o1)>o2.compareColor(o2)){
return 1;
}else if(o1.compareColor(o1)<o2.compareColor(o2)){
return -1;
}else {
if(o1.comparePoint(o1)>o2.comparePoint(o2))
return 1;
else if(o1.comparePoint(o1)<o2.comparePoint(o2))
return -1;
else return 0;
}
}
}
想把方法分类不写在一起,但是又感觉思路不清楚。。。希望前辈指点下我存在的问题。谢谢!
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦