April 3rd, 2008

permalink Euclidean Distance Calculator

The following snippet returns the euclidean distance between two places on the globe using the Yahoo Maps API. Replace API_KEY with your Yahoo Maps API key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# Distance Module
# by Pravin Paratey (pravinp at gmail dot com)
# Code is licenced under Creative Commons Attribution-Noncommercial-Share Alike 2.5 India
# http://creativecommons.org/licenses/by-nc-sa/2.5/in/
 
import urllib2, cgi, re
from math import sqrt
 
class Distance:
    """
    Using yahoo maps api (http://developer.yahoo.com/maps/rest/V1/geocode.html),
    this class is responsible for returning the euclidean distance between
    two places
    """
    def getDistance(self, start, end):
        """ Gets the euclidean distance between start and end """
        (start_x, start_y) = self.getCoords(start)
        (end_x, end_y) = self.getCoords(end)
        # 1 degree = 111.12 kms or 69.047 miles
        return sqrt((start_x - end_x) ** 2 + (start_y - end_y) ** 2) * 111.12
 
 
    def getCoords(self, location):
        """ Gets the co-ordinates for the given location """
        url = 'http://local.yahooapis.com/MapsService/V1/geocode?appid=' +
                API_KEY + '&street=' + urllib2.quote(location)
        response = urllib2.urlopen(url)
        (x, y) = self._parseXML(response.read())
        return float(x), float(y)
 
 
    def _parseXML(self, xml):
        """ Parses XML and returns latitude and longitude """
        m = re.findall('<latitude>(\d+.\d+)</latitude><longitude>(\d+.\d+)</longitude>', xml)
        # In case of multiple matches, return 1st match
        return m[0]
 
if __name__ == '__main__':
    d = Distance()
    print 'Distance in kms: '
    print d.getDistance("Hiranandani, Powai, Mumbai", "Dadar Station, Mumbai")
March 20th, 2007

permalink Twitter Timeline Javascript

This snippet draws a bar graph that tells you your twitter posting frequency per hour in the last 24 hours.

Screenshot

Twitter timeline javascript 0.2

Instructions

  1. Download Twitter Timeline Javascript v0.2 (Requires PlotKit and MochiKit).
  2. Edit TwitterTimeline.js and change the userid variable to your user-id. You'll find your user-id at http://twitter.com/account/badge.
  3. You will need to include the javascript files:
    <script type="text/javascript" src="/js/MochiKit/MochiKit.js"></script>
    <script type="text/javascript" src="/js/PlotKit/Base.js"></script>
    <script type="text/javascript" src="/js/PlotKit/Layout.js"></script>
    <script type="text/javascript" src="/js/PlotKit/Canvas.js"></script>
    <script type="text/javascript" src="/js/PlotKit/SweetCanvas.js"></script>
    <script type="text/javascript" src="/js/TwitterTimeline.js"></script>
  4. Next, add the following lines where you want the Timeline to appear:
    <script type="text/javascript" src="/js/TwitterTimeline.js"></script>
    <div><canvas id="graph" height="200" width="400"></canvas></div>
    

For your reference, TwitterTimeline.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<script type="text/javascript">
<!--
/*
 * Twitter Timeline Javascript v0.2 (March 20, 2007)
 * Pravin Paratey (http://www.dustyant.com)
 * Andrei Virlan  (http://its.squeak.in)
 *
 * Released under Creative Commons Attribution 2.5 Licence
 * http://creativecommons.org/licenses/by/2.5/
 *
 * Changelog:
 * 0.2 (Mar 20, 2007) - Moved to PlotKit to draw graphs
 * 0.1 (Feb 23, 2007) - Initial Release
 */
 
var userid = '754023'; // Change this value to your user-id
 
var timelineArray = new Array();
function drawGraph() {
    var layout = new PlotKit.Layout("bar", {});
    layout.addDataset("sqrt", timelineArray);
    layout.evaluate();
    var canvas = MochiKit.DOM.getElement("graph");
    var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
    plotter.render();
}
 
