File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Creating simple GUI interface for BattleShips Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Reply locked New topic
Author

Creating simple GUI interface for BattleShips

Koert Kopbeen
Greenhorn

Joined: Oct 30, 2005
Posts: 6
Hi everyone,
I am from South Africa and in grade 11 and I really need some help with my Java Project.

We had to create a BattleShips prog. (which I did do and will post here now) which has 5 Ships (consisting of 1 space each), three battleships (each consisting of three spaces) and one aircraft carrier consisting of five spaces. This is on a 2x2 Array (10 spaces horizontally and 10 spaces vertically. We have to display a blank board and the user can type in the column and row that they want to hit. If they hit any of the ships, we have to add to his score (his score starts at 100 and we can decide on a scoring system for ourselves) and it has to show on the board. The same for if he misses, but then we have to deduct from the score.

I don't know how to go any further, so if you can maybe help me tweak the program and help me to create a SIMPLE (it can be REALLY simple) GUI interface, I would really appreciate it.

Thanks for your help. ASAP please it has to be in on the 4th November.

Thanks again! Here follows the class and the driver (I know the code is probably way too long - I could do it shorter, but I prefer to keep it that way! - makes it authentic):


import java.io.*;

public class BattleShips
{

private char [] [] board = new char [10] [10];
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));

void blank ()
{


for (int i = 0 ; i < 10 ; i++)
{

for (int j = 0 ; j < 10 ; j++)
{

board [j] = ' ';
}

}


}


void place ()
{

boolean beset = false;

int countS = 0;
int countA = 0;
int countB = 0;

for (int y = 0 ; y < 5 ; y++)
{

while (!beset)
{
int i = (int) (Math.random () * 10);
int j = (int) (Math.random () * 10);

if (board [j] == ' ')
{


board [j] = 'S';
System.out.println (board [j]);

countS++;
beset = true;

}
else
{
countS = countS;
}

}
beset = false;
}

for (int k = 0 ; k < 3 ; k++)
{
while (!beset)
{
int i = (int) (Math.random () * 10);
int j = (int) (Math.random () * 10);
int one2 = (int) (Math.random () * 2);


if (one2 == 0)
{

if ((board [j] == ' ') && (j < 6))
{
if (board [j + 1] == ' ')
{
if (board [j + 2] == ' ')
{
for (int e = j ; e < j + 3 ; e++)
{

board [e] = 'B';
System.out.println (board [e]);
}

// countB++;
beset = true;

}
}
}
}
else
{

if ((board [j] == ' ') && (i < 6))
{
if (board [i + 1] [j] == ' ')
{
if (board [i + 2] [j] == ' ')
{
for (int e = i ; e < i + 3 ; e++)
{

board [e] [j] = 'B';
System.out.println (board [e] [j]);
}

// countB++;
beset = true;

}
}
}



}
// else
// {
// countB = countB;
// }

}
beset = false;
}

while (!beset)
{
int i = (int) (Math.random () * 10);
int j = (int) (Math.random () * 10);
int oneTwo = (int) (Math.random () * 2);

if (oneTwo == 0)
{

if ((board [j] == ' ') && (j < 6))
{
if (board [j + 1] == ' ')
{
if (board [j + 2] == ' ')
{
if (board [j + 3] == ' ')
{
if (board [j + 4] == ' ')
{
for (int r = j ; r < j + 5 ; r++)
{
board [r] = 'A';
System.out.println (board [r]);

}
beset = true;
}
}
}
}
}
}
else
{
if ((board [j] == ' ') && (i < 6))
{
if (board [i + 1] [j] == ' ')
{
if (board [i + 2] [j] == ' ')
{
if (board [i + 3] [j] == ' ')
{
if (board [i + 4] [j] == ' ')
{
for (int r = i ; r < i + 5 ; r++)
{
board [r] [j] = 'A';
System.out.println (board [r] [j]);

}
beset = true;
}
}
}
}
}

}


// countA++;
// beset = true;
}
// else
//{
// countA = countA;

}


