package?com.imooc.poker_game;
import?com.imooc.poker_game.exception.WrongCommandException;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Scanner;
/**
?*?比較兩位玩家手中的牌:
?*?1)比較兩人手中點數最大的牌,點數大的贏;
?*?2)若兩人點數一樣大,則再按花色比較。
?*
?*?發(fā)牌規(guī)則:
?*?從撲克第一張開始,按照一人一張的順序,每人發(fā)兩張。
?*/
public?class?Game?{
????public?int?numOfGamer?=?2;
????public?int?piecesPerGamer?=?2;
????public?List<Gamer>?gamerInfo(int?numOfGamer){
????????List<Gamer>?gamers?=?new?ArrayList<Gamer>();
????????Scanner?scanner?=?new?Scanner(System.in);
????????for?(int?i=1;?i<=numOfGamer;?i++){
????????????System.out.println("請輸入玩家"+i+"號的信息:");
????????????System.out.print("姓名:");
????????????String?name?=?scanner.next();
????????????System.out.print("ID:");
????????????String?id?=?scanner.next();
????????????try?{
????????????????Gamer?gamer?=?new?Gamer(name,id);
????????????????gamers.add(gamer);
????????????????System.out.println("------------------------");
????????????}?catch?(WrongCommandException?e)?{
????????????????System.out.println(e.getMessage());
????????????????i--;
????????????}
????????}
????????return?gamers;
????}
????public?void?dealing(Poker?poker,?List<Gamer>?gamers){
????????for(int?turns=1;turns<=this.piecesPerGamer;turns++){
????????????for?(int?num=0;?num<this.numOfGamer;num++)?{
????????????????int?index?=?(turns-1)*this.numOfGamer?+?num;
????????????????System.out.println("-----玩家:"+gamers.get(num).name+"-拿牌");
????????????????gamers.get(num).addPiece(poker.pokers.get(index));
????????????}
????????}
????}
????public?Gamer?winner(List<Gamer>?gamers){
????????List<PokerPiece>?bestPieces?=?new?ArrayList<PokerPiece>();
????????for(Gamer?gamer:gamers){
????????????PokerPiece?best?=?gamer.bestPiece();
????????????bestPieces.add(best);
????????????System.out.println("玩家:"+gamer.name+"最大的手牌為:"+best.show());
????????}
????????Collections.sort(bestPieces);
????????PokerPiece?bestOfAll?=?bestPieces.get(0);
????????for?(Gamer?gamer:gamers){
????????????gamer.pieces.contains(bestOfAll);
????????????return?gamer;
????????}
????????return?null;
????}
????public?static?void?main(String[]?args)?{
????????//新建牌局
????????Game?game?=?new?Game();
????????//新建撲克
????????Poker?poker?=?new?Poker();
????????System.out.println("-----新建撲克-----");
????????//洗牌
????????poker.shuffle();
????????System.out.println("-------洗牌-------");
????????//新建玩家
????????List<Gamer>?gamers?=?game.gamerInfo(game.numOfGamer);
????????for(Gamer?gamer:gamers)
????????????System.out.println("歡迎玩家:"+gamer.name);
????????//開始牌局
????????System.out.println("-----開始發(fā)牌-----");
????????game.dealing(poker,gamers);
????????System.out.println("-----結束發(fā)牌-----");
????????System.out.println("-----開始游戲-----");
????????Gamer?winner?=?game.winner(gamers);
????????System.out.println("----玩家:"+winner.name+"獲勝!----");
????????//結束牌局,展示手牌
????????System.out.println("玩家各自的手牌為:");
????????for?(Gamer?gamer:gamers){
????????????System.out.println(gamer.name+":"+gamer.show());
????????}
????}
}
package?com.imooc.poker_game;
import?com.imooc.poker_game.exception.WrongCommandException;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
public?class?Gamer?{
????String?name;
????String?id;
????List<PokerPiece>?pieces;
????public?Gamer(String?name,String?id)?throws?WrongCommandException?{
????????try?{
????????????Integer.parseInt(id);
????????????this.name?=?name;
????????????this.id?=?id;
????????????pieces?=?new?ArrayList<PokerPiece>();
????????}catch?(NumberFormatException?e){
????????????throw?new?WrongCommandException("Gamer?id?should?be?numerical.");
????}
????}
????public?Gamer(){
????}
????/**
?????*?加牌
?????*/
????public?void?addPiece(PokerPiece?piece){
????????pieces.add(piece);
????}
????/**
?????*?最好的牌
?????*/
????public?PokerPiece?bestPiece(){
????????Collections.sort(this.pieces);
????????return?this.pieces.get(0);
????}
????/**
?????*?展示玩家所有牌
?????*/
????public?String?show(){
????????List<String>?pieces_str?=?new?ArrayList<String>();
????????for?(PokerPiece?piece:this.pieces){
????????????pieces_str.add(piece.show());
????????}
????????return?pieces_str.toString();
????}
}
package?com.imooc.poker_game;
import?com.imooc.collection_map.Course;
import?java.util.ArrayList;
import?java.util.Comparator;
import?java.util.List;
public?class?PokerPiece?implements?Comparable<PokerPiece>{
????Integer?pattern;
????Integer?figure;
????public?PokerPiece(Integer?pattern,Integer?figure){
????????this.figure?=?figure;
????????this.pattern?=?pattern;
????}
????@Override
????public?int?compareTo(PokerPiece?o)?{
????????int?rule1?=?o.figure.compareTo(this.figure);
????????if(rule1?==?0){
????????????return?this.pattern.compareTo(o.pattern);
????????}else{
????????????return?rule1;
????????}
????}
????@Override
????public?boolean?equals(Object?obj){
????????if?(this?==?obj)?return?true;
????????if?(obj?==?null)?return?false;
????????if?(!(obj?instanceof?Course))?return?false;
????????PokerPiece?piece?=?(PokerPiece)?obj;?//以上都通過,則強轉obj進一步判斷。
????????if?(this.pattern?==?null?||?this.figure?==?null)?{
????????????if?(piece.pattern?==?null?||?this.figure?==?null)?return?true;
????????????else?return?false;
????????}else{
????????????if(this.figure.equals(piece.figure)&&this.pattern.equals(piece.pattern))?return?true;
????????????else?return?false;
????????}
????}
????public?String?show(){
????????String?figure;
????????String?pattern;
????????switch?(this.figure){
????????????case?11:
????????????????figure?=?"J";
????????????????break;
????????????case?12:
????????????????figure?=?"Q";
????????????????break;
????????????case?13:
????????????????figure?=?"K";
????????????????break;
????????????case?14:
????????????????figure?=?"A";
????????????????break;
????????????default:
????????????????figure?=?this.figure+"";
????????????????break;
????????}
????????switch?(this.pattern){
????????????case?1:
????????????????pattern?=?"黑桃";
????????????????break;
????????????case?2:
????????????????pattern?=?"紅心";
????????????????break;
????????????case?3:
????????????????pattern?=?"梅花";
????????????????break;
????????????case?4:
????????????????pattern?=?"方塊";
????????????????break;
????????????default:
????????????????pattern?=?"";
????????????????break;
????????}
????????return?pattern+figure;
????}
}
package?com.imooc.poker_game;
import?java.util.ArrayList;
import?java.util.Collection;
import?java.util.Collections;
import?java.util.List;
public?class?Poker?{
????public?List<PokerPiece>?pokers?=?new?ArrayList<PokerPiece>();
????public?int[]?patterns?=?new?int[]{1,2,3,4};
????public?int[]?figures?=?new?int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14};
????/**
?????*?新建撲克
?????*/
????public?Poker(){
????????for(int?pattern:patterns){
????????????for(int?figure:figures){
????????????????pokers.add(new?PokerPiece(pattern,figure));
????????????}
????????}
????}
????/**
?????*?洗牌
?????*/
????public?void?shuffle(){
????????Collections.shuffle(pokers);
????}
????/**
?????*?展現牌
?????*/
????public?void?show(){
????????for(PokerPiece?piece:pokers){
????????????System.out.print(piece.show()+",");
????????}
????}
}
package?com.imooc.poker_game.exception;
public?class?WrongCommandException?extends?Exception{
????public?WrongCommandException(String?message)?{
????????super(message);
????}
}