将值从一个数组添加到另一个数组

时间:2016-04-26 19:39:42

标签: java arrays

我正在为Yahtzee游戏编写代码,我正在处理的类接受一定数量的具有指定值的骰子,这在构造函数中确定。此类中还使用了两个数组,Available []和Fixed []。所有骰子都在Available []数组中开始,而Fixed []数组与Available []数组具有相同的长度,但对于它的所有值都为0,因为任何低于1的值都不是用于其他评分方法。

有一个名为keep()的方法可以为您提供一个值,该值应该从Available []数组移动到Fixed []数组。如果keep给出的值不在Available array []中,则忽略它。

我知道您无法从数组中删除值,但我知道您可以更改它们。我编写了一个测试用例,它调用keep()方法来保存值3和5,这两个值都可以在[3,3,3,5,6]的可用数组中找到。问题是,当我调用该方法时,它返回一个新的[3,3,5,0,0]的可用数组和一个[0,0,0,0,0]的固定数组。相反,我希望可用的数组为[3,3,6,0,0],而固定数组为[3,5,0,0,0]。这是我对keep方法的代码。

 public void keep(int value)
  {
  if(rolls < rollsMax)
  {
  for(int i = 0; i < Available.length - 1; i++)
  {
      if(Available[i] == value)
      {
          Fixed[i] = Available[i];
          Available[i] = Available[i + 1];
          Available[Available.length - 1] = 0;
      }
  }

  }
}

具体来说,我不明白为什么

Fixed[i] = Available[i]

未将值添加到Fixed数组。任何帮助,将不胜感激。

以下是整个代码:

    package hw3;

import java.util.Random;

/**
 * This class represents values of a group of dice for a dice game such as Yahtzee in which 
 * multiple rolls per turn are allowed. The number of faces on the dice, 
 * the number of dice in the Hand, and the maximum number of rolls are configurable 
 * via the constructor. At any time some of the dice may be <em>available</em>
 * to be rolled, and the other dice are <em>fixed</em>.  Calls to the 
 * <code>roll()</code> method will select new, random values for the available
 * dice only.  After the maximum number of rolls, all dice are automatically
 * fixed; before that, the client can select which dice to "keep" (change from
 * available to fixed) and which dice to "free" (change from fixed to
 * available).
 * <p>
 * Note that valid die values range from 1 through the given
 * <code>maxValue</code>. 
 */
public class Hand
{
    private int[] fixed;
    private int[] available;
    private int[] values;
    private int groupDice;
    private int valueMax;
    private int rollsMax;
    private int rolls;


  /**
   * Constructs a new Hand in which each die initially has 
   * the (invalid) value zero. 
   * @param numDice
   *   number of dice in this group
   * @param maxValue
   *   largest possible die value, where values range from 1
   *   through <code>maxValue</code>
   * @param maxRolls
   *   maximum number of total rolls
   */
  public Hand(int numDice, int maxValue, int maxRolls)
  {
    groupDice = numDice;
    valueMax = maxValue;
    rollsMax = maxRolls;
    available = values;
  }   

  /**
   * Constructs a new Hand in which each die initially has 
   * the value given by the <code>initialValues</code> array.
   * If the length of the array is greater than the number of dice, the
   * extra values are ignored.  If the length of the array is smaller
   * than the number of dice, remaining dice
   * will be initialized to the (invalid) value 0.
   * <p>
   * This version of the constructor is primarily intended for testing.
   * @param numDice
   *   number of dice in this group
   * @param maxValue
   *   largest possible die value, where values range from 1
   *   through <code>maxValue</code>
   * @param maxRolls
   *   maximum number of total rolls
   * @param initialValues
   *   initial values for the dice
   */
  public Hand(int numDice, int maxValue, int maxRolls, int[] initialValues)
  {
    groupDice = numDice;
    values = new int[numDice];
    valueMax = maxValue;
    rollsMax = maxRolls;
    available = values;

    for(int i = 0; i < numDice; i++)
    {
        if(i >= initialValues.length)
        {
            values[i] = 0;
        }
        else
        {
        values[i] = initialValues[i];
        }
    }
  }  

  /**
   * Returns the number of dice in this group.
   * @return
   *   number of dice in this group
   */
  public int getNumDice()
  {
    return groupDice;
  }

  /**
   * Returns the maximum die value in this group.
   * Valid values start at 1.
   * @return
   *   maximum die value
   */
  public int getMaxValue()
  {
    return valueMax;
  }

  /**
   * Rolls all available dice; that is, each available
   * die value in this group is replaced by a randomly generated
   * value produced by the given random number generator.
   * @param rand
   *   random number generator to be used for rolling dice
   */
  public void roll(Random rand)
  {
      rand = new Random();
      int values = rand.nextInt(valueMax) + 1;
  }

  /**
   * Selects a die value to be moved from the available dice to the
   * fixed dice. Has no effect if the given value is 
   * not among the values in the available dice. Has no effect if
   * the number of rolls has reached the maximum.
   * @param value
   *   die value to be moved from available to fixed
   */
  public void keep(int value)
  {
      if(rolls < rollsMax)
      {
      for(int i = 0; i < available.length; i++)
      {
          if(available[i] == value)
          {
              fixed[i] += available[i];
              available[i] = available[i + 1];
              available[available.length - 1] = 0;
          }
      }

      }
  }

  /**
   * Selects a die value to be moved from the fixed dice to
   * the available dice, so it will be re-rolled in the
   * next call to <code>roll()</code>. Has no effect if the given value is 
   * not among the values in the fixed dice. Has no effect if
   * the number of rolls has reached the maximum.
   * @param value
   *   die value to be moved
   */
  public void free(int value)
  {
    if(rolls < rollsMax)
    {

    }
  }

  /**
   * Causes all die values be moved from the available dice to the
   * fixed dice. Has no effect if
   * the number of rolls has reached the maximum.
   */
  public void keepAll()
  {
    if(rolls < rollsMax)
    {
        for(int i = 0; i < available.length; i++)
        {
            fixed[i] = available[i];
        }
        available[available.length - 1] = 0;
    }
  }

  /**
   * Causes all die values be moved from the fixed dice to the
   * available dice. Has no effect if
   * the number of rolls has reached the maximum.
   */
  public void freeAll()
  {
    if(rolls < rollsMax)
    {
        for(int i = 0; i < available.length; i++)
        {
            available[i] = fixed[i];
        }
        fixed[fixed.length - 1] = 0;
    }
  }

  /**
   * Determines whether there are any dice available to be 
   * rolled in this group.
   * @return
   *   true if there are no available dice, false otherwise
   */
  public boolean isComplete()
  {
      for(int i = 0; i < available.length; i++)
      {
          if(available[i] > 0)
          {
              return false;
          }
      }
   return true;
  }

  /**
   * Returns the values of the dice that are currently fixed (not
   * available to be rerolled) in ascending order.
   * @return
   *   values of the dice that are currently fixed
   */
  public int[] getFixedDice()
  {
    fixed = new int[groupDice];
    return fixed;
  }

  /**
   * Returns the values of the dice that are currently available to
   * be rerolled by a subsequent call to <code>roll()</code>,
   * in ascending order.
   * @return
   *   dice that are available to be rerolled
   */
  public int[] getAvailableDice()
  {
    return available;
  }

  /**
   * Returns all die values in this group, in ascending order.
   * @return
   *   all die values in this group
   */
  public int[] getAll()
  {
    for(int i = 0; i < values.length; i++)
    {
        for(int j = i + 1; j < values.length; j++)
        {
            int temp = 0;
            if(values[i] > values[j])
            {
                temp = values[i];
                values[i] = values[j];
                values[j] = temp;
            }
        }
    }

    return values;
  }

1 个答案:

答案 0 :(得分:0)

在你看之前你以哪种方式得到你的固定[]?如果通过上面代码中的getFixedDice(),它总是返回默认初始化为0的新数组。

这是我粗略的版本:

    private static int[] available = new int[]{3,3,3,5,6};
    private static int[] fixed = new int[available.length];

    public static void main(String[] args)
    {
        Main.keep(3);
        Main.keep(5);

        System.out.println(Arrays.toString(available));
        System.out.println(Arrays.toString(fixed));
    }

    public static void keep(int value) 
    {
          for (int i = 0; i < available.length; i++)
          {
              if(available[i] == value)
              {
                  for (int j = 0; j < fixed.length; j++) 
                  {
                      if (fixed[j] == 0) 
                      {
                          fixed[j] = value;
                          break;
                      }
                  }
                  for(int k = i; k<available.length-1; k++) 
                  { 
                      available[k] = available[k+1]; 
                  }
                  available[available.length-1] = 0;
                  break;
              }
          }
    }

输出:

[3,3,6,0,0]

[3,5,0,0,0]