Sinisterly
C command line game - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: C command line game (/Thread-C-command-line-game)

Pages: 1 2 3


C command line game - Xe.4 - 02-10-2013

this is the source code of a simple game i made.
to win the game, you need to move the 'H' to a hidden point.
distance show you how far it is from your position and the point.

Code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #define x 10 #define y 11 int main() { srand(time(NULL)); char choice=NULL; char board[y][x]; int posx,posy,i,j; int tarx,tary; int dx,dy; for(i=0;i<y;i++) for(j=0;j<x;j++) board[i][j]='x'; tarx=rand()%x;tary=rand()%y; posx=rand()%x;posy=rand()%y; do { posx=rand()%x;posy=rand()%y; }while(tarx==posx && tary==posy); do{system("cls"); for(i=0;i<y;i++) { for(j=0;j<x;j++) if (posx==j && posy==i)printf("H "); else printf("%c ",board[i][j]); printf("\n"); } if(posx==tarx && posy==tary)break; dx=posx-tarx; dy=posy-tary; if(dx<0)dx*=-1; if(dy<0)dy*=-1; printf("move(w,s,a,d)(end=x)\ndistance=%d",dx+dy);choice=getch(); switch(choice) { case 'w': if(posy!=0)posy--;break; case 's': if(posy!=y-1)posy++;break; case 'a': if(posx!=0)posx--;break; case 'd': if(posx!=x-1)posx++;break; case 'x': break; }printf("\n");}while(choice!='x'); system("cls"); if(posx==tarx && posy==tary)printf("!! YOU WIN !!"); return 0; }



C command line game - Xe.4 - 02-10-2013

this is the source code of a simple game i made.
to win the game, you need to move the 'H' to a hidden point.
distance show you how far it is from your position and the point.

Code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #define x 10 #define y 11 int main() { srand(time(NULL)); char choice=NULL; char board[y][x]; int posx,posy,i,j; int tarx,tary; int dx,dy; for(i=0;i<y;i++) for(j=0;j<x;j++) board[i][j]='x'; tarx=rand()%x;tary=rand()%y; posx=rand()%x;posy=rand()%y; do { posx=rand()%x;posy=rand()%y; }while(tarx==posx && tary==posy); do{system("cls"); for(i=0;i<y;i++) { for(j=0;j<x;j++) if (posx==j && posy==i)printf("H "); else printf("%c ",board[i][j]); printf("\n"); } if(posx==tarx && posy==tary)break; dx=posx-tarx; dy=posy-tary; if(dx<0)dx*=-1; if(dy<0)dy*=-1; printf("move(w,s,a,d)(end=x)\ndistance=%d",dx+dy);choice=getch(); switch(choice) { case 'w': if(posy!=0)posy--;break; case 's': if(posy!=y-1)posy++;break; case 'a': if(posx!=0)posx--;break; case 'd': if(posx!=x-1)posx++;break; case 'x': break; }printf("\n");}while(choice!='x'); system("cls"); if(posx==tarx && posy==tary)printf("!! YOU WIN !!"); return 0; }



C command line game - Xe.4 - 02-10-2013

this is the source code of a simple game i made.
to win the game, you need to move the 'H' to a hidden point.
distance show you how far it is from your position and the point.

Code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #define x 10 #define y 11 int main() { srand(time(NULL)); char choice=NULL; char board[y][x]; int posx,posy,i,j; int tarx,tary; int dx,dy; for(i=0;i<y;i++) for(j=0;j<x;j++) board[i][j]='x'; tarx=rand()%x;tary=rand()%y; posx=rand()%x;posy=rand()%y; do { posx=rand()%x;posy=rand()%y; }while(tarx==posx && tary==posy); do{system("cls"); for(i=0;i<y;i++) { for(j=0;j<x;j++) if (posx==j && posy==i)printf("H "); else printf("%c ",board[i][j]); printf("\n"); } if(posx==tarx && posy==tary)break; dx=posx-tarx; dy=posy-tary; if(dx<0)dx*=-1; if(dy<0)dy*=-1; printf("move(w,s,a,d)(end=x)\ndistance=%d",dx+dy);choice=getch(); switch(choice) { case 'w': if(posy!=0)posy--;break; case 's': if(posy!=y-1)posy++;break; case 'a': if(posx!=0)posx--;break; case 'd': if(posx!=x-1)posx++;break; case 'x': break; }printf("\n");}while(choice!='x'); system("cls"); if(posx==tarx && posy==tary)printf("!! YOU WIN !!"); return 0; }



RE: C command line game - Deque - 02-10-2013

Cool idea. But I can't compile it, because I don't have conio.h on Linux. I googled it and it says:

Quote:Most C compilers that target DOS, Windows 3.x, Phar Lap, DOSX, OS/2, or Win32[1] have this header

So, this is platform dependend. I will try on XP later and then I will give you the challenge points. Which compiler do you use?

Edit: Now that I look at the source, I see system("cls"); which also only works on Windows.
Maybe you can create a system independend version for that game. What is the name of your game?


RE: C command line game - Deque - 02-10-2013

Cool idea. But I can't compile it, because I don't have conio.h on Linux. I googled it and it says:

Quote:Most C compilers that target DOS, Windows 3.x, Phar Lap, DOSX, OS/2, or Win32[1] have this header

So, this is platform dependend. I will try on XP later and then I will give you the challenge points. Which compiler do you use?

Edit: Now that I look at the source, I see system("cls"); which also only works on Windows.
Maybe you can create a system independend version for that game. What is the name of your game?


RE: C command line game - Deque - 02-10-2013

Cool idea. But I can't compile it, because I don't have conio.h on Linux. I googled it and it says:

