Saturday 29 June 2013

  DHCP WORKING MECHANISM


DHCP is a very common protocol and we often here about it. DHCP is much more complex than it looks. DHCP IP address assignment process goes through a few steps explained in this article.
DHCP stands for Dynamic Host Configuration Protocol and is used to automatically assign IP configuration to hosts connecting to a network. The Dynamic Host Configuration Protocol (DHCP) provides a framework for passing configuration information to hosts on a TCPIP network. DHCP is based on the Bootstrap Protocol (BOOTP)A DHCP client makes a request to a DHCP server that may or may not reside on the same subnet. The automatic distribution of IP configuration information to hosts eases the administrative burden of maintaining IP networks. In its simplest form, DHCP distributes the IP address, subnet mask and default gateway to a host, but can include other configuration parameters such as name servers and netbios configuration.
DHCP works on DORA process. Discover, Offer, Request, Acknowledge.
A DHCP client goes through six stages during the DHCP process. These stages are:
  • Initializing
  • Selecting
  • Requesting
  • Binding
  • Renewing
  • Rebinding
The DHCP client starts the DHCP process by issuing a DHCPDISCOVER message to its local subnet on UDP port 67. Since the client does not know what subnet it belongs to, a general broadcast is used (destination address 255.255.255.255). If the DHCP server is located on a different subnet, a DHCP-relay agent must be used. The DHCP-relay agent can take several forms. The ip-helper IOS command is used to set up a DHCP-relay agent on a Cisco router.
The DHCP-relay agent forwards the DHCPDISCOVER message to a subnet that contains a DHCP server. Once the DHCP server receives the DHCPDISCOVER message, it replies with a DHCPOFFER message. The DHCPOFFER message contains the IP configuration information for the client. THE DHCPOFFER message is sent as a broadcast on UDP port 68. The client will know that the DHCPOFFER message is intended for it because the client's MAC address is included in the message.
If the client is on a different subnet than the server, the message is sent unicast to the DHCP-relay agent on UDP port 67. The DHCP-relay agent broadcasts the DHCPOFFER on the client's subnet on UDP port 68.

After the client receives the DHCPOFFER, it sends a DHCPREQUEST message to the server. The DHCPREQUEST message informs the server that it accepts the parameters offered in the DHCPOFFER message. The DHCPREQUEST is a broadcast message, but it includes the MAC address of the server, so that other DHCP servers on the network will know which server is serving the client.

The DHCP server will send a DHCPACK message to the client to acknowledge the DHCPREQUEST. The DHCPACK message contains all the configuration information that was requested by the client. After the client receives the DHCPACK, it binds the IP address and is ready to communicate on the network. If the server is unable to provide the requested configuration, it sends a DHCPNACK message to the client. The client will resend the DHCPREQUEST message. If the DHCPREQUEST message does not return a DHCPACK after four attempts, the client will start the DHCP process from the beginning and send a new DHCPDISCOVER message. There is a great diagram of the DHCP process at the "Understanding DHCP" link at the end of this article.

After the client receives the DHCPACK, it will send out an ARP request for the IP address assigned. If it gets a reply to the ARP request, the IP address is already in use on the network. The client then sends a DHCPDECLINE to the server and sends a new DHCPREQUEST. This step is optional, and is often not performed.
Since the DHCP works on broadcast, two pc which are on different networks (or VLANs) cannot work on the DHCP protocol. Does that mean we should have one dedicated server of DHCP in each vlan? No … in Cisco devices IP helper-address command helps to broadcast DHCP messages from one vlan to other vlan.
For more detailed discussion on the DHCP protocol go through the below link from cisco.com

In Windows 7 How to Install and configure PHP, MYSQL, APACHE Server. 


Now I am going to explain " How to Install and Configure PHP, MYSQL, APACHE server on windows 7 operating system". It is very simple. But for the beginners if not done with care then it would be a night mare for them. I'll clearly depict the scenario. Just follow these steps.

Installing APACHE SERVER: 

To Install this software, first we have to download this we can get this software from below link:
But downloading the required file from the given page is also tedious task.
I'll give you a little clarity here:  Just download it from here.
Don't panic. yes, It's a Zip file doesn't look like normal software installation setup.wait,  I'll Mention what are the things you have to do next. OK, now
  1. Unzip the the downloaded file, it will create a folder with name httpd-2.4.3-win32-ssl_0.9.8.
  2. Copy the contents in the file Apache24.
  3. Now in the C Drive, Create a folder with name webserver.
  4. In that webserver folder create a folder Apache and paste the Contents of Apache24.
  5. In the same webserver folder, create a new folder with name www.
  6. Now the editing tasks starts, Try to figure out the file httpd.conf in C:\webserver\Apache\httpd.conf.
Changes we have to do are
At line 32 Modify the Server root Path (path of Apache Folder). Here in my case it is,
ServerRoot "c:/Apache24" 
as
ServerRoot "c:/webserver/Apache"
At line 237, 238 Modify (Path of www folder)
DocumentRoot "c:/Apache24/htdocs"
<Directory "c:/Apache24/htdocs">
as
DocumentRoot "c:/webserver/www"
<Directory "c:/webserver/www">
At line 258 Modify
AllowOverride None 
as
AllowOverride All
Now save the httpd.conf file. It's the time to test our Apache Installation. 
In the www folder create a sample html page like index.html
let us code it as
<html>
<title>
evignan4u Guide to Apache Installation
</title>
<body>
<br><h1> It's done</h1>
</body>
</html>
copy the above code and save it in index.html
Now open the command prompt and execute the following commands
Note: Run the Command prompt as Administrator else it will displays error message like this.
"AH00369: Failed to open the WinNT service manager, perhaps you forgot to log in as Administrator?" 