function twitterCallback(obj) {
	// Create an array to hold the 24 hours of the day
	var hourArray = new Array(24);
 
	// Initialize array
	for(var i=0; i<24; i++) {
		hourArray[i] = 0;
	}
 
	for (var i=0; i<obj.length; i++) {
		// Get date
		var created_at = new Date(obj[i].created_at);
		if(obj[i].user.id == userid) {
			// Increment the hour
			hourArray[created_at.getHours()]++;
		}
	}
 
	// Construct the timeline
	for(var i=0; i<24; i++) {
		timelineArray[i] = new Array(i, hourArray[i]);
	}
	MochiKit.DOM.addLoadEvent(drawGraph);
}
// Makes the twitter call
document.write('<scr'+'ipt type="text/javascript"' +
	'src="http://twitter.com/statuses/friends_timeline/' +
	userid + '.json?callback=twitterCallback"></scr'+'ipt>');
-->
</script>
December 28th, 2006

permalink Limerick Generator - Part I

Synopsis

The following code demonstrates an alien limerick generator. Alien because it does not generate meaningful words. In part II, we'll see how we can add meaningful words.

Code

You can view the code and see it in action here.

Sample Output

orraxa iz hoxori upifoc awhapo ed
ceva ovat ujbazo arli du caded
uc iqek suenvo
zaek ammiip edbeuvo
moix beak kuefyo irogno iluyox xoiqged
July 31st, 2006

permalink FindWindow Illustration

Synopsis

The following code illustrates the use of FindWindow API. This program blanks out the ad in Yahoo Messenger buddy window. It has been tested on Yahoo Messenger 7.5 and 8.0.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// This code illustrates hiding the Ad in the Buddy window of Yahoo Messenger
//
// Copyright (c) 2006, Pravin Paratey
// pravinp[at]gmail[dot]com (http://dustyant.com)
 
#include <windows.h>
 
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpszArgs, int nCmdWnd)
{
	HWND hYahooWnd = FindWindowEx(NULL, NULL, "YahooBuddyMain", NULL);
 
	if(hYahooWnd) // Yahoo Messenger is running
	{
		HWND hAtlWnd = FindWindowEx(hYahooWnd, NULL, "ATL:00821BC0", NULL);
 
		if(hAtlWnd) // Found the window
		{
			HWND hShellWnd = FindWindowEx(hAtlWnd, NULL, "Shell Embedding", NULL);
			if(hShellWnd)
			{
				HWND hDocObjWnd = FindWindowEx(hShellWnd, NULL, "Shell DocObject View", NULL);
				if(hDocObjWnd) // This is the window we must hide
				{
					ShowWindow(hDocObjWnd, SW_HIDE);
				}
			}
		}
	}
	else
	{
		MessageBox(NULL, "No instances of Yahoo Messenger found", "Error", MB_OK);
	}
	return 0;
}

Download

You can download the source code or the binary.

Usage

Pretty straight forward. When yahoo messenger is running, double click the exe. The ads window will disappear :)

April 7th, 2006

permalink Implementing Emoticons in C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public string Emotify(string inputText)
{
	#region Create Emote hashtable
	Hashtable htEmotes = new System.Collections.Hashtable(100);
	htEmotes.Add(":))", "21");
	htEmotes.Add(":)>-", "67");
	htEmotes.Add(":)", "1");
	htEmotes.Add(":-)", "1");
	htEmotes.Add(":((", "20");
	htEmotes.Add(":(", "2");
	// Add other Yahoo emotes
	#endregion
 
 
	StringBuilder sb = new StringBuilder(inputText.Length);
 
	for (int i = 0; i < inputText.Length; i++)
	{
		string strEmote = string.Empty;
		foreach (string emote in htEmotes.Keys)
		{
			if (inputText.Length - i >= emote.Length &&
				emote.Equals(inputText.Substring(i, emote.Length),
				StringComparison.InvariantCultureIgnoreCase))
			{
				strEmote = emote;
				break;
			}
		}
 
		if (strEmote.Length != 0)
		{
			sb.AppendFormat("<img src='images/{0}.gif' alt='{1}' />", htEmotes[strEmote], strEmote);
			i += strEmote.Length - 1;
		}
		else
		{
			sb.Append(inputText[i]);
		}
	}
	return sb.ToString();
}
June 26th, 2004

permalink Auto-tagging of TagBoards

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/perl
# AutoTagger-Perl v0.1a (June 26, 2004)
# Pravin [pravinp at gmail dot com]
# This snippet illustrates automatic tagging of
# http://www.tag-board.com tag boards
 
