• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need advice on my first program  RSS feed

 
Mark Lepro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a Java beginner and decided to try making a small tic tac toe program (2 players). I used no templates whatsoever and simply tried to code it with my own intuition , so don't be surprised if the code/methods/structure seems highly illogical.

Some methods are missing (for example, it is acutally possible to overwrite your opponent, etc), but my main problem comes with my "solution" method. From main() I built a for loop to limit the game to 9 turns, inside the for loop I made an if loop to alternate between player 1 and payer 2 and also to check if a player had already won or not. Well this is the part that doesn't really work. I've been working on it for long enough now that I'm a bit out of patience. It does not really recognize any winner. I don't really know how to build a good algorithm that would check who won so I simply listed all the winning combinations.

I would like helpful advice on how to go forward with my first program. Thanks a lot folks!

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot work out an algorithm like that on screen. You have to do it on paper, writing down the instructions so a Martian can understand them.
You alsoq need to get 99% of that code out of the main method. You have several methods there.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Lepro wrote:I used no templates whatsoever and simply tried to code it with my own intuition , so don't be surprised if the code/methods/structure seems highly illogical.

Hate to say, but that's usually a bad way to go. While it's always very tempting for beginners to jump straight into coding, I'd definitely advise against it. Programming is as much (and probably more) of a mental process as it is to do with coding; and starting to write code before you understand all of the problem you're trying to solve is a recipe for disaster.

My advice:
1. Turn your computer OFF.
2. Write down ALL the requirements for your tic-tac-toe program in English.
3. Don't write a line of code or turn your machine back on until you have completed and understood Step 2.
4. Refactor your program according to what you now know; or possibly start a new one from scratch.

I don't really know how to build a good algorithm that would check who won so I simply listed all the winning combinations.

Again, this is probably because you haven't thought enough about the problem before you started coding, so you're now stuck in a rut of only thinking about the solution in terms of the code you've written.

Think about this, and answer the question IN ENGLISH:
  • When is a game of tic-tac-toe won?


  • Another couple to think about:
  • When is no result possible?
  • When might be the best time to determine whether a player has won?


  • Chew on those for a while and see if it helps.

    Winston
     
    Mark Lepro
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the advice Winston. It is true I jumped into coding before really thinking it through. The way I built it is basically checking before each player's turn if a wining combination has appeared (if ((i % 2 == 0) && solved.equals(nowinner))). That way the program should be able to exit the loop and display who won. Well that bit doesn't work quite well yet
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mark Lepro wrote:Thanks for the advice Winston. It is true I jumped into coding before really thinking it through. The way I built it is basically checking before each player's turn if a wining combination has appeared (if ((i % 2 == 0) && solved.equals(nowinner))). That way the program should be able to exit the loop and display who won. Well that bit doesn't work quite well yet

    Because you're still thinking in terms of the code you've written.

    What if it's wrong? You must know the Bible story about the house built on sand.

    Stop coding. It may sound harsh, but it's the best advice I can give you.

    Winston
     
    Bill Johnston
    Ranch Hand
    Posts: 201
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mark Lepro wrote: ... The way I built it is basically checking before each player's turn if a wining combination has appeared ...


    Mark,

    Just a point to make. While I understand that you really want to work this out and there's nothing wrong with that - I have say that tic-tac-toe is one game where you really don't need to do this. Unlike Chess or like a game that determines a winner points, t-t-t is rather intuitive - that is, the winner is visually obvious. If I ever play a game of t-t-t with someone who wants to continue after one of us has three in a row I think I'm done anyway. ;)
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bill Johnston wrote:Just a point to make. While I understand that you really want to work this out and there's nothing wrong with that - I have say that tic-tac-toe is one game where you really don't need to do this. Unlike Chess or like a game that determines a winner points, t-t-t is rather intuitive - that is, the winner is visually obvious. If I ever play a game of t-t-t with someone who wants to continue after one of us has three in a row I think I'm done anyway. ;)

    Don't knock it too soon.

    Ever tried 4x4 TTT, or 8x8? Or 'Othello' (TTT with a twist)? Or 3D?

    You may be surprised to know that the algorithm for the correct way to play TTT is actually quite complex, and that the game has been studied by many computer scientists over the years, particularly those interested in AI, because of the difference between its mathematical complexity and its apparent visual simplicity.

    @Mark: You'll probably gather that I disagree with Bill here. You've not picked a bad game to start your programming journey with.

    Good luck.

    Winston
     
    Bill Johnston
    Ranch Hand
    Posts: 201
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Bill Johnston wrote:Just a point to make. While I understand that you really want to work this out and there's nothing wrong with that - I have say that tic-tac-toe is one game where you really don't need to do this. Unlike Chess or like a game that determines a winner points, t-t-t is rather intuitive - that is, the winner is visually obvious. If I ever play a game of t-t-t with someone who wants to continue after one of us has three in a row I think I'm done anyway. ;)

    Don't knock it too soon.

    Ever tried 4x4 TTT, or 8x8? Or 'Othello' (TTT with a twist)? Or 3D?

    You may be surprised to know that the algorithm for the correct way to play TTT is actually quite complex, and that the game has been studied by many computer scientists over the years, particularly those interested in AI, because of the difference between its mathematical complexity and its apparent visual simplicity.

    @Mark: You'll probably gather that I disagree with Bill here. You've not picked a bad game to start your programming journey with.

    Good luck.

    Winston


    No I don't really disagree with you - although I admit that it sounded like I did. I did say that there's nothing wrong with wringing out the logic. My main point is that sometimes solving a problem with a computer is counter productive (diminishing returns). Ah! it sounds again like I disagree, can't win! ;) Okay, uncle, you're right Winston - it's worth it.

    Actually I have a t-t-t application that determines a winner. But rather than supply it to the OP, may I point out that there is a t-t-t in the demo/applet folder in your downloaded JDK. At least there is one in mine. The source code is there if you want or need it.

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!