Jump to content

  • Log In with Google      Sign In   
  • Create Account


random words


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 October 2001 - 04:08 AM

I''m trying to make a simple hangman game, I already made functions that read the length of a string and search for a letter in it, but now I would like to know how I could let the program pick a random word (from a file, or a matrix) I already figured out that I will have to use a random number that points to a random word, but where do I put those words I can''t use a string matrix, because a number would only point to a letter of a word, or am I wrong?!? can someone help me thanx in advance...

Sponsor:

#2 Ronin_54   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 October 2001 - 05:05 AM

I guess using a char matrix would be easiest.

Horizontal: each variable would hold one letter, or a null to indicate the end of the word.

Vertical: the seperate words are filled in under each other. You should create a random number between 0 and n (number of words). That random number would be the vertical indicator in your matrix.

ASCII:
x12345678
0DOG-----
1AIRPLANE
2TORNADO-
3CATTLE--
4FORMAT--

I hope you can understand this, ''cause I think I suck at explaining things :D


#3 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 October 2001 - 06:12 AM

do you mean a 2 dimensional array?

like :

char array [5] [10]
{
{0,1,2,3,4,5,6,7,8,9}
{1,''w'',''o'',''r'',''d'',''\0''}
{2,''d'',''o'',''g'',''\0''}
{3,........}
{4,......}
} ;

if so I don''t think it''s easy to use...

#4 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 13 October 2001 - 06:31 AM

Look up the strtok() function. It extracts substrings delimited by a specified set of separators. You could, for example, specify all whitespace (the space character, tabs, newlines, etc) as your separators and use strtok to extract words from any text file.

#5 Ronin_54   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 October 2001 - 09:42 PM

I own the bad habit of calling a 2d-array a matrix :D


#6 Dactylos   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 October 2001 - 09:57 PM

quote:
Original post by da_cobra
do you mean a 2 dimensional array?

like :

char array [5] [10]
{
{0,1,2,3,4,5,6,7,8,9}
{1,''w'',''o'',''r'',''d'',''\0''}
{2,''d'',''o'',''g'',''\0''}
{3,........}
{4,......}
} ;

if so I don''t think it''s easy to use...

You can do it like you wrote, only loose the first row, and the initial number in each row (Ronin_54 only had them to show column- and row-numbers, they were not supposed to be in the strings)

for example:
  
const int NUMBER_OF_WORDS = 3;
const int MAX_WORD_LENGTH = 10;
char word_list[NUMBER_OF_WORDS] [MAX_WORD_LENGTH] =
{
"tornado",
"dog",
"cattle"
// you get the point

};

// then to get a random string from array, do:

int word_index = rand() % NUMBER_OF_WORDS;
char * word = word_list[word_index];

// now ''word'' is a pointer to a word in the array ''word_list''





#7 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 October 2001 - 05:47 AM

I think that last one could work, so now I try that to put it in a function like this :

void newword(char *word) ;

void main()
{
char word[10]="" ;
newword(word) ;
cout << word << endl ;
}

void newword(char *word)
{
int const words=5 ;
int const max=10 ;
int a=0 ;
char list [words] [max] =
{
"word", "car", "house", "dog", "cat"
} ;
int word_index = rand() % words ;
word = list[word_index];
}

but my word stays "" ?!?

what am I doing wrong now?
and no I can't use : *word=list[word_index] ;
because then I get the following error :
error C2440: '=' : cannot convert from 'char [10]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

again thanx in advance

Edited by - da_cobra on October 14, 2001 12:49:57 PM

#8 Ronin_54   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 October 2001 - 08:42 AM

YOu aren''t storing an entire word in your variable now

Either figure out how to use the pointer correctly, or read every single character of your word into a seperate array entry (ie. word[10]) till you get a NULL...

#9 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 14 October 2001 - 08:58 AM

quote:
Original post by da_cobra
void newword(char *word)
{
int const words=5 ;
int const max=10 ;
int a=0 ;
char list [words] [max] =
{
"word", "car", "house", "dog", "cat"
} ;
int word_index = rand() % words ;
word = list[word_index];
}



1.) You create your wordlist every time you need a random word, which is bad design.
2.) Yo declare word to be a constant array of chars, which you don''t want. You want a pointer, so there''s no data copying.


// note that these are global; they are declared outside *any* function
const int MAX_WORDS = 5;
const int MAX_CHARS = 10;
// 2D array of chars:
char word_list[MAX_WORDS][MAX_CHARS] =
{
"word",
"car",
"house",
"dog",
"cat"
};

int main(int argc, char *argv[])
{
int word_index = rand() % MAX_WORDS;
// note that word is a char *, not char[10]
char *word = word_list[word-index];
cout << word << endl;
}


