Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Minor Question About Java  (Read 1245 times)

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Minor Question About Java
« on: December 04, 2010, 05:54:45 pm »

-snip-
« Last Edit: June 18, 2015, 02:15:16 pm by Bauglir »
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Minor Question About Java
« Reply #1 on: December 04, 2010, 06:02:17 pm »

As far as I know you can't really do that. However, may I ask why you're unable to use foo.lenght (with foo the name of the array) as the upper bound of a loop?
Logged

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: Minor Question About Java
« Reply #2 on: December 04, 2010, 06:06:10 pm »

-snip-
« Last Edit: June 18, 2015, 02:15:58 pm by Bauglir »
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Minor Question About Java
« Reply #3 on: December 04, 2010, 06:31:42 pm »

You most definitely can't do that in Java. The names of the different variables (your battleships) have to be known at compile-time, or else your program won't compile.


Also, if you would be some kind as to post the code I'll have a quick look at it. Maybe I can figure out if it's possible to salvage the code, though it might just as well be spaghetti code, knowing the way most profs write example programs for students.
Alternatively, if he's using Swing for the gui, I might be able to salvage it so you can add your own back end, if that'd be easier for you.
« Last Edit: December 04, 2010, 06:34:01 pm by Virex »
Logged

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: Minor Question About Java
« Reply #4 on: December 04, 2010, 06:40:40 pm »

-snip-
« Last Edit: June 18, 2015, 02:16:10 pm by Bauglir »
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Minor Question About Java
« Reply #5 on: December 04, 2010, 06:54:12 pm »

I take it you're bound to the existing code? Because it'd probably help both you and your grip of the language if you'd just rewrite the whole damn thing, keeping only the GUI (and if you really want to I can probably help you rewire that too). I also assume you're pretty much bound to primitive types (no HashMaps, Arraylists and other fancy stuff?)


That looks ugly as hell and unintuitive. I'd just've made a constructor for the battleship that accepts a 1-d array. Then you can call it with

Code: [Select]
ships[i] = new Ship(iArray[i]); //Why not use shipData or another descriptive name instead of iAray here?
That is, assuming it is not possible to make the objects on the fly while parsing the data file, because in that case I would've just parsed one line of the file (probably into a String, because I'm lazy), fed that into the constructor of the ship (which would pick the String apart and set the variables) and then read the next line.
« Last Edit: December 04, 2010, 06:57:50 pm by Virex »
Logged

Shurikane

  • Bay Watcher
    • View Profile
    • http://www.shurikane.com
Re: Minor Question About Java
« Reply #6 on: December 04, 2010, 06:57:37 pm »

Based on what you're trying to do at least as far as I understand, you might be able to do what you seek to do using either of these:

Code: [Select]
List<Something> myThings = new ArrayList<Something>();
This above does pretty much the same job as a basic ol' array but it has nifty tools in it.

Code: [Select]
Map<String, Something> myThings = new HashMap<String, Something>();
Read up on maps a bit and you will find a piece where I think you can supply two keys to lead to one entry (such as X-Y coordinates!)  Otherwise, you could always make up a coordinates convention that fits into a single key variable and use the version above.

In the end, you want your method to return a list or a map and then you can work off that.  The list has the ability to know how many elements it has in it.  The map, I don't remember off the top of my head.

CAUTION: Check with your teacher to make sure you are allowed to use these above tools in the exercise you are doing, if you choose to use them.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Minor Question About Java
« Reply #7 on: December 04, 2010, 07:00:21 pm »

Havn't found a way to give HashMap 2 keys yet. You could probably just write a little function to combine the coordinates into a single string though, or just use the Point class (can't remember what you need to include for that, but if you're using netbeans it'll tell you).


Edit: Hashmap also knows it's size.
« Last Edit: December 04, 2010, 07:01:58 pm by Virex »
Logged

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: Minor Question About Java
« Reply #8 on: December 04, 2010, 07:19:38 pm »

-snip-
« Last Edit: June 18, 2015, 02:16:51 pm by Bauglir »
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

eerr

  • Bay Watcher
    • View Profile
Re: Minor Question About Java
« Reply #9 on: December 07, 2010, 02:34:07 am »

If I remember correctly, most array types in java can be extended.
Logged