Please take a look to the coding of my TicTacToe game

Hi everone, I’ve spent a couple of hours coming up with a script for a TicTacToe game and would really love to read your comments or critiques about it hopping that I can learn from your feedback.

If you have the time to look at it, here is is so please download it, play it and let me know your thoughts and comments, would like to know if what you see in it is ‘best practice’ or what would you recommend as best practices that I can implement in there. Thank you

HERE IT IS NOW https://gist.github.com/rarodriguezp/1f44a9ad97f2470d430efd7b85058e0a

ps: copy pasting it here wouldn’t look good so I rather shared a gist URL.

2 Likes

Let me be more specific about things that I would like to have some feedback about ‘best practices’ VS what I coded:

  • Global variables should be avoided, right? I read in another book that we should probably share variables as parameters between functions. Said that,
    – should I refactor my ‘played_choices’ for not being global?
    – some functions already have about 3 params, wouldn’t I be abusing on number of parameters a function should have?
    – am I making overall well usage of my functions?

  • I just noticed that I should refactor not to use a function called ‘user turn’ and just call ‘play_it’ directly instead. This is the case:

    def user_turn(quadrantes, user_input, symbol):
    play_it(quadrantes, user_input, symbol)
    
  • ‘who_first’ function only takes cares of asking the user who plays well. Is it ok? It there a better way to achieve that?

  • ‘x_or_o’ function only takes cares of asking the user if they want to play with the ‘X’ or ‘O’. Is it ok? It there a better way to achieve that?

  • Last lines of the program is just a ‘while true’ loop that makes the ‘user’ or ‘computer’ play first, if you notice the code in the ‘if’ and ‘elif’ is the same just changing the order that functions are called. Is this correct or is this supposed to be coded in a better way?

I definitely got rid of user_turn function and replace it with play_it

Here’s latest gist for latest version https://gist.github.com/rarodriguezp/47934ef4881d02df86d1b878ee7a35f0

It all works fine so that was definitely not needed.

Any other feedback would be appreciated it. What about the global variable thing?

Thanks.

If I may be so bold to mention this game is simple and fun.

That said, the code looks clear and obvious at first glance but surely adding a few comments next to each would have been a good idea in to build the practice of good documentation for larger projects.

Even a simple
#this prints out the board each turn.

Especially since ‘imprime’ is not English.

Best,
farmerPaco

Thank you so much farmerPaco for looking into my code.

Certainly I have the bad habit of not commenting on my code, thank you for bringing that up to my attention.

Hey nice job. Tic-tac-toe is always a really good exercise to work on Python.

Thank you sir :slight_smile: and happy holidays btw