Thursday, January 23, 2014

Pushbox game in java.

Source code for the Game I designed with Kaustubh Kumbhar. Feel free to modify it and use it for educational and non-profit purpose.

How to use.
use appletviewer to run the code.
keep caps on while playing. 

Violet square is Hero. (tht little moving character in games we like to call hero)
Black square is hurdle. 
Yellow square is a box and you must push all yellow boxes into black squares.
Red square is wall.

Rules:
  • Movement of Hero is done by using keys w as up, a as left, s as down, d as right.
  • Hero is restricted through walls ans hurdles.
  • Hero can push only one object at a time.
  • Remember only pushing is possible pulling is restricted 
  • When you push box in hurdles both the object and hurdles will disappear and the position acquired by hurdle before pushing will become free space.
  • Play keeping caps on.

C:\jdk\bin\javac appjpushbox.java

C:\jdk\bin\appletviewer appjpushbox.java

/*
<applet code="appjpushbox.class" height=500 width=500>
</applet>
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import javax.swing.*;

public class appjpushbox extends Applet implements KeyListener     
{
char a[][]=new char[11][9];
char temp;
int i,j,x1,y1,upos,final1;

public void init()
{
addKeyListener(this);
//Designing the Stage 
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
a[i][j]='a';
}
}
for(i=0;i<11;i++)
{
a[i][0]='H';
a[i][8]='H';
}
for(i=0;i<9;i++)
{
a[0][i]='H';
a[10][i]='H';
}
a[1][1]='X';
a[1][3]='X';
a[1][5]='X';
a[2][7]='X';
a[3][7]='X';
a[8][7]='X';
a[9][1]='X';
a[1][6]='U';
a[2][2]='U';
a[3][5]='U';
a[5][2]='U';
a[6][6]='U';
a[8][4]='U';
a[8][6]='U';
a[3][2]='O';
a[2][3]='H';
a[2][4]='H';
a[4][1]='H';
a[4][2]='H';
a[5][5]='H';
a[5][7]='H';
a[7][2]='H';
a[7][3]='H';
a[7][4]='H';

x1=10;
y1=-425;
}

public void keyPressed(KeyEvent ke)
{
char ch=ke.getKeyChar();
System.out.println("\n\tKey Pressed :"+ch);
int key=ke.getKeyCode();
switch(ch)
{
case 'W':
upos=pos();
if(a[(upos/9)-1][upos%9]!='H')
{
if(a[(upos/9)-1][upos%9]=='U'&&a[(upos/9)-2][upos%9]=='X')
{
a[(upos/9)][upos%9]='a';
a[(upos/9)-1][upos%9]='O';
a[(upos/9)-2][upos%9]='a';
}
else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]=='H'))
break;

else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]=='U'))
break;

else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]!='H'))
{
temp=a[(upos/9)-2][upos%9];
a[(upos/9)-2][upos%9]=a[(upos/9)-1][upos%9];
a[(upos/9)-1][upos%9]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)-1][upos%9];
a[(upos/9)-1][upos%9]=temp;
}
else if(a[(upos/9)-1][upos%9]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)-1][upos%9];
a[(upos/9)-1][upos%9]=temp;
}
}
final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;
case 'S' : upos=pos();
if(a[upos/9+1][upos%9]!='H')
{

if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='X'))
{
a[(upos/9)][upos%9]='a';
a[(upos/9)+1][upos%9]='O';
a[(upos/9)+2][upos%9]='a';
}

else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='H'))
break;

else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='U'))
break;
else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]!='H'))
{
temp=a[upos/9+2][upos%9];
a[upos/9+2][upos%9]=a[upos/9+1][upos%9];
a[upos/9+1][upos%9]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)+1][upos%9];
a[(upos/9)+1][upos%9]=temp;
}
else if(a[(upos/9)+1][upos%9]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)+1][upos%9];
a[(upos/9)+1][upos%9]=temp;
}
}
final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;
case 'A' : upos=pos();
if(a[upos/9][upos%9-1]!='H')
{
if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='X'))
{
a[(upos/9)][upos%9]='a';
a[(upos/9)][upos%9-1]='O';
a[(upos/9)][upos%9-2]='a';
}

else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='H'))
break;

else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='U'))
break;

else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]!='H'))
{
temp=a[upos/9][upos%9-1];
a[upos/9][upos%9-1]=a[upos/9][upos%9-2];
a[upos/9][upos%9-2]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9-1];
a[(upos/9)][upos%9-1]=temp;
}
else if(a[(upos/9)][upos%9-1]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9-1];
a[(upos/9)][upos%9-1]=temp;
}
}final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;
case 'D' : upos=pos();
if(a[upos/9][upos%9+1]!='H')
{

if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='X'))
{
a[(upos/9)][upos%9]='a';
a[(upos/9)][upos%9+1]='O';
a[(upos/9)][upos%9+2]='a';
}

else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='H'))
break;

else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='U'))
break;
else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]!='H'))
{
temp=a[upos/9][upos%9+1];
a[upos/9][upos%9+1]=a[upos/9][upos%9+2];
a[upos/9][upos%9+2]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9+1];
a[(upos/9)][upos%9+1]=temp;
}
else if(a[(upos/9)][upos%9+1]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9+1];
a[(upos/9)][upos%9+1]=temp;
}

}final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;

}
repaint();
}

public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void paint(Graphics g)  // For displaying the array
{
x1=10;
y1=0;
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j]=='H')
{
g.setColor(Color.red);
g.fillRect(x1,y1,40,40);
g.setColor(Color.black);
g.drawRect(x1+1,y1+1,38,38);
}
if(a[i][j]=='U')
{
g.setColor(Color.orange);
g.fillRect(x1+3,y1+3,34,34);
}
if(a[i][j]=='X')
{
g.setColor(Color.darkGray);
g.fillRect(x1+3,y1+3,34,34);
}
if(a[i][j]=='O')
{
g.setColor(Color.magenta);
g.fillRect(x1+3,y1+3,34,34);
}
if(a[i][j]=='a')
{
g.setColor(Color.green);
g.fillRect(x1,y1,40,40);
g.setColor(Color.black);
g.drawRect(x1+1,y1+1,38,38);
}
x1=x1+40;
}
x1=10;
y1=y1+40;
}
}
public int pos()
{
int pos;
for(i=1;i<11;i++)
{
for(j=1;j<9;j++)
{
if(a[i][j]=='O')
{
pos=9*i+j;
return(pos);
}
}
}
return -1;
}
public int finish()
{
int final2=0;
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j]=='U')
{
final2=1;
return(final2);
}
}
}
return(final2);
}
}

Sunday, June 30, 2013

Google Person Finder - Helping Uttarakhand Floods victims.


Uttarakhand Floods has shown what our so called progress has done to our mother earth and how she took the revenge. The disaster in Uttarakhand was really heart wrenching and many of the victims are still missing and struggling for support. Many big organizations and trusts are helping these victims. The web-giant Google also helping victims to find and report missing persons. 

You can see the "resources" link given on the Google homepage. 




You can visit the link to report about someone or to find the missing person in a directory of missing persons.  
They also have as SMS service to find the information about missing person. It can be used as follow.

"You can request status via SMS by sending an SMS to 9 77 33 00 000 with the message Search person-name
For example, if you are searching for Jayanth, send the message Search Jayanth"

The person finder service is created for finding missing persons in flood or earthquake like situations.

you can visit the page directly on following link.

https://google.org/personfinder/2013-uttrakhand-floods/


If you are developer you can contribute the project and access the repo and also write into it using the api provided on the link.

Saturday, December 29, 2012

Create your own youtube + slideshare presentation with popcorn.js.


I created a demo of youtube + slideshare presentation quite a long ago. Many asked me to explain how to do it. Specially this guy Michael Britt So here it is.

First of all some basics about popcorn.js. As given on their website - 
Popcorn.js is an HTML5 media framework written in JavaScript for filmmakers, web developers, and anyone who wants to create time-based interactive media on the web. Popcorn.js is part of Mozilla's Popcorn project.
Now the presentation.

What to do?

We are gonna synchronize the youtube video hosted on youtube.com with slideshare presentation hosted on slideshare.net. We'll specify for what duration which slide to be opened?

How?

Youtube and slideshare give the facility to embed  the videos and presentations hosted on their sites respectively on our webpage. Popcorn.js has youtube player which enables synchronizing youtube video to run the jobs specified in plugins. Slideshare player API let us jump to any slide we want using jumpTo function in API. (Slideshare player API)

popcorn.js has many plugins for some of the popular websites like facebook, twitter, flicker, etc. The one we are using is a "code" plugin. This plugin let us run the javascript code on the same page when the specified video duration is reached.

So lets start..

First we have to add the youtube video and slideshare presentation. Remember we are not gonna use youtube embed player instead we will be using popcorn.js youtube player to embed the video so that we can   monitor the video duration.

add script to the head section of code.



JavaScript

  
  <script type="text/javascript" src="http://popcornjs.org/code/dist/popcorn-complete.min.js"> </script>



 Now following code will embed the youtube player with given link to the page


JavaScript

  
  <script type="text/javascript"> 

    // ensure the web page (DOM) has loaded 
    document.addEventListener("DOMContentLoaded", function () { 
        var vstart=1; 

        // Create a popcorn instance by calling the Youtube player plugin 
        var example = Popcorn.youtube( 
        '#video', 
        'http://www.youtube.com/watch?v=Zm1c2WTD93w&rel=0' ); 
        example.play();
    });

  </script>


Code Description


1st line with a eventlistner ensures that the video has been loaded. After that the function starts. Here we are creating a popcorn instance by calling youtube player plugin. The plugin has 2 necessary attributes - id and link. id attribute specifies the id of the page element where the player will be embedded. In our case we have a div tag with id - video. Hence first attribute #video and second attribute is a link to the youtube video that has to be embed. 
To start the video we must use the event "play" instance_name.play(). In our case example.play().

Now let us add slideshare presentation with slideshare player api.


JavaScript

  
<script type="text/javascript">

  var flashMovie;

  //Load the flash player. Properties for the player can be changed here.
  function loadPlayer() {

      //allowScriptAccess from other domains
      var params = { allowScriptAccess: "always" };
      var atts = { id: "player" };

      //doc: The path of the file to be used
      //startSlide: The number of the slide to start from
      /*rel: Whether to show a screen with related slideshows 
        at the end or not. 0 means false and 1 is true.. */
     
      var flashvars = { doc : "linux-100419010247-phpapp01", startSlide : 1, rel : 0 };
                      
      //Generate the embed SWF file
      swfobject.embedSWF("http://static.slidesharecdn.com/swf/ssplayer2.swf", "player", "598", "480", "8", null, flashvars, params, atts);

      //Get a reference to the player
      flashMovie = document.getElementById("player");
  }

  //Jump to the appropriate slide
  function jumpTo() {

      flashMovie.jumpTo(parseInt(document.getElementById("slidenumber").value));
  }

  //Update the slide number in the field for the same
  function updateSlideNumber()  {
      document.getElementById("slidenumber").value = flashMovie.getCurrentSlide();
  }           
  </script>

Code description



This code will embed the slideshare player with specified ppt in the code by the attribute flashvars.
 Most of the attributes and functions in the code are self explanatory. The one which is hard to extract from a slides is a "doc" attribute of flashvar variable in code. It is the path of the file to be used but this path is not a link to that presentation. It can be extracted by a simple json request as follows.

http://www.slideshare.net/api/oembed/2?url=http://www.slideshare.net/haraldf/business-quotes-for-2011&format=json 

This link will give a json formatted response in which a attribute named "slide_image_baseurl" gives a value like this.

//image.slidesharecdn.com/110103quotes2010-12-110103073149-phpapp01/95/slide-

When you trim down "//image.slidesharecdn.com/" this part and "/95/slide-" this part you will get the value for doc variable.The function jumpTo()is the function which will when called with a slidenumber shows the specified slide. So in our code plugin we will be calling this function to change the slide. The updateSlideNumber() will update the changed slide number.

Now the code plugin.

According to its description on popcornjs.org

Allows for the ability to run arbitrary code ( JavaScript functions ) according to video timing.
so we will be using this plugin to call our slideshare player functions.
The plugin has following attributes that we will be using in our code. You can read about other options on http://popcornjs.org/popcorn-docs/plugins/ .

Start : Start of plugin working.
End : End of plugin working.
OnStart : The function to call on starting of the code plugin.
OnEnd : The function to call on end of the code plugin.

