曬一下自己的,貌似比較玩家最大牌友點(diǎn)問題,有木有可以指出一下,是啥問題?
package?com.imooc; import?java.util.ArrayList; import?java.util.List; public?class?Person?{ ???public?String?id; ???public?String?name; ???public?List<Poker>?ownPoker?=?new?ArrayList<Poker>(); ???public?Person(String?id,?String?name){ ???????this.id?=?id; ???????this.name?=?name; ???????this.ownPoker?=?new?ArrayList<Poker>(); ???} }
2016-01-20
2016-01-20
2016-01-20
2016-01-20
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
?* 游戲類
?*/
public class PlayGame {
??? public List<Poker> pokers = new ArrayList<Poker>();
??? public List<Person> players= new ArrayList<Person>();
??? private?? String[] cardVals = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
??? private static String[] cardColors = {"紅桃","黑桃","梅花","方塊"};
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?PlayGame pg = new PlayGame();
??????? System.out.println("------創(chuàng)建撲克牌------");
??????? pg.createPoker();
??????? System.out.println("--------創(chuàng)建玩家---------");
??????? pg.createPlayer();
??????? pg.shuffer();
??????? System.out.println("-------開始發(fā)牌--------");
??????? pg.deal();
??????? pg.comparePoker();
?? ?}
?? ?
?? ?/**
?? ? * 創(chuàng)建撲克牌
?? ? */
?? public void createPoker(){
?? ??? for(int i=0; i<cardColors.length;i++){
???? ??? ? String cardColor = cardColors[i];
???? ??? ? for(int j=0; j<cardVals.length; j++){
???? ??? ??? ? String cardVal =? cardVals[j];
???? ??? ??? ? pokers.add(new Poker(cardVal,cardColor));
???? ??? ? }
????? }
????? System.out.print("為[");
????? for(Poker poker:pokers){
????? System.out.print(poker.cardColor+poker.cardVal+ ",");
????? }
????? System.out.print("]");
?? }
?? /**
??? * 洗牌
??? */
?? public void shuffer(){
?? ??? System.out.println("-------開始洗牌--------");
?? ??? Collections.shuffle(pokers);
?? ??? System.out.println("-------結(jié)束洗牌--------");
?? ?? ?
?? ??? // 測(cè)試洗牌是否成功
?? ?? /* for(Poker poker:pokers){
?? ??? ??? System.out.print(poker.cardColor+poker.cardVal);
?? ??? }*/
?? }
? ?
?? /**
??? * 發(fā)牌
??? */
?? public void deal(){
?? ? int j = 0;
?? ? for(int i=0; i<2; i++){
?? ??? ? for(Person player:players){
?? ??? ??? ? System.out.println("玩家"+player.name+"拿牌");
?? ??? ??? ? Poker poker = pokers.get(j);
?? ??? ??? ? player.ownPoker.add(poker);
?? ??? ??? ? j++;
?? ??? ??? }
?? ?? }
?? ? for(Person player:players){
?? ??? ? System.out.print("玩家"+player.name+"擁有");
?? ??? ? for(Poker poker:player.ownPoker){
?? ??? ??? ?System.out.print(poker.cardColor+poker.cardVal);
?? ??? ? }
?? ??? ? System.out.println();
?? ? }
?? }
? ?
?/**
? *比較牌的大小
? */
?? public void comparePoker(){
?? ??? ComparatorPoker cpr = new ComparatorPoker();
?? ?? ?
?? ??? //1.先比較玩家自己手里的牌;
?? ????? for(Person player:players){
?? ??????? Collections.sort(player.ownPoker, cpr);
?? ??????? System.out.println("玩家:"+player.name+"最大的牌為"+
?? ???????????? player.ownPoker.get(player.ownPoker.size()-1).cardColor+
?? ???????????? player.ownPoker.get(player.ownPoker.size()-1).cardVal);
?? ????? }
?? ??? //2.比較對(duì)手玩家最大的牌;
?? ????? System.out.println(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0)));
?? ?????? if(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0))>0){
?? ??? ??? ? System.out.println("玩家"+players.get(0).name+"獲勝");
?? ?????? }else{
?? ??? ??? ?? System.out.println("玩家"+players.get(1).name+"獲勝");
?? ?????? }
?? }
? ?
?? /**
??? * 創(chuàng)建玩家
??? */
? public void createPlayer(){
?? ?
?? ? for(int i=0; i<2; i++){
?? ??? ? System.out.println("請(qǐng)輸入"+(i+1)+"位玩家的ID");
?? ??? ?
?? ??? ? Boolean flag =false;
?? ??? ?
?? ??? ? while(true){
?? ??? ??? ? try{
?? ??? ??? ??? ? Scanner sc = new Scanner(System.in);
?? ??? ??? ??? ? String playerId;
?? ??? ??? ??? ?? playerId = sc.nextInt()+"";
?? ??? ??? ??? ?? System.out.println("請(qǐng)輸入"+(i+1)+"位玩家的姓名");
?? ??? ??? ??? ?? String playerName = sc.next();
?? ??? ??? ??? ?? players.add(new Person(playerId,playerName));
?? ??? ??? ??? ?? break;
?? ??? ??? ??? ? }catch(Exception e){
?? ??? ??? ??? ? System.out.println("您輸入的不是整數(shù),請(qǐng)重新輸入");
?? ??? ??? ??? ? continue;
?? ??? ??? ? }
?? ??? ? }
?? ? }
?? ? System.out.println("成功添加");
?? ? for(Person player:players){
?? ??? ? System.out.println("歡迎玩家:"+player.id+":"+player.name);
?? ? }
? }
?
}
2016-01-20
2016-01-20