LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2006, 03:21 PM   #1
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
Question General C questions - for everyone


Hi all...

I am new to programming in C. I have just now bought a book. ^_^
So I have a 3D array with numbers in it from 0 to 9. The sequence of numbers are random. The main point of the "game" is to organize these into the right sequence: 0,1,2,3,4,5,6,7,8.
You can do this by pressing A,S,D or W keys. The 0 is not displayed. Instead there is space. So my problem is that I do not really know how to check the keyboard for the input. I tryed ti use scanf and do{}while but it does not really work good because I have to press RETURN every time I enter a letter.
So how can I avoid pressing RETURN each time? I tryed to use getchar but no success.
Here is the code. Might be messy. Embarrassed
Code:
Code:
#include <stdio.h>
#include <stdlib.h>

int a[3][3] = {{0,3,6},{1,4,7},{2,5,8}};    
int i,j,tmp,x,y;
char c, chr;

void anything()
{
     for (i=0; i<3; i++){
      for (j=0; j<3; j++){
          if (a[i][j]==0){
             printf ("  ");
             x = i;
             y = j;
          }else{
             printf("%d ", a[i][j]);
          }
          
      }
      printf ("\n");
  }
  
  return;
}
void moveA(){
    if(y>0){
        tmp=a[x][y];
        a[x][y]=a[x][y-1];
        a[x][y-1]=tmp;                 
        printf("\n");         
        anything();                             
    }else{
        printf("\nThis is an invalid movement. \n");
        anything();
    }
}
void moveD(){
    if(y<2){
        tmp=a[x][y];
        a[x][y]=a[x][y+1];
        a[x][y+1]=tmp;                 
        printf("\n");         
        anything();                             
    }else{
        printf("\nThis is an invalid movement. \n");
        anything();
    }
}
void moveS(){
    if(x<2){
        tmp=a[x][y];
        a[x][y]=a[x+1][y];
        a[x+1][y]=tmp;
        anything();
    }else{
        printf("\nThat is an invalid movement. \n");
        anything();
    }
}
void moveW(){
    if(x>0){
        tmp=a[x][y];
        a[x][y]=a[x-1][y];
        a[x-1][y]=tmp;
        anything();
    }else{
        printf("\nThis is an invalid movemenet.\n");
        anything();
    }
}
void test(){    
    if(a[0][0]==0 && a[0][1]==1 && a[0][2]==2 && a[1][0]==3 && a[1][1]==4 && a[1][2]==5 && a[2][0]==6 && a[2][1]==7 && a[2][2]==8){        
        printf("\n\n CONGRATULATIONS\n");
    }else{
        printf("\nNot now...\n");
    }
    return;
}
int main()
{

  printf("COOL \t GAME. \t ^_^\n");
  printf("Press A to move the space to left, D to right, W to up and S to down. q to exit..\n");
  printf("If you think you have finished press T (test)\n");
  printf("____________________________________________\n\n");
  anything();
  do{
      scanf("%c", &c);
      switch(c){
          case 97:    moveA(); break;
          case 100: moveD(); break;
          case 115:    moveS(); break;
          case 119:    moveW(); break;
          case 116: test();  break;
      }
  }while(c!=113);
  system("PAUSE");    
  return 0;
}
I wrote it under windows but I used MinGW compiler (gcc) to be sure that it can be run on linux after compiling too.

Dani
 
Old 09-19-2006, 04:28 PM   #2
pda_h4x0r
Member
 
Registered: Feb 2006
Location: somewhere in cyberspace
Distribution: Debian, Familiar
Posts: 380

Rep: Reputation: 31
Use SDL to read the keyboard.

Here's a sample:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>

int main(int argc, char** argv) {

if(SDL_Init() < 0) return 0;  //error

unsigned char* keyboard;

do {

keyboard = SDL_GetKeyState(NULL);

//index keyboard by SDLK_<keyname>
if(keyboard[SDLK_RETURN]) printf("You hit enter!\n");
if(keyboard[SDLK_SPACE]) printf("You hit space!\n");
if(keyboard[SDLK_a]) printf("You hit A!\n");
if(keyboard[SDLK_w]) printf("You hit W!\n");
if(keyboard[SDLK_s]) printf("You hit S!\n");
if(keyboard[SDLK_d]) printf("You hit D!\n");
if(keyboard[SDLK_q]) printf("You hit Q!\n");

} while (!keyboard[SDLK_q]);

SDL_Quit();

return 0;

}
You will need to include the paths to the SDL include files and libs--use the sdl-config command to get these (assuming you have actually installed SDL in the first place).

