자바 간이 포커 게임 (모 과 망 작업)

22739 단어 JAVA
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
package cardGame;
/**
 * --------------------      ----------------------
 * 1.       ,      :   ,  ,  ,  ,      :2-10,J,Q,K,A,      .
 * 2.    ,     ID,  ,     ,         .
 * 3.  ,      "     "    .
 * 4.  ,            ,      ,      ,         ,     .
 * 5.         
 *         :                 ,     .
 *                    ,       .  (  >  >  >  )   
 * */
public class Start {

    public static void main(String[] args) {
        ShuffleAllCards Shuffle = new ShuffleAllCards();
        Shuffle.CardsCreate();
        Shuffle.ShuffleCards();
        LetUsPlay began = new LetUsPlay();
        began.CreatePlayers();
        began.GiveCards();
        began.CompareCards();
    }

}
package cardGame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ShuffleAllCards {
    //cards      ,    static,              .size 0 .
    public static List cards;
    public static List oldCards;
    public String[] number = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; 
    public String[] color = {"  ","  ","  ","  "};
    public ShuffleAllCards(){
        ShuffleAllCards.cards = new ArrayList();
        ShuffleAllCards.oldCards = new ArrayList();
    }
    /**
     *     52  
     * */
    public void CardsCreate(){
        System.out.println("-----------    !-----------");
        //        52  .
        //         ,  A,   A,   A,   A,   2,   2,   2,   2, .....
        //  0-51          .
        for(int i=0;ifor (int j = 0; j < color.length; j++) {
                //      A,      A, 
                String OneCard = color[j]+number[i];
                //      List ,list    .
                cards.add(OneCard);
                oldCards.add(OneCard);
            }
        }
        if(cards.size() == 52){
            System.out.println(cards);      
            System.out.println("-----------       -------");
        }
    }
    /**
     *     52     
     * */
    public void ShuffleCards(){
        System.out.println("-----------    -----------");
        Collections.shuffle(cards);
        System.out.println();
        System.out.println("-----------    -----------");
    }
}
package cardGame;

import java.util.ArrayList;
import java.util.List;
/**
 *    , ID/name/  
 *       ShuffleAllCards .
 * */
public class Players {
    public int ID;
    public String name;
    public List HandCards;  

    public Players() {

    }
    //     
    public Players(int ID,String name) {
        this.ID = ID;
        this.name = name;
        //       List,  set     .
        this.HandCards = new ArrayList();
    }
    public Players(int ID,String name,List HandCard){
        this.ID = ID;
        this.name = name;
        //       List,  set     .
        this.HandCards = new ArrayList();
    }   
}
package cardGame;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class LetUsPlay {
    Scanner console = new Scanner(System.in);
    //      List
    public List players;
    public LetUsPlay() {
        //     List
        players = new ArrayList();             
    }

    /**
     *       
     * */
    public void CreatePlayers(){
        System.out.println("-----------      -----------");
        System.out.println("        ?");
        int playerCounter = console.nextInt();
        for(int i=0;itry{
            //         
                System.out.println("    "+(i+1)+"   ID: ");         
                int playerId = console.nextInt();
                System.out.println("    "+(i+1)+"     : ");
                String playerName = console.next(); 
                Players player = new Players(playerId,playerName);
                players.add(player);
            }catch(InputMismatchException ex){
                System.out.println("---        ID!---");
                console.next();
                i--;
                continue;
            }
        }               
    }

    /**
     *      ,            
     * */
    public void GiveCards(){
        //       ,      .  5   
        for(int i=0;i//           ,    1-5  
            players.get(i).HandCards.add(ShuffleAllCards.cards.get(i));
            //      ,   6-10  , 6    1   .
            players.get(i).HandCards.add(ShuffleAllCards.cards.get(i+players.size()));
        }
    }   
    /**
     *          
     *    :                 ,     .
     *               ,       .
     * (  >  >  >  )     A,   Q,   6,   3,
     * */
    public void CompareCards(){
        //     Map                  oldCards            .
        Map theBiggerOne = new HashMap();
        //                  theBiggerOne   
        for(int i=0;i//                
            String firstCard = players.get(i).HandCards.get(0);
            String secondCard = players.get(i).HandCards.get(1);
            //  oldCards       ,cards      ,
            //             (firstCard,secondCard),   5,      5 oldCards    ,
            int first = ShuffleAllCards.oldCards.indexOf(firstCard);
            int second = ShuffleAllCards.oldCards.indexOf(secondCard);
            //       ,         .
            //               ,       theBiggerOne   .
            if(first < second){
                theBiggerOne.put(second, players.get(i));
            }else if(first > second){
                theBiggerOne.put(first,players.get(i));
            }else if(first == second){
                System.out.println("   ,    "+players.get(i).ID +"           ");
            }
        }
        //  KeySet  ,  Map    " " Set  .            .
        Set set = theBiggerOne.keySet(); 
        //            (key ),   Array,
        Object[] obj = set.toArray();
        //   ,     
        Arrays.sort(obj);
        //            oldCards    
        Integer biggestCardNumber = (Integer) obj[obj.length - 1];
        //          value ,                 .
        Players winner = theBiggerOne.get(biggestCardNumber);
        //  oldCards           , "  5"
        String biggestCardName = ShuffleAllCards.oldCards.get(biggestCardNumber);
        //      
        System.out.println("-----------    !-----------");
        System.out.println(winner.ID+"    "+winner.name+"     ,      "+biggestCardName);
        System.out.println("             ? (Y/N)");
        String YorN = console.next();
        System.out.println(YorN);
        //equals  ,==   .
        if(YorN.equals("Y") ){
            //       ,            
            //     ,     .
            System.out.println("        : ");
            //getKey(theBiggerOne,players.get(i))   getkey  ,          .
            //ShuffleAllCards.oldCards.get(getKey(theBiggerOne,players.get(i))),         .
            for(int i=0;i"    "+ players.get(i).name+"    : "+
                                    players.get(i).HandCards.get(0)+"   "+players.get(i).HandCards.get(1)+
                                    "         :"+ 
                                    ShuffleAllCards.oldCards.get(getKey(theBiggerOne,players.get(i))));                 
            }               
        }else if(YorN.equals("N") ){
            System.out.println("-----------    ~~-----------");
        }else{
            System.out.println("   Y N");
        }
    }

    /**
     *   map value  map key,
     *     map  (theBiggerOne) value (players  ),   value   key (       )
     * (          ,        :map    value key(  key   key))
     * ===================================================
     * hashmap key     , value     
     *           get(key) value      key 
     *            ,        value      key.(       )
     * */
     public static Integer getKey(Map map,Players players2){
         Integer key = 0;
         //Map,HashMap     Iteratable  .      for  .
         for(Integer getKey: map.keySet()){
             if(map.get(getKey).equals(players2)){
                 key = getKey;
             }
         }
         return key;
     }
}

