Java 雅兹提满座酒店

Java 雅兹提满座酒店,java,arrays,Java,Arrays,我正在尝试创建一个简单的yahtzee程序,我一直在检查房子是否满了 所示的计数数组是骰子值的数组,例如计数[1]=3表示存在1值的3对。计数[3]=2意味着有一个3value die的2pair /** * If the category is not yet taken, scoreFullHouse * makes sure there are two of one kind and three * of another. If there are, it puts a score

我正在尝试创建一个简单的yahtzee程序,我一直在检查房子是否满了

所示的计数数组是骰子值的数组,例如计数[1]=3表示存在1值的3对。计数[3]=2意味着有一个3value die的2pair

/**
 * If the category is not yet taken, scoreFullHouse
 * makes sure there are two of one kind and three
 * of another.  If there are, it puts a score
 * of 25 into the HOUSE category, marks the category as taken,
 * and adds 25 to the total score.
 *
 * @param count is the count array of all dice values
 */
private void scoreFullHouse(int[] count)
{
    if (!taken[Category.HOUSE.ordinal()]) //Checks if the house category is taken.
    {
        for (int i = 1; i < 7; ++i)
        {
          if (count[i]=2 || count[i]=3)
          {
            if (count[i+1]=3)
            {
                scores[Category.HOUSE.ordinal()] = HOUSE_VALUE; //Makes the HOUSE_VALUE(25) the score
                taken[Category.HOUSE.ordinal()] = true; //Sets category to taken
                total = total + HOUSE_VALUE;  //Adds the total
                break;
            }

        }
    }
}

有很多方法可以做到这一点

int six = 6;
int values[] = new int[six+1]; // waste a byte so it indexes right
for(int i = 0; i < dice.length; i++) {
    values[dice[i]]++;  // keep counts
}

// now do...
boolean hasAThree = false;
boolean hasATwo = false;

for(int i = 1; i <= six; i++) {
    if(values[i] == 3) {
        hasAThree = true;
    }
    if(values[i] == 2) {
        hasATwo = true;
    }
}

if(hasAThree && hasATwo) {
    // full house
}
intsix=6;
int值[]=新的int[six+1];//浪费一个字节,使其索引正确
for(int i=0;i对于(inti=1;i需要注意的几点:

首先,“count[i]=n”将“n”的值赋给索引“i”处“count”中的变量。另一方面,“count[i]==n”计算“count[i]”和“n”是否为相同值的布尔值。因此,您的if语句应如下所示:

if(count[i] == 3){
    // do something
}
第二点并不是从语法上解决任何错误,但在Java(和许多其他语言)中,标准约定从零开始,部分原因是数组和类似的数据结构将它们包含的第一个值计为索引“0”,第二个值计为索引“1”,依此类推。这还有一个额外的好处,即能够使条件小于要循环通过的数组的长度。目前,循环从不检查数组中的第一个值(从索引“1”开始),并且在尝试访问索引“6”处的值时会抛出错误(假设count的长度为6,每个骰子面有一个索引)。下面是一个相当标准的循环示例:

for(int i = 0; i < count.length; i++){
    // do something
}
for(int i=0;i
(还要注意,i++比++i更常用,即使在这种情况下它们是相同的)

实际答案:

至于一个确定满屋的简单方法,请记住总共只有五个骰子,所以满屋只有两个和三个骰子。这一事实,再加上你不能用两个或三个骰子加五个骰子的事实,意味着你只需检查以确保没有骰子值出现在一系列的情况下两次或三次以外的时间:

for(int i = 0; i < 6; i++){
    if(count[i] != 2 && count[i] != 3){
        // any "count" which enters this body is not a full house.
        return;
    }
}
// any "count" that continues through the method beyond the loop will be a full house.
for(int i=0;i<6;i++){
如果(计数[i]!=2和计数[i]!=3){
//任何进入这个身体的“伯爵”都不是满座的。
返回;
}
}
//通过循环之外的方法继续进行的任何“计数”都将是满座。

据我所知,实际的得分部分看起来不错,尽管我不能100%确定只看到这一部分。

什么是-Yahztee满屋?@james:一个值的三个骰子,另一个值的两个。你的第一行是
newint[7]
,因为参数是数组的长度,正如你提到的,我们浪费了一个字节(索引
0
)。
(新的int[6])[6]
将抛出一个ArrayIndexOutboundsException。在
for
边界中也有一个bug。@AlexR yahtzee中只有5个骰子。是的,但每个骰子仍然是六边的。
dice.length==5
但是
values.length==6+1
(浪费的索引)但是for循环是错误的。它找不到任何六。我代码中的for循环通常看起来像
for(int i=0;i
,所以我不必担心:D还需要注意
值[0]==0
无论如何,这样你甚至不必从
1开始就仍然是正确的。循环是错误的,因为零是合法的,而检测可能是解决方案的某些事情的代码实际上一点用处都没有。这篇文章的一半要点是在坏的编码行为成为习惯之前停止它们。没有理由重新设置零索引除了1出现在名单上的次数之外,我很想听到任何可能的情况,这些情况要么需要将其他值设置为零,要么我编写的示例代码将任何不是满屋的东西标识为满屋(反之亦然)。
for(int i = 0; i < 6; i++){
    if(count[i] != 2 && count[i] != 3){
        // any "count" which enters this body is not a full house.
        return;
    }
}
// any "count" that continues through the method beyond the loop will be a full house.