Last edited by pda_h4x0r; 09-19-2006 at 04:31 PM.
 
Old 09-19-2006, 04:57 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
If ye wishes not ta use SDL, ye can use this code ta start taking input unbuffered
Code:
   /* set terminal in raw mode */
   struct termios cur,raw;
   tcgetattr(STDIN_FILENO,&cur);
   raw=cur;
   raw.c_lflag&=~(ICANON|ECHO|ISIG);
   raw.c_cc[VMIN]=1;
   raw.c_cc[VTIME]=0;
   tcsetattr(STDIN_FILENO,TCSAFLUSH,&raw);
th'n ask fer input using getchar(), then finish with
Code:
   /* restore and terminate */
   tcsetattr(STDIN_FILENO,TCSAFLUSH,&cur);
And if ye be asking, today be Talk Like a Pirate day.

Last edited by tuxdev; 09-19-2006 at 05:21 PM.
 
Old 09-19-2006, 05:20 PM   #4
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
this guy is new to C programing and you guys are suggesting him to use SDL etc?

vargadanis,
Standard C does not provide any mechanism to do what you
want.
Your array is 2D not 3D.
 
Old 09-19-2006, 06:02 PM   #5
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
You are rite, pankaj99. ^_^
I solved it with _getch() function from conio.h. I do not not what is that SDL thing so I was thinking on the alternatives of conio.h under linux. Any ideas?
 
Old 09-19-2006, 06:08 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
ye ignored me post. Me code puts th' console into "cooked" mode, where th' characters typed are not echoed and immediately available ta getchar(). _getch() be a reserved name, don't use it.
 
Old 09-19-2006, 06:15 PM   #7
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
I have just now found the perfect sollution.
include <ncuses.h>

than use getch() function. Works perfectly. ^_^
 
Old 09-19-2006, 06:20 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Ye needs ncurses for that, and ncurses is overkill in this case. Ye can print '\r' ta go back ta th' begining of th' line ye are printing, and print over what ye needs ta, and place th' cursor on th' line. Ye should also use 'w', 'a', 's', and 'd' instead of numerical constants because th' numbers may mean diffrent things based on th' text encoding, e.g. EBCDIC.

Last edited by tuxdev; 09-19-2006 at 06:23 PM.
 
Old 09-19-2006, 08:04 PM   #9
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
you are rite... I tried to use "" marks instead of ''. It did not work but with '' worked prperly. That is ok...
I said it works perfectly... It should work perfectly but it doesn't. I installed libncurses5.deb from the packages.ubuntu.com site and tried to include ncurses.h without any success. Why isn't it working? What else shall I install?
 
Old 09-20-2006, 01:13 AM   #10
pankaj99
Member
 
Registered: Mar 2006
Location: India
Distribution: Fedora
Posts: 47

Rep: Reputation: 15
what you are trying to do is not in the standard C language.
The header file <ncurses.h> is not defined by the std C.
http://en.wikipedia.org/wiki/C_standard_library.


To compile using ncurses ,you have to link against the ncurses library
in the command line.
 
Old 09-20-2006, 10:32 AM   #11
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
Cool.... And how?
 
Old 09-20-2006, 11:28 AM   #12
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Quote:
Originally Posted by vargadanis
Cool.... And how?
-lncurses in the compile command line
 
Old 09-20-2006, 11:43 AM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Also make sure you installed the libncurses-dev package
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
general questions piva.francesco Linux - Software 4 05-05-2005 02:44 AM
General Questions gcobb Linux - Software 2 10-21-2004 12:09 AM
few general questions and what now Vid Linux - Newbie 1 01-27-2004 10:10 PM
general questions ChrisC Linux - General 1 03-31-2003 07:28 PM
Help Me: Few General Questions ?? alteredinsanity Linux - General 2 09-12-2002 01:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration