Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Simple Tic-Tac-Toe Game

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
this is my first posting here. I hope I will post more soon.
I have written a simple program to play tic-tac-toe game.
Here is the code for it:
I hope I get some remarks on it or anything I should change in the program.


 
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Returning '\u0000' to indicate no winner is a strange choice and harkens back to older days of programming. This kind of value is called a "semaphor" to signal some sort of condition. It adds to the cognitive weight of your program.

Since you can return a boolean, why not just write this?

That's more straightforward to read and understand, isn't it?
 
Junilu Lacar
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 24-51 contains duplicated logic with only minor differences. You can extract the logic into a single method and parameterize the parts that differ.
 
Marshal
Posts: 72944
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Extracting the repeated code would not only be good programming, but it would also guide you towards more object‑orientated programming. Your current offering shows no sign of using object design anywhere, I am afraid. Making everything static is a sign of that.
 
Junilu Lacar
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, your logic for checking for a winner is checking too much of the grid. Think about it: if a player is going to win, they'll only win in the row, column, or if applicable, the diagonal that their last move belongs to.

This is essentially how you could write the logic:

In fact, you can combine that with my previous advice:
 
Junilu Lacar
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a great example of a comment that's a code smell (something that's not quite right in your code):

It's a smell because you had to add the comment to explain what the value represented. The other way I suggested using a hasWinner() method doesn't require such a comment because the code is self-explanatory. Strive to write clear, expressive code that explains itself well enough that it doesn't need any comment.    
 
Junilu Lacar
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Campbell's point about object-orientation, try to separate user input and output from your main game logic. User input and output are different concerns that are best kept separate. This will allow you to test your main game logic more easily once you learn how to use testing frameworks like JUnit. Keeping separate concerns separate in your program also makes it easier to change the program and adapt it to new requirements. Right now, your code is very tightly coupled to input/output via that keyboard/console.
 
Junilu Lacar
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example of how you might write this in a more object-oriented way:

This demonstrates how you would separate input/output concerns from your main game logic. Think of how you might write your program differently if you had your main game logic in a TicTacToe class and it was used this way.

 
Junilu Lacar
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Choosing good names to use in your program is essential to making it readable/comprehensible. Avoid terms that are more about implementation. One example of that in your program is "grid". Have you ever heard anybody play TicTacToe and refer to what they're playing on as a "grid"? If anything, it's referred to as a "board" or simply the "game".  As much as possible, use terms from the problem domain, not the solution domain. The name "grid" tells me the programmer is thinking about the implementation whereas a name like "game" would indicate the programmer is thinking more about the problem domain.

The difference is evident here:
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've made multiple modifications to the game taking into account all the advises except the object orientation one because I am not comfortable with object oriented programming yet .
Here is the code :



Thank you so much for your efforts and useful advises. This forum is going to be my goto for asking questions and getting help while I am learning Java.

 
Junilu Lacar
Sheriff
Posts: 16242
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice to see you've incorporated some of the changes suggested.

Here's one version of this game that I've implemented in the past. Looking at it now, there might be some things I'd change about it but this will at least give you some ideas for how it might be written in a more object-oriented way.

By writing it this way, I can easily write automated tests for it using JUnit.
 
Campbell Ritchie
Marshal
Posts: 72944
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are there two of you working at the same place with the same exercise?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic