首页 > IT职业 > IT生活 > 简易扑克游戏---各位爸爸求指点指点
新手的一批,写了好久,功能都有了,就是写的有点多,不够精简,还请多多指教,没有用到contains ,脑子不够用了。
Poker 纸牌类
package com.qianlian;
import java.util.List;
import java.util.Random;
public class Poker implements Comparable{
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 {
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 pTemp;
public Set pokers;
public Map mapPlayer;
public String pokPoint;
public String color;
public Poker tempAdd;
public Scanner console;
public int ID;
public String pName;
public List tempPok;
public Iterator it;
public PlayPoker(){
pokers = new HashSet();
console = new Scanner(System.in);
tempPok = new ArrayList();
mapPlayer =new HashMap();
pTemp = new ArrayList();
}
public void addPoker(){
System.out.println("======================创建扑克牌======================");
for(int j=0;j 红心 > 方块 > 梅花》===================================");
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,找出人名。思路好像没错,但是试了好多次没有成功。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4479/viewspace-2807928/ ,如需转载,请注明出处,否则将追究法律责任。