Programming with Passion

Make the best out of everything.

Wednesday, 27 November 2019

Snake Game using javascript (P5.js)

Snake Game using javascript (P5.js)



Follow me at Instagram here.
Subscribe to my youtube channel here.

Just learnt something new called P5.js and tried this classic snake game, back in the days I tried it in C++ also, click here to check that one out.

With P5 it looks better. For any suggestion comment below.




Snake Game Code below you can try it out in the online p5 editor.


function setup() {
  createCanvas(600, 400);
  stroke(100);
  s = new Snake();
  s.foodCrd();
  
}

function draw() {
  background(0);
  fill(0);
  rect(0,0,400,400);
  fill(255);
  textSize(20);
  text("P5 Snake", 420, 30);
  textSize(15);
  text("Use arrows to control", 420, 50);
  
  text("Score - ", 420, 70);
  text((s.length)*4, 490, 70);
  s.draw();
  s.dead();
  s.check();
  s.move();
  s.food();
  
  frameRate(10);
  
  
}

function Snake(){
 this.x = 200;
  this.y = 200;
  this.tailx = [];
  this.taily = [];
  this.length = 0;
  this.xspeed = 1;
  this.yspeed = 0;
  this.move = function(){
    
    for(i = this.length-1; i > 0; i--)
    {
       this.tailx[i] = this.tailx[i-1];  
       this.taily[i] = this.taily[i-1];   
    }
    this.tailx[0] = this.x;
    this.taily[0] = this.y;
    
      this.x += this.xspeed*10;
      this.y += this.yspeed*10;
      if(this.x > 390)
        this.x = 0;
      else if(this.x < 0)
        this.x = 390;
      if(this.y < 0)
        this.y = 390;
      else if(this.y > 390)
        this.y = 0;
    
  }
  
  
  this.draw = function(){
    rect(this.x,this.y,12,12);
    
    for(i=0; i < this.length; i++)
      rect(this.tailx[i], this.taily[i], 12, 12);
    
    
  }
  
  this.change = function(x,y){
    this.xspeed = x;
    this.yspeed = y;
  }
  
  this.food = function(){
    fill(150,0,0);
    rect(this.r,this.p,12,12);
    fill(255);
  }
  
  this.foodCrd = function(){
    this.r = int(random(0,400)/10)*10;
    this.p = int(random(0,400)/10)*10; 
    
  }
  
  this.check = function(){
     if(this.x == this.r && this.y == this.p)
     {
       this.foodCrd(); 
       this.length+=1;
     }
       
  }
  
  this.dead = function(){
     for(i=0; i<this.length; i++)
       if(this.x == this.tailx[i] && this.y == this.taily[i])
       {
          this.length = 0;
           this.tailx = [];
         this.taily = [];
           
       }
  }
}

function keyPressed(){
   
    if(keyCode === UP_ARROW)
      s.change(0,-1);
    else if(keyCode === DOWN_ARROW)
      s.change(0,1);
    else if(keyCode === LEFT_ARROW)
      s.change(-1,0);
    else if(keyCode === RIGHT_ARROW)
      s.change(1,0); 
  }


I hope you like it. 
Follow me at Instagram here.
Subscribe to my youtube channel here.

Thursday, 7 November 2019

Juspay Interview Experience

Juspay Interview Experience 2020

About Juspay


Juspay Technologies Pvt Ltd. provides mobile payment solutions. Founded in 2012 by Vimal Kumar and Ramanathan, with a vision to redefine online payments experience by providing 1-click payments on web and mobile, Bangalore-based Juspay Technologies has come a step further with their latest offering, Juspay Safe. Juspay Safe is a specially designed browser for online banking and payments applications and Express Checkout – Payment Gateway Booster. Juspay Safe’s focus is on improving security and user experience for banking. Juspay is not a Payment Gateway (like Citrus, CCAvenue, PayU) but it works with any gateway or aggregator with zero interference in the Merchant-PG relations.
JUSPAY processes payments for most TOP merchants like BookMyShow, Amazon, MakeMyTrip, Snapdeal, Freecharge, Mobikwik, Swiggy, Yatra etc. The company processes more than 4 million transactions per month.
Role - Quality Assurance Engineer
Salaray Offered - 5 LPA

Recruitment Process

It was an off campus drive so crowd was huge can't exactly tell the numbers. There were 5 rounds as follows
1. Online Test - It was a 1:30 hr long test which consisted of aptitude, psychometric test, XML Soap, testing mcqs and one coding question.
Around 25-30 students passed the test.

2. Testing - We were given an mobile application and were supposed to test the app for potential bugs and make a bug report. The app was basically a payment gateway app which consisted of different payment methods and we were supposed to test all those methods. After this round 16 students were shortlisted.

3. Lean-a-thon - This round was basically conducted to check the learning capability of the candidate, So we were given a code in pure script (that is what they use also) along with few study material and we were supposed to add new features to the code by backtracking the code which sounds easy but really is difficult if you don't have any familiarity with the language. I almost gave up in this round but found my way back in. The code was basically of a cube rotation which had features to move the cube in x, y, and z direction our task was to add new buttons which changes the direction, let's say if the cube is rotating clockwise after clicking the button it should move anti-clockwise. Also we were supposed add buttons to change velocity of the cube. 
Four students passed this round.

4. Technical Interview - Questions were asked from DBMS and Data Structure, like code to find loop in linked list, height of tree, find the second highest salary from employee table, normalization forms etc. Interview was about 30-40 min long.

5. HR Interview - This interview lasted exactly 40 mins. It was a telephonic interview. He basically asked me story and asked me why I wanted to work as a Quality Assurance Engineer, Then he asked me a puzzle, the puzzle was we have 25 horses and we have to find the 3 fastest horses. What is the minimum number of steps to find the 3 fastest horses.
When he asked me if I have any questions I asked him a few followed by when will I get the feedback (Always ask question if Interviewer asks you if you have any).
He then asked when can I join and congratulated me for getting selected =D

If you have any questions comment below.

Thursday, 5 September 2019

Diamond Patter using only one while loop | Yamaha Interview Question

Diamond Patter using only one while loop

This problem was asked in Yamaha Interview. Please consider doing it yourself before looking at the answer.

#include<stdio.h>
int main(){
    int line = 1, p = 25, sp=2, st=1;
int spptr = 0, stptr = 0;
    while(p--){
        if(spptr < sp){
            printf("  ");
            spptr++;
        }
        else if (stptr < st){
            printf("* ");
            stptr++;
        }
        else{
            printf("\n");
            line++;
            sp = 3 - line;
            if (sp<0)
                sp = -sp;
            st = (line*2)-1;
            if(line>3){
                st = st%4;
            } 
            spptr = 0;
            stptr = 0;
        }
    }
}

Here is I have taken a variable p = 25 which
is indicating the grid size in which the
diamond is printed, a line number initially 1,
sp(spaces to print) initially 2,
st(stars to print), spptr(space pointer
currently at 0),stptr(star pointer
currently at 0).So, now inside the while loop
it checks if the spptr is less than sp or not
if it is then it prints spaces,else it checks
if stptr is less than st or not if yes than it
prints the star, else it increments the line
number,then changes the spaces to print
according to the line number, changes the
stars to print according to line number,
and re-initialises the spptr & stptrto 0.

Tuesday, 25 June 2019

Merge Sort Visualisation

Merge Sort Visualisation in C



In this program 100 random numbers are generated which are represented on x-y graph using lines
and merge sort is applied on the array, as elements changes their position the graph is redrawn and the visualisation can be seen. Also current position of index is shown by red lines.

Below is the code written in C language.
Compiler used - GCC

Do Follow me on instagram at https://www.instagram.com/dev_it_rish/



