2

部屋にプレイヤーのセットがあり、すべてのプレイヤーが手札にカードのセットを持っている状況があります。

HashMap<Integer,ArrayList<Integer>> useridToCardsinTheirHand = new HashMap<Integer, ArrayList<Integer>>();

どのプレイヤーも「電話をかける」ことができます。「呼び出し」を行ったプレーヤーの値が最小かどうか (集計カードの最小値) を確認する必要があります。

上記のロジックを続行するには、LinkedhashMap を再度使用します

private static LinkedhashMap<Integer,Integer> useridTotalRank = new LinkedhashMap<Integer,Integer>();

値が同点の場合は、優先アルゴリズムが実行されます。

すべてのロジックで hashMap を使用します。

HashMaps を使用しているため、問題に直面しています。ロジックの流れがぎこちない。

私は再設計を計画しています。私は MultiMap を使用していますが、それでも同様の問題が予想されます。使用するコレクションの種類についての提案はありますか?

私が試した疑似コードは::

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class NewCalling {

    static HashMap<Integer,ArrayList<Integer>> useridToPointsmap = new HashMap<Integer, ArrayList<Integer>>();
    lamposHAshMap hashmapsort = new lamposHAshMap();
    private static HashMap<Integer,Integer> useridTotalRank = new HashMap<Integer,Integer>();

    private static int jokercard = 0x27;
    private static int closedjokercard =0x25 ;
    private static List<Integer> faceCards = new ArrayList<Integer>();



    @SuppressWarnings("unchecked")
    public static void main(String args[]) {

        /**
         * Assuming the calling is made and the Player id is hard Coded
         */
        boolean callingflag = true;
        int callinguserid = 2;

        /*************** Preparing the information which will be given by the ********/
        ArrayList<Integer> cardsArray1 = new ArrayList<Integer>(); 
        ArrayList<Integer> cardsArray2 = new ArrayList<Integer>(); 
        ArrayList<Integer> cardsArray3 = new ArrayList<Integer>(); 
        ArrayList<Integer> cardsArray4 = new ArrayList<Integer>(); 


        cardsArray1.add(0x01);
        cardsArray1.add(0x1A);
        //cardsArray1.add(0x33);

        cardsArray2.add(0x21);
        cardsArray2.add(0x03);
        cardsArray2.add(0x32);

        cardsArray3.add(0x21);
        cardsArray3.add(0x03);
        cardsArray3.add(0x32);

        cardsArray4.add(0x01);
        cardsArray4.add(0x02);
        cardsArray4.add(0x32);
        cardsArray4.add(0x31);


        useridToPointsmap.put(1,cardsArray1);
        useridToPointsmap.put(2,cardsArray2);
        useridToPointsmap.put(3,cardsArray3);
        useridToPointsmap.put(4,cardsArray4);


        faceCards.add(0,10);
        faceCards.add(1,11);
        faceCards.add(2,12);
        faceCards.add(3,13);


        /*************** Preparing the information which will be given by the ********/

        if(callingflag)
        {
            int calledUserTp = totalPointsByUserid(callinguserid,jokercard);
            System.out.println("Total Points of the User Who has made a Call is ::"+calledUserTp);

            HashMap<Integer,Integer> useridTotalRankMap = totalPointsforEveryUser(jokercard);
            LinkedHashMap<Integer,Integer> useridTotalRankMapSorted = new LinkedHashMap<Integer, Integer>();
            useridTotalRankMapSorted = (LinkedHashMap<Integer, Integer>) sortByComparator(useridTotalRankMap);
            for(Map.Entry<Integer, Integer> entry :useridTotalRankMapSorted.entrySet())
            {
                System.out.println( entry.getKey() +"----"+entry.getValue());
                if(entry.getKey() == callinguserid )
                {
                    System.out.println( "GOOD CALL");
                    break;
                }

            }

        }
    }


    /** Gives the Cards Rank    **/
    static int getCardRank(int Cardhexvalue)
    {
        int rank = Cardhexvalue & 15;
        return rank;

    }
    /** Gives the Cards Suit    **/

    static int getCardSuit(int Cardhexvalue)
    {
        int suit = (Cardhexvalue>>4);
        return suit;

    }
    // METHODS REQUIRED 
    private static HashMap<Integer,Integer> totalPointsforEveryUser(int jokerCardVal)
    {
        for(Map.Entry<Integer, ArrayList<Integer>> entry :useridToPointsmap.entrySet())
        {
            int sum = 0;
            int playerId = entry.getKey();
            ArrayList<Integer> cardsList = entry.getValue();

            for (Integer s : cardsList)
            {
                if (getCardRank(s) != getCardRank(jokerCardVal)) {
                    if (faceCards.contains(s))
                    {
                        sum += 10;
                    }
                    else
                    {
                        sum += getCardRank(s);
                    }
                }             
            }
            useridTotalRank.put(playerId, sum);

        }

        return useridTotalRank;
    }


    private static int totalPointsByUserid(int userId,int jokerCardVal)
    {
        ArrayList<Integer> cardsList = useridToPointsmap.get(userId);
        int sum = 0;
        for (Integer s : cardsList)
        {

            if (getCardRank(s) != getCardRank(jokerCardVal)) {
                if (faceCards.contains(s))
                {
                    sum += 10;
                }
                else
                {
                    sum += getCardRank(s);
                }
            }            
        }   
        return sum;
    }

    @SuppressWarnings("unchecked")
    private static Map sortByComparator(Map unsortMap) {

        List list = new LinkedList(unsortMap.entrySet());

        //sort list based on comparator
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getValue())
                .compareTo(((Map.Entry) (o2)).getValue());
            }
        });

        //put sorted list into map again
        Map sortedMap = new LinkedHashMap();
        for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry)it.next();
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }   

    private static boolean checkForNoDuplicates(HashMap<Integer, Integer > useridTotalRankMapSorted)
    {
        Collection<Integer> valuesList = useridTotalRankMapSorted.values();
        Set<Integer> valuesSet = new HashSet<Integer>(useridTotalRankMapSorted.values());

        System.out.println("-----"+ valuesList.size() +"----"+valuesSet.size());
        if(valuesList.size() == valuesSet.size())
        {
            return true;
        }
        else
            return false;

    }

    // Stack

    public static HashMap getDuplicateValues(HashMap in)
    {
        // Clone input HashMap because we're removing stuff from it
        in = (HashMap)in.clone();
        HashMap rval = new HashMap();
        Object[] keys = in.keySet().toArray();

        // iterate through all keys
        for(int x=0;x<keys.length;x++) {
            Object value = in.get(keys[x]);
            in.remove(keys[x]);
            // if value is in input HashMap, store it in duplicate HashMap because it has another value
            if(in.containsValue(value)) {
                rval.put(keys[x],value);
            }
            // if value is in duplicate HashMap, store it also because it HAD another value earlier
            if(rval.containsValue(value)) {
                rval.put(keys[x],value);
            }
        }

        return(rval);
    }


    public static HashMap<Object, ArrayList<Object>> gettingTheDuplicates(HashMap map) {
        HashMap<Object, ArrayList<Object>> newMap =  new HashMap<Object, ArrayList<Object>>();

        Set<Entry> set = map.entrySet();
        for(Entry entry : set) {
            ArrayList list = new ArrayList();
            if(newMap.containsKey(entry.getValue())) {
                list=newMap.get(entry.getValue());
            }
            list.add(entry.getKey());
            newMap.put(entry.getValue(), list);
        }
        return newMap;
    }


    private static void priorityCheck(int playerId1,int playerId2,int jokerCardVal)
    {
        int jokerCountPlayerOne = 0;
        int jokerCountPlayerTwo = 0;
        int openJokerCountPlayerOne = 0;
        int openJokerCountPlayerTwo = 0;
        List<Integer> playerOneCards = useridToPointsmap.get(playerId1);
        List<Integer> playerTwoCards = useridToPointsmap.get(playerId2);
        System.out.println("Current game player cards-----"+playerOneCards);
        System.out.println("Tied game player cards--------"+playerTwoCards);
        int playerOneCardCount =  playerOneCards.size();
        int playerTwoCardCount =  playerTwoCards.size();
        // Hard coded
        // playerOneCardCount = 4;
        //playerTwoCardCount = 4;
        // jokerCountPlayerOne = 1;
        // jokerCountPlayerTwo = 1;
        //openJokerCountPlayerOne =1;
        //openJokerCountPlayerTwo =2;
        System.out.println("---jokerCardVal---"+jokerCardVal);
        System.out.println("---playerOneCardCount---playerTwoCardCount"+playerOneCardCount+"  "+playerTwoCardCount);


        if  (playerOneCards.contains(jokerCardVal))
        {
            openJokerCountPlayerOne++;
        }
        if  (playerTwoCards.contains(jokerCardVal))
        {
            openJokerCountPlayerTwo++;
        }
        if  (playerOneCards.contains(0))
        {
            jokerCountPlayerOne++;
        }
        if  (playerTwoCards.contains(0))
        {
            jokerCountPlayerTwo++;
        }
        if (playerOneCardCount == playerTwoCardCount)
        {
            if (jokerCountPlayerOne == jokerCountPlayerTwo)
            {
                if (openJokerCountPlayerOne == openJokerCountPlayerTwo)
                {
                    System.out.println("Still Tie Occurring---------------");
                }  else
                {
                    if (openJokerCountPlayerOne > openJokerCountPlayerTwo)
                    {
                        System.out.println("First player has high rank  based on open joker");
                    }   else
                    {
                        System.out.println("Second player has high rank  based on open joker");
                    }
                }
            }  else
            {
                if (jokerCountPlayerOne > jokerCountPlayerTwo)
                {
                    System.out.println("First player has high rank  based on joker");
                }   else
                {
                    System.out.println("Second player has high rank  based on joker");
                }
            }
        }  else
        {
            if (playerOneCardCount < playerTwoCardCount)
            {
                System.out.println("First player has high rank based on Count");
            }   else
            {
                System.out.println("Second player has high rank based on count");
            }
        }
    }



    // New Priority Check
    private static List<Integer> priorityNew(ArrayList<Integer> tocompare)
    {

        // ArrayList to array 
        Integer[] sortedArray = (Integer[]) tocompare.toArray();
        bubble_srt(sortedArray,sortedArray.length);
        List<Integer> mapValuesNew = Arrays.asList(sortedArray);

        return mapValuesNew;

    }



    public static void bubble_srt( Integer a[], int n ){
        int i, j,t=0;
        for(i = 0; i < n; i++){
            for(j = 1; j < (n-i); j++){
                //    System.out.print(" I am in first "+a[j-1]+"  "+a[j] +"*********"+whichCardHigher(a[j-1],a[j]));   
                if(WhichuserHigher(a[j-1],a[j])){
                    t = a[j-1];
                    a[j-1]=a[j];
                    a[j]=t;
                }
            }
        }

    }


    public static boolean WhichuserHigher(int user1,int user2)

    {
        if(getNumberofCards(user1) == getNumberofCards(user1) )  
        {
            if(getNumberofJoker(user1) == getNumberofJoker(user2))
            {
                if(getNumberofClosedJoker(user1) == getNumberofClosedJoker(user2))
                {
                    System.out.println("Its a Mega Tie");
                }
                else
                {
                    if(getNumberofClosedJoker(user1) > getNumberofClosedJoker(user2))
                    {
                        return true;
                    }
                    else
                    {   
                        return false;
                    }
                }
            }
            else
            {
                if(getNumberofJoker(user1) > getNumberofJoker(user2))
                {
                    return true;
                }
                else
                {   
                    return false;
                }
            }
        }
        else
        {
            if(getNumberofCards(user1) >getNumberofCards(user2))
            {
                return true;
            }
            else
            {   
                return false;
            }

        }
        return false;

    }

    public static int getNumberofCards(int user)
    {
        int noOfCards = 0;
        for(Map.Entry<Integer, ArrayList<Integer>> entry :useridToPointsmap.entrySet())
        {
            if(entry.getKey() == user)
            {
                noOfCards = entry.getValue().size();
            }
        }
        return noOfCards;

    }
    public static int getNumberofJoker(int user)
    {
        int noOfJokers = 0;
        int count = 0;
        for(Map.Entry<Integer, ArrayList<Integer>> entry :useridToPointsmap.entrySet())
        {
            if(entry.getKey() == user)
            {
                for(int i= 0 ;i< entry.getValue().size();i++)
                {
                    if(Integer.parseInt(entry.getValue().toString()) == jokercard)
                    {
                        count ++;
                    }
                }
            }
        }
        noOfJokers = count;
        return noOfJokers;

    }
    public static int getNumberofClosedJoker(int user)
    {
        int noOfClosedJokers = 0;
        int count = 0;
        for(Map.Entry<Integer, ArrayList<Integer>> entry :useridToPointsmap.entrySet())
        {
            if(entry.getKey() == user)
            {
                for(int i= 0 ;i< entry.getValue().size();i++)
                {
                    if(Integer.parseInt(entry.getValue().toString()) == closedjokercard)
                    {
                        count ++;
                    }
                }
            }
        }
        noOfClosedJokers = count;
        return noOfClosedJokers;

    }

}
4

1 に答える 1

1

Room、Player、Card 用の単純な Java Bean を用意してください (おそらく Card 用は不要かもしれません)。これにより、将来新しい要件が発生したときにクラスのプロパティを変更できます。最終的には、List Room のみを管理する必要があるため、Room オブジェクトを取得した場合、そこから Player を簡単に取得できます。

Class Room{
   String roomName;
   String location;
   String List<Player>;
   // have getters setters for each of them
}

このようにすると、処理コードが存在する場合は関連するクラスにのみ配置できるため、管理も容易になります。たとえば、部屋クラスでは、その部屋のユーザーを返す getLeastValueCard というメソッドを使用でき、部屋全体のループを作成できます。

これが理にかなっていることを願っています。

于 2012-07-26T06:27:10.580 に答える