新手的一批,写了好久,功能都有了,就是写的有点多,不够精简,还请多多指教,没有用到contains ,脑子不够用了。
Poker 纸牌类
package com.qianlian;
import java.util.List;
import java.util.Random;
public class Poker implements Comparable<Poker>{
private String color;
private String point;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public Poker(String color, String point) {
this.color = color;
this.point = point;
}
@Override
public int hashCode() {
Random random = new Random();
int result =random.nextInt(100);
return result;
}
@Override
public int compareTo(Poker o) {
return this.getPoint().compareTo(o.getPoint());
}
}
重写了hashcode,因为我的set会自动排序,没有洗牌的效果,所以重写不知道有没有更好的方法。
重写了compareTo ,因为在后面要到sort,难道用 Comparator,就要用Comparable吗
PokerPlayer 玩家类
package com.qianlian;
import java.util.ArrayList;
import java.util.List;
public class PokerPlayer {
private int id;
private String name;
public PokerPlayer(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
}
感觉没问题
ComparatorPoker Comparator类
package com.qianlian;
import java.util.Comparator;
import javax.naming.spi.DirStateFactory.Result;
public class ComparatorPoker implements Comparator<Poker> {
public int pokColor1;
public int pokColor2;
public int resultColor;
public int resultPoint;
public int pokPoint1;
public int pokPoint2;
public String pokStrPoint1;
public String pokStrPoint2;
@Override
public int compare(Poker o1, Poker o2) {
switch (o1.getColor()) {
case "梅花":
pokColor1 = 1;
break;
case "方块":
pokColor1 = 2;
break;
case "红心":
pokColor1 = 3;
break;
case "黑桃":
pokColor1 = 4;
break;
default:
System.out.println("不存在!");
break;
}
switch (o2.getColor()) {
case "梅花":
pokColor2 = 1;
break;
case "方块":
pokColor2 = 2;
break;
case "红心":
pokColor2 = 3;
break;
case "黑桃":
pokColor2 = 4;
break;
default:
System.out.println("你是真的皮!");
break;
}
switch (o1.getPoint()) {
case "J":
pokStrPoint1="11";
break;
case "Q":
pokStrPoint1="12";
break;
case "K":
pokStrPoint1="13";
break;
case "A":
pokStrPoint1="14";
break;
default:
pokStrPoint1 = o1.getPoint();
break;
}
switch (o2.getPoint()) {
case "J":
pokStrPoint2="11";
break;
case "Q":
pokStrPoint2="12";
break;
case "K":
pokStrPoint2="13";
break;
case "A":
pokStrPoint2="14";
break;
default:
pokStrPoint2 = o2.getPoint();
break;
}
pokPoint1 = Integer.parseInt(pokStrPoint1);
pokPoint2 = Integer.parseInt(pokStrPoint2);
resultPoint = pokPoint1 - pokPoint2;
resultColor = pokColor1 - pokColor2;
if(resultPoint!=0){
return resultPoint;
}else{
return resultColor;
}
}
}
有点啰嗦,把JQKA和数字转成统一类型比较,再去规定花色的大小。最后先比较点数,相同在比较花色。
PlayPoker 开始游戏
package com.qianlian;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
public class PlayPoker {
public List<Poker> pTemp;
public Set<Poker> pokers;
public Map<PokerPlayer,List> mapPlayer;
public String pokPoint;
public String color;
public Poker tempAdd;
public Scanner console;
public int ID;
public String pName;
public List<Poker> tempPok;
public Iterator it;
public PlayPoker(){
pokers = new HashSet<Poker>();
console = new Scanner(System.in);
tempPok = new ArrayList<Poker>();
mapPlayer =new HashMap<PokerPlayer,List>();
pTemp = new ArrayList<Poker>();
}
public void addPoker(){
System.out.println("======================创建扑克牌======================");
for(int j=0;j<4;j++){
switch (j) {
case 0:
color = "红心";
break;
case 1:
color = "黑桃";
break;
case 2:
color = "方块";
break;
case 3:
color = "梅花";
break;
default:
System.out.println("这行不会出现吧!");
break;
}
for(int i=1;i<=13;i++){
switch (i) {
case 1:
pokPoint = "A";
break;
case 11:
pokPoint = "J";
break;
case 12:
pokPoint = "Q";
break;
case 13:
pokPoint = "K";
break;
default:
pokPoint = Integer.toString(i);
break;
}
tempAdd = new Poker(color, pokPoint);
pokers.add(tempAdd);
System.out.println(tempAdd.getColor()+tempAdd.getPoint());
}
}
System.out.println("======================扑克牌创建成功======================");
}
public void forEachPoker(){
System.out.println("======================洗牌======================");
for(Poker p:pokers){
System.out.println(p.getColor()+p.getPoint());
}
System.out.println("======================洗牌成功======================");
}
public void playPoker(){
//抽出前四张
System.out.println("=====================将要用到的牌====================");
it = pokers.iterator();
int i = 0;
while(it.hasNext()&&i<4){
Poker tempP=(Poker) it.next();
tempPok.add(tempP);
System.out.println(tempPok.get(i).getColor()+tempPok.get(i).getPoint());
i++;
}
//第一名玩家得到牌
for(int j=0;j<3;j=j+2){
pTemp.add(new Poker(tempPok.get(j).getColor(), tempPok.get(j).getPoint()));//0,2
}
//实例化第一名玩家
System.out.println("请输入玩家1代号数字int");
ID = console.nextInt();
System.out.println("请输入玩家1姓名");
pName = console.next();
PokerPlayer pPlayer1 = new PokerPlayer(ID, pName);
mapPlayer.put(pPlayer1, pTemp);
Collections.sort(pTemp, new ComparatorPoker());
String p1Max = pTemp.get(1).getColor()+ pTemp.get(1).getPoint();
String p1Poker = pTemp.get(0).getColor()+pTemp.get(0).getPoint()+" "+pTemp.get(1).getColor()+pTemp.get(1).getPoint();
pTemp.clear();
//第二名玩家得到牌
for(int j=1;j<4;j=j+2){
pTemp.add(new Poker(tempPok.get(j).getColor(), tempPok.get(j).getPoint()));//1,3
}
//实例化第二名玩家
System.out.println("请输入玩家2代号int");
ID = console.nextInt();
System.out.println("请输入玩家2姓名");
pName = console.next();
PokerPlayer pPlayer2 = new PokerPlayer(ID, pName);
mapPlayer.put(pPlayer2, pTemp);
Collections.sort(pTemp, new ComparatorPoker());
String p2Max = pTemp.get(1).getColor()+ pTemp.get(1).getPoint();
String p2Poker = pTemp.get(0).getColor()+pTemp.get(0).getPoint()+" "+pTemp.get(1).getColor()+pTemp.get(1).getPoint();
System.out.println(pPlayer1.getName()+" vs "+pPlayer2.getName());
System.out.println("======================开始发牌!================================");
System.out.println(pPlayer1.getName()+": "+p1Poker);
System.out.println(pPlayer2.getName()+": "+p2Poker);
System.out.println("======================各自手中最大牌================================");
System.out.println(pPlayer1.getName()+" : "+p1Max);
System.out.println(pPlayer2.getName()+" : "+p2Max);
pTemp.clear();
pTemp.add(new Poker(p1Max.substring(0, 2), p1Max.substring(2, 3)));
pTemp.add(new Poker(p2Max.substring(0, 2), p2Max.substring(2, 3)));
Collections.sort(pTemp, new ComparatorPoker());
String winPok = pTemp.get(1).getColor()+pTemp.get(1).getPoint();
String winPer;
if(winPok==p1Max){
winPer = pPlayer1.getName();
}else{
winPer = pPlayer2.getName();
}
System.out.println("=============================小常识《黑桃 > 红心 > 方块 > 梅花》===================================");
System.out.println("胜利者是: "+winPer+"————————> "+winPok);
}
public static void main(String[] args) {
PlayPoker pp = new PlayPoker();
pp.addPoker();
pp.forEachPoker();
pp.playPoker();
}
}
怎么用contains呢 ,map的value是个List,最后比较出来最大的牌,用contiains检索包含在那个entry,找出人名。思路好像没错,但是试了好多次没有成功。
热门评论
兄弟,这是我写的,可以一起交流,互相学习。
http://www.imooc.com/article/19391