2016-04-26 15 views
0

私はYahtzeeゲームのコードを書いています。私がやっているクラスでは、指定された値を持つ特定の数のダイスがコンストラクタで決定されます。このクラスでも[Available]とFixed []の2つの配列が使用されています。 Fixed []配列はAvailable配列と同じ長さですが、1より小さい値は他のスコアリングに使用されないため、すべてのダイスはAvailable []配列から開始しますメソッド。ある配列から別の配列に値を加算する

keep()メソッドには値があり、その値をAvailable []配列からFixed []配列に移動する必要があります。 keepによって与えられた値がAvailable配列[]にない場合、それは無視されます。

配列から値を削除できないことは知っていますが、変更できることはわかっています。私は、keep()メソッドを呼び出して値3と5を保持するテストケースを作成しました。どちらも[3,3,3,5,6]のAvailable配列にあります。問題は、私がメソッドを呼び出すと、[3、3、5、0、0]の新しいAvailable配列と[0、0、0、0、0]のFixed配列が返されることです。代わりに、利用可能な配列を[3、3、6、0、0]に、固定配列を[3,5,0,0,0]にします。ここでは、私はkeepメソッドのためのコードです。

Fixed[i] = Available[i] 

が固定配列に値を追加されていない理由

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

    } 
} 

具体的には、私は理解していません。どんな助けもありがとう。あなたはそれを見て前に、あなたがあなたの固定を取得

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配列の値をAvailable配列の値で上書きします。 '+ ='をしたいですか? –

答えて

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上記のコードからgetFixedDice()を介して、それは常に0

により、デフォルトでは初期化された新しい配列を返す場合はここに私のラフバージョンです、6,0,0]

[3、5、0、0]

+0

あなたが正しいです、私はkeep()メソッドと一緒にgetFixedDice()メソッドを修正しましたが、今は完全に動作します。ありがとうございました! – Zebs

関連する問題