Introduction

The main purpose of this blog is to share our experience with the world and get experience from the world. This will be a new venture for us and we are sure, we shall learn a lot with it. we will try to Explore our mind which will be very helpful for all us. As from its name, its a blog covering all the topics. we will try to post all the interesting topics and give our opinion. Do give your point of view in comments.


"NAVIGATE VIA BLOG ARCHIVE ON RIGHT TO GET WHAT YOU ARE LOOKING FOR"


Thursday, October 13, 2011

C Code For Rat In a Maze Game

//In this code the rat moves by itself

#include <math.h>
#include <stdio.h>
#include <time.h>
int x,y;
int main ()
{
int i,j,z,v,s,b,c,q=0,m,n,o,u;
char a[x][y];
for (i=0;i<x;i++)
{for (j=0;j<y;j++)
{a[i][j]='X';}}
a[x-1][0]='C';
a[x-1][y-1]='R';
z=x/2;
v=x;
for (i=0;i<=z;i++)
{
a[v-1][1]='T';
v--; }
s=y/2;
b=2;
for (i=0;i<(y-3);i++)
{
a[x/2][b]='T';
b++;
}
c=x/2;
for(i=0;i<=z;i++)
{
a[c][y-2]='T';
c++;}
for (i=0;i<(x-1);i++)
{for (j=0;j<y;j++)
    {q=rand()%2;
    if (a[i][j]!='T')
    {{if (q==0)
     {a[i][j]='o';}
    if (q==1)
     {a[i][j]='X';}}       
         }}}
for (i=0;i<x;i++)
{for(j=0;j<y;j++)
{if (a[i][j]=='T')
    a[i][j]='o';}}
for(i=0;i<x;i++)
{for (j=0;j<y;j++)
{printf("%c ",a[i][j]);} printf("\n");  }
u=a[x-1][y-1];
n=x-1;
o=y-1;
for(i=0;i>-1;i++)
{  
sleep (1);
system("clear");

//left
if ((a[n][o-1]!='X') && o>0 && a[n][o-1]!='1')
{ a[n][o-1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
    {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
    o=o-1;if (a[n][o]==a[x-1][0])
    {printf("\n\t\t\t\tYOU WON\n"); break;}
  // left (force right)
  if (o==0 && a[n+1][o]=='X' && a[n-1][o]!='X')
   {a[n][o+1]='R'; a[n][o]='1';
    sleep(1);
    system("clear");
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
    o=o+1;}
continue;
}

//down
if((a[n+1][o]!='X') && n<(x-1) && a[n+1][o]!='1')
{  a[n+1][o]='R';
   a[n][o]='1';
   for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
   printf("%c ",a[i][j]); printf("\n");}
n=n+1;if (a[n][o]==a[x-1][0])
{printf("\n\t\t\t\tYOU WON\n"); break;}
   // down (force right)
if (a[n+1][o]=='X') {a[n][o+1]='R'; a[n][o]='1';
    sleep(1);
    system("clear");
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;} continue; }

//up
{if ((a[n-1][o]!='X') && n>0)
{ a[n-1][o]='R';
  a[n][o]='1';
  for(i=0;i<x;i++)
  {for (j=0;j<y;j++)
  printf("%c ",a[i][j]); printf("\n");}
n=n-1;
   if (a[n][o]==a[x-1][0])
      {printf("\n\t\t\t\tYOU WON\n"); break;}
  // up (force down)
  if (a[n-1][o]=='X' && a[n][o-1]=='X' && a[n][o+1]=='X')
      {a[n+1][o]='R';
      a[n][o]='1';
      for(i=0;i<x;i++)
      {for (j=0;j<y;j++)
      printf("%c ",a[i][j]); printf("\n");}
      n=n+1; if (a[n][o+1]!='X') {a[n][o+1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;} }

continue;}}

//right
if((a[n][o+1]!='X') && (o<(y-1)) )
{a[n][o+1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;
if (a[n][o]==a[x-1][0])
{printf("\n\t\t\t\tYOU WON\n"); break;} continue;
}

// force down

if (a[n+1][o]=='1')

      {a[n+1][o]=='R'; a[n][o]=='S';}

  }

}// end loop
}//end main       

1 comment:

  1. one more good example with explanation
    http://technofrendzz.blogspot.in/

    ReplyDelete

Related Posts with Thumbnails