#include<stdio.h>
#include<graphics.h>
#include<time.h> 
int maxy=480;
void merge(int arr[], int l, int m, int r) 
    int i, j, k; 
    int n1 = m - l + 1; 
    int n2 =  r - m; 
  
    /* create temp arrays */
    int L[n1], R[n2]; 
  
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++) 
        L[i] = arr[l + i]; 
    for (j = 0; j < n2; j++) 
        R[j] = arr[m + 1+ j]; 
  
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray 
    j = 0; // Initial index of second subarray 
    k = l; // Initial index of merged subarray 
    while (i < n1 && j < n2) 
    { 
        if (L[i] <= R[j]) 
        { 
            arr[k] = L[i]; 
            i++; 
        } 
        else
        { 
            arr[k] = R[j]; 
            j++; 
        } 
        k++;
delay(8);
cleardevice();
for(int z=0;z<100;z++)
{
if(z==i || z==j || z==k)
setcolor(RED);
line((4*z)+120,maxy-arr[z],(4*z)+120,maxy);
if(z==i || z==j  || z==k)
setcolor(WHITE);
    } 
  
    /* Copy the remaining elements of L[], if there 
       are any */
    while (i < n1) 
    { 
        arr[k] = L[i]; 
        i++; 
        k++; 
delay(8);
cleardevice();
for(int z=0;z<100;z++)
{
if(z==i || z==j  || z==k)
setcolor(RED);
line((4*z)+120,maxy-arr[z],(4*z)+120,maxy);
if(z==i || z==j  || z==k)
setcolor(WHITE);
    } 
  
    /* Copy the remaining elements of R[], if there 
       are any */
    while (j < n2) 
    { 
        arr[k] = R[j]; 
        j++; 
        k++; 
delay(8);
cleardevice();
for(int z=0;z<100;z++)
{
if(z==i || z==j  || z==k)
setcolor(RED);
line((4*z)+120,maxy-arr[z],(4*z)+120,maxy);
if(z==i || z==j  || z==k)
setcolor(WHITE);
    } 
  
/* l is for left index and r is right index of the 
   sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r) 
    if (l < r) 
    { 
        // Same as (l+r)/2, but avoids overflow for 
        // large l and h 
        int m = l+(r-l)/2; 
  
        // Sort first and second halves 
        mergeSort(arr, l, m); 
        mergeSort(arr, m+1, r); 
  
        merge(arr, l, m, r); 
    } 


int main()
{
int gd=DETECT,gm;
srand(time(0)); 
int temp,n,i,k,j,arr[100];
for(i=0;i<100;i++)
arr[i] = rand()%480;
initgraph(&gd,&gm,NULL);
mergeSort(arr, 0, 99);
scanf("%d",&n);
closegraph();
}

Do Follow me on instagram at https://www.instagram.com/dev_it_rish/



Saturday, 22 June 2019

Taking a non-standard input format

Scanf

Taking a non-standard input format

#include <cstdio> 
using namespace std;
int N; // using global variables in contests can be a good strategy 
char x[110];  /make it a habit to set array size a bit larger than needed
int main() 
scanf("%d\n", &N); 
while (N--) {
 // we simply loop from N, N-1, N-2, ..., 0 
scanf("0.%[0-9]...\n", &x); // ‘&’ is optional when x is a char array 
// note: if you are surprised with the trick above,
// please check scanf details in www.cppreference.com printf("the digits are 0.%s\n", x); 

Follow me on Instagram - https://www.instagram.com/dev_it_rish/
Source - Competitive Programming by Steven Halim

Monday, 14 May 2018

2048 C++ code

2048 C++ code


If you find any bugs in this game do comment and let me know. 

My GitHub repository
https://github.com/Rishabh1998/2048

C++ code 

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<process.h>
#include<fstream.h>
int grid[4][4]={0},hs=0,x,y,i,j,k,p,q,fup,fleft,fright,fdown,sc=0,lastscore=0;
int next[15]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},count=0;
char key,*out;
void hsc()  //TO CALCULATE & STORE HIGH SCORE
{
ofstream fout("High.txt"); //DATA HANDLING USED
fout<<sc;
fout.close();
settextstyle(1,0,1);
setcolor(0);
itoa(hs,out,10);
outtextxy(500,150,out);
setcolor(YELLOW);
itoa(sc,out,10);
outtextxy(500,150,out);
hs=sc;
}

void change();
void generate();
void clrsc()
{
itoa(sc,out,10);
setcolor(0);
outtextxy(500,250,out);
}
void up()
{
fup=0;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if(grid[i][j]==0)
for(k=i+1;k<4;k++)
{
if(grid[k][j])
{
grid[i][j]=grid[k][j];
grid[k][j]=0;
--j;
fup=1;
break;
}
}
else if(grid[i][j]){
for(k=i+1;k<4;k++)
{
if(!grid[k][j])continue;
else if(grid[i][j]==grid[k][j])
{
grid[i][j]+=grid[k][j];
grid[k][j]=0;
fup=1;
clrsc();
sc+=grid[i][j];
}break;
}
}
}
}
 if(fup){ change();delay(100); generate(); }
}


void right()
{
fright=0;
for(i=3;i>0;i--)
{
for(j=0;j<4;j++)
{
if(grid[j][i]==0)
for(k=i-1;k>=0;k--)
{
if(grid[j][k])
{
grid[j][i]=grid[j][k];
grid[j][k]=0;
--j;
fright=1;
break;
}
}
else if(grid[j][i]){
for(k=i-1;k>=0;k--)
{
if(!grid[j][k])continue;
else if(grid[j][i]==grid[j][k])
{
grid[j][i]+=grid[j][k];
grid[j][k]=0;
fright=1;
clrsc();
sc+=grid[j][i];
}
break;
}
}
}
if(fright){
change();
delay(100);
generate(); 
}
}


void left()
{
fleft=0;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if(grid[j][i]==0)
for(k=i+1;k<4;k++)
{
if(grid[j][k])
{
grid[j][i]=grid[j][k];
grid[j][k]=0;
--j;
fleft=1;
break;
}
}
else if(grid[j][i]){
for(k=i+1;k<4;k++)
{
if(!grid[j][k])continue;
else if(grid[j][i]==grid[j][k])
{
grid[j][i]+=grid[j][k];
grid[j][k]=0;
fleft=1;
clrsc();
sc+=grid[j][i];
}
break;
}
}
}
}
 if(fleft){
change();
delay(100);
generate(); 
}
}

void down()
{
fdown=0;
for(i=3;i>0;i--)
{
for(j=0;j<4;j++)
{
if(grid[i][j]==0)
for(k=i-1;k>=0;k--)
{
if(grid[k][j])
{
grid[i][j]=grid[k][j];
grid[k][j]=0;
--j;
fdown=1;
break;
}
}
else if(grid[i][j]){
for(k=i-1;k>=0;k--)
{
if(!grid[k][j])continue;
else if(grid[i][j]==grid[k][j])
{
grid[i][j]+=grid[k][j];
grid[k][j]=0;
fdown=1;
clrsc();
sc+=grid[i][j];
}
break;
}
}
}
}

if(fdown){
change();
delay(100);
generate();
}
}

void draw()
{
ifstream fin("HIGH.TXT");
fin.seekg(0);
fin>>hs;
fin.close();
settextstyle(3,0,1);
outtextxy(200,50,"2048");
outtextxy(500,100,"HIGH SCORE");
itoa(hs,out,10);
outtextxy(500,150,out);
outtextxy(500,200,"SCORE");
outtextxy(500,250,"0");
outtextxy(500,280,"Use directional");
outtextxy(500,305,"keys to play.");
outtextxy(500,350,"Press 'e' to exit");
line(200,100,440,100);
line(440,100,440,340);
line(440,340,200,340);
line(200,340,200,100);
line(200,160,440,160);
line(200,220,440,220);
line(200,280,440,280);
line(260,100,260,340);
line(320,100,320,340);
line(380,100,380,340);
}
void change()
itoa(lastscore,out,10);
setcolor(0);
outtextxy(500,250,out);
itoa(sc,out,10);
setcolor(YELLOW);
outtextxy(500,250,out);
if(hs<sc)
hsc();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(grid[i][j])
{
setcolor(YELLOW);
p=210+j*60;
q=110+i*60;
bar(p,q,p+40,q+40);
itoa(grid[i][j],out,10);
//floodfill(p+20,q+20,YELLOW);
setcolor(BLACK);
outtextxy(p+5,q+5,out);
}
else{
//setcolor(BLACK);
p=210+j*60;
q=110+i*60;
bar(p,q,p+40,q+40);
//itoa(grid[i][j],out,10);
//floodfill(p+20,q+20,WHITE);

//setcolor(WHITE);
//outtextxy(p+15,q+10,out);
}
}
void generate()
{
count=0;
srand(time(0));
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(!grid[i][j]){
next[count++]=i;
next[count++]=j;
}
if(count){
x=rand()%count;
if(x%2==0)grid[next[x]][next[x+1]]=2;
else      grid[next[x-1]][next[x]]=2;
change();
}
for(i=0;i<count;i++)next[i]=-1;
}

play(){
while(1)
{
while(kbhit()){
key=getch();
switch(key)
{
case(72):  up();        //up
break;
case(77):   right();          //right
break;
case(80):    down();         //down
break;
case(75):  left();            //left
break;
case('e'):exit(0);
default:break;
}

}
}
}
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setbkcolor(BLACK);
//setcolor(WHITE);
draw();
settextstyle(1,0,1);
generate();
generate();
play();
getch();
closegraph();
}

Happy Coding.

Tuesday, 21 November 2017

Example for Gui, Event handling and multi threading in JAVA

import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Date;

class calc extends JFrame implements ActionListener,Runnable
{
private JLabel n1,n2,r,c;
private JTextField f1,f2,f3;
private JButton a,s,m,d;
Thread th,dt;

public calc(){
super("CALCULATOR");
setLayout(null);
c = new JLabel("Caculator");
c.setFont(new Font("Comic Sans MS",Font.BOLD,24));

n1 = new JLabel("Number 1");
n2 = new JLabel("Number 2");
r = new JLabel("Result");
f1 = new JTextField(10);
f2 = new JTextField(10);
f3 = new JTextField(10);
a = new JButton("+");

s = new JButton("-");
m = new JButton("*");
d = new JButton("/");
a.setFont(new Font("Comic Sans MS",Font.BOLD,24));
s.setFont(new Font("Comic Sans MS",Font.BOLD,24));
m.setFont(new Font("Comic Sans MS",Font.BOLD,24));
d.setFont(new Font("Comic Sans MS",Font.BOLD,24));
c.setBounds(150,30,200,50);
add(c);
n1.setBounds(30,60,200,50);
add(n1);
f1.setBounds(100,75,200,25);
n2.setBounds(30,90,200,50);

f2.setBounds(100,105,200,25);
r.setBounds(30,120,200,50);
f3.setBounds(100,135,200,25);
a.setBounds(100,170,50,50);
s.setBounds(200,170,50,50);
m.setBounds(100,240,50,50);
d.setBounds(200,240,50,50);
add(f1);
add(n2);

add(r);

add(f2);
add(f3);
add(a);
add(s);
add(m);
add(d);
a.addActionListener(this);
s.addActionListener(this);
m.addActionListener(this);
d.addActionListener(this);

th=new Thread(this);
th.start();

dt=new Thread(this);
dt.start();
}
public void run()
{
if(Thread.currentThread()==th)
{
for(;;)
{
if(c.getText().equals("Calculator"))
{
c.setText("                ");
}
else
{
c.setText("Calculator");
}
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}
}
if(Thread.currentThread()==dt)
{
for(;;)
{
Date d=new Date();
this.setTitle(d.toString());
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}
}

}
public void actionPerformed(ActionEvent e)
{
int num = Integer.parseInt(f1.getText());
int num2 = Integer.parseInt(f2.getText());
int re=0;
if(e.getSource()==a)
{
re=num+num2;
f3.setText(" "+re);

}
if(e.getSource()==s)
{
re=num-num2;
f3.setText(" "+re);
}
if(e.getSource()==m)
{
re=num*num2;
f3.setText(" "+re);
}
if(e.getSource()==d)
{
re=num/num2;
f3.setText(" "+re);
}


}


public static void main(String args[]){
calc c = new calc();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(400,400);
c.setVisible(true); 
}

}