use HTTP::Request::Common;
use LWP::UserAgent;
 
# Enter your details here
my $myname = 'pravin';
my $myurl = 'http://dustyant.com';
my $mymessage = 'Test Message';
 
# Add taggy names to this list
my @taggylist = (
	'oh_its_dee',
	'me_reiya',
	'Vighy',
	'rrighton');
 
# Code Starts
$ua = LWP::UserAgent->new();
foreach (@taggylist)
{
	print '$_\\t';
	$ua->request(POST 'http://www.tag-board.com/add.tag',
		[name => $_,
		tagname => $myname,
		tagurl =>$myurl,
		message => $mymessage]);
	print 'done\\n';
}
July 1st, 2003

permalink Tilings Project

This code generated certain tile patterns. It was a lab assignment for the graphics course.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// tiling.cpp - Project titled Tilings
// Pravin Paratey (July 01, 2003)
 
#include &lt;math.h&gt;
#include &lt;GL/gl.h&gt;
#include &lt;stdio.h&gt;
#include &lt;GL/glut.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
 
#define CELL_WIDTH 20
#define CELL_HEIGHT 20
 
int choice=4;
const float PI=3.14;
 
void pattern1()
{
	float x,y,t;
 
	// __
	//  |
	glBegin(GL_LINE_STRIP);
	for(t=0;t&lt;=3.15;t+=0.05)
	{
		x = 2.0*cos(t);
		y = 2.0*sin(t);
		glVertex2f(x-1.0,y-1.0);
	}
	glEnd();
	// |_
	glBegin(GL_LINE_STRIP);
	for(t=0;t&lt;=3.15;t+=0.05)
	{
		x = 2.0*cos(t);
		y = 2.0*sin(t);
		glVertex2f(1.0-x,1.0-y);
	}
	glEnd();
 
	glBegin(GL_LINE_STRIP);
	for(t=PI/2;t&lt;=PI;t+=0.01)
	{
		x = 2.0*cos(t);
		y = 2.0*sin(t);
		glVertex2f(x+1.0,y-1.0);
	}
	glEnd();
 
	glBegin(GL_LINE_STRIP);
	for(t=3*PI/2;t&lt;=2*PI;t+=0.01)
	{
		x = 2.0*cos(t);
		y = 2.0*sin(t);
		glVertex2f(x-1.0,y+1.0);
	}
	glEnd();
}
 
void pattern2(int i, int j)
{
	// This calculates which tile to put
	if(i % 2 != 0)
		j++;
	if (j % 2 == 0)
	{
		glBegin(GL_LINES);
		glVertex2f(-1.0,1.0);
		glVertex2f(0.0,0.5);
		glVertex2f(0.0,0.5);
		glVertex2f(1.0,1.0);
		glVertex2f(0.0,0.5);
		glVertex2f(0.0,-0.5);
		glVertex2f(0.0,-0.5);
		glVertex2f(-1.0,-1.0);
		glVertex2f(0.0,-0.5);
		glVertex2f(1.0,-1.0);
		glEnd();
	}
	else
	{
		glBegin(GL_LINES);
		glVertex2f(-1.0,1.0);
		glVertex2f(-0.5,0.0);
		glVertex2f(-0.5,0.0);
		glVertex2f(-1.0,-1.0);
		glVertex2f(-0.5,0.0);
		glVertex2f(0.5,0.0);
		glVertex2f(0.5,0.0);
		glVertex2f(1.0,1.0);
		glVertex2f(0.5,0.0);
		glVertex2f(1.0,-1.0);
		glEnd();
	}
}
 
void custom()
{
	int r;
	float x,y,t;
 
	r = 1+(int)(6.0*rand()/(RAND_MAX+1.0));
 
	//r=4;
/*
	if (r == 1)
	{
		// T1
		glBegin(GL_LINES);
		glVertex2f(-1.0,1.0);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-1.0,-1.0);
		glVertex2f(-0.5,-0.5);
		glVertex2f(1.0,-1.0);
		glVertex2f(-1.0,1.0);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(1.0,1.0);
		glVertex2f(0.5,0.5);
		glVertex2f(1.0,-1.0);
		glEnd();
	}
	else if (r == 2)
	{
		//T2
		glBegin(GL_LINES);
		glVertex2f(-1.0,1.0);
		glVertex2f(-0.5,0.5);
		glVertex2f(-0.5,0.5);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-1.0,-1.0);
		glVertex2f(-0.5,-0.5);
		glVertex2f(0.5,-0.5);
		glVertex2f(0.5,-0.5);
		glVertex2f(1.0,-1.0);
		glVertex2f(0.5,-0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(1.0,1.0);
		glVertex2f(0.5,0.5);
		glVertex2f(-0.5,0.5);
		glEnd();
	}
	else if (r == 3)
	{
		//T3
		glBegin(GL_LINES);
		glVertex2f(-1.0,1.0);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,-0.5);
		glVertex2f(0.5,-0.5);
		glVertex2f(-1.0,-1.0);
		glVertex2f(1.0,1.0);
		glVertex2f(-0.5,0.5);
		glVertex2f(-0.5,0.5);
		glVertex2f(-0.5,-0.5);
		glVertex2f(-0.5,-0.5);
		glVertex2f(1.0,-1.0);
		glEnd();
	}
	else if (r == 4)
	{
		//T4
		glBegin(GL_LINES);
		glVertex2f(-1.0,1.0);
		glVertex2f(-0.5,-0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(1.0,-1.0);
		glVertex2f(-1.0,-1.0);
		glVertex2f(1.0,1.0);
		glEnd();
	}
*/
	if(r == 1)
	{
		glBegin(GL_LINE_STRIP);
		glVertex2f(-1.0,0.0);
		glVertex2f(-0.5,0.0);
		glVertex2f(-0.5,0.5);
		glVertex2f(0.0,0.5);
		glVertex2f(0.0,1.0);
		glEnd();
		glBegin(GL_LINE_STRIP);
		glVertex2f(0.0,-1.0);
		glVertex2f(0.0,-0.5);
		glVertex2f(0.5,-0.5);
		glVertex2f(0.5,0.0);
		glVertex2f(1.0,0.0);
		glEnd();
	}
	else if(r == 2)
	{
		glBegin(GL_LINES);
		glVertex2f(-1.0,0.0);
		glVertex2f(0.0,1.0);
		glEnd();
		glBegin(GL_LINES);
		glVertex2f(0.0,-1.0);
		glVertex2f(1.0,0.0);
		glEnd();
	}
	else if(r == 3)
	{
		glBegin(GL_LINE_STRIP);
		for(t=PI/2;t&lt;=PI;t+=0.01)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(x+1.0,y-1.0);
		}
		glEnd();
 
		glBegin(GL_LINE_STRIP);
		for(t=3*PI/2;t&lt;=2*PI;t+=0.01)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(x-1.0,y+1.0);
		}
		glEnd();
	}
	else if(r == 4)
	{
		glBegin(GL_LINE_STRIP);
		glVertex2f(-1.0,0.0);
		glVertex2f(-0.5,0.0);
		glVertex2f(-0.5,-0.5);
		glVertex2f(0.0,-0.5);
		glVertex2f(0.0,-1.0);
		glEnd();
		glBegin(GL_LINE_STRIP);
		glVertex2f(0.0,1.0);
		glVertex2f(0.0,0.5);
		glVertex2f(0.5,0.5);
		glVertex2f(0.5,0.0);
		glVertex2f(1.0,0.0);
		glEnd();
	}
	else if(r == 5)
	{
		glBegin(GL_LINES);
		glVertex2f(-1.0,0.0);
		glVertex2f(0.0,-1.0);
		glEnd();
		glBegin(GL_LINES);
		glVertex2f(1.0,0.0);
		glVertex2f(0.0,1.0);
		glEnd();
	}
	else
	{
		glBegin(GL_LINE_STRIP);
		for(t=0;t&lt;=3.15;t+=0.05)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(x-1.0,y-1.0);
		}
		glEnd();
 
		// |_
		glBegin(GL_LINE_STRIP);
		for(t=0;t&lt;=3.15;t+=0.05)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(1.0-x,1.0-y);
		}
		glEnd();
	}
}
 
