package?pokerGame;
import?java.util.ArrayList;
import?java.util.List;
public?class?Player?{
//id只能是整數(shù)
private?int?id;
private?String?name;
private?List<Poker>?cards;
public?Player(){
this.id=1;
this.name="Not?named";
this.cards=new?ArrayList<Poker>();
}
public?Player(int?id,String?name){
this.id=id;
this.name=name;
this.cards=new?ArrayList<Poker>();
}
//給玩家添加手牌
public?void?addCard(Poker?poker){
this.cards.add(poker);
}
//返回玩家最大的手牌
public?Poker?getMaxCard(){
//得到兩張手牌?并比較大小?返回最大的那張
Poker?poker1=this.cards.get(0);
Poker?poker2=this.cards.get(1);
if(poker1.compareTo(poker2)==1){
return?poker1;
}
else{
return?poker2;
}
}
public?void?outputCards(){
if(!this.cards.isEmpty()){
Poker?poker1=this.cards.get(0);
Poker?poker2=this.cards.get(1);
System.out.println(this.name+":["+poker1.getColor()+poker1.getValue()
+","+poker2.getColor()+poker2.getValue()+"]");
}
}
public?int?getId()?{
return?id;
}
public?String?getName()?{
return?name;
}
public?List<Poker>?getCards(){
return?this.cards;
}
public?void?setCards(List<Poker>?cards)?{
this.cards?=?cards;
}
}
package?pokerGame;
import?java.util.List;
public?class?Poker?implements?Comparable<Poker>{
private?String?color;
private?String?value;
public?Poker(String?color,String?value){
this.color=color;
this.value=value;
}
//實(shí)現(xiàn)可比較接口?如果兩者相等?返回0?如果本牌大于參數(shù)?返回1?否則返回-1
@Override
public?int?compareTo(Poker?o)?{
//?TODO?Auto-generated?method?stub
//得到花色數(shù)組和點(diǎn)數(shù)數(shù)組
List<String>?colors=globalValues.colors;
List<String>?values=globalValues.values;
//把用字符串表示的點(diǎn)數(shù)轉(zhuǎn)換為整型變量?注意A,J,Q,K要轉(zhuǎn)換成數(shù)字
String[]?tempValue={this.value,o.getValue()};
int[]?value=new?int[2];
for(int?i=0;i<2;i++){
switch(tempValue[i]){
case?"A":
value[i]=1;
break;
case?"J":
value[i]=11;
break;
case?"Q":
value[i]=12;
break;
case?"K":
value[i]=13;
break;
default:
value[i]=Integer.valueOf(tempValue[i]);
}
}
int?value1=value[0];
int?value2=value[1];
if(value1>value2){
return?1;
}
else?if(value1<value2)
{
return?-1;
}
//兩張牌的點(diǎn)數(shù)相等??則比較花色
else{
String?color1=this.color;
String?color2=o.getColor();
int?index1=colors.indexOf(color1);
int?index2=colors.indexOf(color2);
if(index1>index2){
return?1;
}
else?if(index1<index2){
return?-1;
}
else{
return?0;
}
}
}
public?String?getColor()?{
return?color;
}
public?String?getValue()?{
return?value;
}
}
package?pokerGame;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.Iterator;
import?java.util.List;
public?class?Cards?{
//所有的52張牌?聲明為L(zhǎng)ist是為了使用Collections.shuffle(List<?>?list)
private?List<Poker>?cards;
public?Cards(){
System.out.println("------------創(chuàng)建撲克牌------------");
cards=new?ArrayList<Poker>();
createCards();
}
public?void?createCards(){
List<String>?colors=globalValues.colors;
List<String>?values=globalValues.values;
//添加52張牌
for(int?i=0;i<4;i++){
for(int?j=0;j<13;j++){
Poker?poker=new?Poker(colors.get(i),values.get(j));
cards.add(poker);
}
}
System.out.println("------------撲克牌創(chuàng)建成功!------------");
}
public?void?outputCards(){
if(!cards.isEmpty()){
Iterator<Poker>?iterator=this.cards.iterator();
System.out.print("為:[");
while(iterator.hasNext()){
Poker?poker=iterator.next();
System.out.print(poker.getColor()+poker.getValue()+",");
}
System.out.println("]");
}
}
//洗牌
public?void?shuffleCards(){
System.out.println("------------開(kāi)始洗牌---------");
Collections.shuffle(this.cards);
System.out.println("------------洗牌結(jié)束---------");
}
//給玩家發(fā)牌
public?void?sendCards(List<Player>?players){
System.out.println("--------開(kāi)始發(fā)牌-------");
//每個(gè)玩家拿兩張牌
for(int?j=0,index=0;j<2;j++){
for(int?i=0;i<players.size();i++,index++){
System.out.println("----玩家:"+players.get(i).getName()+"--拿牌");
//交替順序得到前4張牌
Poker?poker=this.cards.get(index);
players.get(i).addCard(poker);
}
}
System.out.println("------發(fā)牌結(jié)束!---");
}
}
package?pokerGame;
import?java.util.Arrays;
import?java.util.HashSet;
import?java.util.List;
public??class?globalValues?{
public?static?List<String>?colors=
Arrays.asList(new?String[]{"方塊","梅花","紅桃","黑桃"});
public?static?List<String>?values=
Arrays.asList(new?String[]
{"A","2","3","4","5","6","7","8","9","10","J","Q","K"});
}
package?pokerGame;
import?java.util.ArrayList;
import?java.util.InputMismatchException;
import?java.util.List;
import?java.util.Scanner;
public?class?PokerGame?{
private??Scanner?console;
private?Cards?cards;
private?List<Player>?players;?
public?PokerGame(){
console=new?Scanner(System.in);
}
public?void?gameStart(){
createCards();
createPlayers();
dispatchCards();
startCompare();
}
//創(chuàng)建撲克牌并進(jìn)行打印輸出并進(jìn)行洗牌
private?void?createCards(){
cards=new?Cards();
cards.outputCards();
cards.shuffleCards();
}
private?void?createPlayers(){
players=new?ArrayList<Player>();
//創(chuàng)建兩個(gè)玩家
for(int?i=0;i<2;i++){
try{
System.out.println("請(qǐng)輸入第"+(i+1)+"位玩家的ID和姓名:");
System.out.println("輸入ID:");
int?id=console.nextInt();
System.out.println("輸入姓名:");
String?name=console.next();
Player?player1=new?Player(id,name);
players.add(player1);
}
catch(InputMismatchException?e){
System.out.print("請(qǐng)輸入整數(shù)類(lèi)型的ID!");
i--;
console.next();
}
}
System.out.println("—————?dú)g迎玩家:"+players.get(0).getName());
System.out.println("—————?dú)g迎玩家:"+players.get(1).getName());
}
//給玩家發(fā)牌
private?void?dispatchCards(){
cards.sendCards(players);
}
//開(kāi)始游戲
private?void?startCompare(){
Poker[]?pokers=new?Poker[2];
System.out.println("-----開(kāi)始游戲----");
for(int?i=0;i<2;i++){
Player?player=players.get(i);
Poker?poker=player.getMaxCard();
System.out.println("玩家"+player.getName()+"最大的手牌為:"
+poker.getColor()+poker.getValue());
pokers[i]=poker;
}
//輸出輸贏結(jié)果
if(pokers[0].compareTo(pokers[1])==1){
System.out.println("----玩家"+players.get(0).getName()+"獲勝----");
}
else?{
System.out.println("玩家"+players.get(1).getName()+"獲勝");
}
//輸出玩家的手牌
System.out.println("玩家各自的手牌為:");
players.get(0).outputCards();
players.get(1).outputCards();
}
}
package?pokerGame;
public?class?testMain?{
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
PokerGame?pokerGame=new?PokerGame();
pokerGame.gameStart();
}
}
2015-07-21
截圖 ~