提问人:Nermin 提问时间:10/9/2020 更新时间:5/18/2021 访问量:6851
在 C 语言中创建类似拼字游戏的游戏。如何检查字符串是否包含某些字符,然后将字符引用到数组并相应地对其进行评分
Creating a Scrabble like game in C. How to check if a string contains certain chars, then refer the char to an array and score it accordingly
问:
我正在做 CS50,我的任务是创建一个类似“拼字游戏”的游戏。以下是实现详细信息。
目标是让两名玩家输入他们的话,得分较高的玩家获胜。 分数在名为 POINTS[] 的数组中计分。链接到包含额外详细信息的图像。
已经实现的几行代码不是我写的,而是提供的。
我尝试过:我意识到我首先必须将字符串转换为字符。我这样做是这样做的:
`int main(void)
{
int score = 0;
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
for (int i = 0, n = strlen(word1); i < n; i++)
{
char c1 = word1[i];
char c2 = c1;`
然后,我声明了一个分数变量,并手动检查了一些输入的字母,递增分数变量,看看我的想法是否可行。我这样做,所以我可以了解应该如何完成 for 循环,因为我是新手,我觉得这样更容易获得循环的想法。我像这样检查每个字母:
int main(void)
{
int score = 0;
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
for (int i = 0, n = strlen(word1); i < n; i++)
{
char c1 = word1[i];
char c2 = c1;
if (c2 == 'a')
{
score = POINTS[0];
printf("%i", score);
}
else if (c2 == 'b')
{
score = POINTS[1];
printf("%i", score);
}
else if (c2 == 'c')
{
score = POINTS[2];
printf("%i", score);
}
else if (c2 == 'd')
{
score = POINTS[3];
printf("%i", score);
}
现在它起作用了,但只有当用户按该顺序输入的单词 ABCD 时才有效,我将从数组中获取分配给 score 变量的值。
此外,所有的计算都必须放在一个函数中,变量范围也在那里发挥作用。为了便于参考,这里是我得到的完整代码,根本没有我的编辑。
#include <cs50.h>
#include <stdio.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
}
int compute_score(string word)
{
// TODO: Compute and return score for string
}
我宁愿不写代码作为答案,而是伪解决方案,否则我不会使用别人的代码来学习。
答:
我宁愿不写代码作为答案,而是伪解决方案,否则我不会使用别人的代码来学习。
你走在正确的道路上!字符只是整数,你可以对它们进行数学运算。您可以通过减去“a”将它们转换为从 0 开始的数字。'a' - 'a' 为 0。'b' - 'a' 是 1,'z' - 'a' 是 25。
请务必先将字符小写,并检查结果是否没有偏离数组边界。
[注意:ASCII字符可以这样处理,因为它们是单个字节。可变宽度编码(如 UTF-8)更为复杂。]
这就是我使用 CS50 库的实现
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.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);
if(score1>score2){
printf("Player 1 wins!\n");
}
else if(score1<score2){
printf("Player2 wins!\n");
}
else{
printf("Tie!\n");
}
// TODO: Print the winner
}
int compute_score(string word)
{
int sum=0;
int numb;
int n = strlen(word);
for(int i=0; i<n; i++){
if(isupper(word[i])){
numb = word[i]-65;
numb = POINTS[numb];
}
if(islower(word[i])){
numb = word[i]-97;
numb = POINTS[numb];
}
else{
numb = 0;
}
sum+=numb;
}
return sum;
评论
我认为我之前的答案有点跳枪并放弃了代码,而不是您要求的伪解决方案:P
但基本上,我最终所做的并奏效了,是创建一个 for 循环,其中我有一个 if/else 语句,该语句通过提示时给出的单词中的每个字母进行集成。如果它们是大写的,我会减去 65(因为 65 是大写字母 A 的 ASCII 代码),如果它们是小写的,我会减去 97(小写字母 a 的 ASCII 代码)。
然后,我将该值添加到给定的数组中的点中,这将为我提供分数。
然后,只需一个简单的问题,即制作一个 if/else 语句来打印获胜者。
对不起,如果我的解释有点令人困惑。我希望这是有道理的!:D
我非常高兴我设法做到了哈哈哈!
评论
POINTS[tolower(c) - 'a']
isalpha(c)