|
I'm creating a simple game, main algorithm for the game is in a main method along with this additional method on how to choose the player difficulty:code:
|
# ? Dec 6, 2010 13:41 |
|
|
# ? Apr 25, 2024 00:56 |
|
What language are you coming from? It is fairly clear that you are not "thinking in Java".
|
# ? Dec 6, 2010 14:51 |
|
You probably want something more like...code:
epswing fucked around with this message at 16:07 on Dec 6, 2010 |
# ? Dec 6, 2010 15:58 |
|
Recently I've begun taking classes at my university in Java, which is my first language, so I have a question that's probably pretty simple. We just started covering MVC architecture, and in a project we're doing I have to create a class "Tile" that extends JButton, and then in my GUI create a 9x9 2D array of Tile objects to be displayed as a big square of buttons, and then using a set of radio buttons and a combo box I can set the background and the foreground of the tiles. Holding the Color information of the tiles I have another array of a class, TileInfo. My problem is that my Tile class is my view, the main GUI class is the controller, and the TileInfo is the model, but when my ActionListener for the tiles is called, I don't know how I can make my program differentiate between different objects in the Array without creating 81 if statements, and then after that be able to reference the TileInfo object that corresponds to the Tile.
|
# ? Dec 6, 2010 18:33 |
|
epswing posted:You probably want something more like... An enum for difficulty, stored as an attribute somewhere it makes sense.
|
# ? Dec 6, 2010 19:34 |
|
His program might depend on the implementation of each player class...
|
# ? Dec 6, 2010 19:41 |
|
epswing posted:His program might depend on the implementation of each player class... ...so it should be encapsulated inside the player class itself to avoid doing extra conditionals in various places. Well OK, "player" as a class concept for difficulty most likely isn't the correct place for that code but anyways.
|
# ? Dec 6, 2010 19:50 |
|
I don't presume to know how you're building your program. I'm just showing you a way to create and return a new Player, because your original problem was scope.
|
# ? Dec 6, 2010 19:54 |
|
Puppet Pal Claudius posted:Recently I've begun taking classes at my university in Java, which is my first language, so I have a question that's probably pretty simple.
|
# ? Dec 7, 2010 01:02 |
|
I have created an SSL socket server, I think correctly using code similar to:code:
I have created a certificate using: keytool -genkey -keystore mySrvKeystore -keyalg RSA I have code else where that starts a listener and accepts when a connection is made. I started up the java server with the additional vm params: -Djavax.net.ssl.keyStore=<the store filename> -Djavax.net.ssl.keyStorePassword=<the password> I created a quick java client to connect (and have also used openssl for testing). I see on the server side the connection made, but the client: 1. Cannot retrieve the certificate for viewing 2. Writes data on the socket, but nothing appears on the server side What am I missing?
|
# ? Dec 7, 2010 17:34 |
|
You need to use the certificate you created in your client and add it to the truststore for the SSLConnectionFactory or whatever it's called before trying to connect to the server. Or you can disable the check for the server's certificate but that's not a good idea in the real world. Oh also make sure that your certificate includes your hostname in the common name field.
|
# ? Dec 7, 2010 17:50 |
|
I am trying to create a server and client java classes that communicate through socket. I can create a connection, however when I attempt to pass data I get a null pointer. I can successfully connect, however I am having some problems passing messages back and fourth. code:
code:
|
# ? Dec 7, 2010 21:32 |
|
Where (which line) does the NPE occur? You're not showing us enough code. Show us the sendMessage method, and the rest of the body of your receiveMessage method.
|
# ? Dec 7, 2010 21:43 |
|
send method was taken from the fallowing, http://zerioh.tripod.com/ressources/sockets.html This seems to be doing what I need it to on the client side. code:
code:
'System.out.println("server>" + tempInt);' DholmbladRU fucked around with this message at 22:49 on Dec 7, 2010 |
# ? Dec 7, 2010 21:47 |
Instead of: while(s.nextLine() != null) don't you want: while (s.hasNextLine())
|
|
# ? Dec 7, 2010 22:50 |
|
thanks got that all figured out. havnt programed any java in awhile.
|
# ? Dec 8, 2010 00:29 |
|
I am trying to use a priority queue to hold objects for a game I and some others are creating, as such:code:
|
# ? Dec 8, 2010 22:10 |
|
Trick Question posted:I am trying to use a priority queue to hold objects for a game I and some others are creating... Doesn't look like the problem is here, what method are you using to remove something code:
code:
If you override the .equals() and .hashCode() method of your card class the above code will work, but instead consider only instantiating one card object per card. As for the sorting, does this line of the PriorityQueue javadocs apply to you: JavaDocs posted:The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()). Surface fucked around with this message at 21:28 on Dec 9, 2010 |
# ? Dec 8, 2010 22:47 |
|
To clarify, if you mean you want to remove the highest-priority item in the queue, the method you're looking for is poll, not remove.
|
# ? Dec 8, 2010 23:05 |
|
Surface alluded to this, but I'll make it clear. When
|
# ? Dec 9, 2010 00:32 |
|
Epswing is totally on the money here. And whatever you do, don't just overload .equals() and leave .hashCode() as the default implementation- it'll violate the .equals() contract and gently caress things up in hilariously subtle ways.
|
# ? Dec 9, 2010 06:45 |
|
especially if you are fond of using HashMap and HashSet
|
# ? Dec 9, 2010 19:26 |
|
Internet Janitor posted:Epswing is totally on the money here. And whatever you do, don't just overload .equals() and leave .hashCode() as the default implementation- it'll violate the .equals() contract and gently caress things up in hilariously subtle ways. Haha yes I should edit this into my post.
|
# ? Dec 9, 2010 21:28 |
|
What's really fun is teaching students in a second-semester Java data structures class the proper way to implement equals and hashCode, especially when the concept of hash tables hasn't been introduced yet so a hash code seems pointless to them. (Why teach hashCode without mentioning hash tables? If we're teaching them to override equals, it's better that we mention hashCode at the same time since the two are so closely related.) I saw so much of this last semester: code:
Some students at least understood that you could use the hashcodes to short-circuit for inequality but that it doesn't say anything about equality, but even that makes my skin crawl a little bit for some reason.
|
# ? Dec 9, 2010 22:04 |
|
Flobbster posted:
|
# ? Dec 9, 2010 23:30 |
|
Flobbster posted:What's really fun is teaching students in a second-semester Java data structures class the proper way to implement equals and hashCode, especially when the concept of hash tables hasn't been introduced yet so a hash code seems pointless to them. (Why teach hashCode without mentioning hash tables? If we're teaching them to override equals, it's better that we mention hashCode at the same time since the two are so closely related.) I always thought the contract for hashCode was that it had to match the same things that equals did, but after reading the spec that's not the case. God drat that's a terrible way to introduce subtle bugs. That code is still inexcusable, they should at least check for nulls and that's its the same type!
|
# ? Dec 10, 2010 01:27 |
|
MEAT TREAT posted:I always thought the contract for hashCode was that it had to match the same things that equals did, but after reading the spec that's not the case. God drat that's a terrible way to introduce subtle bugs. Whoops missed an important word! nvm this
|
# ? Dec 10, 2010 01:29 |
|
Thanks for all the help. After addingcode:
|
# ? Dec 10, 2010 02:42 |
|
for the life of me I can't figure out how graphics2D is supposed to work. the two relevant classes: http://pastebin.com/vMsCXtcC and: http://pastebin.com/PjzNe2hD I've tried setting jPanel1 equal to the draw object, then running the draw object. I've tried passing display.jPanel1 into the drawImage functions, and now I've tried the current iteration. No matter what I do I get nothing to the screen. I know the images are in the right place because the older iteration of this program was just using the graphics library to draw, and while it worked it looked terrible and took forever to refresh. edit: sorry about all the commented out lines I've been throwing poo poo at this for a few hours now and I know it's something really minor and easy edit2: not nearly as annoying now Necc0 fucked around with this message at 05:12 on Dec 10, 2010 |
# ? Dec 10, 2010 04:07 |
|
Trick Question posted:The remove works just fine, and our game is basically working. Still, it ain't quite sorting as I'd like it to. Anyone have any idea why this is?
|
# ? Dec 10, 2010 04:24 |
Necc0 posted:for the life of me I can't figure out how graphics2D is supposed to work. the two relevant classes: Might wanna use something like http://www.pastebin.com/ instead of posting a ton of code in the thread.
|
|
# ? Dec 10, 2010 04:48 |
|
RitualConfuser posted:In what way is it not sorting correctly? Preferably, I'd have it sorting by suit then value. However, right now, it doesn't appear to be sorting at all, as such: code:
code:
|
# ? Dec 10, 2010 04:57 |
|
I made a simple implementation of Card with suit and value as int's and it sorts fine for me using that Comparator. What does the implementation of Card look like?
|
# ? Dec 10, 2010 05:09 |
|
fletcher posted:Might wanna use something like http://www.pastebin.com/ instead of posting a ton of code in the thread. thanks a bunch
|
# ? Dec 10, 2010 05:11 |
|
RitualConfuser posted:I made a simple implementation of Card with suit and value as int's and it sorts fine for me using that Comparator. What does the implementation of Card look like? Like this. be forewarned, I probably haven't commented that as well as I should have.
|
# ? Dec 10, 2010 05:53 |
|
E: I probably should have tested that out first... I think what you might be looking for is a SortedSet instead of a PriorityQueue. That assumes that you can't have more than one card of the same suit/value though, which may not be what you want. RitualConfuser fucked around with this message at 07:13 on Dec 10, 2010 |
# ? Dec 10, 2010 06:34 |
|
RC: what you even I don't know TQ, neither Card and CardComparator is the source of your bugs. You problem is that you have code that looks something like this: code:
I know you are doing this, or something equivalent, because these elements are actually in heap order, where the element at index N is smaller than the elements at index 2(N+1)-1 and 2(N+1). That is because PriorityQueue is internally implemented using the "heap" data structure. What you should be doing instead is something like: code:
|
# ? Dec 10, 2010 06:55 |
|
rjmccall posted:RC: what you even I don't know TQ: what was the purpose of keeping the cards in a queue rather than a set or other collection?
|
# ? Dec 10, 2010 07:11 |
|
The main purpose was that i could just import the priorityQueue and it'd auto-sort real easy.
|
# ? Dec 10, 2010 08:07 |
|
|
# ? Apr 25, 2024 00:56 |
|
What I'm getting at is that using a queue would suggest that the cards are going to be queued and then dequeued at some point. But, it seems like you'd probably want to iterate through the hand multiple times, and as rjmccall and Surface said, you can't iterate through the items in a PriorityQueue in sorted order without dequeueing them. So, if you need the ability to iterate through the cards multiple times (in order), then it's probably not the right structure.
RitualConfuser fucked around with this message at 08:43 on Dec 10, 2010 |
# ? Dec 10, 2010 08:40 |