★ wanayoo — archive 1999 http://java.webdeveloper.com/Bat/source.htmlNouvelle recherche | Portail wanayoo
Computer Digital Expo

The Event For Leading Edge Flash Development


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
IT Training
Reach IT Professionals
Internet Jobs
Conference Calling
Submit Your Site
Website Monitoring
Hacker's Secrets
Conference Calling
Find a Consultant
Business Search
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:


Bat


//This program was completely written by Peter Wetsel.
//If there are any questions about this code you
//can email me at phwetsel@eos.ncsu.edu
//To get it to compile using jdk type "javac -nowarn battle.java".
//
//
//




import java.util.*;
import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.lang.*;

public class battle extends java.applet.Applet implements Runnable {
	Color col;
	String message= new String("");//holds the message placed in the upper left
	String Status = new String("");
	String message2[]= new String[5];//holds the destroyed messages in order for repainting.
	int hits=0;//the total number of hits by the player
	int numDestroyed=0;//the number of ships destroyed by the player
	int battlepos[]=new int[4];//the 4 positions of the battleship
    int carrierpos[]=new int[5];//the 5 positions of the carrier
	int subpos[]=new int[3];//the 3 positions of the submarine
	int patrolpos[]=new int[2];//the 2 positions of the patrol boat
	int destroyerpos[]=new int[3]; //the 3 positions of the destroyer
	int guesses=0;// the number of guesses by the player
	int player[][]=new int[10][10]; //to hold the positions of the player.(not until version 2)
	int guessed[][]=new int[10][10]; //hold the places that have been guessed by the player
	int computer[][]=new int[10][10]; //holds where the computers ships are
	int battleshipHitCt, destroyerHitCt, subHitCt, patrolHitCt, carrierHitCt;//keeps the hit count for each of the ships
	boolean init=false;//false until play is pushed.
	boolean ok=false;//boolean that is true if current boat position chosen by random is ok(getcarrier(), etc.)
	boolean battleOK=true;//false when the type of ship is destroyed
	boolean patrolOK=true;//...
	boolean subOK=true;//...
	boolean destroyerOK=true;//...
	boolean carrierOK=true;//...
	boolean won=false;//true when the player has won
	int o;

	public void getCarrier(){
		int x, y, dir;  //will hold a random x & y coordinate(0-9), and a random direction(0-1)(N,E,S,W)
		x=(int)(10*Math.random());
		y=(int)(10*Math.random());
		dir=(int)(100*Math.random()/25);
		if(dir==0)
		{
			while(y<4){
				y=(int)(10*Math.random());
			}
			for (int i=0; i<5; i++){
				computer[x][y-i]=1;
				carrierpos[i]=x*10+y-i;
			}
		}
		if (dir==1)
		{
			while(x>5){
				x=(int)(10*Math.random());
			}
			for (int i=0; i<5; i++){
				computer[x+i][y]=1;
				carrierpos[i]=(x+i)*10+y;
			}
		}
		if (dir==2)
		{
			while(y>5){
				y=(int)(10*Math.random());
			}
			for (int i=0; i<5; i++){
				computer[x][y+i]=1;
				carrierpos[i]=x*10+y+i;
			}
		}
		if (dir==3)
		{
			while(x<4){
				x=(int)(10*Math.random());
			}
			for (int i=0; i<5; i++){
				computer[x-i][y]=1;
				carrierpos[i]=(x-i)*10+y;
			}
		}
	}

	public void getBattle(){
		int x, y, dir;
		x=(int)(10*Math.random());
		y=(int)(10*Math.random());
		dir=(int)(100*Math.random()/25);
		while(!ok){
			ok=true;
			x=(int)(10*Math.random());
			y=(int)(10*Math.random());
			dir=(int)(100*Math.random()/25);
			if(dir==0)
			{
				while(y<3){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<4; i++){
					o=computer[x][y-i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==1)
			{
				while(x>6){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<4; i++){
					o=computer[x+i][y];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==2)
			{
				while(y>6){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<4; i++){
					o=computer[x][y+i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==3)
			{
				while(x<3){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<4; i++){
					o=computer[x-i][y];
					if (o==1){
						ok=false;
					}
				}

			}
		}

		if(dir==0)
		{
			for (int i=0; i<4; i++){
				computer[x][y-i]=1;
				battlepos[i]=x*10+y-i;
			}
		}
		if (dir==1)
		{
			for (int i=0; i<4; i++){
				computer[x+i][y]=1;
				battlepos[i]=(x+i)*10+y;
			}
		}
		if (dir==2)
		{
			for (int i=0; i<4; i++){
				computer[x][y+i]=1;
				battlepos[i]=x*10+y+i;
			}
		}
		if (dir==3)
		{
			for (int i=0; i<4; i++){
				computer[x-i][y]=1;
				battlepos[i]=(x-i)*10+y;
			}
		}

	}

	public void getSub(){
		int x, y, dir;
		x=(int)(10*Math.random());
		y=(int)(10*Math.random());
		dir=(int)(100*Math.random()/25);
		while(!ok){
			ok=true;
			x=(int)(10*Math.random());
			y=(int)(10*Math.random());
			dir=(int)(100*Math.random()/25);
			if(dir==0)
			{
				while(y<2){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x][y-i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==1)
			{
				while(x>7){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x+i][y];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==2)
			{
				while(y>7){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x][y+i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==3)
			{
				while(x<2){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x-i][y];
					if (o==1){
						ok=false;
					}
				}

			}
		}

		if(dir==0)
		{
			for (int i=0; i<3; i++){
				computer[x][y-i]=1;
				subpos[i]=x*10+y-i;
			}
		}
		if (dir==1)
		{
			for (int i=0; i<3; i++){
				computer[x+i][y]=1;
				subpos[i]=(x+i)*10+y;
			}
		}
		if (dir==2)
		{
			for (int i=0; i<3; i++){
				computer[x][y+i]=1;
				subpos[i]=x*10+y+i;
			}
		}
		if (dir==3)
		{
			for (int i=0; i<3; i++){
				computer[x-i][y]=1;
				subpos[i]=(x-i)*10+y;
			}
		}

	}

	public void getDestroyer(){
		int x, y, dir;
		x=(int)(10*Math.random());
		y=(int)(10*Math.random());
		dir=(int)(100*Math.random()/25);
		while(!ok){
			ok=true;
			x=(int)(10*Math.random());
			y=(int)(10*Math.random());
			dir=(int)(100*Math.random()/25);
			if(dir==0)
			{
				while(y<2){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x][y-i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==1)
			{
				while(x>7){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x+i][y];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==2)
			{
				while(y>7){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x][y+i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==3)
			{
				while(x<2){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<3; i++){
					o=computer[x-i][y];
					if (o==1){
						ok=false;
					}
				}

			}
		}

		if(dir==0)
		{
			for (int i=0; i<3; i++){
				computer[x][y-i]=1;
				destroyerpos[i]=x*10+y-i;
			}
		}
		if (dir==1)
		{
			for (int i=0; i<3; i++){
				computer[x+i][y]=1;
				destroyerpos[i]=(x+i)*10+y;
			}
		}
		if (dir==2)
		{
			for (int i=0; i<3; i++){
				computer[x][y+i]=1;
				destroyerpos[i]=x*10+y+i;
			}
		}
		if (dir==3)
		{
			for (int i=0; i<3; i++){
				computer[x-i][y]=1;
				destroyerpos[i]=(x-i)*10+y;
			}
		}

	}

	public void getPatrol(){
		int x, y, dir;
		x=(int)(10*Math.random());
		y=(int)(10*Math.random());
		dir=(int)(100*Math.random()/25);
		while(!ok){
			ok=true;
			x=(int)(10*Math.random());
			y=(int)(10*Math.random());
			dir=(int)(100*Math.random()/25);
			if(dir==0)
			{
				while(y<1){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<2; i++){
					o=computer[x][y-i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==1)
			{
				while(x>8){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<2; i++){
					o=computer[x+i][y];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==2)
			{
				while(y>8){
					y=(int)(10*Math.random());
				}
				for (int i=0; i<2; i++){
					o=computer[x][y+i];
					if (o==1){
						ok=false;
					}
				}

			}
			if (dir==3)
			{
				while(x<1){
					x=(int)(10*Math.random());
				}
				for (int i=0; i<2; i++){
					o=computer[x-i][y];
					if (o==1){
						ok=false;
					}
				}

			}
		}

		if(dir==0)
		{
			for (int i=0; i<2; i++){
				computer[x][y-i]=1;
				patrolpos[i]=x*10+y-i;
			}
		}
		if (dir==1)
		{
			for (int i=0; i<2; i++){
				computer[x+i][y]=1;
				patrolpos[i]=(x+i)*10+y;
			}
		}
		if (dir==2)
		{
			for (int i=0; i<2; i++){
				computer[x][y+i]=1;
				patrolpos[i]=x*10+y+i;
			}
		}
		if (dir==3)
		{
			for (int i=0; i<2; i++){
				computer[x-i][y]=1;
				patrolpos[i]=(x-i)*10+y;
			}
		}

	}

	public void run(){// to be in here so can compile
	}


	public void init() {

		for (int i=0; i<10; i++){//initializes the guessed matrix to be all 2's(not been guessed)
			for (int j=0;j<10; j++){
				guessed[i][j]=2;
			}
		}
		for (int i=0; i<2; i++){//initializes array to be all zeros
			patrolpos[i]=0;
		}
		for (int i=0; i<2; i++){
			destroyerpos[i]=0;
		}
		for (int i=0; i<2; i++){
			subpos[i]=0;
		}
		for (int i=0; i<2; i++){
			carrierpos[i]=0;
		}
		for (int i=0; i<2; i++){
			battlepos[i]=0;
		}
		battleshipHitCt=0;//initialize all hit counts
		destroyerHitCt=0;
		subHitCt=0;
		patrolHitCt=0;
		carrierHitCt=0;
	    //get all boat positions
		getCarrier();
		ok=false;
		getBattle();
		ok=false;
		getDestroyer();
		ok=false;
		getSub();
		ok=false;
		getPatrol();
	}

	public void paint(Graphics g) { //this is the shots paint method... draws a line from its last position to its current position, basically
	   if (!init && !won){//draws screen before play is pushed
			g.setColor(col.white);
			g.fillRect(0,0,600,320);
			g.setColor(col.black);
			g.fillRect(20,20,30,25);
			g.setColor(col.white);
			g.drawString("PLAY", 23,30);
			g.setColor(col.black);
			g.drawString("This is different that usual battleship, here the goal is",23,65);
			g.drawString("to find all of the computers ships in the least number of guesses.",23,85);
			g.drawString("<30   turns  --- You have too much luck!", 23, 105);
			g.drawString("31-50 turns  --- Excellent!", 23, 125);
			g.drawString(">=51  turns  --- You have no skills!", 23, 145);
			g.drawString("PRESS PLAY TO BEGIN", 23, 165);
		}
		if (init && !won){//draws grid and messages
			g.setColor(col.black);
			g.fillRect(0,0,600,320); //makes big, black rectangle washover
			g.setColor(col.white);
			g.drawString("# of Guesses = " + guesses,500, 280);
			for (int i=0; i<11; i++)
				g.drawLine(20,20+i*28, 300, 20+i*28);
			for (int i=0; i<11; i++)
				g.drawLine(20+i*28,20,20+i*28,300);
			char ch='A';
			for (int i=0; i<10; i++){//draws letter accross the top
				g.drawString(ch+" ",30+28*i,13);
				ch++;
			}
			ch='1';
			for (int i=0; i<9; i++){//draws numbers down the side
				g.drawString(ch+" ",10,40+28*i);
				ch++;
			}
			g.drawString("10", 6,290);
			g.drawString(message,420,40);
			for (int i=0; i<numDestroyed; i++){
				g.drawString(message2[i], 305, 40+(i+1)*20);
			}
			g.setColor(col.black);
			//next puts in any circles already guessed
			for (int i=0; i<10; i++)
				for (int j=0; j<10;j++){
					if (guessed[i][j]==1)	 {
						g.setColor(col.red);
						g.fillOval(i*28+30, j*28+30, 10,10);
					}
					if (guessed[i][j]==0)	 {
						g.setColor(col.white);
						g.fillOval(i*28+30, j*28+30, 10,10);
					}
				}
		}
	}

	public boolean seeifahit(int x, int y){//sees if the shot was a hit or not
		if(computer[x][y]==1)
			return true;
		return false;
	}

	public String getshiptype(int x, int y){//returns the name of the ship that was hit
		for (int i=0;i<4; i++)
			if (battlepos[i]==x*10+y){
				battleshipHitCt++;
				return "Battleship";
		    }
		for (int i=0;i<3; i++)
			if (subpos[i]==x*10+y){
				subHitCt++;
				return "Submarine";
			}
		for (int i=0;i<2; i++)
			if (patrolpos[i]==x*10+y){
				patrolHitCt++;
				return "Patrol Boat";
			}
		for (int i=0;i<5; i++)
			if (carrierpos[i]==x*10+y){
				carrierHitCt++;
				return "Carrier";
			}
		for (int i=0;i<3; i++)
			if (destroyerpos[i]==x*10+y){
				destroyerHitCt++;
				return "Destroyer";
			}
		return "  ";
	}

	public void gameOver(){//displays guesses and ends
		if (!won){
			Graphics g = getGraphics() ;
			g.setColor(col.black);
			g.fillRect(0,0,600,320); //makes big, black rectangle washover
			g.setColor(col.white);
			g.drawString("YOU WIN!!", 200,100);
			String out=new String("It took you "+guesses+" tries!");
			g.drawString(out, 200,120);
			g.drawString("Press refresh on your browser to play again", 200, 150);
			won=true;
		}
	}

	public boolean destroyed(){

		if(battleshipHitCt==4 && battleOK)
		{
			battleOK=false;
			return true;
		}
		if(carrierHitCt==5 && carrierOK)
		{
			carrierOK=false;
			return true;
		}
		if(subHitCt==3 && subOK)
		{
			subOK=false;
			return true;
		}
		if(patrolHitCt==2 && patrolOK)
		{
			patrolOK=false;
			return true;
        }
		if(destroyerHitCt==3 && destroyerOK)
		{
			destroyerOK=false;
			return true;
		}
		return false;
	}

	public boolean mouseDown(Event e, int x, int y) {//handles when a mouse key is pressed(either key)
		String type= new String("");
		if (!won){
			message="";
			Graphics g = getGraphics() ;
			g.setColor(col.white);
			int xindex, yindex, val;
			if (x>20 && y>20 && y<300 && x<300 && init){
				guesses++;
				g.setColor(col.black);
				g.fillRect(490,260, 550, 285);
				g.setColor(col.white);
				g.drawString("# of Guesses = " + guesses,500, 280);
				g.setColor(col.black);
				g.fillRect(419,30,580,95);
				g.setColor(col.white);
				xindex=(x-20)/28;//converts x coordinate into index(A-J) in form of (0-9)
				yindex=(y-20)/28;//converst y coordinate into index(0-9)
				val=guessed[xindex][yindex];
				if (val==2){
					if (seeifahit(xindex,yindex)){
						type=getshiptype(xindex, yindex);
						hits++;
						g.setColor(col.white);
						guessed[xindex][yindex]=1;
						if (!destroyed()){
							message=type + " Hit";
							g.drawString(message, 420, 40);
						}
						else{
							g.setColor(col.white);
							message2[numDestroyed]=new String("");
							message2[numDestroyed]=type + "Destroyed";
							numDestroyed++;
							g.drawString(message2[numDestroyed-1] , 305, 40+(numDestroyed)*20);
						}
						g.setColor(col.red);
					}
					else guessed[xindex][yindex]=0;
					g.fillOval(xindex*28+30, yindex*28+30, 10,10);
				}
                else {
					g.setColor(col.white);
					message="That has already been guessed";
					g.drawString(message,420,40);
				}
			}

			if (x>20 && x<40 && y>20 && y<40 && !init){
				init=true;
				paint(g);
			}
			if (hits>=17){
				gameOver();
				System.exit(0);
			}
		}
		return true;
	}
}


Back to the Bat applet page.


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z

How to Add Java Applets to Your Site

New on the Java Boutique:

New Article:
J2EE Deployment Specification
Why is it that J2EE applications written in the same language, Java, may not coexist on different servers? While the answer isn't simple Sun has set out to alleviate these problems with the "J2EE Deployment Specification".
... [more articles]

New Tutorial:
Designing Packages for Stability
Are your designs stable? This month Samudra will focus on the change impact relationship involved in designing classes showing us how to take them into account when designing the package relationship.
... [more Tutorials]

Year in Review:
The Best of 2002
The year 2002 saw 201 new applets posted to JavaBoutique. Check out the 2 most popular apps from each month of 2002!
... [popular applets]

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.

internet.com logo


Developer News

Deadline Looms for IBM's AIX

HP, Dell Pledge Java Support

Putting Java in ONE Cup



Database Journal Launched!
Jupiter Media has announced the launch of Database Journal. DBJ offers SQL courses and other database related resources for beginner to expert developers.



PhotoMap
PhotoMap is made of many pictures and links. You can travel in "photo map world" where you have never visit yet.
Download of the Week
BSCOutline v5.10
BSCOutline Java Outline Applet is a customizable tree view control for use in Web pages, similar to that used by Windows File Manager or Explorer. BSCOutline has been written to be the simple to use and small to store.

JB User Poll
How does .NET fit into your programming?
The JavaBoutique Top 15:
1. Webmatic
2. ChompText
3. PingPong
4. DJPopMenu
5. RushHour
6. Distorter
7. NavBar
8. 3DMaze
9. AScroll2
10. Nibbly
11. AnimLetters_anim
12. Encyclo
13. BMMenu
14. BMI
15. Viewer
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
HP, Dell Pledge Java Support
Despite Microsoft's decision to drop Java from the Windows OS, HP and Dell say they will continue to ship the runtime environment as part of their OEM agreements.

Case Study: Tradeshop.com
This online jewelry craftsman focuses on one-on-one customer service, using an old-fashioned small business approach to create e-commerce success.

Information Technology in Context
Nicholas Carr's article in the Harvard Business Review has stirred up a firestorm of controversy about the relevance of IT to business strategy. Columnist George Spafford says before people completely discard what Carr has to say, there are some issues to take into account.


100's of firms that help your Web site be found

Jupitermedia Corp. is publisher of the internet.com and EarthWeb networks.
Copyright 2003 Jupitermedia Corporation All Rights Reserved.

Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/
http://www.earthweb.com/