CS50 lab2 scrabble 用c语言做一个游戏

一个游戏:拼字游戏

两个玩家,每人输入一个单词,看谁得分高。

得分标准为

框架代码已经给出,需要自己写两个函数:一个是compute_score(string word),另一个是通过比较分数而得出结果。

框架代码:

#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);

    // remind: Print the winner


}
int compute_score(string word)
// remind: Compute and return score for string

实现代码:

#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 small_letters[] = {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122};

int capital_letters[] = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90};
int temp_points[]={};

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("the player 1 is win\n");
    }
    else if(score1 < score2)
    {
        printf("the player 2 is win\n");
    }
    else
    {
        printf("pie!\n");
    }
}

int compute_score(string word)
{
    int score = 0;
    //traverse the wold and get the number from arphabet
    for(int i=0; i < strlen(word); i++)
    {
        if(islower(word[i]))
        {
            for(int f=0;f<sizeof(small_letters);f++)
            {
                if(word[i] == small_letters[f])
                {
                    temp_points[i] =POINTS[f];
                    score +=temp_points[i];
                }

            }
        }
        else if(isupper(word[i]))
        {
            for(int f=0;f< sizeof(capital_letters); f++)
            {
                if(word[i] == capital_letters[f])
                {
                    temp_points[i] =POINTS[f];
                    score +=temp_points[i];
                }
            }
        }
        else
        {
            i += 1;
        }
    }
    return score;
    // TODO: Compute and return score for string
}

结果展示:

有任何问题和建议请私信或评论,谢谢。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值