Junilu Lacar wrote:I just got done facilitating a Global Day of Code Retreat over the weekend so I'm still in kind of a "stretch yourself" mood. I said that I didn't have a compelling reason to refactor the Player.moves() method further. I guess I was wrong. You can do some reification and turn (row, col) into a real object:
Here, I have reified (row, column), two separate values that when taken together signify a "position" on the game board and made it into a real object of type Position. That causes a lot of ripple refactoring but it can be done slowly and methodically.
You can make that even smaller like this:
Note that I added a comment to explain WHY the return value is hard-coded to true. To channel The Most Interesting Man in the World, I don't often write comments but when I do, it's to explain WHY I'm doing something.
Sean Paulson wrote:I see, so you are basically taking all the code i used and just simplifying and then simplifying and then simplifying more, basically like in math. More little methods.
Junilu Lacar wrote:. . . It's straightforward, expressive, and simple.
Junilu Lacar wrote:. . . in Java, "multidimensional arrays" are really just nested arrays, arrays whose elements are themselves arrays.
Junilu Lacar wrote:. . . The 4 Rules of Simple Design . . .
Junilu Lacar wrote:
Sean Paulson wrote:im not sure if i know what you mean by using reference equality.
Please don't quote the entire response. It makes the thread much longer than it needs to be and more difficult to read. Delete the parts of the quote that are not directly relevant to your response. Thanks.
Better to say one array containing two other arrays. The outer array is probably best called an array of arrays.Sean Paulson wrote:
Junilu Lacar wrote:. . . in Java, "multidimensional arrays" are really just nested arrays, arrays whose elements are themselves arrays.
so two single arrays put together?
Campbell Ritchie wrote:
Better to say one array containing two other arrays. The outer array is probably best called an array of arrays.Sean Paulson wrote:...so two single arrays put together?
Sorry for not being clear, but I was responding to the OP saying, “two single arrays put together.”Junilu Lacar wrote:. . . Sorry to nitpick but I think you left out "for example" in that first sentence. . . .
Campbell Ritchie wrote:Sorry for not being clear, but I was responding to the OP saying, “two single arrays put together.”
Sean Paulson wrote:Im planning on using this code again soon so i will probaly try and fix it up a lot because, i am wanting to learn to make a gui right now and will use the tick tack toe game code.
Also i will study those concepts you mentioned and some more.
Junilu Lacar wrote:
Sean Paulson wrote:Im planning on using this code again soon so i will probaly try and fix it up a lot because, i am wanting to learn to make a gui right now and will use the tick tack toe game code.
Also i will study those concepts you mentioned and some more.
If you're going to create a GUI, a well-factored program that separates display concerns from non-display concerns will make it much easier for you. In my program, I know exactly which methods need to be overridden: the pickAnOpenSpot() method and the drawBoard() method. That's it, nothing else should need to be changed to add a GUI.
This would be an example of the adhering to the Open-Closed Principle (OCP) that states that a class should be open for extension but closed for modification.
Another exercise you might want to try is to make your Game class more intelligent. There are states of the game where it can be determined with 100% certainty that the game will end in a draw, even when there are still more moves available for the players to make. I was thinking of implementing something like .
Skool. Stay in. Smartness. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|