|
| 10 Feb 2012 04:21 PM |
I need to make a Battleship game for a project in my Java class. I'm wondering if there's a better way to go about it than using two-dimensional arrays. I'm going to have the user be able to place their ships, rotating and moving them to their desire.
Basically, I just have a size field (number of spaces it occupies), a position field (array of integers of length two), and a direction field (string representing Left, Right, Up, and Down). I use a numeric for loop to determine if the ship is occupying a coordinate or if the ship cannot move to a desired location.
Any suggestions? |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 10 Feb 2012 04:36 PM |
| There's nothing wrong with using two-dimensional arrays for this. I just sincerely hope that you are hiding the arrays as a private variable in a class with methods that give you easy control over it. |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 10 Feb 2012 04:42 PM |
| Well battleship works off a grid-system so I can't see any other, efficient, way of doing it. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 04:42 PM |
Yes, of course. :P All the "sensitive" data is private. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 04:46 PM |
In my Java class, all we are doing is simple println things like printing our name and stuff. You're making Battleship.
Can I transfer to your class? |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 06:54 PM |
Haha.
To be honest, I'm going beyond the project requirements by creating a human vs pc Battleship. We were just supposed to make a Battleship game to sink all boats in the fewest amount of turns; there is no computer output in the project.
But I like AI. :) |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 10 Feb 2012 07:07 PM |
Lucky, my AP Computer science (Basically Java programming) is introductory level at best. :/
Although I did figure out how to write to files today... that was at least good. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 07:09 PM |
"Although I did figure out how to write to files today... that was at least good."
<3 I just got an idea. Game log!
File writing is very important. Which did you use, PrintWriter or FileWriter? |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 10 Feb 2012 07:18 PM |
PrintWriter.
I just dumped a bunch of random characters into a file. :/ Nothing useful yet. |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 10 Feb 2012 07:22 PM |
| My computer science class is where I do ALL my homework. My teacher knows I can program so she just lets me. Although on the final for the first semester, we were supposed to draw a snowman however, she expected a full-out 3D game from me. -_- |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 10 Feb 2012 07:24 PM |
| Yeah, I kinda regularly finish the week's work in half a period. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 07:24 PM |
Ah.
When using PrintWriter, you automatically overwrite the file, even if it exists. If you want to append to the file, you need to use a FileWriter.
FilerWriter fw = new FileWriter("FileName.txt", true); // Note, if you instantiate a FileWriter using false, it overwrites the file by default. PrintWriter fileOut = new PrintWriter(fw); // Notice the input is the FileWriter object, NOT a string.
You can also check to see if a file exists using the File object.
File theFile = new File("FileName.txt") if (theFile.exists()) { System.out.println("The file exists!"); }
You can also input a File object as the argument when instantiating a File/PrintWriter object. Remember, PrintWriter ALWAYS overwrites the existing file with a blank file.
File theFile = new File("FileName.txt"); FileWriter fw = new FileWriter(theFile, true); PrintWriter fileOut = new PrintWriter(fw);
fileOut.println("Appending to the old file!"); fileOut.close();
Don't forget to call the close method of the PrintWriter object before you exit the program. |
|
|
| Report Abuse |
|
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
| |
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 10 Feb 2012 07:27 PM |
@Agent
I'm sort of curious, how are you making battleship, Swing or something different? |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 07:35 PM |
Pure console. XD Will be the most epic console Battleship in the universe!
I don't know enough of the swing library yet. Otherwise, I WOULD use GUIs. |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 10 Feb 2012 07:40 PM |
Pure console battleship... XD
What could possibly go wrong? |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 10 Feb 2012 07:49 PM |
^ If it was in C++, you'd be amazed. T_T
As it's said, C++ is like shooting yourself in the foot. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 08:45 PM |
At least you guys HAVE programming classes. Our only 'programming' class is Web Page Design, where you learn barely any HTML, no Javascript or CSS. I seriously learned the first month's worth of HTML in 3 days. Then I went and learned Javascript and CSS in class while she explained to everyone how make text bold.
According to my teacher, I'm some sort of 'prodigy' who will be a professional at some point.
I'm not an excellent programmer, it's just that everyone else in my school only has enough computer knowledge to use Facebook :\
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 10 Feb 2012 09:12 PM |
"At least you guys HAVE programming classes."
The closest my school gets is a class to learn about MS Word. |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 10 Feb 2012 09:15 PM |
I think all my school has is powerpoint :/
Anyways this battleship game sounds pretty cool Maybe you should share it with us when you finish ^_^ |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 09:53 PM |
| UR ALL LUCKY I DONT EVEN HAVE A CLASS I HAZ TO LEARN FROM NOOBISH FW33 MAWDZ!!!!!!!!!(which helps......not all the time...) |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 10:03 PM |
| Ack, you guys are lucky. I'm in the most technically illiterate area of the world as far as I can see. I don't even think they have a class relating to technology within 100 miles of where I live. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 10:06 PM |
Just to clear things up about my class, it's a college class. I got the chance to take college classes during high school. :)
So yeah, it's not like I'm being taught at the high school. There's only one teacher at my school that knows ANYTHING about programming, and he's a math teacher. XD |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 10:10 PM |
"Maybe you should share it with us when you finish ^_^"
You'll have to download a Java IDE like Eclipse or NetBeans (both are really nice, but NetBeans takes a while to install, but I had to also get the full JDK for the NetBeans program). Both programs can run off a flash drive, and you CAN put the Java stuff on the flash drive as well. This enables use on ANY compatible computer in the operating system you use.
I can post the source code here after it has been graded by the professor. :P |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 10:10 PM |
Oh :o Cool. However, I still feel like I'm in a technically illiterate area..I'm moving to Canada when I'm older. They have a better educational system there. My friend who lives there is currently in high school, and they have a programming class. Apparently programming classes are more common than not there.
Not to offend any hardcore patriots of America, but Canada has a way better educational system as far as I'm concerned. |
|
|
| Report Abuse |
|
|