Nils Anton

Greenhorn
+ Follow
since Jan 29, 2016
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
17
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nils Anton

I took both your suggestion and used the rectangle class. I've also created a custom class, Card, that extends the imageIcon-class. When a Card is created a rectangle is also created, so when I click on a rectangle I can check which Card it's part of - and then flip that Card; change its imageIcon.

Thanks again for the help .

For those curious, here's my code:

CODE LINK:
https://www.dropbox.com/sh/9m5n64vqmmw5o8w/AACl_h1eO6seW42TULZ-jZGha?dl=0

CODE - ImageLump-class (WiP name):


CODE - Card-class:


IMAGE EXAMPLES:


8 years ago
Thank you Piet for the further help .
8 years ago
Hello!

Full disclosure: this problem is part of an exercise - so I'm only looking for tips & suggestions - yet I'm completely stuck, so I feel the need to ask for help.

I'm trying to create a window that displays images. You can turn over the images by clicking on them, and move them by holding them.

I wanted to use JFrame, JPanel, JLabels, and ImageIcons to solve the task, yet I'm now allowed to use JLabels - so I've attempted to use a paintComponent instead.

I currently got a JFrame, with a JPanel, and an image drawn on the JPanel. I've also been able add a MouseAdapter to the JPanel that gets the coordinates of the area being pressed. Yet I don't know how to use it to select my ImageIcon - maybe save the JPanel area the ImageIcon occupies, so I can check the mouse coordinates against it? - or something else?

Your help is highly appreciated!

/Cheers

CODE LINK:
https://www.dropbox.com/sh/9m5n64vqmmw5o8w/AACl_h1eO6seW42TULZ-jZGha?dl=0

CODE:


IMAGE EXAMPLE:
8 years ago

Hmmm. Really?

NIM is one of the oldest computer games there is, and the reason is that it has a very simple winning strategy.

In fact, I remember going to an exhibition at Kensington Olympia when I was about 7 (which would have been 1964) and getting 30 seconds of fame on national TV because I worked out how to "beat the computer" at a NIM game with either 13 or 15 matchsticks on the bottom row.

So you might want to look up the way to win rather than just having the computer take sticks at random.



It's time for me to do some investigation .
8 years ago
All the formatting tips are great - they make sense - so I'll be sure to use them in the future .

That's fine. Just remember this: Your methods should do one thing and only one thing well. If it's calculating things, avoid mixing in input and/or display. If it's display, avoid mixing in lots of calculations and/or input. Those things that you want to mix in, that's what return values and parameters are for. So, main calls a method to get user input, then passes the input on the method that does calculations, then passes the results to another method that displays them.



I like this way of thinking, since it makes it easier to decide what a method should contain.

Note that a constructor should never call any public or protected methods that are not specified as "final". If any subclass of your Player class happens to override that non-final method you will encounter problems that are very difficult to debug.



I'll try to keep this in mind.


Thank you all for the help .
8 years ago
@Winston Gutkowski: thank you for the welcome .


However, while I see a HumanPlayer, I don't see any ComputerPlayer class. What are the differences between them?
Try to explain it to us IN ENGLISH, not in "Java-speak".



The human has to pick at least 1 stick, and can pick up to half of the sticks left - rounded down, i.e. if there's 21 sticks left she can take 10 sticks.

The computer randomly takes a number of sticks - while following the above rules.

Note that this is NOT the game itself, simply the "playing area"; and many games - including Chess, Go, Connect4/5, Mastermind, etc - have one. It's also the thing that you're likely to want to display (however you do that).



Sounds like a fun thing to add - I haven't done any "graphics" yet - yet I'm sure there's good guides on the net for how to do it .

Also: DON'T include underscores ('_') in your variable names. It's regarded as "Python-esque", and generally frowned on in the Java community. You can find proper naming conventions here; and I advise you to memorise them as quickly as possible.



I'll stay away from the underscores, and I've already bookmarked the link : ).


@Junily Lacar: loads of great feedback

One unnecessarily short name: playerNr_ (it does sound stupid when I read it out loud ), one unnecessarily long: playerNumber, playerName, etc.

I've got no excuse for the lines:


I'll be sure to stay away from underscore naming.

Overall, this code strongly hints at someone who has experience writing programs in another language. I doubt this is your first rodeo, right Cowboy?


I've before played around some in C#, UnityScript, Python, and done some visual scripting in Kismet and Hammer. Yet I recently started studying programming at school, so I feel I don't quite get what:

In a program this small, mixing concerns is not much of a big deal. In larger programs, however, you should think about separation of concerns, particularly the concerns of doing calculations vs input/display.

Using inheritance and polymorphism to simplify the decision to prompt or not prompt for input works in this case but experienced hands will detect a slight smell there and will keep a wary eye on it. Mixed responsibilities tend to make for code that is more difficult to maintain.

As for using interfaces vs abstract classes, my rule of thumb is to use an interface when you know there will be a definite need to have an unknown number of different implementations. Also, when the idea represented is along or crosses application layer boundaries (View, Control, Data, Service, Model, etc.), prefer interfaces as they have the least amount of coupling.



Yet I appreciate your attempt to explain it to me : ).

2 seconds is an awfully long time to simulate a computer trying to make a decision. Are you trying to make the computer look stupid or what? ;)



I wouldn't want to look stupid when I'm playing against it ;).


Thank you both for taking time out of your Friday to help me! : ).

8 years ago
Hello!

As a school exercise I've created a Nim game. The exercise is supposed to teach me about abstract classes, inheritance, and interfaces. Therefore I decided to have an abstract Player-class that the human and computer player inherit methods and instance variables from.

I would love to get some feedback on the code - especially the Player-, HumanPlayer-, and ComputerPlayer-class.

Also, I think that I understand abstract classes, and inheritance - yet I don't think I understand interfaces - more than that they're abstract classes. I've read some articles about them, yet I still don't feel that I know when to use an interface. Could you help me by giving me an example of when you should use an interface?

And answering this question as well: when initializing a variable in a constructor, is it better to have the constructor call a set-method, i.e.:


or have the constructor initialize the variables:

?


Link to java files:
https://www.dropbox.com/sh/9m5n64vqmmw5o8w/AACl_h1eO6seW42TULZ-jZGha?dl=0

"GameTestDrive.java" is used to run the game.

Your help is appreciated .

CODE:










8 years ago
@Dave: I think it displays what the Dice rolled - so in this case 2.

@Henry: local variables don't get default values. Instance variables get default values. If they're created at the same time - would the instance variable then overwrite the local variabel?

I just also found another error in the code: the "nsides" variabel in PairOfDice should be "nSides".

Also, thanks for the welcome .
8 years ago
Hello!

The task I'm attempting to solve is part of a school assignment. I've already solved it one way - yet that solution is rather ugly - so I'm attempting to solve it another way.

The task requires me to create a new class PairOfDice that creates two Dice objects, rolls them, checks if they're a pair, and then writes the result. I'm trying to have the PairOfDice class create two Dice classes that roll themselves, save their value to themselves, and I then have the PairOfDice class check if they rolled a pair, and lastly write out the result. Yet when I attempt to have the PairOfDice class create two Dice objects, they are created with a wrong number of sides (value), yet I don't understand why.

I've attempted to search this forum, and the Internet, yet I still don't understand what the error is - would you please help me?

Dice class:


PairOfDice class:


PairOfDiceTestRun class:



/Regards,
Nils

EDIT: I've done some debugging - when I run:


dice01 gets the value:
dice01 Dice(6, 2) Dice

which I assume has something to do with toString - does it automatically rewrite dice01's value when it's created?
8 years ago