2016-04-26 109 views
0

我正在编写一个Yahtzee游戏的代码,我正在处理的类采用一定数量的具有指定值的骰子,这是在构造函数中确定的。有两个数组也可以在这个类中使用,Available []和Fixed []。所有的骰子都从Available []数组开始,而Fixed []数组的长度与Available []数组的长度相同,但是它的所有值都为0,因为任何小于1的值都不用于其他评分方法。将一个数组中的值添加到另一个

有一个方法叫做keep(),它给你一个值,并且该值应该从Available []数组移动到Fixed []数组。如果keep给出的值不在Available数组[]中,则忽略它。

我知道你不能从数组中删除值,但我知道你可以改变它们。我写了一个测试用例,它调用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] 

不加入将值固定阵列。任何帮助,将不胜感激。

这里是整个代码:

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; 
    } 
+0

为什么'eclipse'标签? –

+0

这是一个意外,我改变了它 – Zebs

+0

'Fixed [i] = Available [i]'用Fixed数组的值覆盖Fixed数组的值。你想要'+ ='吗? –

回答

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]

+0

你说得对,我修正了getFixedDice()方法和keep()方法,现在它完美地工作。谢谢! – Zebs

相关问题