# Helpful Ideas to get started

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-05-2009
kpridd
Helpful Ideas to get started
This game should create a series of hidden symbols which the player needs to guess which symbols and the order. The symbols are !, @, &, and %. The string created using these symbols should have them in any order. The user gets to say how many symbols are in the string. Once the user selects the number, then the string is created randomly, and the user can then guess. There should be functions for creating the string and for comparing it to the player's choice. The string needs to be passed as a pointer to each of these functions

Hello I do not need you to do this for me just wanting ideas on how to go about getting the symbols to randomly mix up.

I know i need to set each of them = to a number so would
Code:

```int Symbol() { ! = 0 & = 1 % = 2 @ = 3 }```
would this work or is there another way to do it?
Code:

```int GetRandNum(int Max) { return rand() % max; }```
Max would be the number of symbols the user has selected to be in the puzzle.
I guess i dont understand how i get the symbols to jumble. I am a huge novice when it comes to C++ any ideas or help would be appreciated thanks
• 12-05-2009
nadroj
I imagine there is no restriction on how many times each symbol can be used in each "game". That is, its not a requirement to have at least one of each symbol, or anything, correct?

Your right that you (obviously) have to randomize the sequence of symbols with "rand", with the random numbers being from, say, 0 - 3. Heres some code/pseudocode showing the flow of logic to do this
Code:

```// declare a const array of 'char's to store your 4 symbols, called, say, 'SYMBOLS' (constants are usually capitalized) string sequence; int length = ""; // ask user for length and store it in length // iterate (i.e. for loop) from 1 to 'length'```
And in your for loop you need to randomly choose one of the values from SYMBOLS and append to the 'sequence' string.

Hopefully (1) this is clear enough, yet (2) not too clear as to give the exact solution.
• 12-05-2009
kpridd
okay i get everything you stated except how to store the 4 symbols
Code:

`const char SYMBOLS{ !, %, &, @}`
i know what an array is.. would it be a two-dimensional array or just one? i tried this it didn't work.. haha
Code:

`const char SYMBOLS[!, &, %, @]`
• 12-05-2009
nadroj
Its one-dimensional, i.e.
Code:

`const char SYMBOLS[] = {};`
In the braces ({}) you put a comma-separated list of characters. Note that characters are denoted by surrounding the character by single quotes, like
Code:

`'@'`
After you have that array and a random number from 0-3 you can do something like
Code:

`sequence += SYMBOLS[your_random_number];`
To append the random character to your string.
• 12-05-2009
kpridd
ya thanks i just figured out the array i have this for my code to jumble

Code:

```                  string sequence = SYMBOLS;           int length = sequence.size();           for (int i = 0; i < length; ++i)           {           int index1 = (rand() % length);           int index2 = (rand() % length);           char temp = sequence[index1];           sequence[index1] = sequence[index2];           sequence[index2] = temp;            }```
however when i cout << "sequence" the symbols dont mix up each time.. they stay the same.. and there is always just four.. no matter what number i type into the lenght
• 12-05-2009
kpridd
i think one of the problems is because i reset length when i say
int length = sequence.size()
not sure what to do
• 12-05-2009
nadroj
Quote:

Code:

```          string sequence = SYMBOLS;           int length = sequence.size();```

Your basically creating a string copy of the char array SYMBOLS and storing it into sequence. So their contents (and therefore size) will always be the exact same. You should just do something like
Code:

`string sequence = "";`
to create an empty string. You then later append to it, like
Code:

`sequence += SYMBOLS[your_number]'`
Also, instead of this
Code:

```          int index1 = (rand() % length);           int index2 = (rand() % length)```
You want to "% 4".

If any of this is unclear why you should do it this way, then say so and I can explain. Its of no use to you if you just type things you are told without understanding why (especially during a test when you cant ask someone!)
• 12-05-2009
kpridd
what would "your_number" be equivalent too? the number they type in or just a randomly generated number?
• 12-05-2009
nadroj
It would be the random number you generate. You keep the variable you already have, "length", for the number they type in.
• 12-05-2009
kpridd
Code:

```                    cout << "Enter the number of symbols you would like to guess" << endl;           cin >> length;                     sequence += SYMBOLS[GetRandNum(4)];                               for (int i = 0; i < length; ++i)           {           int index1 = (rand() % 4);           int index2 = (rand() % 4);           char temp = sequence[index1];           sequence[index1] = sequence[index2];           sequence[index2] = temp;            }                     cout << sequence << endl;                      system("PAUSE");           break;```
lol I'm sorry if i seem dumb.. now it isn't even returning symbols when i type in a number
• 12-05-2009
nadroj
Arrghhh! Please re-read my posts with a clear mind!

I suggested that instead of initializing the sequence variable as you had above, to just do (exactly)
Code:

`string sequence = "";`
to create the empty string. Also as I described above, you later append to this string with the given random number to determine which character to append to it. So basically
Code:

```string sequence = ""; //for loop from 1 to length {   int num = //generate random number   sequence += SYMBOLS[num]' }```
So the last line will append the random character (SYMBOLS[num]) to the string.
• 12-05-2009
kpridd
Thanks everything seems to be working it gives me the correct numbers and it mixes them up. Only problem is that sometimes it randomly gives an empty space as a character? what could be causing that
• 12-05-2009
nadroj
Well, your obviously assigning or printing a space! :P

Post all of your code, exactly as your running it, and I can see if theres anything fishy going on.

EDIT: I think the problem is probably with the mod ("%"). I think "rand() % 4" generates numbers from 0 to 4, so try "rand() % 3". If that doesnt fix it, then post all of your code.
• 12-05-2009
kpridd
Code:

```#include<iostream> #include<ctime> #include<cstdlib> #include<string> using namespace std; int length, index1, index2, input, num, i, tries = 0; string guess; char temp; char SYMBOLS[4] = {'!', '&', '@', '%'}; void Seed() {     srand(time(0)); } int GetRandNum(int max) {  return rand() % max;    } int main() { Seed();   while((input != 1) && (input != 3)) { cout << "Welcome to the game of Logic!" << endl; cout << "What would you like to do?" << endl << endl; cout << "1 - Play the game of Logic" << endl; cout << "2 - Read instructions on how to play Logic" << endl; cout << "3 - Quit" << endl; cin >> input;          switch(input) {       case 1:                     cout << "Enter the number of symbols you would like to guess" << endl;           cin >> length;                             string sequence = "";                         for (i = 0; i < length; ++i)           {                     num = GetRandNum(4);           sequence += SYMBOLS[num];           index1 = (rand() % 3);           index2 = (rand() % 3);           temp = sequence[index1];           sequence[index1] = sequence[index2];           sequence[index2] = temp;           }         do         {                       cout << "Guess the order in which the symbols are in" << endl;         cin >> guess;                               if (guess != sequence)         {         ++tries;         cout << "Try again" << endl;         cout << tries << endl;         }         else         {         cout << "You got it!" << endl;         }                   }         while (guess != sequence);         system("PAUSE");         break;     } }     system("PAUSE");   return 0;   }```
Messed around with the %3 and stuff still sometimes came up with a space
• 12-05-2009
nadroj
Code:

```          for (i = 0; i < length; ++i)           {                     num = GetRandNum(4);           sequence += SYMBOLS[num];           index1 = (rand() % 3);           index2 = (rand() % 3);           temp = sequence[index1];           sequence[index1] = sequence[index2];           sequence[index2] = temp;           }```
Remove all of the lines in red, as I dont know what its doing, but if you just need to append to sequence, then you dont need the red lines. Also, change the "4" to "3" in the GetRandNum call, if you have verified that 3 is correct (to generate from 0 to 3).
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last