void truchet()
{
	int r;
	float x,y,t;
 
	// Randomly select one truchet to draw
	r = 1+(int)(2.0*rand()/(RAND_MAX+1.0));
 
	if(r == 1)
	{
		// __
		//  |
		glBegin(GL_LINE_STRIP);
		for(t=0;t&lt;=3.15;t+=0.05)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(x-1.0,y-1.0);
		}
		glEnd();
 
		// |_
		glBegin(GL_LINE_STRIP);
		for(t=0;t&lt;=3.15;t+=0.05)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(1.0-x,1.0-y);
		}
		glEnd();
	}
	else
	{
		glBegin(GL_LINE_STRIP);
		for(t=PI/2;t&lt;=PI;t+=0.01)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(x+1.0,y-1.0);
		}
		glEnd();
 
		glBegin(GL_LINE_STRIP);
		for(t=3*PI/2;t&lt;=2*PI;t+=0.01)
		{
			x = cos(t);
			y = sin(t);
			glVertex2f(x-1.0,y+1.0);
		}
		glEnd();
	}
}
 
void myDraw()
{
	float x, y;
	float t;
 
	glClear(GL_COLOR_BUFFER_BIT);
	//glViewport(0,0,100,100);
	glColor3f(0.0,0.0,1.0);
 
 
	for(int i=0;i&lt;glutGet(GLUT_WINDOW_WIDTH)+CELL_WIDTH;i+=CELL_WIDTH)
		for(int j=0;j&lt;glutGet(GLUT_WINDOW_HEIGHT)+CELL_HEIGHT;j+=CELL_HEIGHT)
		{
			glViewport(i,j,CELL_WIDTH,CELL_HEIGHT);
			if(choice == 1)
				pattern1();
			else if(choice == 2)
				pattern2(i/CELL_WIDTH,j/CELL_HEIGHT); //Passing co-ords
			else if (choice == 3)
				truchet();
			else if (choice == 4)
				custom();
		}
	glFlush();
}
 
void GetParams()
{
	printf("\nTilings\nBy Pravin Paratey[July 10, 2003]");
	printf("\n[1] Pattern 1 [Circles] (one motif repeated)");
	printf("\n[2] Pattern 2 [Cairo Tiles] (2 motifs placed alternately)");
	printf("\n[3] Truchet (2 motifs chosen randomly)");
	printf("\n[4] Custom Pattern (6 motifs chosen randomly)");
	printf("\nEnter Choice: ");
	scanf("%i",&choice);
}
 
 
int main(int argc, char *argv[])
{
 
	GetParams();
 
	// Initialise random seed
	srand(time(NULL));
	// Glut initializations
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(400,400);
	glutInitWindowPosition(100,100);
	glutCreateWindow("Tilings - Pravin Paratey");
 
	glClearColor(1.0,1.0,1.0,0.5);
	glutDisplayFunc(myDraw);
	glutMainLoop();
	return 0;
}
March 20th, 2003

permalink FIFO Page Replacement Algo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Lab7 - To implement FIFO page replacement algo
 * Pravin Paratey (pravin[AT]iitb.ac.in)
 */
 
#include <stdio.h>
 
 
int FrameBuffer[100][2];
 
int main()
{
	FILE *fp;
	int numFrames;
	int pageHits=0;
	int count=0;
	int pageNum;
	int purgeMem;
	int i;
	int eof;
	int leastUsed=0;
	int leastIndex=0;
 
	fp = fopen("lru.in","r");
	fscanf(fp, "%i", &numFrames);
 
	while(1)
	{
		eof = fscanf(fp, "%i", &pageNum);
		if (eof == -1)
			break;
 
		printf("[%i] Requested\n",pageNum);
		purgeMem=1;
		for(i=0;i<numFrames;i++)
			if (FrameBuffer[i][0] == pageNum)
			{
				pageHits++;
				FrameBuffer[i][1]++;
				purgeMem=0;
				printf("[%i] Found\n",pageNum);
			}
		if (purgeMem)
		{
			leastUsed=100;
			leastIndex=0;
			for(i=0;i<numFrames;i++)
			{
				if(FrameBuffer[i][1] < leastUsed)
				{
					leastUsed=FrameBuffer[i][1];
					leastIndex=i;
				}
			}
			printf("[%i] Not Found ... added -%i- purged\n",pageNum,FrameBuffer[leastIndex][0]);
			FrameBuffer[leastIndex][0] = pageNum;
			FrameBuffer[leastIndex][1] = 0;
		}
		count++;
 
	}
 
	printf("Total pages requested=%i\nPage Hits=%i\nHit Rate=%f",
			count, pageHits, (float)pageHits/(float)count);
	fclose(fp);
	return 0;
}
February 3rd, 2003

permalink Blockz

This is a block game. You got to click on the blocks at the edges. They coagulate at the center. Blocks disappear if 4 or more consecutive blocks have the same color. When I abandoned this project, I hadn't put in the disappearing logic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*@@ Wedit generated application. Written Sun Feb 02 15:00:40 2003
 @@header: c:\downloads\blockz\blockzres.h
 @@resources: c:\downloads\blockz\blockz.rc
 Do not edit outside the indicated areas */
/* Pravin Paratey pravin[at]iitb[dot]ac[dot]in*/
/*<---------------------------------------------------------------------->*/
/*<---------------------------------------------------------------------->*/
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//#include <commctrl.h>
//#include <string.h>
#include "blockzres.h"
 
// Linked list
struct dalist
{
	int x;
	int y;
	struct dalist *next;
}dalist;
 
 
struct dalist *mylist;
 
/*<---------------------------------------------------------------------->*/
HINSTANCE hInst;		// Instance handle
HWND hwndMain;		//Main window handle
 
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
/* --- The following code comes from c:\lcc\lib\wizard\statbar.tpl. */
 
// Global Variables for the status bar control.
 
HWND  hWndStatusbar;
HBRUSH hbrArray[8]; // Array for blocks
HPEN hpenArray[8];
INT blockArray[16][16];
 
/*------------------------------------------------------------------------
 Procedure:     UpdateStatusBar ID:1
 Purpose:       Updates the statusbar control with the appropiate
                text
 Input:         lpszStatusString: Charactar string that will be shown
                partNumber: index of the status bar part number.
                displayFlags: Decoration flags
 Output:        none
 Errors:        none
 
------------------------------------------------------------------------*/
void UpdateStatusBar(LPSTR lpszStatusString, WORD partNumber, WORD displayFlags)
{
    SendMessage(hWndStatusbar,
                SB_SETTEXT,
                partNumber | displayFlags,
                (LPARAM)lpszStatusString);
}
 
 
/*------------------------------------------------------------------------
 Procedure:     MsgMenuSelect ID:1
 Purpose:       Shows in the status bar a descriptive explaation of
                the purpose of each menu item.The message
                WM_MENUSELECT is sent when the user starts browsing
                the menu for each menu item where the mouse passes.
 Input:         Standard windows.
 Output:        The string from the resources string table is shown
 Errors:        If the string is not found nothing will be shown.
------------------------------------------------------------------------*/
LRESULT MsgMenuSelect(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
{
    static char szBuffer[256];
    UINT   nStringID = 0;
    UINT   fuFlags = GET_WM_MENUSELECT_FLAGS(wparam, lparam) & 0xffff;
    UINT   uCmd    = GET_WM_MENUSELECT_CMD(wparam, lparam);
    HMENU  hMenu   = GET_WM_MENUSELECT_HMENU(wparam, lparam);
 
    szBuffer[0] = 0;                            // First reset the buffer
    if (fuFlags == 0xffff && hMenu == NULL)     // Menu has been closed
        nStringID = 0;
 
    else if (fuFlags & MFT_SEPARATOR)           // Ignore separators
        nStringID = 0;
 
    else if (fuFlags & MF_POPUP)                // Popup menu
    {
        if (fuFlags & MF_SYSMENU)               // System menu
            nStringID = IDS_SYSMENU;
        else
            // Get string ID for popup menu from idPopup array.
            nStringID = 0;
    }  // for MF_POPUP
    else                                        // Must be a command item
        nStringID = uCmd;                       // String ID == Command ID
 
    // Load the string if we have an ID
    if (0 != nStringID)
        LoadString(hInst, nStringID, szBuffer, sizeof(szBuffer));
    // Finally... send the string to the status bar
    UpdateStatusBar(szBuffer, 0, 0);
    return 0;
}
 
 
/*------------------------------------------------------------------------
 Procedure:     InitializeStatusBar ID:1
 Purpose:       Initialize the status bar
 Input:         hwndParent: the parent window
                nrOfParts: The status bar can contain more than one
                part. What is difficult, is to figure out how this
                should be drawn. So, for the time being only one is
                being used...
 Output:        The status bar is created
 Errors:
------------------------------------------------------------------------*/
void InitializeStatusBar(HWND hwndParent,int nrOfParts)
{
    const int cSpaceInBetween = 8;
    int   ptArray[40];   // Array defining the number of parts/sections
    RECT  rect;
    HDC   hDC;
 
   /* * Fill in the ptArray...  */
 
    hDC = GetDC(hwndParent);
    GetClientRect(hwndParent, &rect);
 
    ptArray[nrOfParts-1] = rect.right;
    //---TODO--- Add code to calculate the size of each part of the status
    // bar here.
 
    ReleaseDC(hwndParent, hDC);
    SendMessage(hWndStatusbar,
                SB_SETPARTS,
                nrOfParts,
                (LPARAM)(LPINT)ptArray);
 
    UpdateStatusBar("Ready", 0, 0);
    //---TODO--- Add code to update all fields of the status bar here.
    // As an example, look at the calls commented out below.
 
//    UpdateStatusBar("Cursor Pos:", 1, SBT_POPOUT);
//    UpdateStatusBar("Time:", 3, SBT_POPOUT);
}
 
 
/*------------------------------------------------------------------------
 Procedure:     CreateSBar ID:1
 Purpose:       Calls CreateStatusWindow to create the status bar
 Input:         hwndParent: the parent window
                initial text: the initial contents of the status bar
 Output:
 Errors:
------------------------------------------------------------------------*/
static BOOL CreateSBar(HWND hwndParent,char *initialText,int nrOfParts)
{
    hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_BORDER|SBARS_SIZEGRIP,
                                       initialText,
                                       hwndParent,
                                       IDM_STATUSBAR);
    if(hWndStatusbar)
    {
        InitializeStatusBar(hwndParent,nrOfParts);
        return TRUE;
    }
 
    return FALSE;
}
 