Quote:Most C compilers that target DOS, Windows 3.x, Phar Lap, DOSX, OS/2, or Win32[1] have this header

So, this is platform dependend. I will try on XP later and then I will give you the challenge points. Which compiler do you use?

Edit: Now that I look at the source, I see system("cls"); which also only works on Windows.
Maybe you can create a system independend version for that game. What is the name of your game?


RE: C command line game - Xe.4 - 02-10-2013

well I don't know what i should name it..., i think i should name it "Find the door"

i can compile this in linux but it might not work with windows...
i don't know how to make it system independent, i'm a beginner.
Code:
#include <stdio.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> #include <time.h> #define x 7 #define y 7 int getch(void) { struct termios oldattr, newattr; int ch; tcgetattr( STDIN_FILENO, &oldattr ); newattr = oldattr; newattr.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; } int main() { srand(time(NULL)); char choice=NULL; char board[y][x]; int posx,posy,i,j; int tarx,tary; int dx,dy; for(i=0;i<y;i++) for(j=0;j<x;j++) board[i][j]='x'; tarx=rand()%x;tary=rand()%y; posx=rand()%x;posy=rand()%y; do { posx=rand()%x;posy=rand()%y; }while(tarx==posx && tary==posy); do{system("clear"); for(i=0;i<y;i++) { for(j=0;j<x;j++) if (posx==j && posy==i)printf("H "); else printf("%c ",board[i][j]); printf("\n"); } if(posx==tarx && posy==tary)break; dx=posx-tarx; dy=posy-tary; if(dx<0)dx*=-1; if(dy<0)dy*=-1; printf("move(w,s,a,d)(end=x)\ndistance=%d",dx+dy);choice=getch(); switch(choice) { case 'w': if(posy!=0)posy--;break; case 's': if(posy!=y-1)posy++;break; case 'a': if(posx!=0)posx--;break; case 'd': if(posx!=x-1)posx++;break; case 'x': break; }printf("\n");}while(choice!='x'); system("clear"); if(posx==tarx && posy==tary)printf("!! YOU WIN !!"); return 0; }



RE: C command line game - Xe.4 - 02-10-2013

well I don't know what i should name it..., i think i should name it "Find the door"

i can compile this in linux but it might not work with windows...
i don't know how to make it system independent, i'm a beginner.
Code:
#include <stdio.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> #include <time.h> #define x 7 #define y 7 int getch(void) { struct termios oldattr, newattr; int ch; tcgetattr( STDIN_FILENO, &oldattr ); newattr = oldattr; newattr.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; } int main() { srand(time(NULL)); char choice=NULL; char board[y][x]; int posx,posy,i,j; int tarx,tary; int dx,dy; for(i=0;i<y;i++) for(j=0;j<x;j++) board[i][j]='x'; tarx=rand()%x;tary=rand()%y; posx=rand()%x;posy=rand()%y; do { posx=rand()%x;posy=rand()%y; }while(tarx==posx && tary==posy); do{system("clear"); for(i=0;i<y;i++) { for(j=0;j<x;j++) if (posx==j && posy==i)printf("H "); else printf("%c ",board[i][j]); printf("\n"); } if(posx==tarx && posy==tary)break; dx=posx-tarx; dy=posy-tary; if(dx<0)dx*=-1; if(dy<0)dy*=-1; printf("move(w,s,a,d)(end=x)\ndistance=%d",dx+dy);choice=getch(); switch(choice) { case 'w': if(posy!=0)posy--;break; case 's': if(posy!=y-1)posy++;break; case 'a': if(posx!=0)posx--;break; case 'd': if(posx!=x-1)posx++;break; case 'x': break; }printf("\n");}while(choice!='x'); system("clear"); if(posx==tarx && posy==tary)printf("!! YOU WIN !!"); return 0; }



RE: C command line game - Xe.4 - 02-10-2013

well I don't know what i should name it..., i think i should name it "Find the door"

i can compile this in linux but it might not work with windows...
i don't know how to make it system independent, i'm a beginner.
Code:
#include <stdio.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> #include <time.h> #define x 7 #define y 7 int getch(void) { struct termios oldattr, newattr; int ch; tcgetattr( STDIN_FILENO, &oldattr ); newattr = oldattr; newattr.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; } int main() { srand(time(NULL)); char choice=NULL; char board[y][x]; int posx,posy,i,j; int tarx,tary; int dx,dy; for(i=0;i<y;i++) for(j=0;j<x;j++) board[i][j]='x'; tarx=rand()%x;tary=rand()%y; posx=rand()%x;posy=rand()%y; do { posx=rand()%x;posy=rand()%y; }while(tarx==posx && tary==posy); do{system("clear"); for(i=0;i<y;i++) { for(j=0;j<x;j++) if (posx==j && posy==i)printf("H "); else printf("%c ",board[i][j]); printf("\n"); } if(posx==tarx && posy==tary)break; dx=posx-tarx; dy=posy-tary; if(dx<0)dx*=-1; if(dy<0)dy*=-1; printf("move(w,s,a,d)(end=x)\ndistance=%d",dx+dy);choice=getch(); switch(choice) { case 'w': if(posy!=0)posy--;break; case 's': if(posy!=y-1)posy++;break; case 'a': if(posx!=0)posx--;break; case 'd': if(posx!=x-1)posx++;break; case 'x': break; }printf("\n");}while(choice!='x'); system("clear"); if(posx==tarx && posy==tary)printf("!! YOU WIN !!"); return 0; }



RE: C command line game - Deque - 02-10-2013

You can clear the screen by printing lots of empty lines instead of using system().
But two versions are fine too. Thank you. I will add this to the compilation.

I have two suggestion for improvement:
1. add a newline at the end of !! YOU WIN !!
2. count and display the number of total moves that the user needed to win the game