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

rob64464

Random line in a text file. [C#]

11 posts in this topic

Hello, How do you pick a random line in a text file. The text file does NOT have a set amount of lines and it will change all the time so I cannot do it my way . So i need to sort of open it and see how many lines there are and then I can take it from there. So any ideas? Thanks alot, Rob [edited by - rob64464 on March 26, 2004 3:29:16 PM]
0

Share this post


Link to post
Share on other sites
Open it, read through the entire thing and record the number of line break characters. Then, just create a random number between 0 and the number of lines, and seek to that line. What exactly is the problem?
0

Share this post


Link to post
Share on other sites
yes, thats the idea was having. But how do I find how many line break characters there are?

Cheers,
Rob
0

Share this post


Link to post
Share on other sites
Either:

- read the file in some kind of text mode, and read a line at a time until this is no longer possible. Count up the number of lines you read as you did it.

Or:

- read the file as binary, and look at each character in turn, counting the instances of line breaks. For text files under Windows this will be a CR/LF sequence (ascii 13 followed by ascii 10); for anything else it will just be a CR (13). I think I got that right anyway. Damn windows and its bizarre CR/LF legacy >_<
0

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Open it, read through the entire thing and record the number of line break characters. Then, just create a random number between 0 and the number of lines, and seek to that line. What exactly is the problem?


Actually, the number of lines would be the number of breaks-1, wouldn''t it?


a
b
c


2 breaks, 3 lines.
0

Share this post


Link to post
Share on other sites
Depends whether your file ends with a newline. "text files" under Unix-like systems are supposed to, but there isn''t really a way to enforce that given the "everything is a file" mentality. On windows... good luck.
0

Share this post


Link to post
Share on other sites
Initialise a counter to zero.
Initialise the chosen line to an empty string.
Attempt to read a line.
If you failed to read a line, you're done; return the chosen line.
Increment counter.
This line has a one in 'counter' chance of being the line you want.
So if a random float from 0.0 to 1.0 is smaller than (1.0 / counter), store the current line as the chosen one.
Repeat from the 'attempt to read a line' part.



[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]


[edited by - Kylotan on March 28, 2004 6:57:17 PM]
0

Share this post


Link to post
Share on other sites
I have the same problem as rob
but Im still a noob.,
can anybody provide an example, please.

Thank you!
0

Share this post


Link to post
Share on other sites
If you are using C# you can use System.IO.StreamReader to do the work for you. The ReadLine function will return a line as a string (with a line being defined as ending with a \n or a \r\n combination).

Magius
0

Share this post


Link to post
Share on other sites
A better, and possibly slower, way might be if you read in a byte(character by character) at a time, and basically check for newline characters and count them up. I think that is the easiest way because some of the other methods may eliminate whitespaces for you and break at parts other than a new line.
0

Share this post


Link to post
Share on other sites
quote:
Original post by EbonySeraphim
A better, and possibly slower, way might be if you read in a byte(character by character) at a time, and basically check for newline characters and count them up. I think that is the easiest way because some of the other methods may eliminate whitespaces for you and break at parts other than a new line.

Nope, ReadLine is the superior way of doing this. Your suggestion is a hack.
0

Share this post


Link to post
Share on other sites