我正在編寫一個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;
}
爲什麼'eclipse'標籤? –
這是一個意外,我改變了它 – Zebs
'Fixed [i] = Available [i]'用Fixed數組的值覆蓋Fixed數組的值。你想要'+ ='嗎? –