advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

QvK - Queens versus Knights


//////////////////////////////////////////////////////////////////////////////////
//						University Malaysia Sarawak, Malaysia
//
//	Programmers : Maclean Patrick,Halikul Lenando,Roslan Sulaiman,Syamsul Hanif
//
//	The applet code for the HTML file is :
//
//	<applet code=queensvsknights.class width=650 height=513></applet>
//
//	Files included needed for this file:

//	QueensvsKnights.class
//	QueensvsKnights.java
//	knightshoji3d.gif
//	pointX.gif
//	queenShoji.gif
//	boardGrid.gif
//
//	Instructions:
//
//	A left mouse click will place a Knight
//	A right mouse click will place a Queen
//
//	The object of the game is to place the 4 Queens and 4 Knights in positions that
//	do not threathen each other.  If that bores you then you can just try placing 8
//	Queens on the board without them threathening each other (8 Queens Problem).
//
//	Any comments please email me at elixus@email.com
//

import java.awt.*;
import java.*;
import java.applet.*;

public class  QueensvsKnights extends Applet

{
	static int count,queen,knight;
	int  turn=0;
	int array[][] = new int[8][8];
	int arrx,arry,orix,oriy,posx,posy,check,check1,empty;
	int bj[] = {-2,-2,-1,1,2,2,1,-1};
	int bi[] = {-1,1,2,2,1,-1,-2,-2};

	String str1,str2,str3;
	Font appFont;
	Image imageQueen,imageBoard,imageKnight,imageX;

	public void init()
	{
		Color c = Color.blue;
		setBackground(c);
		appFont= new Font("Verdana",Font.BOLD,14);
		count = 0;
		queen=0;
		knight=0;

		imageQueen = getImage(getCodeBase(), "queenShoji.gif");
		imageBoard = getImage(getCodeBase(), "boardGrid.gif");
		imageKnight = getImage(getCodeBase(),"knightshoji3d.gif");
		imageX		= getImage(getCodeBase(),"pointX.gif");

		for(int i=0;i<8;i++)
			for(int j=0;j<8;j++)
				array[i][j] = 0;

	}

	public void paint(Graphics g)
	{
		g.setColor(Color.yellow);
		g.drawImage(imageBoard,0,0,512,512,this);

		if(turn == 1 )
		{
			str1 = String.valueOf(count);
			str2 = String.valueOf(queen);
			str3 = String.valueOf(knight);

			g.setFont(appFont);
			g.drawString("Mouse Clicks: " + str1,520,20);
			g.drawString("Queens Used: " +str2,520,40);
			g.drawString("Knights Used: "+str3,520,60);

			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					if(array[i][j] == 3)
					{
						//draws image of the queen
						g.drawImage(imageQueen,i*64,j*64,64,64,this);
					}

					if(array[i][j] == 2)
					{
						//draws image of knight
						g.drawImage(imageKnight,i*64,j*64,64,64,this);
					}

					if(array[i][j] == 1)
					{
						g.drawImage(imageX,i*64,j*64,64,64,this);
					}
				}
			}

		}
	}

	public boolean mouseDown(Event evt, int  x, int  y)
	{
		count++;

		orix=x;
		oriy=y;

		arrx=(orix-0)/64;
		arry=(oriy-0)/64;

		if((evt.modifiers & Event.META_MASK) !=0) // 0 = right button & 1 = left button
		{
			//this is the queen function
			check = checkKnight(arrx,arry);	//check for the existance of a knight
			empty = checkEmpty(arrx,arry);	//check status of square

			if(empty !=1 && check != 1)
			{
				turn = 1 ;
				array[arrx][arry] = 3;
				queen++;
				queenmark(arrx,arry);//marking the queen
				repaint();
				return true;
			}
				else return false;

		}
		else
		{
			//this is the knight function
			empty = checkEmpty(arrx,arry); //check the status of the square

			if(empty != 1)
			{
				turn = 1 ;
				array[arrx][arry] = 2;
				knight++;

				NewMarkKnight(arrx,arry);

				repaint();
				return true;
			}
				else return false;


		}

	}


////////////////////////////////////////////////////////////////////////////////////////////
	public void NewMarkKnight(int arrx, int arry)
	{
		int i,xi,yi;

	    for (i=0;i<8;i++)
		{
			xi = arrx + bi[i];
			yi = arry + bj[i];

			if (xi >= 0 && xi < 8 && yi >=0 && yi < 8)
				array[xi][yi] = 1;
		}

	}
