• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

da_cobra

random words

15 posts in this topic

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...
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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''



0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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;
}
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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]);
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites