Thread: Moving the cursor

  1. #1
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166

    Moving the cursor

    I was thinking of making a simple (keyword: simple! lol) tic-tac-toe game, but how do I make the cursor move around the command prompt window using the arrowkeys?

    Suppose i printed something like this to the screen (without numbers):

    _1_|_2_|_3_
    _4_|_5_|_6_
    7 | 8 | 9

    then i wanted to go back and say print an X in the number 5 cell. how would i do that?

    PS. Sorry if this is in the wrong forum. I couldn't decide if this was more of a C++ question or a game programming one.
    Last edited by dra; 05-01-2005 at 09:34 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read the faq for starters, then if that dont solve your problem do a search of the boards.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    try making that boadrs like
    Code:
     
    char[][]={'_', '_', '_', '_', '_', '_', '_',
                   '_', '_', '_', '_', '_', '_'}
    and print it out like that on screen.
    and you can make a switch case or if on which char possision the "X" is going to be. and clrscr() write the variable between each key press. then you can have the X moves. and some other key to mark the char[x][y] permanent.

    if this was what yo ment.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you are doing this in a Windows console, (a "DOS" box), you might want to take a look through my 6 part consoles tutorial starting here.

    Hey SC, long time no see.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Thanks for the guide adrian. finally found what I was looking for (COORD). I had gotoxy() in mind, but when I tried it, i realized Dev-C++ wasn't going to support it because it's not a standard function.

    COORD does the trick though. just a few more things to type. Thanks for everyone's help.

  6. #6
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    I got COORD to work, and have a simple semi-working code, but I can't get it "move" after the first initial move.

    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    
    enum
    {
      C           = 99,
      W           = 119,
    };
    
    int ch = getch();
    int i = 1;
    
    using namespace std;
    
    int X;
    int Y;
    int moveto( int moveX, int moveY );
    
    int main()
    {
    
    while ( i == 1 ){
        switch (ch) {
    case C:
         X = 0, Y = 0;
         moveto(X,Y);
         break;
    
    case W:
         X = 16, Y = 30;
         moveto(X,Y);
         break;
         }
         cout<<"move";
    }
    }
    
    
    
    int moveto( int moveX, int moveY )
    {
        HANDLE hOut;
        COORD Position;
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
        Position.X = moveX;
        Position.Y = moveY;  
        SetConsoleCursorPosition(hOut,Position);
    }
    PS. I know the indentation is horrible. haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving the Console Cursor & Printing In Color
    By Adak in forum C Programming
    Replies: 0
    Last Post: 03-09-2009, 11:31 AM
  2. Deconstraining Cursor While Moving Window
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2006, 06:17 AM
  3. Moving the cursor around
    By doormat in forum Linux Programming
    Replies: 6
    Last Post: 04-22-2004, 06:29 AM
  4. Stopping the cursor from moving
    By Inept Pig in forum C Programming
    Replies: 3
    Last Post: 07-30-2002, 03:25 PM
  5. Mouse in 800x600 24Bit Mode?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-11-2001, 01:38 AM