Changing the path the Apache bin directory.
cd C:\webserver\Apache\bin 

Install Apache as a service.
httpd.exe -k install

Now run the service by typing following command.
httpd.exe 

It will pop a window and allow access to it.
While running this command you may see one warning message like this:
AH00558: httpd.exe: Could not reliably determine the server's fully qualified do main name, using fe80::d8cd:fa63:d1e9:6083. Set the 'ServerName' directive globa lly to suppress this message
Don't Panic. It's just a error message and it's not going to do any thing.
If you try to change the domain name at line 213 then it will lead to following error
ERROR: 
H00558: httpd: Could not reliably determine the server's fully qualified domai 
d1c:848a:3498:56c4. Set the 'ServerName' directive globally to suppress this me 
(OS 10048)Only one usage of each socket address (protocol/network address/port) 
d. : AH00072: make_sock: could not bind to address [::]:80 
(OS 10048)Only one usage of each socket address (protocol/network address/port) 
d. : AH00072: make_sock: could not bind to address 0.0.0.0:80 
AH00451: no listening sockets available, shutting down 
AH00015: Unable to open logs
So, Don't do it.
Now, We have completed the Installation of Apache.
Now open the browser and type http://localhost
If You can see the Message "It' done" then it's OK and Apache Installed on your computer successfully.
else go through the process one more time carefully or leave a comment below.

What really happens when you navigate to a URL

As a software developer, you certainly have a high-level picture of how web apps work and what kinds of technologies are involved: the browser, HTTP, HTML, web server, request handlers, and so on.
In this article, we will take a deeper look at the sequence of events that take place when you visit a URL.

1. You enter a URL into the browser

It all starts here:
image

2. The browser looks up the IP address for the domain name

 image

The first step in the navigation is to figure out the IP address for the visited domain. The DNS lookup proceeds as follows:

  • Browser cache – The browser caches DNS records for some time. Interestingly, the OS does not tell the browser the time-to-live for each DNS record, and so the browser caches them for a fixed duration (varies between browsers, 2 – 30 minutes).
  • OS cache – If the browser cache does not contain the desired record, the browser makes a system call (gethostbyname in Windows). The OS has its own cache.
  • Router cache – The request continues on to your router, which typically has its own DNS cache.
  • ISP DNS cache – The next place checked is the cache ISP’s DNS server. With a cache, naturally.
  • Recursive search – Your ISP’s DNS server begins a recursive search, from the root nameserver, through the .com top-level nameserver, to Facebook’s nameserver. Normally, the DNS server will have names of the .com nameservers in cache, and so a hit to the root nameserver will not be necessary.
Here is a diagram of what a recursive DNS search looks like:
500px-An_example_of_theoretical_DNS_recursion_svg
One worrying thing about DNS is that the entire domain like wikipedia.org or facebook.com seems to map to a single IP address. Fortunately, there are ways of mitigating the bottleneck:
  • Round-robin DNS is a solution where the DNS lookup returns multiple IP addresses, rather than just one. For example, facebook.com actually maps to four IP addresses.
  • Load-balancer is the piece of hardware that listens on a particular IP address and forwards the requests to other servers. Major sites will typically use expensive high-performance load balancers.
  • Geographic DNS improves scalability by mapping a domain name to different IP addresses, depending on the client’s geographic location. This is great for hosting static content so that different servers don’t have to update shared state.
  • Anycast is a routing technique where a single IP address maps to multiple physical servers. Unfortunately, anycast does not fit well with TCP and is rarely used in that scenario.
Most of the DNS servers themselves use anycast to achieve high availability and low latency of the DNS lookups.

3. The browser sends a HTTP request to the web server

image
You can be pretty sure that Facebook’s homepage will not be served from the browser cache because dynamic pages expire either very quickly or immediately (expiry date set to past).
So, the browser will send this request to the Facebook server:
GET http://facebook.com/ HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, [...]
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; [...]
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: facebook.com
Cookie: datr=1265876274-[...]; locale=en_US; lsd=WW[...]; c_user=2101[...]
The GET request names the URL to fetch: “http://facebook.com/”. The browser identifies itself (User-Agent header), and states what types of responses it will accept (Accept and Accept-Encoding headers). The Connection header asks the server to keep the TCP connection open for further requests.
The request also contains the cookies that the browser has for this domain. As you probably already know, cookies are key-value pairs that track the state of a web site in between different page requests. And so the cookies store the name of the logged-in user, a secret number that was assigned to the user by the server, some of user’s settings, etc. The cookies will be stored in a text file on the client, and sent to the server with every request.
There is a variety of tools that let you view the raw HTTP requests and corresponding responses. My favorite tool for viewing the raw HTTP traffic is fiddler, but there are many other tools (e.g., FireBug) These tools are a great help when optimizing a site.
In addition to GET requests, another type of requests that you may be familiar with is a POST request, typically used to submit forms. A GET request sends its parameters via the URL (e.g.: http://robozzle.com/puzzle.aspx?id=85). A POST request sends its parameters in the request body, just under the headers.
The trailing slash in the URL “http://facebook.com/” is important. In this case, the browser can safely add the slash. For URLs of the form http://example.com/folderOrFile, the browser cannot automatically add a slash, because it is not clear whether folderOrFile is a folder or a file. In such cases, the browser will visit the URL without the slash, and the server will respond with a redirect, resulting in an unnecessary roundtrip.

4. The facebook server responds with a permanent redirect

image
This is the response that the Facebook server sent back to the browser request:
HTTP/1.1 301 Moved Permanently
Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0,
      pre-check=0
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Location: http://www.facebook.com/
P3P: CP="DSP LAW"
Pragma: no-cache
Set-Cookie: made_write_conn=deleted; expires=Thu, 12-Feb-2009 05:09:50 GMT;
      path=/; domain=.facebook.com; httponly
Content-Type: text/html; charset=utf-8
X-Cnection: close
Date: Fri, 12 Feb 2010 05:09:51 GMT
Content-Length: 0
The server responded with a 301 Moved Permanently response to tell the browser to go to “http://www.facebook.com/” instead of “http://facebook.com/”.
There are interesting reasons why the server insists on the redirect instead of immediately responding with the web page that the user wants to see.
One reason has to do with search engine rankings. See, if there are two URLs for the same page, say http://www.igoro.com/ and http://igoro.com/, search engine may consider them to be two different sites, each with fewer incoming links and thus a lower ranking. Search engines understand permanent redirects (301), and will combine the incoming links from both sources into a single ranking.
Also, multiple URLs for the same content are not cache-friendly. When a piece of content has multiple names, it will potentially appear multiple times in caches.

5. The browser follows the redirect

image
The browser now knows that “http://www.facebook.com/” is the correct URL to go to, and so it sends out another GET request:
GET http://www.facebook.com/ HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, [...]
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; [...]
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Cookie: lsd=XW[...]; c_user=21[...]; x-referer=[...]
Host: www.facebook.com
The meaning of the headers is the same as for the first request.

6. The server ‘handles’ the request

image
The server will receive the GET request, process it, and send back a response.
This may seem like a straightforward task, but in fact there is a lot of interesting stuff that happens here – even on a simple site like my blog, let alone on a massively scalable site like facebook.
  • Web server softwareThe web server software (e.g., IIS or Apache) receives the HTTP request and decides which request handler should be executed to handle this request. A request handler is a program (in ASP.NET, PHP, Ruby, …) that reads the request and generates the HTML for the response. In the simplest case, the request handlers can be stored in a file hierarchy whose structure mirrors the URL structure, and so for example http://example.com/folder1/page1.aspx URL will map to file /httpdocs/folder1/page1.aspx. The web server software can also be configured so that URLs are manually mapped to request handlers, and so the public URL of page1.aspx could be http://example.com/folder1/page1.
  • Request handlerThe request handler reads the request, its parameters, and cookies. It will read and possibly update some data stored on the server. Then, the request handler will generate a HTML response.
One interesting difficulty that every dynamic website faces is how to store data. Smaller sites will often have a single SQL database to store their data, but sites that store a large amount of data and/or have many visitors have to find a way to split the database across multiple machines. Solutions include sharding (splitting up a table across multiple databases based on the primary key), replication, and usage of simplified databases with weakened consistency semantics.
One technique to keep data updates cheap is to defer some of the work to a batch job. For example, Facebook has to update the newsfeed in a timely fashion, but the data backing the “People you may know” feature may only need to be updated nightly (my guess, I don’t actually know how they implement this feature). Batch job updates result in staleness of some less important data, but can make data updates much faster and simpler.

7. The server sends back a HTML response

image
Here is the response that the server generated and sent back:
HTTP/1.1 200 OK
Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0,
    pre-check=0
Expires: Sat, 01 Jan 2000 00:00:00 GMT
P3P: CP="DSP LAW"
Pragma: no-cache
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
X-Cnection: close
Transfer-Encoding: chunked
Date: Fri, 12 Feb 2010 09:05:55 GMT

2b3
��������T�n�@����[...]
The entire response is 36 kB, the bulk of them in the byte blob at the end that I trimmed.
The Content-Encoding header tells the browser that the response body is compressed using the gzip algorithm. After decompressing the blob, you’ll see the HTML you’d expect:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" 
      lang="en" id="facebook" class=" no_js">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
...
In addition to compression, headers specify whether and how to cache the page, any cookies to set (none in this response), privacy information, etc.
Notice the header that sets Content-Type to text/html. The header instructs the browser to render the response content as HTML, instead of say downloading it as a file. The browser will use the header to decide how to interpret the response, but will consider other factors as well, such as the extension of the URL.

8. The browser begins rendering the HTML

Even before the browser has received the entire HTML document, it begins rendering the website:
 image

9. The browser sends requests for objects embedded in HTML

image
As the browser renders the HTML, it will notice tags that require fetching of other URLs. The browser will send a GET request to retrieve each of these files.
Here are a few URLs that my visit to facebook.com retrieved:
  • Imageshttp://static.ak.fbcdn.net/rsrc.php/z12E0/hash/8q2anwu7.gif
    http://static.ak.fbcdn.net/rsrc.php/zBS5C/hash/7hwy7at6.gif
  • CSS style sheetshttp://static.ak.fbcdn.net/rsrc.php/z448Z/hash/2plh8s4n.css
    http://static.ak.fbcdn.net/rsrc.php/zANE1/hash/cvtutcee.css
  • JavaScript files
    http://static.ak.fbcdn.net/rsrc.php/zEMOA/hash/c8yzb6ub.js
    http://static.ak.fbcdn.net/rsrc.php/z6R9L/hash/cq2lgbs8.js
Each of these URLs will go through process a similar to what the HTML page went through. So, the browser will look up the domain name in DNS, send a request to the URL, follow redirects, etc.
However, static files – unlike dynamic pages – allow the browser to cache them. Some of the files may be served up from cache, without contacting the server at all. The browser knows how long to cache a particular file because the response that returned the file contained an Expires header. Additionally, each response may also contain an ETag header that works like a version number – if the browser sees an ETag for a version of the file it already has, it can stop the transfer immediately.
Can you guess what “fbcdn.net” in the URLs stands for? A safe bet is that it means “Facebook content delivery network”. Facebook uses a content delivery network (CDN) to distribute static content – images, style sheets, and JavaScript files. So, the files will be copied to many machines across the globe.
Static content often represents the bulk of the bandwidth of a site, and can be easily replicated across a CDN. Often, sites will use a third-party CDN provider, instead of operating a CND themselves. For example, Facebook’s static files are hosted by Akamai, the largest CDN provider.
As a demonstration, when you try to ping static.ak.fbcdn.net, you will get a response from an akamai.net server. Also, interestingly, if you ping the URL a couple of times, may get responses from different servers, which demonstrates the load-balancing that happens behind the scenes.

10. The browser sends further asynchronous (AJAX) requests

image
In the spirit of Web 2.0, the client continues to communicate with the server even after the page is rendered.
For example, Facebook chat will continue to update the list of your logged in friends as they come and go. To update the list of your logged-in friends, the JavaScript executing in your browser has to send an asynchronous request to the server. The asynchronous request is a programmatically constructed GET or POST request that goes to a special URL. In the Facebook example, the client sends a POST request to http://www.facebook.com/ajax/chat/buddy_list.php to fetch the list of your friends who are online.
This pattern is sometimes referred to as “AJAX”, which stands for “Asynchronous JavaScript And XML”, even though there is no particular reason why the server has to format the response as XML. For example, Facebook returns snippets of JavaScript code in response to asynchronous requests.
Among other things, the fiddler tool lets you view the asynchronous requests sent by your browser. In fact, not only you can observe the requests passively, but you can also modify and resend them. The fact that it is this easy to “spoof” AJAX requests causes a lot of grief to developers of online games with scoreboards. (Obviously, please don’t cheat that way.)
Facebook chat provides an example of an interesting problem with AJAX: pushing data from server to client. Since HTTP is a request-response protocol, the chat server cannot push new messages to the client. Instead, the client has to poll the server every few seconds to see if any new messages arrived.
Long polling is an interesting technique to decrease the load on the server in these types of scenarios. If the server does not have any new messages when polled, it simply does not send a response back. And, if a message for this client is received within the timeout period, the server will find the outstanding request and return the message with the response.

Conclusion

Hopefully this gives you a better idea of how the different web pieces work together.

30 Essential SEO Resources for Beginners

For many bloggers, SEO seems like some kind of black magic that only super technical people can ever get the hang of.
I’ll admit, starting out with SEO can be very intimidating.
The massive amounts of information (and misinformation) is enough to send any blogger into a state I like to call “information paralysis”, where they are unable to act.
I’d like to alleviate some of that today.
Below I’ve outlined some of the most essential and trusted SEO resources, from blogs, to tools, to guides, to forums and conferences: it’s all here.
Dive in below, and don’t let the fear of learning something new stop you from utilizing this great inbound marketing aspect.

SEO Blogs

It’s always good practice to keep updated on the world of SEO with the top SEO blogs.
Below I’ve outlined a few of my personal favorites, covering search engine news, tips, and algorithm changes.
  • Search Engine Land – One of the biggest blogs on search engine news and information.
  • SEOmoz – One of my favorite SEO blogs, I’m also a contributor, split between the main blog and the YouMoz blog, filled with good SEO articles.
  • SEO Book – Aaron Wall creates some truly great articles, love him or hate him, you should be reading his blog.
  • Matt Cutts – Head of Google’s webpam team, you should probably listen to him or something.
  • Search Engine Journal – Great blog for link building, PPC, and optimization information.
  • Google’s Blog – Google’s main blog, not much on SEO specifically, but it’s good to know what Big G is up to.

--> SEO Guides Blogs can be an incredible source of information. Unfortunately, for the newbie, they are too split up: info is spread throughout hundreds (or thousands) of posts.
These complete guides offer a more thorough offering on learning SEO.

SEO Tools

SEO can be an immense time sink if you aren’t careful.
Definitely check out a few of the SEO tools below that can save you countless hours and a ton of frustration.
Just be sure not to spend too much time playing around with the tools instead of actually using them!
  • Google Keyword Tool – If there is one “getting started” tool that you must learn about, it’s this one. Know it well, you’ll use it forever (or at least as long as Google is around).
  • SEOmoz’s Tools – A comprehensive suite of tools that I’ve started to fall in love with. It’s paid (and a premium price), but very useful.
  • Google Webmaster Tools – If you are going to play Big G’s game (whitehat style), you need to use their tools.
  • Open Site Explorer – A individual SEOmoz tool that’s perfect for checking competitor’s backlinks, as well as other metrics. More comprehensive if you’re a pro user.
  • Serp IQ – One of my recent favorite tools to emerge on the market, tons of great uses from competition analysis to keyword discovery, this tool is insanely fast and has a ton of great features.
  • Scribe SEO – A super useful tool (especially if you are focusing on WordPress SEO) because it breaks down keyword density to perfection.
  • SpyFu – Great for spying on your competitors, specifically their advertising.
  • Wordtracker Keyword Tool – A classic, must use for keyword research.
  • Market Samurai – Still one of the best keyword analysis tools on the market, perfect for checking SEO competition, although it runs somewhat slowly (Adobe Air).

SEO Forums

Although guides and blogs can offer a ton of essential information, sometimes you are going to have questions.
Forums are a great spot to get these questions answered.

Conferences

A bit advanced for most, but a great way to really dive into SEO is to know about (and go to) the many search engine related conferences.
  • SES Conference – SES is the leading global event series about search and social marketing, with a focus on tactics and best practices.
  • Blueglass – BlueGlass’ Internet Marketing Conferences have quickly become known in the web marketing circles as being premier events for learning the latest strategies, interacting with the speakers and networking
  • Search Marketing Expo – Another leading search engine conference.

In Windows 7 Working Java Program/Project/source code for Snakes and Ladders Game with Rules


Are You Looking Source Code or a Working Java Program for Snakes and Ladders Game.
Then Here is the Java Project for  Snakes and Ladders Game.

Scroll down for code/Java Program. Copy the Code and Paste it and Just simply run the code in java Platform.

       Rules for Snakes and Ladders Game

Snakes and Ladders is a board game usually played by children. The Hasbro version, called Chutes and Ladders, shows children making various decisions, then the consequences of that action. Good actions allow the player to climb a ladder and get closer to the finish, while bad decisions make the player slide down a chute, which takes him farther from the finish

Players
  • Snakes and Ladders is played by two to four players, each with her own token to move around the board.

Moving

  • Players roll a die or spin a spinner, then move the designated number of spaces, between one and six. Once they land on a space, they have to perform any action designated by the space.
  • If the space a player lands on is at the bottom of a ladder, he should climb the ladder, which brings him to a space higher on the board.

Snakes/Chutes

  • If the space a player lands on is at the top of a snake/chute, she must slide down to the bottom of it, landing on a space closer to the beginning.

Winning

  • The winner is the player who gets to the last space on the board first, whether by landing on it from a roll, or by reaching it with a ladder.
    Working Java Program/Project/source code for Snakes and Ladders Game.

#include
#include
#include
#include
#include
#include
#include
int k=1, i, user=0, dice=0, x1=50, y1=410, x2=70, y2=410, dir1=0,
dir2=0,
ch;
int cnt1=1, cnt2=1;
void *obj1, *obj2, *o1, *o2, *dot, *back, *turn, *ready;
unsigned int size;
void ladder1()
{
            int m,n;
            for(m=0;m<=250;m+=250)
            for(n=0;n<=m;n+=250)
            {
                        setcolor(DARKGRAY);
                        line(53+m,57+n,55+m,55+n);
                        line(53+m,57+n,133+m,137+n);
                        line(55+m,55+n,135+m,135+n);
                        line(133+m,137+n,135+m,135+n);
                        setfillstyle(SOLID_FILL, YELLOW);
                        floodfill(55+m,58+n,DARKGRAY);
                        line(68+m,42+n,70+m,40+n);
                        line(68+m,42+n,148+m,122+n);
                        line(70+m,40+n,150+m,120+n);
                        line(148+m,122+n,150+m,120+n);
                        floodfill(70+m,43+n,DARKGRAY);
                        line(65+m,65+n,78+m,52+n);
                        line(68+m,68+n,81+m,55+n);
                        floodfill(79+m,54+n,DARKGRAY);
                        line(75+m,75+n,88+m,62+n);
                        line(78+m,78+n,91+m,65+n);
                        floodfill(89+m,64+n,DARKGRAY);
                        line(85+m,85+n,98+m,72+n);
                        line(88+m,88+n,101+m,75+n);
                        floodfill(99+m,74+n,DARKGRAY);
                        line(95+m,95+n,108+m,82+n);
                        line(98+m,98+n,111+m,85+n);
                        floodfill(109+m,84+n,DARKGRAY);
                        line(105+m,105+n,118+m,92+n);
                        line(108+m,108+n,121+m,95+n);
                        floodfill(119+m,94+n,DARKGRAY);
                        line(115+m,115+n,128+m,102+n);
                        line(118+m,118+n,131+m,105+n);
                        floodfill(129+m,104+n,DARKGRAY);
                        line(125+m,125+n,138+m,112+n);
                        line(128+m,128+n,141+m,115+n);
                        floodfill(139+m,114+n,DARKGRAY);
            }
}
void ladder2()
{
            int p,q=0;
            for(p=0;p<=180;p+=155)
            {
                        line(100+p,330-q,140+p,290-q);
                        line(100+p,330-q,102+p,332-q);
                        line(102+p,332-q,142+p,292-q);
                        line(142+p,292-q,140+p,290-q);
                        floodfill(141+p,292-q,8);
                        line(115+p,345-q,155+p,305-q);
                        line(115+p,345-q,117+p,347-q);
                        line(117+p,347-q,157+p,307-q);
                        line(157+p,307-q,155+p,305-q);
                        floodfill(155+p,307-q,8);
                        line(112+p,322-q,125+p,335-q);
                        line(114+p,320-q,127+p,333-q);
                        floodfill(125+p,334-q,8);
                        line(122+p,312-q,135+p,325-q);
                        line(124+p,310-q,137+p,323-q);
                        floodfill(135+p,324-q,8);
                        line(132+p,302-q,145+p,315-q);
                        line(134+p,300-q,147+p,313-q);
                        floodfill(145+p,314-q,8);
                        q+=95;
            }
}
void snake1()
{
            int x,y=0,h=2;
            for(x=0;x<=200;x+=125)
            {
                        arc(120+x,110+y,10,85,60);
                        arc(258+x,85+y,190,240,80);
                        arc(112+x,118+y,10,80,60);
                        arc(250+x,93+y,190,290,80);
                        arc(275+x,100+y,250,270,70);
                        line(250+x,170+y,250+x,165+y);
                        line(250+x,165+y,230+x,160+y);
                        line(230+x,160+y,218+x,155+y);
                        line(130+x,50+y,115+x,47+y);
                        line(121+x,59+y,106+x,52+y);
                        line(106+x,52+y,114+x,48+y);
                        circle(114+x,52+y,1);
                        setfillstyle(1,h);
                        floodfill(116+x,52+y,8);
                        y+=230;          h+=8;
            }
}
void snake2()
{
            arc(130,220,150,180,40);
            arc(130,220,180,253,40);
            arc(105,328,273,80,70);
            arc(143,220,150,180,40);
            arc(143,215,180,230,40);
            arc(112,328,265,50,75);
            arc(80,354,45,72,115);
            line(102,400,104,402);
            line(102,400,107,399);
            line(95,200,110,185);
            line(110,185,109,200);
            line(110,185,111,182);
            circle(104,198,1);
            setfillstyle(1,12);
            floodfill(103,199,8);
}
void snake3()
{
            arc(255,118,320,0,170);
            arc(265,118,305,0,170);
            line(384,229,361,260);
            line(425,120,429,105);
            line(428,105,435,120);
            line(428,105,429,100);
            circle(430,115,1);
            setfillstyle(1,6);
            floodfill(430,117,8);
}
void numbering()
{
            outtextxy(50,393,"1    2         4    5    6    7    8         10");
            outtextxy(50,353,"20   19   18        16   15   1    13        11");
            outtextxy(50,313,"21   22             25   26             29   30");
            outtextxy(50,273,"40   39   38   37   36   35   34   33   32   31");
            outtextxy(50,233,"41        43   44   45        47   48        50");
            outtextxy(50,193,"60        58   57   56   55        53   52     ");
            outtextxy(50,153,"61   62   63   64        66   67   68   69     ");
            outtextxy(50,113,"80   79             76   75   74   73        71");
            outtextxy(50,73, "81        83   84   85   86   87        89   90");
            outtextxy(50,33, "100  99   98   97   96   95   94   93   92   91");
            setcolor(15);
//          outtextxy(480,40,"Lakshmi Narayana's");
            outtextxy(465,50,"The Classic Game of");
            settextstyle(GOTHIC_FONT, HORIZ_DIR, 2);
            setcolor(LIGHTRED);
            outtextxy(470,60,"Snake & Ladder");
            setcolor(WHITE);
            settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
}
void status()
{
            setcolor(YELLOW);
            outtextxy(480, 95, "Dice output...");
            setlinestyle(SOLID_LINE, 1, 3);
            rectangle(480, 110, 600, 230);
            outtextxy(480, 260, "Turn...");
            rectangle(480, 275, 600, 300);
}
void welcome()
{
            float octave[]={130.81, 146.83, 164.81, 174.61, 196, 220, 246.94}, n;
            for (int i=0; i<50; i++)
            { n=random(6); sound(octave[n]*4); delay(150); nosound(); }
}
void dispdice()
{
            switch (dice)
            {
                        case 1: putimage(535, 165, dot, COPY_PUT);          break;
                        case 2: putimage(515, 145, dot, COPY_PUT);
                                    putimage(555, 185, dot, COPY_PUT);          break;
                        case 3: putimage(515, 145, dot, COPY_PUT);
                                    putimage(535, 165, dot, COPY_PUT);
                                    putimage(555, 185, dot, COPY_PUT);          break;
                        case 4: putimage(515, 145, dot, COPY_PUT);
                                    putimage(555, 145, dot, COPY_PUT);
                                    putimage(515, 185, dot, COPY_PUT);
                                    putimage(555, 185, dot, COPY_PUT);          break;
                        case 5: putimage(515, 145, dot, COPY_PUT);
                                    putimage(555, 145, dot, COPY_PUT);
                                    putimage(535, 165, dot, COPY_PUT);
                                    putimage(515, 185, dot, COPY_PUT);
                                    putimage(555, 185, dot, COPY_PUT);          break;
                        case 6: putimage(515, 145, dot, COPY_PUT);
                                    putimage(515, 165, dot, COPY_PUT);
                                    putimage(515, 185, dot, COPY_PUT);
                                    putimage(555, 145, dot, COPY_PUT);
                                    putimage(555, 165, dot, COPY_PUT);
                                    putimage(555, 185, dot, COPY_PUT);          break;
            }
}
void getdice()
{          dice=random(6);         dice++; dispdice();     }
void play()
{
            getimage(50, 410, 60, 420, o1);
            getimage(70, 410, 80, 420, o2);
            putimage(50, 410, obj1, COPY_PUT);
            putimage(70, 410, obj2, COPY_PUT);
            while (1)
            {
                        if (user==0)
                        {
                                    putimage(487, 282, turn, COPY_PUT);
                                    setcolor(GREEN);
                                    outtextxy(480, 285, "    Player 1");
again:                          ch=getch();
                                    if (ch==13) getdice();
                                    else if (ch==27)          exit(1);
                                    else goto again;
                                    setcolor(YELLOW);
                                    if (cnt1==96 && dice>=4)     { delay(1000); goto invalid1; user=1; }
                                    else if (cnt1==97 && dice>=3)         { delay(1000); goto invalid1; user=1;
}
                                    else if (cnt1==98 && dice>=2)         { delay(1000); goto invalid1; user=1;
}
                                    else if (cnt1==99 && dice>=1)         { delay(1000); goto invalid1; user=1;
}
                                    for (i=1; i<=dice; i++, cnt1++)
                                    {
                                                putimage(x1, y1, o1, COPY_PUT);
                                                if (dir1==0)
                                                {
//                                                          size=imagesize(x1, y1, x1+10, y1+10);
//                                                          o1=malloc(size);
                                                            getimage(x1, y1, x1+10, y1+10, o1);
                                                            x1+=40;
                                                            if (x1>410)     x1-=40, y1-=40, dir1=1;
//                                                          size=imagesize(x1, y1, x1+10, y1+10);
//                                                          o1=malloc(size);
                                                            getimage(x1, y1, x1+10, y1+10, o1);
                                                            putimage(x1, y1, obj1, COPY_PUT);
                                                            delay(1000);   goto ch1;
                                                }
                                                else
                                                {
                                                            size=imagesize(x1, y1, x1+10, y1+10);
//                                                          o1=malloc(size);
                                                            getimage(x1, y1, x1+10, y1+10, o1);
                                                            x1-=40;
                                                            if (x1<50)       x1+=40, y1-=40, dir1=0;
//                                                          size=imagesize(x1, y1, x1+10, y1+10);
//                                                          o1=malloc(size);
                                                            getimage(x1, y1, x1+10, y1+10, o1);
                                                            putimage(x1, y1, obj1, COPY_PUT);
                                                            delay(1000);   goto ch1;
                                                }
ch1:                                         if (cnt1==99)  goto over;
                                    }
                                    if (cnt1==12 || cnt1==72 || cnt1==78)
                                    {
                                                putimage(x1, y1, o1, COPY_PUT);
                                                x1-=80;           y1-=80;
//                                              size=imagesize(x1, y1, x1+10, y1+10);
//                                              o1=malloc(size);
                                                getimage(x1, y1, x1+10, y1+10, o1);
                                                putimage(x1, y1, obj1, COPY_PUT);
                                                if (cnt1==12)              cnt1=34;
                                                else if (cnt1==72)      cnt1=94;
                                                else if (cnt1==78)       { cnt1=100; goto over; }
                                    }
                                    else if (cnt1==22 || cnt1==46)
                                    {
                                                putimage(x1, y1, o1, COPY_PUT);
                                                x1+=40;          y1-=40;
//                                              size=imagesize(x1, y1, x1+10, y1+10);
//                                              o1=malloc(size);
                                                getimage(x1, y1, x1+10, y1+10, o1);
                                                putimage(x1, y1, obj1, COPY_PUT);
                                                if (cnt1==22)              cnt1=38;
                                                else if (cnt1==46)       cnt1=54;
                                                dir1=1;
                                    }
                                    else if (cnt1==36 || cnt1==99)
                                    {
                                                putimage(x1, y1, o1, COPY_PUT);
                                                x1+=160;        y1+=120;
//                                              size=imagesize(x1, y1, x1+10, y1+10);
//                                              o1=malloc(size);
                                                getimage(x1, y1, x1+10, y1+10, o1);
                                                putimage(x1, y1, obj1, COPY_PUT);
                                                if (cnt1==36)              cnt1=9;
                                                else if (cnt1==99)       cnt1=66;
                                                dir1=0;
                                    }
                                    else if (cnt1==62)
                                    {
                                                putimage(x1, y1, o1, COPY_PUT);
                                                y1+=240;
//                                              size=imagesize(x1, y1, x1+10, y1+10);
//                                              o1=malloc(size);
                                                getimage(x1, y1, x1+10, y1+10, o1);
                                                putimage(x1, y1, obj1, COPY_PUT);
                                                cnt1=2;
                                    }
                                    else if (cnt1==90)
                                    {
                                                putimage(x1, y1, o1, COPY_PUT);
                                                x1-=80;           y1+=160;
//                                              size=imagesize(x1, y1, x1+10, y1+10);
//                                              o1=malloc(size);
                                                getimage(x1, y1, x1+10, y1+10, o1);
                                                putimage(x1, y1, obj1, COPY_PUT);
                                                cnt1=48;
                                    }
                                    if (dice==5 || dice==6) user=0; else user=1;
invalid1:                     putimage(500, 130, back, COPY_PUT);
                        }
                        else
                        {
                                    putimage(487, 282, turn, COPY_PUT);
                                    setcolor(BROWN);
                                    outtextxy(480, 285, "    Player 2");
                                    setcolor(YELLOW);
again2:                                    ch=getch();
                                    if (ch==13) getdice();
                                    else if (ch==27)          exit(1);
                                    else goto again2;
                                    if (cnt2==96 && dice!=4)     { delay(1000); goto invalid2; user=0; }
                                    else if (cnt2==97 && dice!=3)          { delay(1000); goto invalid2; user=0;
}
                                    else if (cnt2==98 && dice!=2)          { delay(1000); goto invalid2; user=0;
}
                                    else if (cnt2==99 && dice!=1)          { delay(1000); goto invalid2; user=0;
}
                                    for (i=1; i<=dice; i++, cnt2++)
                                    {
                                                putimage(x2, y2, o2, COPY_PUT);
                                                if (dir2==0)
                                                {
//                                                          size=imagesize(x2, y2, x2+10, y2+10);
//                                                          o2=malloc(size);
                                                            getimage(x2, y2, x2+10, y2+10, o2);
                                                            x2+=40;
                                                            if (x2>440)     x2-=40, y2-=40, dir2=1;
//                                                          size=imagesize(x2, y2, x2+10, y2+10);
//                                                          o2=malloc(size);
                                                            getimage(x2, y2, x2+10, y2+10, o2);
                                                            putimage(x2, y2, obj2, COPY_PUT);
                                                            delay(1000);   goto ch2;
                                                }
                                                else
                                                {
//                                                          size=imagesize(x2, y2, x2+10, y2+10);
//                                                          o2=malloc(size);
                                                            getimage(x2, y2, x2+10, y2+10, o2);
                                                            x2-=40;
                                                            if (x2<50)       x2+=40, y2-=40, dir2=0;
//                                                          size=imagesize(x2, y2, x2+10, y2+10);
//                                                          o2=malloc(size);
                                                            getimage(x2, y2, x2+10, y2+10, o2);
                                                            putimage(x2, y2, obj2, COPY_PUT);
                                                            delay(1000);   goto ch2;
                                                }
ch2:                                         if (cnt2==99)  goto over;
                                    }
                                    if (cnt2==12 || cnt2==72 || cnt2==78)
                                    {
                                                putimage(x2, y2, o2, COPY_PUT);
                                                x2-=80;           y2-=80;
//                                              size=imagesize(x2, y2, x2+10, y2+10);
//                                              o2=malloc(size);
                                                getimage(x2, y2, x2+10, y2+10, o2);
                                                putimage(x2, y2, obj2, COPY_PUT);
                                                if (cnt2==12)              cnt2=34;
                                                else if (cnt2==72)      cnt2=94;
                                                else if (cnt2==78)       { cnt2=100; goto over; }
                                    }
                                    else if (cnt2==22 || cnt2==46)
                                    {
                                                putimage(x2, y2, o2, COPY_PUT);
                                                x2+=40;          y2-=40;
//                                              size=imagesize(x2, y2, x2+10, y2+10);
//                                              o2=malloc(size);
                                                getimage(x2, y2, x2+10, y2+10, o2);
                                                putimage(x2, y2, obj2, COPY_PUT);
                                                if (cnt2==22)              cnt2=38;
                                                else if (cnt2==46)       cnt2=54;
                                                dir2=1;
                                    }
                                    else if (cnt2==36 || cnt2==99)
                                    {
                                                putimage(x2, y2, o2, COPY_PUT);
                                                x2+=160;        y2+=120;
 //                                             size=imagesize(x2, y2, x2+10, y2+10);
 //                                             o2=malloc(size);
                                                getimage(x2, y2, x2+10, y2+10, o2);
                                                putimage(x2, y2, obj2, COPY_PUT);
                                                if (cnt2==36)              cnt2=9;
                                                else if (cnt2==99)       cnt2=66;
                                                dir2=0;
                                    }
                                    else if (cnt2==62)
                                    {
                                                putimage(x2, y2, o2, COPY_PUT);
                                                y2+=240;
//                                              size=imagesize(x2, y2, x2+10, y2+10);
//                                              o2=malloc(size);
                                                getimage(x2, y2, x2+10, y2+10, o2);
                                                putimage(x2, y2, obj2, COPY_PUT);
                                                cnt2=2;
                                    }
                                    else if (cnt2==90)
                                    {
                                                putimage(x2, y2, o2, COPY_PUT);
                                                x2-=80;           y2+=160;
//                                              size=imagesize(x2, y2, x2+10, y2+10);
//                                              o2=malloc(size);
                                                getimage(x2, y2, x2+10, y2+10, o2);
                                                putimage(x2, y2, obj2, COPY_PUT);
                                                cnt2=48;
                                    }
                                    if (dice==5 || dice==6) user=1; else user=0;
invalid2:                     putimage(500, 130, back, COPY_PUT);
                                    delay(1000);
                        }
            }
over: }
void main()
{
            int gd=DETECT, gm;
            initgraph(&gd, &gm, "d:\tc\bgi");
            randomize();
            size=imagesize(487, 282, 593, 293);
            turn=malloc(size);
            getimage(487, 282, 593, 293, turn);
            rectangle(100, 100, 110, 110);
            setfillstyle(1, 2);
            floodfill(102, 102, 15);
            size=imagesize(100, 100, 110, 110);
            obj1=malloc(size);
            getimage(100, 100, 110, 110, obj1);
            cleardevice();
            rectangle(100, 100, 110, 110);
            setfillstyle(1, 6);
            floodfill(102, 102, 15);
            size=imagesize(100, 100, 110, 110);
            obj2=malloc(size);
            getimage(100, 100, 110, 110, obj2);
            cleardevice();
            o1=malloc(size);        o2=malloc(size);
            setcolor(WHITE);
            setfillstyle(SOLID_FILL, 15);
            rectangle(500, 130, 580, 210);
            floodfill(510, 140, 15);
            size=imagesize(500, 130, 580, 210);
            back=malloc(size);
            getimage(500, 130, 580, 210, back);
            setcolor(0);
            setfillstyle(1, 0);
            rectangle(535, 165, 545, 175);
            floodfill(540, 170, 0);
            size=imagesize(535, 165, 545, 175);
            dot=malloc(size);
            getimage(535, 165, 545, 175, dot);
            cleardevice();
            setcolor(WHITE);
            setfillstyle(SOLID_FILL, 15);
            rectangle(500, 130, 580, 210);
            floodfill(510, 140, 15);
            for(int i=0;i<=360;i+=40)
                        for(int j=0;j<=360;j+=40)
                        {
                                    setfillstyle(SOLID_FILL, k);
                                    bar(50+j,30+i,90+j,70+i);
                                    k+=2;
                        }
            ladder1();        ladder2();
            snake1();       snake2();           snake3();
            numbering();  status();
            welcome();     play();
            if (cnt1>=99)              outtextxy(480, 330, "PLAYER 1 WINS!");
            else if (cnt2>=99)     outtextxy(480, 330, "PLAYER 2 WINS!");
            getch();
}