Results 1 to 20 of 60
Thread: Help with Battleship Game
- 04-04-2012, 07:11 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Help with Battleship Game
I've been messing around with Java lately and tried making a Battleship game, just text for now but will eventually make a GUI. I did the classic bad action of coding and not frequently testing. I've ran through my code and it all makes sense and seems like it should work, but nothing happens when i try running Battlescreen. NO errors are appearing in Eclipse either. Can anyone spot my errors? I feel like it has something to do with my separating of classes, but i'm really at a loss of words.
Ship
BattleScreenJava Code:package battleship; import java.util.Random; import ljing.*; public class Ship { private int size; String name; public Ship(int s, String string){ size = s; name = string; } public void placeShip(int [][]a) { Random r = new Random(); int orientation = r.nextInt(2); if(orientation == 0)//Vertical Ship { int x = r.nextInt(10); int y = r.nextInt(10-size); for(int z=0;z<size;z++) { a[y+z][x] = 1; } } else//Horizontal Ship { int x = r.nextInt(10-size); int y = r.nextInt(10); for(int z=0; z < size; z++) { a[y][x+z] = 1; } } } }
runJava Code:package battleship; import java.util.Random; import java.util.Scanner; import ljing.*; public class BattleScreen { public static void main(String [] args){ int[][]myfield = new int [10][10]; int[][]compfield = new int [10][10]; int[][]pos = new int [10][10]; Board board = new Board(myfield); Board comboard = new Board(compfield); int hit =0; int comphit=0; Ship boat = new Ship(2,"Boat"); boat.placeShip(myfield); boat.placeShip(compfield); Ship cruise = new Ship(3,"Cruise"); cruise.placeShip(myfield); cruise.placeShip(compfield); Ship sub = new Ship(4,"SubMarine"); sub.placeShip(myfield); sub.placeShip(compfield); Ship battle = new Ship(5,"Battleship"); battle.placeShip(myfield); battle.placeShip(compfield); Ship air = new Ship(6,"AirCraftCarrier"); air.placeShip(myfield); air.placeShip(compfield); while (hit <20 && comphit<20){ board.displayField(myfield); comboard.displayField(compfield); run.attack(pos,compfield); run.attack(pos,myfield); } if(hit==20) System.out.println("YOU WIN!"); if(comphit==20) System.out.println("COMPUTER WIN!"); } }
BoardJava Code:package battleship; import java.util.Random; import java.util.Scanner; import ljing.*; public final class run { int hit; int comphit; public run() { } public static void attack(int[][] a, int[][] b) { int row = 0; int col = 0; int value = 0; int hit = 0; { Scanner reader = new Scanner(System.in); System.out.println("X space Y of target"); row = reader.nextInt(); col = reader.nextInt(); if (row <= 0 || col <= 0 || row > 10 || col > 10) { System.out.println("coordinates out of board. Use different value"); System.out.println(); } while (row <= 0 || col <= 0 || row > 10 || col > 10) ; if (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { System.out.println("You already used this coordinate! Use another one!"); } } while (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { if (a[col - 1][row - 1] == 1) { b[col - 1][row - 1] = 3; System.out.println("Hit!"); hit++; } else { b[col - 1][row - 1] = 2; System.out.println("Missed!"); } } } public static int randomrow() { Random r = new Random(); int row = r.nextInt(10); return row; } public static int randomcol() { Random r = new Random(); int col = r.nextInt(10); return col; } public static void compattack(int[][] a, int[][] b) { int row = 0; int col = 0; int value = 0; int comphit = 0; { randomrow(); randomcol(); if (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { randomrow(); randomcol(); } } while (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { if (a[col - 1][row - 1] == 1) { b[col - 1][row - 1] = 3; System.out.println("Hit!"); comphit++; } else { b[col - 1][row - 1] = 2; System.out.println("Missed!"); } } } }
Java Code:package battleship; import java.util.Random; import java.util.Scanner; import ljing.*; public class Board { int row; int column; Scanner reader = new Scanner(System.in); int size = reader.nextInt(); public Board(int[][] a) { for (int col = 1; col < a.length; col++) { for (int row = 1; row < a.length - 1; row++) { a[row][col] = 0; } } } public void displayField(int[][] a) { System.out.println(" Battle Ship!"); System.out.println(" ----------------"); System.out.println(" "); System.out.println(" 1 2 3 4 5 6 7 8 9 10"); System.out.println(" -----------------"); int rownum = 1; for (int row = 0; row < a.length - 1; row++) { System.out.print(rownum + " |"); rownum++; for (int col = 0; col < a[0].length - 1; col++) { System.out.print(" " + a[row][col]); } System.out.println(); } } }
- 04-04-2012, 08:54 PM #2
Re: Help with Battleship Game
Try debugging the code by adding lots of printlns to show the values of variables as they are changed and used and to how the execution flow. The printed output will show what the computer sees and help you find the problem.
If you don't understand my response, don't ignore it, ask a question.
- 04-05-2012, 07:41 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
I dont even really know where tot start with the print lines. I run battlescreen but am confused as to why nothing at all happens. Should my run.java be within battlescreen or should it remain a separate class?
- 04-05-2012, 07:58 PM #4
Re: Help with Battleship Game
You should print the values of the variables that are used in the program every time they are changed and when they are used to make decisions. Foe example after line 26 print out the values of row and column.
At line 44 print out the value of: a[col - 1][row - 1]
What is the lonely ; on line 33 for?
What is the { on line 22 and the } on 38 for?
Put a println at the beginning of EVERY method and constructor to show you when they have been executed. Print out the values of any parameters passed to the methods.Last edited by Norm; 04-05-2012 at 08:13 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-06-2012, 06:45 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
I fixed the fields in the battlescreen class, but now am getting an error of making a static reference to a non static field for myfield and compfield. It doesnt seem to run past the fields and doesn't enter the first loop.Java Code:package battleship; import java.util.Random; import java.util.Scanner; import ljing.*; public class BattleScreen{ private int[][] myfield = new int [10][10]; private int[][] compfield = new int [10][10]; private int[][] pos = new int [10][10]; static Board board = new Board(myfield); Board comboard = new Board(compfield); static int hit =0; static int comphit=0; public static void main(String [] args){ Ship boat = new Ship(2,"Boat"); boat.placeShip(myfield); boat.placeShip(compfield); Ship cruise = new Ship(3,"Cruise"); cruise.placeShip(myfield); cruise.placeShip(compfield); Ship sub = new Ship(4,"SubMarine"); sub.placeShip(myfield); sub.placeShip(compfield); Ship battle = new Ship(5,"Battleship"); battle.placeShip(myfield); battle.placeShip(compfield); Ship air = new Ship(6,"AirCraftCarrier"); air.placeShip(myfield); air.placeShip(compfield); while (hit <20 && comphit<20){ System.out.println ("Entering game Loop"); board.displayField(myfield); comboard.displayField(compfield); run.attack(pos,compfield); run.attack(pos,myfield); } if(hit==20) System.out.println("YOU WIN!"); if(comphit==20) System.out.println("COMPUTER WIN!"); } }
- 04-06-2012, 06:51 PM #6
Re: Help with Battleship Game
Move all the code in the main() method to the class constructor. Have the main() call the constructor:getting an error of making a static reference to a non static fieldThen nothing will be done from a static context.Java Code:public static void main(String [] args){ new BattleScreen(); }Last edited by Norm; 04-06-2012 at 06:54 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-07-2012, 06:40 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
So this is a program i've been trying to debug for awhile, i think i started it when i was way young, about 8 or 9 years ago, and just found it and wanted to try to complete it.However, i am not really understanding why i put down what i did for the attack method within run.java. ill try to walk through it below...
What was this here for?Java Code:public static void attack(int[][] a, int[][] b) { int row = 0; int col = 0; int value = 0; int hit = 0; { Scanner reader = new Scanner(System.in); System.out.println("Attack: X and Y coordinate in one command"); row = reader.nextInt(); col = reader.nextInt(); if (row <= 0 || col <= 0 || row > 10 || col > 10) { System.out.println("coordinates out of board. Use different value"); System.out.println(); }
and how does this logic make sense?Java Code:// while (row <= 0 || col <= 0 || row > 10 || col > 10)
Same Questions here...Java Code:if (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { System.out.println("You already used this coordinate! Use another one!"); } }
Ok, so pretty much i don't get any of the statements made by each of the methods, and i'm not sure where they came from. Any input would be great as i'm lost on this.Java Code:while (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { if (a[col - 1][row - 1] == 1) { b[col - 1][row - 1] = 3; System.out.println("Hit!"); hit++; } else { b[col - 1][row - 1] = 2; System.out.println("Missed!"); } } }
- 04-07-2012, 01:22 PM #8
Re: Help with Battleship Game
One big problem you have is the many different variables with the same name in separate classes. They are all different variables and are not shared. For example if you change the value of row in one class, none of the values of row in any of the other classes will be changed.
You need to think through how you want those variables used and where their values should be keep.
From your questions about this program I suggest that you leave it for a while and work on simpler programs to build up some knowledge. Trying to get this large program with many classes is going to be very frustrating for you.
Or if you can find a local mentor to help it would be useful. There is way to many things to begin trying to help you understand and fix with this program.If you don't understand my response, don't ignore it, ask a question.
- 04-08-2012, 06:51 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
How would i restructure this to make it update the board via toString method? is this the better way to go? Or what should i fix first structurally that would be an easy beneficial fix. and also how would i rename the variables and what advantage would it give me?
- 04-08-2012, 06:59 PM #10
Re: Help with Battleship Game
The toString() method is mainly used for reporting the contents of a class. Often very useful when debugging.
Its not used for doing updates to a class.
Your program's class have variables with the same name like row, col and hit. It looks like there should only be one variable for each that all the classes use. You need to look at how each of those variables are used and see if they should have only one value used by all the classes or if each class can have its own version. Renaming them to be unique names could help you realize what they are used for.If you don't understand my response, don't ignore it, ask a question.
- 04-08-2012, 07:16 PM #11
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
So i am trying to make it so it doesn't display the computer's ship location. But, i can not think of how to construct the if statement to check and see if the placeShip method in Ship class is the compfield. In that case i want it to do nothing or display 0 instead of a 1.
obviously, if (placeship(compfield) )does not work.
- 04-08-2012, 07:30 PM #12
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
Also, want to declare hit as a public variable, and want to be able to change it from any class. How do i call it within another class to increase it from attack? this.hit = hit?
- 04-08-2012, 07:48 PM #13
Re: Help with Battleship Game
Use getter and setter methods to control access to the hit variable.
If you don't understand my response, don't ignore it, ask a question.
- 04-08-2012, 08:31 PM #14
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
Thanks for the help so far Norm, really appreciate it, and am starting to get the hang of everything once again. I feel a little iffy with the getter and setter methods i just created mainly because Eclipse didn't like them first because i was making a static reference to a nonstatic method. Should my code look like the following:
Or can they not be static? It somehow doesn't feel like Eclipse made the correct adjustments. Or should i make a game instance of Battleship like make a field in the run classJava Code:public class Battleship { private int[][] myfield = new int[10][10]; private int[][] compfield = new int[10][10]; private int[][] pos = new int[10][10]; Board board = new Board(myfield); Board comboard = new Board(compfield); static int hit = 0; static int comphit = 0; public int gethits() { return hit; } public static void sethits() { hit ++; } public int getcomphits() { return comphit; } public static void setcomphits(){ comphit ++; }and then call everything from Battleship game.method(); , game.sethits(); etc..Java Code:/** The game itself. */ private Battleship game;
Last edited by Lucid15; 04-08-2012 at 08:35 PM.
- 04-08-2012, 08:50 PM #15
Re: Help with Battleship Game
Personally, I'd get rid of all the static usages in the program.
For testing I'd have the computer play both sides. Make the display of the boards dependent on changes to hit and comphit. Save the last value and compare the new to the last, if no change. don't display the boards. Initialize the first values to -1 so the first time the boards will be displayedIf you don't understand my response, don't ignore it, ask a question.
- 04-08-2012, 09:30 PM #16
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
SO i initialize the game instance as shown here. But when i try to make a reference to it, it gives me the static reference error whenever game.method is shown.Java Code:package battleship; import java.util.Random; import java.util.Scanner; import static ljing.Program.*; public final class run { private Battleship game; public static void attack(int[][] a, int[][] b) { int arow = 0; int acol = 0; int value = 0; { Scanner reader = new Scanner(System.in); System.out.println("Attack: X and Y coordinate in one command"); arow = reader.nextInt(); acol = reader.nextInt(); if (arow <= 0 || acol <= 0 || arow > 10 || acol > 10) { System.out.println("coordinates out of board. Use different value"); System.out.println(); } System.out.println("Entering"); while (arow <= 0 || acol <= 0 || arow > 10 || acol > 10){ if (b[acol - 1][arow - 1] == 2 || b[acol - 1][arow - 1] == 3) { System.out.println("You already used this coordinate! Use another one!"); } if (a[acol - 1][arow - 1] == 1) { b[acol - 1][arow - 1] = 3; game.setHits(); System.out.println("Hit!"); } else { b[acol - 1][arow - 1] = 2; System.out.println("Missed!"); } } } } public static int randomrow() { Random r = new Random(); int row = r.nextInt(10); return row; } public static int randomcol() { Random r = new Random(); int col = r.nextInt(10); return col; } public static void compattack(int[][] a, int[][] b) { int row = 0; int col = 0; int value = 0; int comphit = 0; { randomrow(); System.out.println(row); randomcol(); System.out.println(col); if (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { randomrow(); randomcol(); } } while (b[col - 1][row - 1] == 2 || b[col - 1][row - 1] == 3) { if (a[col - 1][row - 1] == 1) { b[col - 1][row - 1] = 3; System.out.println("Hit!"); game.setCompHits(); } else { b[col - 1][row - 1] = 2; System.out.println("Missed!"); } } } }
- 04-08-2012, 09:33 PM #17
Re: Help with Battleship Game
Please post the full text of the error messageit gives me the static reference error
As I said before, I'd get rid of all the static usages in the program.If you don't understand my response, don't ignore it, ask a question.
- 04-08-2012, 09:41 PM #18
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
when hovering over game.setCompHits() or the like, i get this..
how would i get rid of all the static references? isn't that what i am trying to do now, by creating getters and setters?Cannot make a static reference to the non-static field game
- 04-08-2012, 09:49 PM #19
Re: Help with Battleship Game
Do an edit on all the classes and Replace the String "'static" with ""how would i get rid of all the static references
The one exception is the main() method which must be staticLast edited by Norm; 04-08-2012 at 09:57 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-08-2012, 10:20 PM #20
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Help with Battleship Game
Did that, removed all of the static Strings, and now get the same static reference error on Battleship.java (previously BattleScreen, decided to rename it) at:
Also, another problem i have is when i enter the compattack method, after inputting my or the human attack in run.java, i get the following error:Java Code:while (hit < 20 && comphit < 20) { System.out.println(" Your Field"); System.out.println(""); board.displayField(myfield); System.out.println(" Computer Field"); System.out.println(""); comboard.displayField(compfield); run.attack(pos, compfield); <------ERROR HERE run.compattack(pos, myfield);<------AND HERE }
Here is my run class as of now, I marked where the error occurs:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at battleship.run.compattack(run.java:81)
at battleship.Battleship.run(Battleship.java:65)
at battleship.Battleship.main(Battleship.java:33)
Java Code:package battleship; import java.util.Random; import java.util.Scanner; import static ljing.Program.*; public final class run { private Battleship game; // a=pos b=myfield, or compfield public void attack(int[][] a, int[][] b) { int arow = 0; int acol = 0; int value = 0; { Scanner reader = new Scanner(System.in); System.out.println("Attack: X and Y coordinate in one command"); arow = reader.nextInt(); acol = reader.nextInt(); if (arow <= 0 || acol <= 0 || arow > 10 || acol > 10) { System.out.println("coordinates out of board. Use different value"); System.out.println(); //need to start loop attack method over again and not "lose a turn" } if (b[acol - 1][arow - 1] == 2 || b[acol - 1][arow - 1] == 3) { System.out.println("You already used this coordinate! Use another one!"); } if (b[acol - 1][arow - 1] == 1) { b[acol - 1][arow - 1] = 3; // game.setHits(); System.out.println("Hit!"); } else { b[acol - 1][arow - 1] = 2; System.out.println("Missed!"); } } } public int randomrow() { Random r = new Random(); int randrow = r.nextInt(10); return randrow; } public int randomcol() { Random r = new Random(); int randcol = r.nextInt(10); return randcol; } public void compattack(int[][] a, int[][] b) { int row = 0; int col = 0; { System.out.println("Entering random row comp section"); <----ERROR HAPPENS DIRECTLY AFTER THIS IS PRINTED TO THE CONSOLE----> randomrow(); randomcol(); if (b[randomcol() - 1][randomrow() - 1] == 2 || b[randomcol() - 1][randomrow() - 1] == 3) { System.out.println("You already used this coordinate! Use another one!"); } if (b[randomcol() - 1][randomrow() - 1] == 1) { b[randomcol() - 1][randomrow() - 1] = 3; System.out.println("Entering Compattack hit method"); System.out.println("Hit!"); // game.setCompHits(); } else { b[col - 1][row - 1] = 2; System.out.println("Comp Missed"); System.out.println("Missed!"); } } } }
Similar Threads
-
Battleship game - arrays
By makig in forum New To JavaReplies: 9Last Post: 11-17-2011, 07:21 PM -
Battleship Game - really need help
By thrashsynergy in forum Java AppletsReplies: 5Last Post: 04-25-2011, 07:21 PM -
Battleship game
By kathyla18 in forum New To JavaReplies: 2Last Post: 02-26-2009, 09:42 PM -
Java Battleship Game Help PLEASE
By mars_red in forum New To JavaReplies: 0Last Post: 02-12-2008, 01:09 AM -
Java BattleShip game help
By mars_red in forum Advanced JavaReplies: 0Last Post: 02-12-2008, 12:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks