Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007, 09:33 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
need urgent help...
Hello... I'm not entirley new to Java but I have some problems with an assignment that I'm doing. I hope that you guys can help me with a little bit.
We are asked to write a battleship game. We are asked to enter the location of four ships: a BattleShip, Destroyer, Cruiser & Tug. Theres 2 boards for the game.. A challenger board and a program board.
Once the ships have been placed the game boards should be shown as follows. Notice the challenger's ship locations are shown, but the locations of the program's ships are hidden and it is the user's task to try and sink them. (While the program is shooting back.). The user should be prompted for the co-ordinate of the bow (top leftmost square) of the ship and the direction which can be vertical "V" or horizontal "H". For example:

The game should be something like this:

Challenger Program
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0~ ~ ~ D D D D ~ ~ ~ 0~ ~ ~ ~ ~ ~ ~ ~ ~ ~
1~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 1~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 2~ ~ ~ ~ ~ ~ ~ ~ ~ ~
3~ ~ ~ ~ B ~ ~ C C C 3~ ~ ~ ~ ~ ~ ~ ~ ~ ~
4~ ~ ~ ~ B ~ ~ ~ ~ ~ 4~ ~ ~ ~ ~ ~ ~ ~ ~ ~
5~ ~ ~ ~ B ~ ~ ~ ~ ~ 5~ ~ ~ ~ ~ ~ ~ ~ ~ ~
6~ ~ ~ ~ B ~ ~ ~ ~ ~ 6~ ~ ~ ~ ~ ~ ~ ~ ~ ~
7~ ~ ~ ~ B ~ ~ ~ ~ ~ 7~ ~ ~ ~ ~ ~ ~ ~ ~ ~
8~ T T ~ ~ ~ ~ ~ ~ ~ 8~ ~ ~ ~ ~ ~ ~ ~ ~ ~
9~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 9~ ~ ~ ~ ~ ~ ~ ~ ~ ~

----------------------------------------------------------

Anyway...
I've done this so far... but the code below shows only one board. I need to
show to boards.. one for the program and one for the challenger.
And I need to know how to enter the vertical/horizontal co-ordinates.. what code shoud I add.

The code is aliitle bit messy.. but any help would be really appreciated.
Thanks alot in advance.


-------------------------------------------------------------
Code:
public class BattleshipGame { //This class represents the game board and the rules of the game private char[][] board; private int numberOfMoves; //keeps track of the number of moves public final static int MOVE_MISS = 0; public final static int MOVE_HIT = 1; public final static int MOVE_INVALID = 2; public final static String[] ResultMessages = {"Miss", "Hit!", "Invalid Move"}; public BattleshipGame() { board = new char[10][10]; initializeBoard(); placeShip(4, true, 'B'); // battleship placeShip(3, false, 'D'); // destroyer placeShip(2, true, 'C'); // cruiser placeShip(1, false, 'T'); // tug numberOfMoves = 0; } public void initializeBoard() { // This sets all the squares to be waves for (int i = 0; i < board.length; ++i) { for (int j = 0; j < board[i].length; ++j) { board[i][j] = '~'; } } } public void placeShip(int shipSize, boolean xOriented, char symbol) { // Places a ship of size shipSize, // either along the x or y axis (no diagonal ships) int startx = 0, starty = 0; int x, y; if (xOriented) { x = 1; y = 0; } else { x = 0; y = 1; } boolean goodSpot = false; while (!goodSpot) { // Choose a random spot to put the end of the ship startx = (int) (Math.random() * (board.length - shipSize*x)); starty = (int) (Math.random() * (board[0].length - shipSize*y)); goodSpot = true; // Verify that nothing else is already placed in any of the squares you need for (int i=0; i < shipSize; ++i) { if (board[startx+(i*x)][starty+(i*y)] != '~') { goodSpot = false; } } } for (int i=0; i < shipSize; ++i) { board[startx+(i*x)][starty+(i*y)] = symbol; } } public void printGameBoard(boolean showShipPositions) { //This method displays the game board //if showShipPositions is true the positions of //the ships are also shown String output = "Moves: " + numberOfMoves + "\n\n"; System.out.println(output); output = " "; for (int i = 0; i < board.length; ++i) { output = output + i + " "; } System.out.println(output); for (int i = 0; i < board.length; ++i) { output = "" + i + " "; for (int j = 0; j < board[i].length; ++j) { if (board[i][j] == '.' || board[i][j] == '*') { output += board[i][j] + " "; } else if(showShipPositions){ //show the positions of the ships output += board[i][j] + " "; } else{ output += '~' + " "; } } output += "\n"; System.out.println(output); } } public int move(int x, int y) { numberOfMoves++; // check to see if the coordinates are out of bounds if (x >= board.length || x < 0 || y >= board[0].length || y < 0) { return MOVE_INVALID; } // see if it's a miss if (board[x][y] == '~' || board[x][y] == '.') { board[x][y] = '.'; return MOVE_MISS; } else { //it must be a hit board[x][y] = '*'; return MOVE_HIT; } } public boolean isWon() { //determine if the game is won yet boolean won = true; for (int i = 0; i < board.length; ++i) { for (int j = 0; j < board[i].length; ++j) { if (board[i][j] != '~' && board[i][j] != '*' && board[i][j] != '.') { won = false; } } } return won; } }

Last edited by JavaBean : 11-12-2007 at 12:33 PM. Reason: Code placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-13-2007, 08:52 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
public void printGameBoard(boolean showShipPositions) { //This method displays the game board //if showShipPositions is true the positions of //the ships are also shown String output = "Moves: " + numberOfMoves + "\n\n"; System.out.println(output); output = " "; for (int i = 0; i < 2*board.length; ++i) { output = output + (i%board.length) + " "; } output += "\n -"; for (int i = 0; i < 2*board.length-1; ++i) { output = output + "--"; } System.out.println(output); for (int i = 0; i < board.length; ++i) { output = "" + i + " |"; for (int j = 0; j < 2*board[i].length; ++j) { if(j > board[i].length-1) { output += '~' + " "; } else { if (board[i][j] == '.' || board[i][j] == '*') { output += board[i][j] + " "; } else if(showShipPositions) { //show the positions of the ships output += board[i][j] + " "; } else { output += '~' + " "; } } } // output += "\n"; System.out.println(output); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hi, need some urgent help! jdark New To Java 2 04-18-2008 07:50 AM
Help me ...urgent! googgoo New To Java 7 04-05-2008 09:46 AM
please help urgent ananas7777 AWT / Swing 2 12-25-2007 09:30 AM
NEED urgent help... bezdapez New To Java 7 11-27-2007 05:40 PM
Very Urgent Swamipsn New To Java 2 08-01-2007 02:56 PM


All times are GMT +3. The time now is 10:31 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org