继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第3季,扑克牌

SilenceDean
关注TA
已关注
手记 3
粉丝 9
获赞 6

花了好几个小时自己写的扑克牌代码,参考别人的做了部分修改
package com.imooc;

import java.util.ArrayList;

import java.util.List;

public class Card implements Comparable<Card>{
private String kind;
private String point;

public Card(String kind,String point){
this.kind=kind;
this.point=point;
}
public String getPoint(){
return point;
}

public String getKind(){
return kind;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime result + ((kind == null) ? 0 : kind.hashCode());
result = prime
result + ((point == null) ? 0 : point.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Card))
return false;
Card other = (Card) obj;
if (kind == null) {
if (other.kind != null)
return false;
} else if (!kind.equals(other.kind))
return false;
if (point == null) {
if (other.point != null)
return false;
} else if (!point.equals(other.point))
return false;
return true;
}

public int getIndex(String[] array,String point){
int i=0;

while(!array[i].equals(point)){
    i++;
    }
return i;   
}

@Override
public int compareTo(Card o) {
// TODO Auto-generated method stub
String[] p={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] k={"黑桃","红桃","梅花","方块"};
int pointDifference=getIndex(p, this.point)-getIndex(p, o.point);
int kindDifference=getIndex(k, this.kind)-getIndex(k, o.kind);
int result=pointDifference;
if(result!=0){
return result;
}else if(kindDifference>0){
return 1;
}else{
return -1;
}
}

}
package com.imooc;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Player {
public List<Card> handCardsList=new ArrayList<>();
private int id;
private String name;
public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public int getId(){
return id;

}
public String getName(){
return name;
}
public Player(){

}
public Player(int id,String name){
this.id=id;
this.name=name;
this.handCardsList=handCardsList;
}

}
package com.imooc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Test {
static List<Card> cardList=new ArrayList();
static List<Card> compareList=new ArrayList();
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t=new Test();
t.createCard();

Player p1=new Player();
Player p2=new Player();
Player[] PlayerArray={p1,p2};
Scanner console=new Scanner(System.in);
for( int i=0;i<2;i++){
    System.out.println("请输入第"+(i+1)+"位玩家"+"ID");
    int id=console.nextInt();
    PlayerArray[i].setId(id);
    System.out.println("请输入第"+(i+1)+"位玩家"+"姓名:");
    String name=console.next();
    PlayerArray[i].setName(name);
    System.out.println("成功创建第"+(i+1)+"玩家"+"ID:"+id+" 姓名为:"+name);
}
   System.out.println("--------游戏开始--------");
   //调用collections.shuffle()的洗牌方法
 //  t.shuffleCard(cardList);
    t.shuffle(cardList); //自己写的洗牌方法
t.getHandCards(p1, p2);

Collections.sort(p1.handCardsList);
Card Cardmax1=p1.handCardsList.get(1);
System.out.println("玩家:"+p1.getName()+"最大手牌为"+Cardmax1.getKind()+Cardmax1.getPoint());
Collections.sort(p2.handCardsList);
Card Cardmax2=p2.handCardsList.get(1);
System.out.println("玩家:"+p2.getName()+"最大手牌为"+  Cardmax2.getKind()+Cardmax2.getPoint());
Player winner=t.compareMax(p1,p2);
System.out.println("获胜者为"+winner.getName());
System.out.println("ID为"+p1.getId()+"的玩家"+p1.getName()+"的手牌为"+p1.handCardsList.get(0).getKind()+p1.handCardsList.get(0).getPoint()
        +"和"+p1.handCardsList.get(1).getKind()+p1.handCardsList.get(1).getPoint());
System.out.println("ID为"+p2.getId()+"的玩家"+p2.getName()+"的手牌为"+p2.handCardsList.get(0).getKind()+p2.handCardsList.get(0).getPoint() 
        +"和"+p2.handCardsList.get(1).getKind()+p2.handCardsList.get(1).getPoint() );                                 

System.out.println("--------游戏结束--------");

}

public void createCard(){
String[] cardPoint={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] cardKind={"黑桃","红桃","梅花","方块"};
Card newCard;
for(int i=0;i<cardKind.length;i++){
for(int j=0;j<cardPoint.length;j++){
newCard=new Card(cardKind[i],cardPoint[j]);
cardList.add(newCard);

    }
}

}

public void shuffleCard(List<Card> cardList){
Collections.shuffle(cardList);
System.out.println("--------洗牌完毕--------");
}
public void getHandCards(Player p1,Player p2){
p1.handCardsList.add(cardList.get(0));
System.out.println("ID为"+p1.getId()+"的玩家"+p1.getName()+"获得一张牌");
p2.handCardsList.add(cardList.get(1));
System.out.println("ID为"+p2.getId()+"的玩家"+p2.getName()+"获得一张牌");
p1.handCardsList.add(cardList.get(2));
System.out.println("ID为"+p1.getId()+"的玩家"+p1.getName()+"获得一张牌");
p2.handCardsList.add(cardList.get(3));
System.out.println("ID为"+p2.getId()+"的玩家"+p2.getName()+"获得一张牌");
}

public Player compareMax(Player p1,Player p2){
    int result =p1.handCardsList.get(1).compareTo(p2.handCardsList.get(1));
    if(result>0){
        return p1;
    }else{
        return p2;
    }
}
//随机从cardList中取牌,再存入shuffleList中,直至全部取完。再用shuffleList将cardList替换
public void shuffle(List<Card> cardList){
    List<Card> shuffleList=new ArrayList();

    Random random=new Random();
    int num;
    int i=0;
    while(i<52){
    do{
    num=random.nextInt(52);
    }
    while(shuffleList.contains(cardList.get(num)));
    shuffleList.add(cardList.get(num));
    i++;
    }
cardList.removeAll(cardList);
cardList.addAll(shuffleList);
}

}

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP