• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

ticTacToe trying to learn object oriented programming  RSS feed

 
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


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.
 
Sheriff
Posts: 13458
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that inRow() method is just my rowMarks() method that I renamed so the code would read better.

See how a simple RENAME makes the code read a lot better? It's way more expressive after the refactoring.

And just so you know, the inRow() method is a single line of code, too. It takes advantage of the fact that in Java, "multidimensional arrays" are really just nested arrays, arrays whose elements are themselves arrays.
 
Junilu Lacar
Sheriff
Posts: 13458
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


Absolutely. What I have found is that the more I try to simplify, the easier it is for me to wrap my head around the whole problem and the easier I find it is to work with my code.  Huge chunks of code like the ones that you wrote are very difficult to work with and they tend to hide and harbor bugs, mislead you, and trap you in poor design choices. You often find yourself painted into a corner when you write huge methods so I try to avoid them as much as I can. The smaller the better.

The 4 Rules of Simple Design are a great set of guidelines to follow:

1. All tests pass
2. Clearly expresses intent
3. No duplication of knowledge/representation (DRY - Don't Repeat Yourself)
4. Small (methods, classes, interfaces, APIs, # of dependencies, # of parameters, etc.)
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . It's straightforward, expressive, and simple.



well thanks a lot for the help, really just awesome stuff.

I will for sure check out that book and any other theory type books you can recommend.
I think i just kinda understood obo a little but obviously did not take it far enough. 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 wanna see the other code ill post it.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . in Java, "multidimensional arrays" are really just nested arrays, arrays whose elements are themselves arrays.



so two single arrays put together?
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . The 4 Rules of Simple Design . . .



yeah i had to rewrite the while loop in my game class and part of my  checkWin() function from scratch almost because it had some bug i could not figure out...that sucked.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



lol sorry about that just seen this post
 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?

Better to say one array containing two other arrays. The outer array is probably best called an array of arrays.
 
Junilu Lacar
Sheriff
Posts: 13458
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Sean Paulson wrote:...so two single arrays put together?

Better to say one array containing two other arrays. The outer array is probably best called an array of arrays.


Sorry to nitpick but I think you left out "for example" in that first sentence. One array containing two other arrays is a specific example of "nested arrays" or "array of arrays". Here are some more examples:

The same declarations, formatted differently:
 
Campbell Ritchie
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Sorry to nitpick but I think you left out "for example" in that first sentence.  . . .

Sorry for not being clear, but I was responding to the OP saying, “two single arrays put together.”
 
Junilu Lacar
Sheriff
Posts: 13458
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Sorry for not being clear, but I was responding to the OP saying, “two single arrays put together.”


No problem, I was wondering why OP would think it's just two arrays.

@OP, in the Tic Tac Toe game, the board is actually an array that contains three arrays:
 
Junilu Lacar
Sheriff
Posts: 13458
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 an isWinnable() method that can be tacked on to the check made by the boolean gameNotOver() method.  Something like this:

I'd have to modify the original game code slightly to make the gameNotOver() method protected instead of private. Implementing this would require creating a game tree.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 .



that could be pretty fun. I am wanting to make the snake game next with a gui and after i study up on some of the stuff you mentioned and some other subjects.
 
Sean Paulson
Ranch Hand
Posts: 87
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to post this....i wouldnt say im happy with it but i learned allot...and get that basically why you factor is so if you need to add a new feature you can just add a method or class or w/e instead of searching through a whole application written in linear code. I was able to lower the lines in this game buy over half

Here it is for anyone who wants to check it out. Still have allot to learn...but i had allot of fun.

Game Class


Player Class



Board Class

 
Junilu Lacar
Sheriff
Posts: 13458
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are still a number of things you can improve but good job breaking things down into smaller, more manageable chunks. Keep practicing and don't forget: try to refactor often. Rename, extract and Compose method are the main techniques you want to use 80% or more of the time. Follow the four rules of simple design, KISS, DRY, SLAP, SOLID. No, that's not anything kinky, those are acronyms for design principles that you should get familiar with and learn to apply as you write code. Good luck.
 
Yes, my master! Here is the tiny ad you asked for:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!