运行时牌大小判断会出错...检查不出来5555,诚心求助,谢谢!
第一个类,player
package PokerGame;
import java.util.ArrayList;
import java.util.List;
public class Player {
Integer id;
String name;
List<Poker> hisPoker=new ArrayList<Poker>();
public Player(Integer id,String name){
this.id=id;
this.name=name;
}
public Player(){
}
}
第二个类,poker
package PokerGame;
public class Poker implements Comparable<Poker> {
String flower;
String number;
Integer numCompare;
Integer floCompare;
public Poker(String flower,String number,Integer numCompare,Integer floCompare){
this.flower=flower;
this.number=number;
this.numCompare=numCompare;
this.floCompare=floCompare;
}
public Poker(){
}
@Override
public int compareTo(Poker o) {
// TODO Auto-generated method stub
if(this.numCompare==o.numCompare){
return this.numCompare.compareTo(o.numCompare);
}else
return this.floCompare.compareTo(o.floCompare);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Poker other = (Poker) obj;
if (flower == null) {
if (other.flower != null)
return false;
} else if (!flower.equals(other.flower))
return false;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
return true;
}
}
主程序
package PokerGame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Major {
//把list定义在这里,使用方法后list得知将会随方法改变
List<Poker> pokerList=new ArrayList<Poker>();
//导入scanner对象来接受
Scanner console=new Scanner(System.in);
//创建玩家
Player player1=new Player();
Player player2=new Player();
//创建扑克牌
public void buildPoker(){
String[] flowerS={"方块","梅花","红桃","黑桃"};
Integer[] flowerCompareS={1,2,3,4};
String[] numberS={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
Integer[] numberCompareS={1,2,3,4,5,6,7,8,9,10,11,12,13};
for(int i=0;i<=12;i++){
for(int j=0;j<=3;j++){
Poker poker=new Poker();
poker.number=numberS[i];
poker.numCompare=numberCompareS[i];
poker.flower=flowerS[j];
poker.floCompare=flowerCompareS[j];
pokerList.add(poker);
}
}
System.out.println("扑克牌建立完成啦~~~建立的牌为:");
for(Poker poker:pokerList){
System.out.print(poker.flower+poker.number+" ");
}
}
//洗牌
public void washPoker(){
System.out.println("洗牌时间~~~");
Collections.shuffle(pokerList);//shuffle方法用于打乱顺序
System.out.println("洗牌完成~~~");
}
//输入玩家信息
public void buildPlayer(){
System.out.println("请输入玩家一ID");
Integer id1=console.nextInt();
System.out.println("请输入玩家一姓名");
String name1=console.next();
player1.name=name1;
player1.id=id1;
System.out.println("请输入玩家二ID");
Integer id2=console.nextInt();
System.out.println("请输入玩家二姓名");
String name2=console.next();
player2.name=name2;
player2.id=id2;
System.out.println("欢迎玩家:"+player1.name+"和"+player2.name);
}
//发牌
public void playerGetPoker(){
player1.hisPoker.add(pokerList.get(0));
System.out.println("玩家"+player1.name+"拿牌啦~~~"+pokerList.get(0).flower+pokerList.get(0).number);
player2.hisPoker.add(pokerList.get(1));
System.out.println("玩家"+player2.name+"拿牌啦~~~");
player1.hisPoker.add(pokerList.get(2));
System.out.println("玩家"+player1.name+"拿牌啦~~~"+pokerList.get(2).flower+pokerList.get(2).number);
player2.hisPoker.add(pokerList.get(3));
System.out.println("玩家"+player2.name+"拿牌啦~~~");
}
//判断两个玩家最大的手牌
//用重新定义的compare来进行sort排序,再打印出第一个就可以啦,排序为从小到大
//本游戏精华部分
public void judgeBigOne(){
Collections.sort(player1.hisPoker);
System.out.println("玩家:"+player1.name+"小的手牌为:"+player1.hisPoker.get(0).flower+
player1.hisPoker.get(0).number+",大的手牌为:"+player1.hisPoker.get(1).flower+
player1.hisPoker.get(1).number);
Collections.sort(player2.hisPoker);
System.out.println("玩家:"+player2.name+"小的手牌为:"+player2.hisPoker.get(0).flower+
player2.hisPoker.get(0).number+",大的手牌为:"+player2.hisPoker.get(1).flower+
player2.hisPoker.get(1).number);
}
//判断胜利
public void judgeWinner(){
List<Poker> temp=new ArrayList<Poker>();
temp.add(player1.hisPoker.get(1));
temp.add(player2.hisPoker.get(1));
Collections.sort(temp);
if(temp.get(1).equals(player1.hisPoker.get(1))){
System.out.println("获胜者为"+player1.name+"!~~恭喜");
}else{
System.out.println("获胜者为"+player2.name+"!~~恭喜");
}
System.out.println("~~~~~游戏结束~~~~~");
}
public static void main(String[] args) {
Major mj=new Major();
mj.buildPoker();
mj.washPoker();
mj.buildPlayer();
mj.playerGetPoker();
mj.judgeBigOne();
mj.judgeWinner();
}
}