• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Creating simple GUI interface for BattleShips

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Like the previous poster, I am also from South Africa, and I haven't done GUI either yet. Difference is that I'm in gr. 11 (which makes my post a bit more urgent, doesn't it?!)

We had to create a BattleShips prog. (which I did 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.

If you can maybe help me tweak the program (I don't know how to go any further) 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!):


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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would start with Sun's Swing tutorial...

http://java.sun.com/docs/books/tutorial/uiswing/index.html
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post the same topic to multiple forums. As our FAQ, How To Ask Questions On Java Ranch, states, Carefully Choose One Forum. Crossposting can cause confusion and duplication of effort.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
I would start with Sun's Swing tutorial...

http://java.sun.com/docs/books/tutorial/uiswing/index.html



I think they cover how to change from an app to a GUI in that same tutorial. If you have a specific question or problem, please post again.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn de Queiroz:
...I think they cover how to change from an app to a GUI in that same tutorial...


I expect so. Typically, the logical part of the code (already written here) would be considered the "model," and the GUI would be the "view."

Here's a good intro to the "Model-View-Controller" pattern...

http://csis.pace.edu/~bergin/mvc/mvcgui.html
reply
    Bookmark Topic Watch Topic
  • New Topic