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


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


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


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

good suggestion!
i also make it able to set the size for it.
this is my final code:

for linux using g++:
Code:
#include <stdio.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> #include <time.h> 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=' '; int posx,posy,i,j; int tarx,tary,moves=0; int dx,dy,x,y; printf("Size: x=");scanf("%d",&x); printf(" y=");scanf("%d",&y); char board[y][x]; 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)\n\nsize=%dx%d, distance=%d, moves=%d",x,y,dx+dy,moves);choice=getch();moves++; 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'); if(posx==tarx && posy==tary)printf("\n\n!! YOU WIN !!\n"); return 0; }

for windows using mingw compiler
Code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> int main() { srand(time(NULL)); char choice=' '; int posx,posy,i,j; int tarx,tary,moves=0; int dx,dy,x,y; printf("Size: x=");scanf("%d",&x); printf(" y=");scanf("%d",&y); char board[y][x]; 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)\n\nsize=%dx%d, distance=%d, moves=%d",x,y,dx+dy,moves);choice=getch();moves++; 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'); if(posx==tarx && posy==tary)printf("\n\n!! YOU WIN !!\n"); return 0; }



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

good suggestion!
i also make it able to set the size for it.
this is my final code:

for linux using g++:
Code:
#include <stdio.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> #include <time.h> 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=' '; int posx,posy,i,j; int tarx,tary,moves=0; int dx,dy,x,y; printf("Size: x=");scanf("%d",&x); printf(" y=");scanf("%d",&y); char board[y][x]; 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)\n\nsize=%dx%d, distance=%d, moves=%d",x,y,dx+dy,moves);choice=getch();moves++; 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'); if(posx==tarx && posy==tary)printf("\n\n!! YOU WIN !!\n"); return 0; }

for windows using mingw compiler
Code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> int main() { srand(time(NULL)); char choice=' '; int posx,posy,i,j; int tarx,tary,moves=0; int dx,dy,x,y; printf("Size: x=");scanf("%d",&x); printf(" y=");scanf("%d",&y); char board[y][x]; 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)\n\nsize=%dx%d, distance=%d, moves=%d",x,y,dx+dy,moves);choice=getch();moves++; 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'); if(posx==tarx && posy==tary)printf("\n\n!! YOU WIN !!\n"); return 0; }



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

good suggestion!
i also make it able to set the size for it.
this is my final code:

for linux using g++:
Code:
#include <stdio.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> #include <time.h> 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=' '; int posx,posy,i,j; int tarx,tary,moves=0; int dx,dy,x,y; printf("Size: x=");scanf("%d",&x); printf(" y=");scanf("%d",&y); char board[y][x]; 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)\n\nsize=%dx%d, distance=%d, moves=%d",x,y,dx+dy,moves);choice=getch();moves++; 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'); if(posx==tarx && posy==tary)printf("\n\n!! YOU WIN !!\n"); return 0; }

for windows using mingw compiler
Code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> int main() { srand(time(NULL)); char choice=' '; int posx,posy,i,j; int tarx,tary,moves=0; int dx,dy,x,y; printf("Size: x=");scanf("%d",&x); printf(" y=");scanf("%d",&y); char board[y][x]; 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)\n\nsize=%dx%d, distance=%d, moves=%d",x,y,dx+dy,moves);choice=getch();moves++; 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'); if(posx==tarx && posy==tary)printf("\n\n!! YOU WIN !!\n"); return 0; }



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

You Linux version doesn't work anymore in my terminal. The H stays at the same location. If you have two versions nevertheless, just use system("clear")


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

You Linux version doesn't work anymore in my terminal. The H stays at the same location. If you have two versions nevertheless, just use system("clear")


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

You Linux version doesn't work anymore in my terminal. The H stays at the same location. If you have two versions nevertheless, just use system("clear")


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

yeas they are all system("clear") i just copied the wrong code..


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

yeas they are all system("clear") i just copied the wrong code..