Here When I was making this demo I wanted to change my slides on 2nd and 5th sec of the video to the 2nd and 3rd slide so what I did is simply add this code to the function we called on the "DOMContentLoaded" event. 

JavaScript

example.code({ 
  start: 2, 
  end: 5, 
  onStart: function() { 
    flashMovie.jumpTo("2"); 
  }, 
  onEnd: function() { 
    flashMovie.jumpTo("3"); 
  } 
});

  
For other slides I simple added more "code" plugin options. Thats it. Some space for both players on the page using simple table and my video + slides presentation is ready. You can have a look on the demo here. If any questions feel free to ask.

Tuesday, July 31, 2012

Little bit popcorn.

No its not a meaningless non-technical post. Of course I am talking about popcorn which is the name of one of  mozilla's great projects. Popcorn.js is an HTML5 media framework written in JavaScript for filmmakers, web developers, and anyone who wants to create time-based interactive media on the web. Yes you read it right. 




Popcorn.js lets you synchronize your webpage elements with the embedded video tags and some of the plugins like youtube-plugin or vimeo-plugin lets you synchronize videos hosted on their sites to your webpage elements. 


I am not gonna tell more about its implementation part here. You can see documentation and tutorials for popcorn.js on popcornjs.org. I am just want to tell how popcorn.js can be used in a simple application where you can synchronize your power-point presentation with a related youtube video. 