/*<---------------------------------------------------------------------->*/
/*@@0->@@*/
static BOOL InitApplication(void)
{
	WNDCLASS wc;
 
	memset(&wc,0,sizeof(WNDCLASS));
	wc.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;
	wc.lpfnWndProc = (WNDPROC)MainWndProc;
	wc.hInstance = hInst;
	wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(0,0,0));//(COLOR_WINDOW);
	wc.lpszClassName = "blockzWndClass";
	wc.lpszMenuName = MAKEINTRESOURCE(IDMAINMENU);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	if (!RegisterClass(&wc))
		return 0;
/*@@0<-@@*/
	// ---TODO--- Call module specific initialization routines here
	hbrArray[0] = CreateSolidBrush(RGB(255,0,0));
	hbrArray[1] = CreateSolidBrush(RGB(255,255,0));
	hbrArray[2] = CreateSolidBrush(RGB(0,255,0));
	hbrArray[3] = CreateSolidBrush(RGB(0,255,255));
	hbrArray[4] = CreateSolidBrush(RGB(0,0,255));
	hbrArray[5] = CreateSolidBrush(RGB(255,0,255));
	hbrArray[6] = CreateSolidBrush(RGB(255,125,0));
	hbrArray[7] = CreateSolidBrush(RGB(255,0, 125));
	hpenArray[0] = CreatePen(PS_SOLID,2,RGB(255,0,0));
	hpenArray[1] = CreatePen(PS_SOLID,2,RGB(255,255,0));
	hpenArray[2] = CreatePen(PS_SOLID,2,RGB(0,255,0));
	hpenArray[3] = CreatePen(PS_SOLID,2,RGB(0,255,255));
	hpenArray[4] = CreatePen(PS_SOLID,2,RGB(0,0,255));
	hpenArray[5] = CreatePen(PS_SOLID,2,RGB(255,0,255));
	hpenArray[6] = CreatePen(PS_SOLID,2,RGB(255,125,0));
	hpenArray[7] = CreatePen(PS_SOLID,2,RGB(255,0,125));
	hpenArray[0] = CreatePen(PS_NULL,2,RGB(255,0,0));
	for (int i=0;i<16;i++)
		for(int j=0;j<16;j++)
			blockArray[i][j] = -1;
	srand(time(NULL));
	// Top row
	for (int i=2;i<14;i++)
	{
		for(int j=0