基于数组值的图像

时间:2012-11-02 23:46:41

标签: android arrays image dice

我有一个骰子游戏,我正在尝试更改骰子滚动到“死亡”图像后选择的模具图像。我已经尝试了所有我能想到的东西,并且我总是在索引中找到值“0”或其他东西,但从来没有正确的死。

选择骰子时,会将其值设为负数。示例I选择6将值更改为-6并将模具更改为-6模具图像

如何让它显示并保留我想要的“DEAD”图像。

这是获取图像的区域

            //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;
            }   
}

1 个答案:

答案 0 :(得分:0)

由于rollDice()方法中的以下行,掷骰子值在卷上永远不会为零:

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

这对我来说似乎合乎逻辑。为什么模具上的值永远为零?