Reverse Newline character?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 3strands@gmail.com

    Reverse Newline character?

    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

  • Jim Langston

    #2
    Re: Reverse Newline character?

    <3strands@gmail .com> wrote in message
    news:1126195035 .773587.178530@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > Hi!
    >[/color]
    <SNIP>[color=blue]
    > 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
    >[/color]

    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


    Comment

    • Someonekicked

      #3
      Re: Reverse Newline character?

      If you find how to do it, plz give the answer here ~!! I think its quite
      interesting to know
      <3strands@gmail .com> wrote in message
      news:1126195035 .773587.178530@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > 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
      >[/color]


      Comment

      • Ali Çehreli

        #4
        Re: Reverse Newline character?


        <3strands@gmail .com> wrote in message
        news:1126195035 .773587.178530@ z14g2000cwz.goo glegroups.com.. .
        [color=blue]
        > 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########## #######[/color]

        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(i nt tortoise_positi on, 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_positi on)
        {
        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_whil e()
        {
        sleep(1);
        }

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

        cout << '\n';
        }

        Ali

        Comment

        • Anand

          #5
          Re: Reverse Newline character?

          3strands@gmail. com wrote:[color=blue]
          > 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
          >[/color]

          '\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 = constructRabbit Line(...);
          string tLine = constructTortoi seLine(...);
          cout << rLine << '\n' << tLine << '\r';
          cout.flush();
          ...
          }

          Comment

          • Ron House

            #6
            Re: Reverse Newline character?

            Jim Langston wrote:[color=blue]
            > <3strands@gmail .com> wrote in message
            > news:1126195035 .773587.178530@ z14g2000cwz.goo glegroups.com.. .
            >[color=green]
            >>Hi!
            >>[/color]
            >
            > <SNIP>
            >[color=green]
            >> 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?[/color][/color]

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

            --
            Ron House house@usq.edu.a u

            Comment

            • 3strands@gmail.com

              #7
              Re: Reverse Newline character?

              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

              Comment

              Working...