Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!!
I do a game in C (I don't know the name in English(I'm from Greece) ,maybe hanging or something similar).Anyway, the player have to guess the letters of the word.
When the player give a word,I hide some letters (randmoly).Ιnitially, I show 3 letters.
Also,if the length of word is >= 9 and <= 13 ,I show 4 letters.
If the length of word is > 13 , I show 5 letters.
So, for example if word's length is 15 ,I want to receive 5 unique random positions.(See random_pos(......) function to understand) , but many times I receive numbers at the same position.
Example of an
Output[^]

Focus on start_game() , main() and random_pos() functions.
The others are not important to my problem.

So,I want to receive 3 or 4 or 5 random unique numbers(letters of word) depending on the word's length ,to show to the output.

What I have tried:

My full code until now:

GAME CODE[^]

Thanks a lot guys!!!!!
Posted
Updated 12-Sep-20 1:47am

1 solution

First off, I'm not going off to grab a copy of your whole code, and wade through it looking for the relevant bits. If you want to show us your code don't be lazy: copy'n'paste the relevant fragments and post them here as text - using the "code" widget to enable the syntax highlighter.

Then explain exactly what you did and what happened that you didn't expect or didn't happen that you did.

Probably, you haven't initialized the random number generator:
C++
int main()
{
    srand(time(NULL));
//    ...
    return 0;
}

C++
int numberOfDifferentValues = 5;
for (int i = 0; i < 10; i++)
   {
   int r = rand() % numberOfDifferentValues;
   printf("%u\n", r);
   }
Will give you twenty random values between 0 and 4 inclusive.
If you want those random value to be unique, then create an array containing the values you can use and use the random number to index that array (for a ten letter word):
C++
int numberOfDifferentValues = 5;
int valid[10] = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9};
int indexes = 10;
for (int i = 0; i < numberOfDifferentValues; i++)
   {
   int r = rand() % indexes;
   printf("%u\n", valid[r]);
   indexes--;
   valid[r]  = valid[indexes];
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900