Thread: Helpful Ideas to get started

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    19

    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

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    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[!, &, %, @]

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.
    Last edited by nadroj; 12-05-2009 at 06:19 PM.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    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

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    i think one of the problems is because i reset length when i say
    int length = sequence.size()
    not sure what to do

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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!)

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    what would "your_number" be equivalent too? the number they type in or just a randomly generated number?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    It would be the random number you generate. You keep the variable you already have, "length", for the number they type in.

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    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

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    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

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.
    Last edited by nadroj; 12-05-2009 at 08:06 PM.

  14. #14
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    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

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ideas for my C++ book
    By Carlos in forum C++ Programming
    Replies: 41
    Last Post: 01-06-2004, 03:46 AM
  2. Company Name Ideas
    By brunomiranda in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-16-2003, 05:15 PM
  3. Maze Program, Any ideas
    By drkavngr1911 in forum C++ Programming
    Replies: 14
    Last Post: 11-13-2003, 03:32 PM
  4. 2d game ideas
    By BeholderOf in forum Game Programming
    Replies: 5
    Last Post: 10-27-2003, 06:06 PM
  5. Ideas
    By TechPhreak in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2001, 09:42 PM