使用数组跟踪在计算分数时使用了哪些骰子
Using an array to track which dice were used in the calculation of the score
我在 GetScore 中使用数组 diceUsed 来检查是否使用了一个骰子来增加总分。如果确实相加,则骰子的输出周围应该有“-”(例如掷 1:-1- 3)。但是我的代码的输出没有正确生成。在我的 GetScore 函数的底部,我 运行 一个循环检查分数是否随着骰子的输入而改变。然而,这似乎是错误的。必须在循环中更改什么逻辑才能创建正常运行的输出?在我的代码的最底部是生成的和预期的结果。
int scoring[7][7] =
{
{0, 0, 0, 0, 0, 0, 0},
{0, 100, 200, 1000, 2000, 4000, 8000},
{0, 0, 0, 200, 400, 800, 1600},
{0, 0, 0, 300, 600, 1200, 2400},
{0, 0, 0, 400, 800, 1600, 3200},
{0, 50, 100, 500, 1000, 2000, 4000},
{0, 0, 0, 600, 1200, 2400, 4800}
};
int GetScore (int * dice, int * diceUsed, int numRead)
{
// Initialize variables
int score = 0;
int straight = 1;
int pairs = 0;
// Array to hold which pips were rolled
int pipCount[7] = {};
// Loop through a set of rolls and record the number of times a
// certain die number was rolled
for (int i = 0; i < numRead; i++)
{
// Set number to equal a die rolled
int number = dice[i];
// Add 1 to the pipCount array in the slot of that roll
pipCount[number]++;
}
// Score for straight and pairs, i set to 1 due to first number
// being the number of rolls
// pst
for (int i = 1; i <= numRead; i++)
{
if (pipCount[i] == 2)
pairs++;
if (pipCount[i] == 0)
straight = 0;
}
// Check to see if a straight or 3 pairs were found
if (pairs == 3)
{
for (int i = 1; i <=6; i++)
{
diceUsed[i]++;
}
return 500;
}
if(straight == 1)
{
for (int i = 1; i <= 6; i++)
{
diceUsed[i]++;
}
return 1000;
}
// Checks the position in the scoring array and adds the number in
// that position to the score, loops through the set num of rolls.
// Also checks to see if a die added to the total score and also adds
// one to the array if it was.
for (int i = 1; i <= 6; i++)
{
int scoreChange = score;
score += scoring[i][pipCount[i]];
if (score > scoreChange)
diceUsed[i]++;
}
return score;
}
void WriteDiceInfo (ostream & output, int * dice, int * diceUsed, int numRead, int numRoll)
{
int score = 0;
score = GetScore (dice, diceUsed, numRead);
output << "Roll " << numRoll << ":";
for (int i = 0; i < numRead; i++)
{
// output << "BOOM" << ' ' << diceUsed[i+1];
if (diceUsed[i+1] >= 1)
{
output << " -" << dice[i] << "-";
}
else if (diceUsed[i+1] == 0)
output << ' ' << dice[i];
}
output << " ==> ";
if (score == 0)
output << "Farkle!";
else
output << score;
output << endl;
for (int i = 0; i <=6; i++)
{
diceUsed[i] == 0;
}
}
Generated Output:
Roll 1: -4- -1- -6- -3- -2- -5- ==> 1000
Roll 2: -1- 1 6 1 1 4 ==> 2000
Roll 3: 4 -2- 4 -4- 2 2 ==> 600
Roll 4: -6- 1 6 3 6 ==> 700
Roll 5: -2- -4- -2- -4- -1- -1- ==> 500
Roll 6: -3- 1 ==> 100
Roll 7: 3 4 3 4 6 2 ==> Farkle!
Expected Output:
Roll 1: -4- -1- -6- -3- -2- -5- ==> 1000
Roll 2: -1- -1- 6 -1- -1- 4 ==> 2000
Roll 3: -4- -2- -4- -4- -2- -2- ==> 600
Roll 4: -6- -1- -6- 3 -6- ==> 700
Roll 5: -2- -4- -2- -4- -1- -1- ==> 500
Roll 6: 3 -1- ==> 100
Roll 7: 3 4 3 4 6 2 ==> Farkle!
你的第一个错误是在第二个循环:
for (int i = 1; i <= numRead; i++)
{
if (pipCount[i] == 2)
pairs++;
if (pipCount[i] == 0)
straight = 0;
}
循环的条件必须是i <= 6
。您正在遍历骰子上的数字,而不是遍历骰子。
第二个错误与上一个错误一样,在这部分:
if (pairs == 3)
{
for (int i = 1; i <=6; i++)
{
diceUsed[i]++;
}
return 500;
}
您正在遍历骰子上的数字而不是骰子本身。它应该是这样的:
for (int i = 1; i <= numRead; i++)
{
if (pipCount[i] == 2)
{
diceUsed[i]++;
}
}
下面这个循环也有同样的错误:
if(straight == 1)
{
for (int i = 1; i <= 6; i++)
{
diceUsed[i]++;
}
return 1000;
}
我不知道 "straight" 到底是什么,但我想这应该改成这样:
for (int i = 1; i <= numRead; i++)
{
diceUsed[i]++;
}
你在WriteDiceInfo
的最后一个循环中也犯了同样的错误。
这些是我阅读代码后发现的内容。
我认为您可以找到一些有用的提示:
- 从零开始使用数组。我的意思是使用从元素“0”到元素 "n - 1" 的数组(其中 "n" 是数组的长度,定义它时写的数字)。一开始可能很难,但它会解决很多困惑。
- 当您使用从一开始的数组(通常长度为 "n + 1" 的数组,并且使用来自“1”和 "n" 的元素)时,使您的 for 循环基于减少混乱。写
for (int i = 1; i <= numRead; i++)
而不是 for (int i = 0; i < numRead; i++)
(就像你的 WriteDiceInfo
的第一个循环)。
- 当函数命名为
WriteDiceInfo
时,它应该只写入信息。获取信息并清除它应该是其他事情的职责。你可以让它得到这些作为输入:ostream & output, int * dice, int * diceUsed, int numRead, int score
,然后写出输出。和约。五行 using 和 clearing out 可以在 main 中,或者任何其他想要使用这些的函数中。这样您的代码更易于维护。
我在 GetScore 中使用数组 diceUsed 来检查是否使用了一个骰子来增加总分。如果确实相加,则骰子的输出周围应该有“-”(例如掷 1:-1- 3)。但是我的代码的输出没有正确生成。在我的 GetScore 函数的底部,我 运行 一个循环检查分数是否随着骰子的输入而改变。然而,这似乎是错误的。必须在循环中更改什么逻辑才能创建正常运行的输出?在我的代码的最底部是生成的和预期的结果。
int scoring[7][7] =
{
{0, 0, 0, 0, 0, 0, 0},
{0, 100, 200, 1000, 2000, 4000, 8000},
{0, 0, 0, 200, 400, 800, 1600},
{0, 0, 0, 300, 600, 1200, 2400},
{0, 0, 0, 400, 800, 1600, 3200},
{0, 50, 100, 500, 1000, 2000, 4000},
{0, 0, 0, 600, 1200, 2400, 4800}
};
int GetScore (int * dice, int * diceUsed, int numRead)
{
// Initialize variables
int score = 0;
int straight = 1;
int pairs = 0;
// Array to hold which pips were rolled
int pipCount[7] = {};
// Loop through a set of rolls and record the number of times a
// certain die number was rolled
for (int i = 0; i < numRead; i++)
{
// Set number to equal a die rolled
int number = dice[i];
// Add 1 to the pipCount array in the slot of that roll
pipCount[number]++;
}
// Score for straight and pairs, i set to 1 due to first number
// being the number of rolls
// pst
for (int i = 1; i <= numRead; i++)
{
if (pipCount[i] == 2)
pairs++;
if (pipCount[i] == 0)
straight = 0;
}
// Check to see if a straight or 3 pairs were found
if (pairs == 3)
{
for (int i = 1; i <=6; i++)
{
diceUsed[i]++;
}
return 500;
}
if(straight == 1)
{
for (int i = 1; i <= 6; i++)
{
diceUsed[i]++;
}
return 1000;
}
// Checks the position in the scoring array and adds the number in
// that position to the score, loops through the set num of rolls.
// Also checks to see if a die added to the total score and also adds
// one to the array if it was.
for (int i = 1; i <= 6; i++)
{
int scoreChange = score;
score += scoring[i][pipCount[i]];
if (score > scoreChange)
diceUsed[i]++;
}
return score;
}
void WriteDiceInfo (ostream & output, int * dice, int * diceUsed, int numRead, int numRoll)
{
int score = 0;
score = GetScore (dice, diceUsed, numRead);
output << "Roll " << numRoll << ":";
for (int i = 0; i < numRead; i++)
{
// output << "BOOM" << ' ' << diceUsed[i+1];
if (diceUsed[i+1] >= 1)
{
output << " -" << dice[i] << "-";
}
else if (diceUsed[i+1] == 0)
output << ' ' << dice[i];
}
output << " ==> ";
if (score == 0)
output << "Farkle!";
else
output << score;
output << endl;
for (int i = 0; i <=6; i++)
{
diceUsed[i] == 0;
}
}
Generated Output:
Roll 1: -4- -1- -6- -3- -2- -5- ==> 1000
Roll 2: -1- 1 6 1 1 4 ==> 2000
Roll 3: 4 -2- 4 -4- 2 2 ==> 600
Roll 4: -6- 1 6 3 6 ==> 700
Roll 5: -2- -4- -2- -4- -1- -1- ==> 500
Roll 6: -3- 1 ==> 100
Roll 7: 3 4 3 4 6 2 ==> Farkle!
Expected Output:
Roll 1: -4- -1- -6- -3- -2- -5- ==> 1000
Roll 2: -1- -1- 6 -1- -1- 4 ==> 2000
Roll 3: -4- -2- -4- -4- -2- -2- ==> 600
Roll 4: -6- -1- -6- 3 -6- ==> 700
Roll 5: -2- -4- -2- -4- -1- -1- ==> 500
Roll 6: 3 -1- ==> 100
Roll 7: 3 4 3 4 6 2 ==> Farkle!
你的第一个错误是在第二个循环:
for (int i = 1; i <= numRead; i++)
{
if (pipCount[i] == 2)
pairs++;
if (pipCount[i] == 0)
straight = 0;
}
循环的条件必须是i <= 6
。您正在遍历骰子上的数字,而不是遍历骰子。
第二个错误与上一个错误一样,在这部分:
if (pairs == 3)
{
for (int i = 1; i <=6; i++)
{
diceUsed[i]++;
}
return 500;
}
您正在遍历骰子上的数字而不是骰子本身。它应该是这样的:
for (int i = 1; i <= numRead; i++)
{
if (pipCount[i] == 2)
{
diceUsed[i]++;
}
}
下面这个循环也有同样的错误:
if(straight == 1)
{
for (int i = 1; i <= 6; i++)
{
diceUsed[i]++;
}
return 1000;
}
我不知道 "straight" 到底是什么,但我想这应该改成这样:
for (int i = 1; i <= numRead; i++)
{
diceUsed[i]++;
}
你在WriteDiceInfo
的最后一个循环中也犯了同样的错误。
这些是我阅读代码后发现的内容。
我认为您可以找到一些有用的提示:
- 从零开始使用数组。我的意思是使用从元素“0”到元素 "n - 1" 的数组(其中 "n" 是数组的长度,定义它时写的数字)。一开始可能很难,但它会解决很多困惑。
- 当您使用从一开始的数组(通常长度为 "n + 1" 的数组,并且使用来自“1”和 "n" 的元素)时,使您的 for 循环基于减少混乱。写
for (int i = 1; i <= numRead; i++)
而不是for (int i = 0; i < numRead; i++)
(就像你的WriteDiceInfo
的第一个循环)。 - 当函数命名为
WriteDiceInfo
时,它应该只写入信息。获取信息并清除它应该是其他事情的职责。你可以让它得到这些作为输入:ostream & output, int * dice, int * diceUsed, int numRead, int score
,然后写出输出。和约。五行 using 和 clearing out 可以在 main 中,或者任何其他想要使用这些的函数中。这样您的代码更易于维护。