= = = = = = = = = = = = = = = = = = 다음은 잘못된 발상 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
package cardGame;
/**
 *        
 * */
public class Temp {
//  //     list                 .
//  List theBigOne = new ArrayList();
//  //                ,       .
//  for(int i=0;i
//      //                
//      String firstCard = players.get(i).HandCards.get(0);
//      String SecondCard = players.get(i).HandCards.get(1);
//      //             
//      String FirstCardPoint = firstCard.substring(2, firstCard.length());
//      String SecondCardPoint = SecondCard.substring(2, SecondCard.length());
//      //             
//      String FirstCardColor = firstCard.substring(0,2);
//      String SecondCardColor = SecondCard.substring(0,2);
//      //equals     ,==      ,switch   jdk1.6     String  
//      //start      A,j,Q,K  1,11,12,13,        .
//      int FirstCardPoint1 = 0,SecondCardPoint1 = 0;
//      if(FirstCardPoint=="A" || SecondCardPoint=="A"){
//          FirstCardPoint1= 1;  SecondCardPoint1 = 1;          
//      }
//      if(FirstCardPoint=="J" || SecondCardPoint=="J"){
//          FirstCardPoint1= 11; SecondCardPoint1 = 11;         
//      }
//      if(FirstCardPoint=="Q" || SecondCardPoint=="Q"){
//          FirstCardPoint1= 12; SecondCardPoint1 = 12;             
//      }
//      if(FirstCardPoint=="K" || SecondCardPoint=="K"){
//          FirstCardPoint1= 13; SecondCardPoint1 = 13;             
//      }
//      //end      A,j,Q,K  1,11,12,13,        .
//      //start        >  >  >     14,15,16,17,        .
//      int FirstCardColor1 =0 ,SecondCardColor1 = 0;
//      if(FirstCardColor=="  " || SecondCardColor=="  "){
//          FirstCardColor1= 17;     SecondCardColor1 = 17;         
//      }
//      if(FirstCardColor=="  " || SecondCardColor=="  "){
//          FirstCardColor1= 16; SecondCardColor1 = 16;         
//      }
//      if(FirstCardColor=="  " || SecondCardColor=="  "){
//          FirstCardColor1= 15; SecondCardColor1 = 15;             
//      }
//      if(FirstCardColor=="  " || SecondCardColor=="  "){
//          FirstCardColor1= 14; SecondCardColor1 = 14;             
//      }
//      //end        >  >  >    14,15,16,17,        .
//      //start                 ,       theBigOne list .
//      if(FirstCardPoint1 > SecondCardPoint1 || Integer.parseInt(FirstCardPoint) > Integer.parseInt(SecondCardPoint)){
//          theBigOne.add(firstCard);
//      }
//      else if(FirstCardPoint1 < SecondCardPoint1 || Integer.parseInt(FirstCardPoint) < Integer.parseInt(SecondCardPoint)){
//          theBigOne.add(SecondCard);
//      }
//      else if(FirstCardPoint1 == SecondCardPoint1 || Integer.parseInt(FirstCardPoint) == Integer.parseInt(SecondCardPoint)){
//          //    ,    
//          if(FirstCardColor1 > SecondCardColor1){
//              theBigOne.add(firstCard);
//          }
//          else if(FirstCardColor1 < SecondCardColor1){
//              theBigOne.add(SecondCard);
//          }
//          else if(FirstCardColor1 == SecondCardColor1){
//              System.out.println("   !          ");
//          }
//      }
//      //end                 ,       theBigOne list .
//      
//  }
//  for (String string : theBigOne) {
//      System.out.println(string);
//  }
}

좋은 웹페이지 즐겨찾기