//////////////////////////////////////////////////////////////////////////////////////////////
// marking for the queen
	public void queenmark(int arrx,int arry)
	{
		if (arrx >= 0 && arrx <= 7 && arry >=0 && arry <= 7)
			{
					mark1(arrx,arry);
					mark2(arrx,arry);
					mark3(arrx,arry);
					mark4(arrx,arry);
					mark5(arrx,arry);
					mark6(arrx,arry);
					mark7(arrx,arry);
					mark8(arrx,arry);
			}

	}

	//marking functions, all eight of them
	public void mark1(int row,int col)
	{
		int i,j;
		i=row+1;
		j=col;
		while(i<=7 && j<=7)
		{
			array[i++][j]= 1;

		}

	}

	public void mark2(int row,int col)
	{
		int i,j;
		i=row;
		j=col-1;
		while(i<=7 && j>=0)
		{
			array[i][j--]= 1;

		}

	}

	public void mark3(int row,int col)
	{
		int i,j;

			i=row-1;
			j=col;
			while(i>=0 && j<=7)
			{
				array[i--][j]= 1;
			}

	}

	public void mark4(int row,int col)
	{
		int i,j;
		i=row;
		j=col+1;
		while(i<=7 && j<=7)
		{
			array[i][j++]= 1;

		}

	}


	public void mark5(int row,int col)
	{
		int i,j;
		i=row-1;
		j=col-1;
		while(i>=0 && j>=0)
		{
			array[i--][j--]= 1;

		}

	}

	public void mark6(int row,int col)
	{
		int i,j;
		i=row+1;
		j=col+1;
		while(i<=7 && j<=7)
		{
			array[i++][j++]= 1;

		}

	}

	public void mark7(int row,int col)
	{
		int i,j;
		i=row-1;
		j=col+1;
		while(i>=0 && j<=7)
		{
			array[i--][j++]= 1;

		}

	}

	public void mark8(int row,int col)
	{
		int i,j;
		i=row+1;
		j=col-1;
		while(i<=7 && j>=0)
		{
			array[i++][j--]= 1;
		}

	}

////////////////////
//checking for the knight
	public int checkKnight(int x,int y)
	{

		if(checkKnight1(x,y) == true )return 1;
		if(checkKnight2(x,y) == true )return 1;
		if(checkKnight3(x,y) == true )return 1;
		if(checkKnight4(x,y) == true )return 1;
		if(checkKnight5(x,y) == true )return 1;
		if(checkKnight6(x,y) == true )return 1;
		if(checkKnight7(x,y) == true )return 1;
		if(checkKnight8(x,y) == true )return 1;

		return(0);
	}

////////////////////////////////////////////////////////
// checking the existance of a knight, done by the queen.
	public boolean checkKnight1(int x,int y)
	{
		while(true)
		{
			if(y < 0) return false;
			else
			{
				if(array[x][y--] == 2)
				return true;

			}
		}

	}

	public boolean checkKnight2(int x,int y)
	{
		while(true)
		{
			if(x > 7 || y < 0)return false;
			else
			{
				if(array[x++][y--] == 2)
				return true;

			}
		}

	}

	public boolean checkKnight3(int x,int y)
	{
		while(true)
		{
			if(x > 7)return false;
			else
			{
				if(array[x++][y] == 2)
				return true;

	 		}
		}

	}

	public boolean checkKnight4(int x,int y)
	{
		while(true)
		{
			if(x > 7 || y > 7)return false;
			else
			{
				if(array[x++][y++] == 2)
				return true;

			}
		}

	}

	public boolean checkKnight5(int x,int y)
	{
		while(true)
		{
			if(y > 7)return false;
			else
			{
				if(array[x][y++] == 2)
				return true;

			}
		}
	}

	public boolean checkKnight6(int x,int y)
	{
		while (true)
		{
			if (x < 0 || y > 7) return false;
			else
			{
				if (array[x--][y++] == 2)
				return true;

			}
		}
	}

	public boolean checkKnight7(int x,int y)
	{
		while (true)
		{
			if (x < 0) return false;
			else
			{
				if(array[x--][y] == 2)
				return true;

			}
		}
	}

	public boolean checkKnight8(int x,int y)
	{
		while (true)
		{
			if (x < 0 || y < 0) return false;
			else
			{
				if(array[x--][y--] == 2)
				return true;

			}
		}

	}

	public int checkEmpty(int column, int row)
	{
		if(array[column][row] != 0 ) return 1;
		else return 0;

	}


// the point of no return.
}


Back to the QvK applet page

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs