2012-11-02 47 views
0

我有一個骰子游戲,我試圖改變後骰子旋轉到「死死」的形象,選擇了模具的圖像。我已經試過所有我能想到的,我最後總是發現在指數值「0」或別的東西,但從來沒有正確的模具。形象價值

當選擇骰子它設置它的數值爲負數。例如我選擇6個它的價值變爲-6和模具改變爲-6骰子圖像

我怎樣才能得到它來顯示,並保持「死」像我想要的。

這裏就是它得到

  //Get the Dice Images 
      public Integer getImage(int index) { 
       if (diceValues[index] == 0) { 
         return letterImages.get(index); 

       } else { 
         return diceImages.get((diceValues[index])); 
       } 
      } 

我試圖改變

return letterImages.get(index); 

一切的每一個可能的組合,當我得到它來改變它總是結束圖像的圖像區域達爲「0」或當前選擇的數骰子或者說,我只是不知道它是如何想出了一些其它號碼。

這裏是整個DieManager類

package com.mygames.dice; 

import java.util.HashMap; 
import android.util.Log; 

public class DieManager { 

     // Will return the Indexes of the dice when this is used 
     public static final int INDEX_FLAG = 1; 
     // Will return the values of the dice when this is used 
     public static final int VALUE_FLAG = 2; 
     // Will return the absolute values of the dice when this is used 
     public static final int ABS_VALUE_FLAG = 3; 

     // The array that holds the dice 
     private int[] diceValues = { 0, 0, 0, 0, 0, 0 }; 

     private HashMap<Integer, Integer> diceImages = new HashMap<Integer, Integer>(); 
     private HashMap<Integer, Integer> deadImages = new HashMap<Integer, Integer>(); 
     private HashMap<Integer, Integer> letterImages = new HashMap<Integer, Integer>(); 

     // Sets @newValue to dieValues[@index] 
     public void setValue(int index, int newValue) { 
       Log.w(getClass().getName(), "Index = " + index + " Value = " + newValue); 
       diceValues[index] = newValue; 
     } 

     public DieManager() { 
       super(); 
       initializeMaps(); 
     } 

     private void initializeMaps() { 

       diceImages.put(-6, R.drawable.die6_select); 
       diceImages.put(-5, R.drawable.die5_select); 
       diceImages.put(-4, R.drawable.die4_select); 
       diceImages.put(-3, R.drawable.die3_select); 
       diceImages.put(-2, R.drawable.die2_select); 
       diceImages.put(-1, R.drawable.die1_select); 

       diceImages.put(1, R.drawable.die1_roll); 
       diceImages.put(2, R.drawable.die2_roll); 
       diceImages.put(3, R.drawable.die3_roll); 
       diceImages.put(4, R.drawable.die4_roll); 
       diceImages.put(5, R.drawable.die5_roll); 
       diceImages.put(6, R.drawable.die6_roll); 


       deadImages.put(-1, R.drawable.die1_dead); 
       deadImages.put(-2, R.drawable.die2_dead); 
       deadImages.put(-3, R.drawable.die3_dead); 
       deadImages.put(-4, R.drawable.die4_dead); 
       deadImages.put(-5, R.drawable.die5_dead); 
       deadImages.put(-6, R.drawable.die6_dead); 

       letterImages.put(0, R.drawable.die_no); 
       letterImages.put(1, R.drawable.die_no); 
       letterImages.put(2, R.drawable.die_no); 
       letterImages.put(3, R.drawable.die_no); 
       letterImages.put(4, R.drawable.die_no); 
       letterImages.put(5, R.drawable.die_no); 

     } 

     public void rollDice() { 

       boolean isNewRound = (numOnTable() == 0); 
       for (int j = 0; j < 6; j++) { 

         // If its a new round then the dice value can be changed from 0. 
         // Else it can't 
         if (isNewRound || diceValues[j] != 0) 
           diceValues[j] = (int) ((Math.random() * 6) + 1); 
       } 
     } 

     // Returns the value or absolute value 
     public int getValue(int index, int flag) { 
       if (flag == ABS_VALUE_FLAG) 
         return Math.abs(diceValues[index]); 

       return diceValues[index]; 
     } 

     // If a dice value is 0 then its a letter 
     public int numOnTable() { 
       int count = 6; 
       for (int i : diceValues) { 
         if (i == 0) 
           count--; 
       } 
       return count; 
     } 

     // Picking up makes the dice value 0 
     public void pickUp(int[] highlighted) { 

       for (int i = 0; i < highlighted.length; i++) 
         diceValues[highlighted[i]] = 0; 

     } 

     // A negative value means a die is highlighted 
     public void toggleHighlight(int index) { 
       diceValues[index] *= -1; 
     } 

     public void clearTable() { 
       diceValues[0] = 0; 
       diceValues[1] = 0; 
       diceValues[2] = 0; 
       diceValues[3] = 0; 
       diceValues[4] = 0; 
       diceValues[5] = 0; 

     } 

     // Return the dice that aren't 0 
     public int[] diceOnTable(int flag) { 
       if (flag == ABS_VALUE_FLAG) { 
         int[] array = new int[diceValues.length]; 

         for (int i = 0; i < diceValues.length; i++) 
           array[i] = Math.abs(diceValues[i]); 

         return array; 
       } 

       return diceValues; 
     } 

     //Returns dice that are negative 
     public int[] getHighlighted(int flag) { 
       int[] dirtyArray = { 0, 0, 0, 0, 0, 0 }; 
       int count = 0; 

       for (int j = 0; j < 6; j++) { 
         if (diceValues[j] < 0) { 

           if (flag == INDEX_FLAG) 
             dirtyArray[count++] = j; 

           if (flag == VALUE_FLAG) 
             dirtyArray[count++] = diceValues[j]; 

           if (flag == ABS_VALUE_FLAG) 
             dirtyArray[count++] = Math.abs(diceValues[j]); 
         } 
       } 

       int[] cleanArray = new int[count]; 

       //Returns an array of length 0 
       if (count == 0) 
         return cleanArray; 

       System.arraycopy(dirtyArray, 0, cleanArray, 0, count); 
       return cleanArray; 

     } 

     // Finds in dieValues same @value and excludes @index 
     public int[] findPairs(int index, int flag) { 

       int[] dirtyArray = { 0, 0, 0, 0, 0, 0 }; 

       int count = 0; 

       for (int j = 0; j < 6; j++) { 

         if (j != index 
             && Math.abs(diceValues[j]) == Math.abs(diceValues[index])) { 

           if (flag == INDEX_FLAG) 
             dirtyArray[count++] = j; 

           if (flag == VALUE_FLAG) 
             dirtyArray[count++] = diceValues[j]; 

           if (flag == ABS_VALUE_FLAG) 
             dirtyArray[count++] = Math.abs(diceValues[j]); 
         } 

       } 

       int[] cleanArray = new int[count]; 

       if (count == 0) 
         return cleanArray; 

       System.arraycopy(dirtyArray, 0, cleanArray, 0, count); 
       return cleanArray; 
     } 

      //Get the Dice Images 
      public Integer getImage(int index) { 
       if (diceValues[index] == 0) { 
         return letterImages.get(index); 

       } else { 
         return diceImages.get((diceValues[index])); 
       } 
      } 

      //Get the Number of dice remaining that are not 0 
      public int numDiceRemain() { 
       int counter = 0; 
       for (int j = 0; j < diceValues.length; j++) { 
         if (diceValues[j] > 0) 
          counter++; 
       } 
       return counter; 
      } 
} 

回答

0

骰子值永遠是零上滾動,因爲在你的rollDice以下行的()方法:

diceValues[j] = (int) ((Math.random() * 6) + 1); 

,這似乎合乎邏輯對我來說。爲什麼一個模具的價值永遠是零?

+0

你正確的上卷,但骰子後,選擇「開‘皮卡’」你滾一遍,它的值變爲「0」,因此,它被標記爲得分後,死的死。 – Sobo