C command line game 02-10-2013, 12:15 PM
#1
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.
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]](http://i.imgur.com/WkXnA.png)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)