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

CS50 lab2 scrabble 用c语言做一个游戏,第1张

一个游戏:拼字游戏

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

得分标准为

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

框架代码:

#include 
#include 
#include 
#include 

// 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 
#include 
#include 
#include 

// 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

结果展示:

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/871120.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)