Koert Kopbeen

Greenhorn
+ Follow
since Oct 30, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Koert Kopbeen

Hey

Ja sorry I made a mistake - just two questions.

I am referring to normal Strings in Java. I have seen that it is possible to change the font (I use Ready to Program - the normal font of the code is Courier New: I believe that is the regular font across platforms) in Buttons etc, but I want a certain part of the String to stand out. How do I do that - let's say change a part of the program to Times New Roman or something.

The timer: is there not maybe a method in Java to do it? I would like to display the counted time in the program. How do I do that?

OK, so maybe I do have a third question. Again about Strings: how do I make a certain part of text flicker?

And a fourth one: Whenever I use PrintWriter of the java.io class, it erases the contents of the .txt file completely. No matter what I type in there, it erases it. Any ideas? Anyone had this same problem?

Cool people!

Koert

PS: Mag dinge vir julle net goedgaan - anders as met myself wannneer dit kom by Rekenaarstudie HG!
18 years ago
Hey

I have three questions:

1) How do I change the font of Strings in Java?

2) How do you create a countdown timer (let's say you coded a game and it then says: "The program will exit in 10 seconds if you dont enter a choice") in Java?

Thanks
18 years ago
Hey everyone

I posted the program three times already - each time with great help: thanks. But I really need you guys to help me out NOW. I have created a simple BattleShips program, consisting of a 2X2 array, with 10 spaces. It displays the board, with the column number (char) above it and the row number (int) next to it. The program works just fine as it is, but I really want to add some GUI to it. I have imported GridLayout - java.awt - (if you run the class you will see how it looks) but I don't know how to implement it into my BattleShips.

I basically need to shadow the spaces of the array (whether it contains a value or not) with GridLayout to allow the user to click on the space to show the value of the array.

A miss (or blank space) = 'M' and all other values (ship, battleship, aircraft carrier) = 'X' (for a hit).

Guys I really need you to help me out with it. Can anybody place GridLayout over the spaces of the array?

Thanks. The code follows:



18 years ago
Hey everyone

I sent in a previous post (Creating GUI for BattleShips) and I tweaked my program. it's basically perfect without the GridLayout (which I found and would work PERFECTLY if I knew how to use it. I imported it, but I really don't know what to do. Anyone wanna help me out? As I said previously, the GUI can be VERY simple, it's the idea that counts.

Come on, guys, PLEASE help me!

Jasper

The code follows! Please guys ASAP!!


18 years ago
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
18 years ago
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
18 years ago