I have created a simple demo application resting here. The video is resting on youtube and presentation slides are uploaded on slideshare The link is provided with facebook comment plugin. Feel free to ask if you have any questions. 

Wednesday, September 14, 2011

Web Development with CMS

Data is the most important factor of Internet age. Converting data into information is most necessary task for every programmer. And to use the information most efficiently for the project is the hardest job for the Developer. Those who had used their information most cleverly are now at highest positions like Google in Search Engines and Facebook in social networking. Thus Content Managing Strategy is the base of business in today's age. That gave birth to the new type of softwares - CMS.
CMS means Content Management Systems. As mentioned in Wikipedia page of CMS "A content management system (CMS) is a system providing a collection of procedures used to manage work flow in a collaborative environment."


 

The main motives behind CMS are
  • To Organize data in efficient manner.
  • Let many developers use data with lesser dependency conflicts.
  • Protecting data and access to data on the basis of developer role.
  • Better communication between developers over the platform.
  • Reduce the data redundancy and duplication.
All these issues are very obvious in website developing especially when developer let users store and manage data online like Social Networking Websites. In those cases CMS is the best solution to manage, store and deliver data with security. Joomla! is the new favorite amongst the programmers. The main focus of this post is on the Joomla!.

Joomla! logo

Joomla! is award winning CMS. Joomla! is the open source community project which let site developers build the websites with lesser coding and higher security. It not just provide the environment for development but also gives great connectivity to the databases and server with security also the access to the components of website are efficiently given to the users of CMS. 
Joomla! not only helps in site development but also highly useful in web application development. As the main concern is about information, Joomla! is highly useful in applications and projects in which 'Information or Content Management plays crucial role like 
  • Corporate Web sites or portals.
  • Online magazines, newspapers, and publications.
  • E-commerce and online reservations.
  • Community-based portals.
  • School and College Web sites.
  • Small business websites.
  • Static Websites.
Joomla! is not only for middle developers. As Joomla! is Open Source and a community project anyone can help Joomla! to make it more and more useful. Developers can also contribute by creating add-ons for Joomla!. Joomla is written in PHP - the most used server side scripting language and based on MySQL. 

Some of great websites developed with Joomla!(given on Joomla! official website)
You can also see some great websites build with Joomla! on Joomla Community Site Showcase 

One of my favorites is http://offclassroom.com/. This site is a great project of students group from ADIT college of Gujrat, India.

Joomla! is really great software to work on. Get your Joomla! now, its free and always will be free and start developing and contributing to the WEB 2.0

Technorati claim token : 5Y6CQQF4D3V3

Tuesday, July 26, 2011

Search Engine Optimization - Basic

Web has expanded a lot, and more than that. The search engines now talk in exabytes which is equal to 1 billion gigabytes. Google has more than 4000 million webpages indexed with its powerful search engine. It is very difficult to be on the top of the list in this type of tough fight which gives birth to the new career in IT - Search Engine Optimization. 


 
What is Search Engine Optimization...??

Search engine optimization (SEO) is the process of improving the visibility of a website or a web page in search engines.( source: Wikipedia).

Search Engines has their own strategies to rank the pages or websites. When you search anything through search engine, it gives thousands or even millions of result. No one is going to see all those but the sum of on the top are viewed by users. So if you want your website to be viewed by more and more users all you need is upper rank in those searches. But it is not so easy, for that you need to know how search engines work, how they rank and place the pages in their search results. Even the domain age is checked for stability of domain. Popularity of the product for which the website is created, reputation of the websites is also the factors affecting the rank because Search engines also have the competition to be on the top.

Now all these things are not possible for end users or even the companies single handily. That gives you the 1 more new career opportunity as an Search Engine Optimizer (SEO). SEO uses the some techniques like adding the mostly searched keywords in your website, removing redundant data from your website which highlights your website in Result Engine of the Search Engine.

If you are a company user and can afford an SEO thats good but if you cant then there are some simple tricks which can improve your rank in search results. 
  1. Keep the amount of data on single page High. More the data on webpage more the chances of improvement in rank of that page.
  2. Use keyword in your pages. Keywords are very useful in webpages as all the searches are done by using keywords only.
  3. Keep the keyword density moderate. If you use the keyword ( like SEO in this post) for many times the page may be declared as spam and get penalized by Search Engine.
  4. Text Formatting. While writing webpages useful tags like Meta tags, Title tag, Alt tags for images must be added.
  5. Your Home page should be the face of your site. Users mostly judge the websites by their home page. Add better functionality to your site through links from home page.
 While we are talking about increasing the rank by natural and algorithmic means some search engines provide premium services known as Search Engine Marketing in which paid rankings are provided to the webpages or websites by search engines itself. But though SEO plays vital role in increasing the rankings and most probably preferred by website admins.