Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Created a Tic Tac Toe Java app  RSS feed

 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a Tic Tac Toe Java app. I'm in the process of preparing for advanced Java class in the fall semester.
I was a programming challenge in the book but I modified it to use images for the X's and O's and it also
allows two players to play a game of tic tac toe. Down the line I may make it so you can play against the computer.

Here is the Jar file:
http://christopheradams.com/java/TicTacToe.jar

Here is a video of it:
https://www.facebook.com/ctchrisadams/videos/10153794702766485/

Here is my github with this and all the other examples I've been working through.
https://github.com/gitchrisadams/StartingOutJavaNotes/commits/master

I would love to hear people's feedback on this or any of my other examples on github.

I'm also new to this forum, so just wanted to say to hello to everyone as well. It looks like a great community!
Chris





 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome, Chris!
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:Welcome, Chris!


Thanks Jan!
 
salvin francis
Bartender
Posts: 1591
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome To code ranch Christopher. Thank you for sharing your code with us.

I havent tried out your program yet, but I have looked at your git code at
https://github.com/gitchrisadams/StartingOutJavaNotes/commit/2f2b43ea0a555ebe675a994953a0f5cfdb210c8a

A couple of points from my end :

1. You code benefit from Lists/Arrays instead of having 9 references to each button
2. A lot of code is repeated for Button1Listener ... Button9Listener, can you reduce it to one listener only?
3. You could benefit using loops in your determineWinner method (also note that the code for xWinner, oWinner is exactly the same and is repeated).

 
salvin francis
Bartender
Posts: 1591
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, you have 9 panels in code. I don't think they are necessary.
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Welcome To code ranch Christopher. Thank you for sharing your code with us.

I havent tried out your program yet, but I have looked at your git code at
https://github.com/gitchrisadams/StartingOutJavaNotes/commit/2f2b43ea0a555ebe675a994953a0f5cfdb210c8a

A couple of points from my end :

1. You code benefit from Lists/Arrays instead of having 9 references to each button
2. A lot of code is repeated for Button1Listener ... Button9Listener, can you reduce it to one listener only?
3. You could benefit using loops in your determineWinner method (also note that the code for xWinner, oWinner is exactly the same and is repeated).



Hi Salvin, Thanks for the feedback.

Points 1 and 2:
There may well be a way to do this with ArrayLists but being new to Swing/Java Gui I did not see a way to do this with my current abilities. I'm pretty familiar with Arrays and Arraylists but that didn't seem to fit when I was going through this.
As for the 9 references to each button. The way the game works is that each button needs to respond to a click event so I don't see away around not having a Listener for each button. Not saying, there isn't a way to do this. Just wasn't apparent to me when working this out.

Point 3:
You're right about xWinner and oWinner being the same and I could re-work.
Could you explain how the determineWinner could benefit from loops.

Point 4: Panels
You are definitely right about this. I am learning about GUI layout and am only familiar with a few layouts and how to lay things out in panels and I'm not using any kind of Gui layout tool.

I originally posted this message in Beginning Java forum and noticed it was moved to game development so just a disclosure, I'm not posing this as the next million dollar app I just
started into the Java Swing and Gui stuff this week.

Salvin, thanks so much for your thorough response and taking the time to look at the code. Much appreciated.

Chris



 
salvin francis
Bartender
Posts: 1591
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christopher,
Its nice to see your answer, first of all, let's make some basic code changes.

This tells the programmer that you have a X or a Y or a BLANK value instead of 1 for x and 0 for y. These numbers are meaningful for you and not for any other programmers.
 
salvin francis
Bartender
Posts: 1591
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next, for re-using code, we can use inheritance...

So instead of using JButton we can use TicTacButton. This class also has its own row and col. The game array you made will contain the values for these rows, cols.

Now, lets compact your other code:
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Hi Christopher,
Its nice to see your answer, first of all, let's make some basic code changes.

This tells the programmer that you have a X or a Y or a BLANK value instead of 1 for x and 0 for y. These numbers are meaningful for you and not for any other programmers.


I like the idea of using an Enum, that makes sense.
 
Christopher Adams
Greenhorn
Posts: 17
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Next, for re-using code, we can use inheritance...

So instead of using JButton we can use TicTacButton. This class also has its own row and col. The game array you made will contain the values for these rows, cols.

Now, lets compact your other code:



Nice solution, I like idea of creating the TicTacButton class. Thanks for posting that.
Chris
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!