确保用户输入与我的程序生成的字符匹配 [英] Making sure a user's input matches characters generated by my program

查看:56
本文介绍了确保用户输入与我的程序生成的字符匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用于作业

我正在编写一个拼字游戏的简化游戏,在该游戏中,我的程序随机生成字符,然后用户将尝试从这些生成的字符中创建一个单词,然后获得分数.我遇到的问题是确保用户实际上正在使用提供的字符.我不知道如何解决此问题.我不需要任何代码,但是提示将不胜感激,甚至链接也可以作为起点.谢谢你的帮助!

I'm programming a simplified game of scrabble where I have my program randomly generate characters then the user would try and create a word from those generated characters, then get a score afterwards. The issue I'm having is making sure the user is actually using the characters provided. I have no clue on how to approach this problem. I don't need any code but hints would be appreciated or even links for a point to start at. Thanks for any help!

编辑-我的程序大约有一半[创建字母集的部分]

EDIT - About half my program [The part that creates the letter set]

 void generate_letter_set(int letter_set[] , int size_let , int num_let)  
 {
  int arr[N];

const char let[] = 
{'K','J','X','Q','Z','B','C','M','P','F','H','V','W','Y','G','L','S','U','D','N','R','T','O','A','I','E'};

const int freq[] = 
{ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 6, 6, 6, 8, 9, 9, 12 };

int score[] =
{ 5, 8, 8, 10, 10, 3, 3, 3, 3, 4, 4, 4, 4, 4, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1};

        int index = 0; 
    for(int i = 0 ; i < 26 ; i++) {
for(int f = 0 ; f < freq[i]; f++) {
        arr[index++] = let[i];  //All the 96 letters are stored in let[i]
            //printf("%c " , let[i]);  // Created the letter bank for all the letters 

 } 
}       int letter;
        printf("Your letters are: ");
    for(int l = 0; l < 7; l++){
            letter = rand() % 97; 
        printf("%c ", arr[letter]);


                                }   
}

推荐答案

有很多方法可以在数组中搜索某些字符.您需要的基础是一个非常简单的搜索功能.

There are a lot of different ways to search an array for certain characters. The basis of what you need is a very simple search function.

一个简单的解决方案是使用两个嵌套的 for 循环.假设let []是您要检查的干草堆",而单词是您的用户输入:

One simple solution would be to use two nested for loops. Assuming let[] is your 'haystack' to check and word is your user input:

// Check each letter of word[]...
for (int ii = 0; ii <= lengthOfUserInput; ii++)
{
    char characterToValidate = word[ii];
    // ... for not existing in let[]
    for (int jj = 0; jj <= lengthOfStringOfValues; jj++)
    {
        if (characterToValidate != let[jj])
    }
}

这篇关于确保用户输入与我的程序生成的字符匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