//Poker.java
package?com.imooc;
public?class?Poker?implements?Comparable<Poker>{
private?String?num;
private?String?color;
@Override
public?int?hashCode()?{
final?int?prime?=?31;
int?result?=?1;
result?=?prime?*?result?+?((color?==?null)???0?:?color.hashCode());
result?=?prime?*?result?+?((num?==?null)???0?:?num.hashCode());
return?result;
}
@Override
public?boolean?equals(Object?obj)?{
if?(this?==?obj)
return?true;
if?(obj?==?null)
return?false;
if?(!(obj?instanceof?Poker))
return?false;
Poker?other?=?(Poker)?obj;
if?(color?==?null)?{
if?(other.color?!=?null)
return?false;
}?else?if?(!color.equals(other.color))
return?false;
if?(num?==?null)?{
if?(other.num?!=?null)
return?false;
}?else?if?(!num.equals(other.num))
return?false;
return?true;
}
public?Poker(String?num,?String?color){
this.num?=?num;
this.color?=?color;
}
public?String?getNum()?{
return?num;
}
public?void?setNum(String?num)?{
this.num?=?num;
}
public?String?getColor()?{
return?color;
}
public?void?setColor(String?color)?{
this.color?=?color;
}
@Override
public?int?compareTo(Poker?o)?{
//?TODO?自動生成的方法存根
String?pokerNum?=?"2,3,4,5,6,7,8,9,10,J,Q,K,A";
String?pokerColor?=?"方塊,梅花,紅桃,黑桃";
int?flag;
flag?=?pokerNum.indexOf(this.getNum())-pokerNum.indexOf(o.getNum());
if(flag?==?0){
flag?=?pokerColor.indexOf(this.getColor())-pokerColor.indexOf(o.getColor());
}
return?flag;
}
}
//Player.java
package?com.imooc;
import?java.util.ArrayList;
import?java.util.List;
public?class?Player?{
private?int?id;
private?String?name;
private?List<Poker>?handPoker;
public?Player(int?id,?String?name){
this.id?=?id;
this.name?=?name;
handPoker?=?new?ArrayList<Poker>();
}
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;
}
public?List<Poker>?getHandPoker()?{
return?handPoker;
}
public?void?setHandPoker(List<Poker>?handPoker)?{
this.handPoker?=?handPoker;
}
}
//MakePoker.java
package?com.imooc;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Scanner;
public?class?MakePoker?{
private?List<Poker>?pokerList;
public?List<Poker>?getPokerList()?{
return?pokerList;
}
public?void?setPokerList(List<Poker>?pokerList)?{
this.pokerList?=?pokerList;
}
public?MakePoker(){
pokerList?=?new?ArrayList<Poker>();
String[]?nums?=?{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[]?colors?=?{"黑桃","紅桃","梅花","方塊"};
?
for(int?i=0;?i<colors.length;?i++){
for(int?j=0;?j<nums.length;?j++){
pokerList.add(new?Poker(nums[j],colors[i]));
}
}
System.out.println("撲克牌有:");
for?(Poker?poker?:?pokerList)?{
System.out.print(poker.getColor()+poker.getNum()+"?");
}
System.out.println();
}
public?static?void?main(String[]?args)?{
System.out.println("--------------撲克牌游戲開始--------------");
System.out.println("--------------創建撲克牌---------------");
MakePoker?mp?=?new?MakePoker();
System.out.println("撲克牌創建成功!");
Scanner?input?=?new?Scanner(System.in);
List<Player>?playerList?=?new?ArrayList<Player>();
System.out.println("---------------創建玩家----------------");
for(int?i=0;?i<2;?i++){
try{
System.out.println("請輸入玩家的id:");
int?id?=?input.nextInt();
System.out.println("請輸入玩家的名字:");
String?name?=?input.next();
playerList.add(new?Player(id,name));
System.out.println("創建玩家"+name+"成功!");
}catch(Exception?e){
System.out.println("輸入的id必須為整型!");
}
}
System.out.println("--------------開始洗牌---------------");
Collections.shuffle(mp.getPokerList());
System.out.println("--------------開始發牌---------------");
for(int?j=0;?j<2;?j++){
for?(Player?player?:?playerList)?{
player.getHandPoker().add(mp.getPokerList().get(0));
mp.getPokerList().remove(0);
}
}
System.out.println("-----------發牌結束開始比較大小----------");
Collections.sort(playerList.get(0).getHandPoker());
Collections.sort(playerList.get(1).getHandPoker());
List<Poker>?compareList?=?new?ArrayList<Poker>();
compareList.add(playerList.get(0).getHandPoker().get(1));
System.out.println("玩家"+playerList.get(0).getName()+"最大的牌是"+playerList.get(0).getHandPoker().get(1).getColor()+playerList.get(0).getHandPoker().get(1).getNum());
compareList.add(playerList.get(1).getHandPoker().get(1));
System.out.println("玩家"+playerList.get(1).getName()+"最大的牌是"+playerList.get(1).getHandPoker().get(1).getColor()+playerList.get(1).getHandPoker().get(1).getNum());
Collections.sort(compareList);
for?(Player?player?:?playerList)?{
if(player.getHandPoker().contains(compareList.get(1))){
System.out.println("-----------"+player.getName()+"獲勝------------");
}
}
?
for?(Player?player?:?playerList)?{
System.out.print("玩家"+player.getName()+"的手牌是:");
for?(Poker?poker?:?player.getHandPoker())?{
System.out.print(poker.getColor()+poker.getNum()+"?");
}
System.out.println("");
}
}
}
2016-03-02
666666666666666