Login Register


C command line game filter_list
Author
Message
C command line game #3
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; }
[Image: WkXnA.png]

Reply





Messages In This Thread
C command line game - by Xe.4 - 02-10-2013, 12:15 PM
C command line game - by Xe.4 - 02-10-2013, 12:15 PM
C command line game - by Xe.4 - 02-10-2013, 12:15 PM



Users browsing this thread: 1 Guest(s)