void display ()
{

System.out.println ("A B C D E F G H I J");

for (int i = 0 ; i < 10 ; i++)
{
for (int j = 0 ; j < 10 ; j++)

{
// System.out.println (board [j]);
switch (board [j])
{
case ' ':
System.out.print ("0 ");
break;

case 'S':
System.out.print ("S ");
break;

case 'A':
System.out.print ("A ");
break;

case 'B':
System.out.print ("B ");
break;

case 'X':
System.out.print ("X ");
break;
case 'M':
System.out.print ("M ");
break;

default:
System.out.println ("p ");

}

}

System.out.println ();

}
}


void askUser () throws IOException
{
System.out.println ();
System.out.println ("Welcome to Jasper's BattleShips! ! !");
System.out.println ();
System.out.println ("Your score will start at 100. ");
System.out.println ("The program will terminate if you reach a score below 0. Good Luck!");

int score = 100;

do
{
System.out.println ("Your current score is " + score);
System.out.println ("Please enter the location to hit! ! !");
System.out.print ("Column: ");
String columnIndex = input.readLine ().toLowerCase ();
char columnChar = columnIndex.charAt (0);
int column = 0;

switch (columnChar)
{
case 'a':
column = 0;
break;

case 'b':
column = 1;
break;

case 'c':
column = 2;
break;

case 'd':
column = 3;
break;

case 'e':
column = 4;
break;

case 'f':
column = 5;
break;

case 'g':
column = 6;
break;

case 'h':
column = 7;
break;

case 'i':
column = 8;
break;

case 'j':
column = 9;
break;

default:
System.out.print ("Please stay within the limits.");

}


System.out.print ("Row: ");
int rowInt = Integer.parseInt (input.readLine ());
int row = 0;

switch (rowInt)
{
case 1:
row = 0;
break;

case 2:
row = 1;
break;

case 3:
row = 2;
break;

case 4:
row = 3;
break;

case 5:
row = 4;
break;

case 6:
row = 5;
break;

case 7:
row = 6;
break;

case 8:
row = 7;
break;

case 9:
row = 8;
break;

case 10:
row = 9;
break;


}


switch (board [row] [column])
{
case ' ':
board [row] [column] = 'M';
score = score - 5;
display ();
break;

case 'A':
board [row] [column] = 'X';
score = score + 15;
display ();
break;


case 'B':
board [row] [column] = 'X';
score = score + 10;
display ();
break;


case 'S':
board [row] [column] = 'X';
score = score + 5;
display ();
break;

default:
System.out.print ("FOUT FOKKER!!!");


}

}



while (score > 0);
{
System.out.println ("Game Over");
}





/* if (board [row] [column] == ' ')
{
System.out.println ("Sorry, no hit!!");
board [row] [column] = 'X';

}
else
{
if (board [row] [column] == 'A')



}
}*/
}






/* if (board [row] [column] == ' ')
{
System.out.println ("Sorry, no hit!!");
board [row] [column] = 'X';

}
else
{
if (board [row] [column] == 'A')



}
}*/
}


// The "BattleShipsDriver" class.
import java.io.*;
public class BattleShipsDriver
{
public static void main (String [] args) throws IOException
{
BattleShips mainObj = new BattleShips ();
mainObj.blank ();
mainObj.place ();
mainObj.display ();
mainObj.askUser ();
// Place your code here
} // main method
} // BattleShipsDriver class
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
Sorry Koert, that's not going to happen. Folks here will help people learn Java, but not do their homework for them. If you have a specific problem, describe it along with solutions that you thought of but which didn't work, and we'll help you get going. But we won't help with a fill-in-the-blanks request.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

One hint: java.awt.GridLayout
Should be excellent for your "sea".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating simple GUI interface for BattleShips