ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#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).
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?
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.