Reverse Newline character?

3

3strands

Hi!

I am writing a C++ program for a school project. It's a project and I
could do it without this particualr feature, it's just it would be
better if I could find out one thing:

I'm making a simple game to simulate the rabbit vs. tortoise race. I
need to print a race track on the screen like so:

###############R######
#######T##############

It would be nice if, instead of having to reprint the next tick of the
clock in the race it under the previous line:


#################R####
####T#################

###############R###### SLIPPED!
#######T##############

, which would mean that it would be constantly scrolling, if there was
a way to simply rewrite that exact line on the screen.

So this would be printed:

#################R####
####T#################

The die would roll and moves would be calculated, then the cursor would
be moved up and this would be printed "over" the old racetrack image

###############R###### SLIPPED!
#######T##############

without all of the constant scrolling down the console for each tick of
the clock.

Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?

JII
 
J

Jim Langston

Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?

JII

Platform/OS dependant. With DOS windows you might still be able to use ANSI
commands (not sure) or write directly to the "video memory" (which wouldn't
really be video memory in this case).

You'll need to search the web for your OS (probably windows), "console" and
things like "position".

Maybe: Windows console position c++
or something.

good luck
 
S

Someonekicked

If you find how to do it, plz give the answer here ~!! I think its quite
interesting to know
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

I am writing a C++ program for a school project. It's a project and I
could do it without this particualr feature, it's just it would be
better if I could find out one thing:

I'm making a simple game to simulate the rabbit vs. tortoise race. I
need to print a race track on the screen like so:

###############R######
#######T##############

It would be nice if, instead of having to reprint the next tick of the
clock in the race it under the previous line:


#################R####
####T#################

###############R###### SLIPPED!
#######T##############

, which would mean that it would be constantly scrolling, if there was
a way to simply rewrite that exact line on the screen.

So this would be printed:

#################R####
####T#################

You can use a library that gives you the ability to reposition the cursor on
the screen. (e.g. ncurses).

Or, if you can display the track and everything else on a single line, you
can take advantage of the '\r' (carriage return) character.

Here is an example; just use a different "slowing down function" instead of
sleep(), if you are not running on a unix system:

#include <iostream>
#include <unistd.h>

using namespace std;

int const track_lenght = 20;

void display_track(int tortoise_position, int rabbit_position)
{
cout << '\r';

for (int i = 0; i != track_lenght; ++i)
{
char const tortoise_char = 'T';
char const rabbit_char = 'R';
char const both_char = 'X';
char const track_char = '#';

char display_char = track_char;

if (i == tortoise_position)
{
display_char = ((i == rabbit_position)
? both_char
: tortoise_char);
}
else if (i == rabbit_position)
{
display_char = rabbit_char;
}

cout << display_char;
}

cout << flush;
}

void wait_for_a_while()
{
sleep(1);
}

int main()
{
for (int i = 0; i != track_lenght; ++i)
{
display_track(i / 2, i);
wait_for_a_while();
}

cout << '\n';
}

Ali
 
A

Anand

Hi!

I am writing a C++ program for a school project. It's a project and I
could do it without this particualr feature, it's just it would be
better if I could find out one thing:

I'm making a simple game to simulate the rabbit vs. tortoise race. I
need to print a race track on the screen like so:

###############R######
#######T##############

It would be nice if, instead of having to reprint the next tick of the
clock in the race it under the previous line:


#################R####
####T#################

###############R###### SLIPPED!
#######T##############

, which would mean that it would be constantly scrolling, if there was
a way to simply rewrite that exact line on the screen.

So this would be printed:

#################R####
####T#################

The die would roll and moves would be calculated, then the cursor would
be moved up and this would be printed "over" the old racetrack image

###############R###### SLIPPED!
#######T##############

without all of the constant scrolling down the console for each tick of
the clock.

Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?

JII

'\r' and '\b' are the characters that would help you... (not sure if
it's covered in C++ FAQ.. it's there for sure in C FAQ)

try something like:

....
while(...) {
...
string rLine = constructRabbitLine(...);
string tLine = constructTortoiseLine(...);
cout << rLine << '\n' << tLine << '\r';
cout.flush();
...
}
 
R

Ron House

If this is just a 'one-off' and you don't mind not being portable, it is
the case that most terminal software under X recognises ANSI control
sequences (example, xterm).
 
3

3strands

Great!

Thanks for all of your help. I can't put all of the information
required in my project on a single line, but I will see if I can figure
out how to use ncurses (I use GNU/linux normally, and Win2000
sometimes).

I'll see if I can find the C FAQ and figure out what \b does and if
it's what I'm looking for. If not, the ncurses library may be
necessary. I hope it works in Windows!

Thanks guys!

JII
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top