#10 da_cobra   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 October 2001 - 05:48 AM

what parameters are that : "int argc, char *argv[]" ?!?

#11 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 15 October 2001 - 05:58 AM

Those are the usual arguments for main, but you can overload main to take no parameters: int main(void).

int argc - the number of command line parameters (arg ument c ount). Always >= 1, since the first argument is the program name.
char *argv[] - an array of strings (which are arrays of chars) (arg ument v alues). argv[0] is the program name (1st parameter) and so on.

#12 ace_muncher   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 October 2001 - 05:18 PM

Oluseyi wrote:

// note that these are global; they are declared outside *any* function
const int MAX_WORDS = 5;
const int MAX_CHARS = 10;

Never do this, because global variables are wanted to be avoided. Not only do they use up a lot more space, but they can be changed by any function, which eliminates the purpose of encapsulation in C++. You may, however, define MAX_WORDS and MAX_CHARS as follows:

#define MAX_WORDS 5
#define MAX_CHARS 10

What this does is it looks in your entire program before compiling and replaces wherever it finds MAX_WORDS with 5. Same with MAX_CHARS. Notice also that there is no '';'', because you only want to replace a number, not a statement. These statements should go right below your #include directives.

You may also define a constant integer, like above, but you must pass it to all the functions that will use it, and it will not be able to be changed. The last method is becomming more and more popular.



-Ace

#13 Dactylos   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 October 2001 - 05:43 PM

quote:
Original post by ace_muncher
Oluseyi wrote:

// note that these are global; they are declared outside *any* function
const int MAX_WORDS = 5;
const int MAX_CHARS = 10;

Never do this, because global variables are wanted to be avoided. Not only do they use up a lot more space, but they can be changed by any function, which eliminates the purpose of encapsulation in C++.


That's not true. Since the globals MAX_WORDS and MAX_CHARS are declared with the 'const' keyword they are constants. This means that the program is not allowed to change them. And in the case of an optimizing compiler their values will be inserted directly into the code (just like with a macro).

Declaring consts is actually to prefered to declaring macros.
quote:

You may also define a constant integer, like above, but you must pass it to all the functions that will use it, and it will not be able to be changed.


No, you won't have to pass it to every function that uses it, because it is declared in the global scope (just make sure it is declared *before* any function that uses it).

Edited by - Dactylos on October 15, 2001 12:44:27 AM

#14 BeerNutts   Crossbones+   -  Reputation: 2577

Like
Likes
Like

Posted 17 October 2001 - 07:06 AM

Guys, your killing yourselves with trying to define the bounds of the array. If you know what the array will be at compile time, you don't have to give it bounds.

    
const char *cStringArray[] =
{
"The cat ran up the hill",
"the cow jumped over the moon",
"there's a hole...5...10...he's running over people! Oh you Herschel Walker!",
""
};

void function(void)
{
int NumStrings;
char *cRandomString;

// find the number of strings

for (NumStrings= 0; cStringArray[NumStrings][0] != 0; NumStrings++)
{}

// now NumStrings is the number of strings in array

cRandomString = cStringArray[rand()%NumStrings];

// now you have a random string in cRandomString

}






Edited by - BeerNutts on October 17, 2001 2:11:14 PM

#15 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 18 October 2001 - 03:25 AM

quote:
Original post by da_cobra
I think that last one could work, so now I try that to put it in a function like this :

void newword(char *word) ;

void main()
{
char word[10]="" ;
newword(word) ;
cout << word << endl ;
}

void newword(char *word)
{
int const words=5 ;
int const max=10 ;
int a=0 ;
char list [words] [max] =
{
"word", "car", "house", "dog", "cat"
} ;
int word_index = rand() % words ;
word = list[word_index];
}

but my word stays "" ?!?

what am I doing wrong now?
and no I can''t use : *word=list[word_index] ;
because then I get the following error :
error C2440: ''='' : cannot convert from ''char [10]'' to ''char''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

again thanx in advance

Edited by - da_cobra on October 14, 2001 12:49:57 PM



This is almost correct. You just need to include string.h at the top of your program, and replace

word = list[word_index];

with

strcpy(word, list[word_index]);

#16 kseh   Crossbones+   -  Reputation: 1863

Like
Likes
Like

Posted 19 October 2001 - 07:26 AM

Why hasn''t someone suggested to use words out of a text file? Read the text file once counting each word (typically seperated by a space, carriage return, or punctuation mark) then select a random value between 0 and the number of counted words. This way you can add whatever words you want to the game easilly. I did something like this for a word search type game, it worked well.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS