cs50拼字游戏
我在做cs50拼字游戏,出于某种原因,它只返回数字,而不是告诉我哪个玩家赢了。我知道我肯定在cs50拼字游戏,c,cs50,scrabble,C,Cs50,Scrabble,我在做cs50拼字游戏,出于某种原因,它只返回数字,而不是告诉我哪个玩家赢了。我知道我肯定在for循环中的某个地方出了问题,但我找不到问题:(另外,我在compute\u score函数中添加了int I和int n,因为它一直在说未声明的标识符,我不明白,因为我认为它们在for循环的范围内。这里没有问题,请告知 #include <ctype.h> #include <cs50.h> #include <stdio.h> #include <strin
for
循环中的某个地方出了问题,但我找不到问题:(另外,我在compute\u score
函数中添加了int I
和int n
,因为它一直在说未声明的标识符,我不明白,因为我认为它们在for
循环的范围内。这里没有问题,请告知
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
int compute_score(string word);
int main(void)
{
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
// Score both words
int score1 = compute_score(word1);
int score2 = compute_score(word2);
// TODO: Print the winner
if (score1 > score2)
{
printf("Player1 wins!\n");
}
else if (score1 == score2)
{
printf("Ties!\n");
}
else
{
printf("Player2 wins!\n");
}
}
int compute_score(string word)
{
// TODO: Compute and return score for string
int tem_point[] = {};
int m;
int score = 0;
int i;
int n;
for (i = 0, n = strlen(word); i < n; i++)
{
//Turning to ascii number, "A...Z" correspond "65...90", "a...z" correspond "97...122"
m = get_int("%i", word[i]);
//Comparing ascii numbers to decide whether upper case, lower case or not characters
//Calculate index corresponding to POINTS array and assign points to characters
//If not character, get zero point
if (m < 65 || (m > 90 && m < 97) || m > 122)
{
tem_point[i] = 0;
}
//If upper case
else if (m >= 65 && m <= 90)
{
tem_point[i] = POINTS[m - 65];
}
//If lower case
else
{
tem_point[i] = POINTS[m - 97];
}
score += tem_point[i];
}
return score;
}
get_int
-“提示用户从标准输入中输入一行文本,并返回等效的int;如果文本不代表int或会导致溢出,用户将被重新编译。”-我认为该函数的功能与您预期的有所不同。int temu point[]={}
声明一个无长度的本机int
数组。稍后,您可以将它当作有存储空间的数组来使用,从而调用未定义的行为。注意:如果您希望第i个字符作为一个数字,那么这就是m=word[i];
您可以抛出get_int。作为旁注,您不需要m=get_int(“%i”,word[i]),c中的字符与常量相当。直接使用单词[i]谢谢各位!我接受了你们的建议,用单词[i]替换了m,并在for循环体的第一行声明了int temp_point[],现在当我运行它时,它说是分段错误,我想我的逻辑有问题。
~/pset2/ $ make scrabble
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow scrabble.c -lcrypt -lcs50 -lm -o scrabble
~/pset2/ $ ./scrabble
Player 1: abc
Player 2: abc1
97