Search This Blog

Friday, 26 August 2016

The Snake Game in C++

It is similar to the game which was very common in mobiles few years back. The snake eats food items to increase its length and tries to avoid obstacles in order to win the game. The game has 3 modes- easy, normal and hard. Hard mode has the maximum number of obstacles. The obstacles and food items are generated at random so every time the game is different from the last time. 

The source code is given below-

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<time.h>
#include<stdlib.h>
//created by Aditya Vijayvergia 
//uploaded at thecodingedge.blogspot.com
int snake[40][2],point[20][2],dead[200][2],nd,len,n=20,m=39,pos[2];
void create()
{
   int i;
   len=5;
   srand(time(NULL));
   for(i=0;i<len;i++)
       {
  snake[i][1]=i;
       }
   for(i=0;i<20;i++)
      {
  point[i][0]=rand() %20;
  point[i][1]=rand() %40;
      }
   srand(time(NULL));
   for(i=0;i<nd;i++)
      {
  dead[i][0]=rand() %20;
  dead[i][1]=rand() %40;
      }
}

int found(int x,int y)
{
for(int i=0;i<len;i++)
   if(snake[len-1][0]==x&&snake[len-1][1]==y)
     return 2;

   else if(snake[i][0]==x&&snake[i][1]==y)
     return 1;
return 0;
}

int ifdead(int x,int y)
{

   for(int j=0;j<nd;j++)
   if(dead[j][0]==x&&dead[j][1]==y)
       return 1;
   return 0;
}
int ifpoint(int x,int y)
{
   for(int i=0;i<20;i++)
   {
   if(point[i][0]==x&&point[i][1]==y)
      return 1;
   }
   return 0;
}

void show()
{
   //cout<<"show running";
   clrscr();
   cout<<"\n";
   for(int k=0;k<m+1;k++)
   cout<<"__";
   cout<<"\n";
   for(int i=0;i<n;i++)
   {
   cout<<"|";
   for(int j=0;j<m;j++)
   {
       if(found(i,j)==2)
       cout<<" m";
       else if(found(i,j)==1)
       cout<<" o";
       else if(ifpoint(i,j))
       cout<<" *";
       else
       if(ifdead(i,j))
       cout<<" X";
       else
       cout<<"  ";
   }
   cout<<"|";
   }
   for(k=0;k<m+1;k++)
   cout<<"__";
}

void changepoint(int x,int y)
{
   for(int i=0;i<20;i++)
   if(point[i][0]==x&&point[i][1]==y)
   {
       point[i][0]=-1;
       point[i][1]=-1;
   }
}


void move(int x,int y)
{
    x=(x+n)%n;
    y=(y+m)%m;
    if(ifpoint(x,y))
    {
  pos[0]=x;
  pos[1]=y;
  changepoint(x,y);

    }
    else
    if(ifdead(x,y))
    {
      clrscr();
      cout<<"\n\n\n\t\tGAME OVER\n\n\t\tScore:"<<len-5;

      getch();
      exit(0);
    }
    for(int i=0;i<len-1;i++)
    for(int j=0;j<2;j++)
       snake[i][j]=snake[i+1][j];
    snake[i][0]=x;
    snake[i][1]=y;
    //}
    show();

    if(snake[len-1][0]==pos[0]&&snake[len-1][1]==pos[1])
    {
 len++;
  if(len-5==20)
  {
     clrscr();
     cout<<"\n\n\n\t\tVICTORY";
     exit(0);
  }
 for(i=len-1;i>0;i--)
 for(j=0;j<2;j++)
     snake[i][j]=snake[i-1][j];
 snake[0][0]=pos[0];
 snake[0][1]=pos[1];
 pos[0]=-1;
 pos[1]=-1;
    }
}

void menu()
{
   int choice;
   cout<<"\n\n\tSANKE GAME";
   cout<<"\n\n\n1.easy\n2.normal\n3.hard";
   cin>>choice;
   switch(choice)
   {

   case 1:nd=50;
   break;
   case 2:nd=125;
   break;
   case 3:nd=200;
   break;
   }
}

void main()
{
    clrscr();
    menu();
    create();
    show();
    pos[0]=-1;
    pos[1]=-1;
    char m;
    while(1)
    {
    cout<<"\n\t\t";
    cout<<"enter choice:";
    cin>>m;
    switch(m)
    {
    case 'd':move(snake[len-1][0],snake[len-1][1]+1);
      break;
    case 'w':move(snake[len-1][0]-1,snake[len-1][1]);
      break;
    case 's':move(snake[len-1][0]+1,snake[len-1][1]);
      break;
    case 'a':move(snake[len-1][0],snake[len-1][1]-1);
      break;
    case 'p':exit(0);
    }
    }
}

Sample output is given below.


 






Thursday, 11 August 2016

Generating permutations in Java using array

The following code generates all possible permutations till the entered integer integers. Similar program can be made to permute the characters of a string.

Explanation:
First we enter only 1 in a array.
Then for entering 2 we have two choices- before 1 and after 1 as- "21" and "12".
The program uses recursion so for "21" we can add 3 at 3 different places as "321", "231" and "213" i.e., at first position, between them and in the last position.

The code is given below: 

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Aditya Vijayvergia @ thecodingedge.blogspot.com
 */
public class GeneratingPermutations {

    static int perm(int []a,int n,int m)
    {                          //n=the element that is being added in this recursive call
        int []b=new int[m];    //m=total no of elemets
        for(int i=0;i<n;i++)
            {
                System.arraycopy(a, 0, b, 0, m);//copy the state of array a to array b
                for(int j=n-1;j>i;j--)//shift values 1 step ahead
                    b[j]=b[j-1];
                b[i]=n;//store n at the empty location
                if(n==m)
                {
                    System.out.println(Arrays.toString(b));
                }
                else{    
                    perm(b,n+1,m);   
                    }
            }
        return 0;
    }
    
    public static void main(String[] args) {
        int m;
        System.out.print("enter the number of elements:");
        Scanner in=new Scanner(System.in);
        m=in.nextInt();
        int []a=new int[m];
        perm(a,1,m);
    }
}

Sample Output:

Tuesday, 2 August 2016

Maze Solving Algorithm



This maze solving algorithm works on the principle of Backtracking. It is implemented in Java.

The main idea is to solve the problem is move from the starting point in all the possible directions until we reach the goal. While moving in a particular direction, when ever the path splits(i.e., a node is encountered) one of the path is chosen. We move on the same path until we encounter a dead end(or a leaf). When a leaf is reached, we backtrack to the last encountered node and chose another path. This procedure is followed recursively until either we reach the goal or we reach the first encountered node again by backtracking from all possible ways and hence no solution to the maze exists. When we have reached our goal, we can mark the path used.

The code is explained with comments. Further for better understanding, i have used some print statements as comments. The code can be tested on other maze problem too by simply changing the maze array used in the code.

Open link to go to View Source Code.
Click here to download class file.



The output of the code is given under: