• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Win or Tie method  RSS feed

 
Marshal
Posts: 6255
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

john quick wrote:He is saying that because Turn is set to 9 the program will return TIE no matter what.  So basically no need to have tie in the code. -2 is never called and I need to make sure it is.

Not sure it is that.

Anyway, what is that -2 in your code? Don't understand looking to those few lines. And if you need to make sure is -2 returned as you say, why you need that if statement at all then? If statement gives you max 50% of probability to return what you want.

 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My instructor replied and stated I do need TIE.  -2 should be returned if the winner is not NOUGHT, CROSS, or TIE
 
Liutauras Vilda
Marshal
Posts: 6255
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, any progress? Do we have solution to look at?
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This is where I am currently at.  According to my instructor the problem lies in the code above.  I honestly am not sure exactly what to put in to correct the issue.  TIE needs to be in the code, so that stays.  Since my instructor says -2 should be returned if the winner is not NOUGHT, CROSS, or TIE, I feel -2 should not be in the code but I am 100% sure.  Honestly I'm burned out on this project.
 
Marshal
Posts: 61702
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does −2 mean? You shou‍ld make symbolic constants for all those numbers, e.g.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-2 represents empty cells on the board.
 
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

john quick wrote:-2 represents empty cells on the board.


No, it doesn't.  There could be a winner even when there are still empty cells on the board. If there's a winner, you don't return -2, you return CROSS or NOUGHT, depending on which player won. You return -2 when the game can continue because there is still no winner and there are still empty spots on the board that the next player can choose to mark.
 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I have been saying since the very beginning of this thread, this method does not make sense at all.  It's named winOrTie, which implies a boolean return value in answer to the question, "Was there a win or tie?" The question implies that the logic that uses the answer to that question will determine if the game should terminate or not. You could refactor that name to isGameOver to get rid of the "Or" in the name and just express the idea as one condition instead of either of two conditions.  The trouble with the current design is that instead of returning a boolean, it returns an int and the possible values returned represent four different states. This is a bad design and a bad requirement.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my instructor says this is the only hint he can give me....





 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This small method has been a thorn in your side for so long I'm almost tempted to just give you the code but I won't.  Here are your problems:

1. You haven't figured out how to determine when one of the players has won.
2. You haven't figured out how to determine when the game has ended in a draw

Unless you figure out these three (#1 is a combination of two outcomes: CROSS wins or NOUGHT wins) things, you won't be able to complete this method.

The structure of the code is literally:

Determining whether CROSS or NOUGHT has won is not a straightforward condition as you have tried so many times before. You need to scan all the rows, columns, and diagonals of the Tic Tac Toe board to see if any of them contain three of the same marks. Whoever has three marks in a row has won the game.  If you still don't know how to iterate over the rows, columns, and diagonals, then that's another problem you need to figure out before you can complete this method.
 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I simply cannot understand is how you still haven't seemed to learn how to align/indent your code properly, even after all the examples you've been given that show you the proper way to do it. This is not a matter of aesthetics or being anal about alignment and indentation. Properly aligned code is essential to being able to think clearly and correctly about the logic that the code represents. For me, poor alignment indicates poor understanding of the logic and organization of the program.
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . You need to scan all the rows, columns, and diagonals of the Tic Tac Toe board . . .

You don't need to scan them all, or more precisely you wouldn't have needed to scan them all had the exercise been specified correctly in the first place. If you watch children playing noughts and crosses (=TicTacToe), they do not say, “play on,” nor, “You've won!” The only claim you will hear is, “I've won!” If the exercise hadn't required this daft method, OP could have written a gameWon method which behaves more like real‑life players.
 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're absolutely right, Campbell, on multiple points. That's pretty much what I said at the start of this thread. It's always the last player to have moved who will win if there is, in fact, a winner and it will be a row, column, or diagonal that intersects the last cell to be marked that will have the winning series of marks.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem has to do with the assignment and my instructor(s).

I know you guys have said the whole thing is sloppy looking and terrible, I realize that.  BUT...it is what I have been given and what I have to use.  No variation here is allowed. As for what is in the output when I run the project..  What I posted earlier is what my instructor WANTS to see in the output.  I'm not getting that in the output...

I sent my current code to my instructor(s).  I asked what is still wrong with for it not to show what THEY want it to show in the output.  

He replies...

Your program does not run properly because of the last few lines of code in your winOrTie( ) method:





Your program will return TIE no matter what the user enters into the cell for the first time because turn is always set to 9 so return -2 is never called.



I respond asking him to confirm that I need TIE, he replies...

You do need to have TIE in your code.  -2 should be returned if the winner is not CROSS, NOUGHT, or TIE.



Here is the only hint that I can provide you:





As for the alignment of the code, again it is in the instructions.  It looks just like that!  My instructor sends me the above with the code looking just like that!
 
Liutauras Vilda
Marshal
Posts: 6255
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

john quick wrote:it is what I have been given and what I have to use.

Alright.

john quick wrote:As for the alignment of the code, again it is in the instructions.

STOP right here!

Please post those 9 lines of code in a well formatted and indented form.
I think we are obliged here to override the poorly formatted and indented code instruction you have been given, otherwise that would be a disservice for the community to show it like that.

Waiting well formatted and indented code - so we could start over from there.

Example of nicely formatted code. Providing two different forms:

1.

2.
 
Liutauras Vilda
Marshal
Posts: 6255
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3.
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what Junilu is saying is that returning −2 every time there are < 9 turns taken is inconsistent with the rules of the game. It would mean that a board looking like this:-
OXO
   OX
   XO
which contains seven symbols would be reported as game in progress when in fact O has won.
 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

john quick wrote:

As for the alignment of the code, again it is in the instructions.  It looks just like that!  My instructor sends me the above with the code looking just like that!


I'm sorry, but if this were a real western town and I were a real lawman, I would have to put your instructor in jail if that were true. Then I'd tar and feather him.
 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

john quick wrote:

As for the alignment of the code, again it is in the instructions.  It looks just like that!  My instructor sends me the above with the code looking just like that!


I'm sorry, but if this were a real western town and I were a real lawman, I would have to put your instructor in jail if that were true. Then I'd tar and feather him.


@OP However, in all fairness to your instructor, I think it's only right to ask him to clarify his requirement regarding formatting. You may be jumping to conclusions based on unintentional omissions or assumptions on his part. Can you please ask your instructor if the above formatting is what he would prefer rather than the examples we have shown you?

Preferably, you would write something like this:

Dear instructor:

Regarding code formatting, I was wondering if what you suggested is formatted the way you'd prefer, that is:

rather than what seems to be a more standard formatting like this:

---
Please make sure that the formatting is preserved in the email that you send your instructor so that it's crystal clear what you're asking and we can clear this matter up once and for all.
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and also verify that you have copied the formatting correctly.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will ask my instructor tomorrow.  They do not respond on the weekends.
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

john quick wrote:. . . .  They do not respond on the weekends.

hahahahahahahahaha
They have more sense than to respond to emails on Sundays. Good for them. Nobody is paying them to stay awake on Sunday listening for emails from students. Not like us on the Ranch; we get paid twice as much for answering posts on Sunday as we would on Monday to Friday
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, my instructor responded and said either way is acceptable for the Tic Tac Toe program.
 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

john quick wrote:Okay, my instructor responded and said either way is acceptable for the Tic Tac Toe program.


That is quite disappointing.

Just so you know, given these two versions:


The first version is NOT ACCEPTABLE for *any* program in my book nor is it acceptable anywhere where people actually get paid to write code. Use your best judgement on what you want to do.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd rather go with option 2 and not the one the instructor sent me.
 
Junilu Lacar
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's good to hear.

Figure out how to iterate over the array and count how many of a particular mark there are in it. This is just a starting point. One approach that V. Anton Spraul recommends in his book, Think like a Programmer is to start with a similar but simpler problem if you find that something is too complicated to tackle right away. If you're having trouble figuring out how to iterate over your board array to figure out if a player has won, then simplify the problem.  One way to do that is to pretend that your board consists of just a single row, like this:

I know this is not what your Tic Tac Toe board is like but once you learn how to deal with something simpler, you can take what you learned and apply it to the more complex problem.  How would you examine the above array to see if there are 3 "X" marks in a row? 3 "O" marks in a row? Any empty spaces left? No spaces left?

Try to write code to answer the above questions first. Once you can get that code to work correctly, it will be easier to figure out what to do with your actual game board.
 
Liutauras Vilda
Marshal
Posts: 6255
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And talking about having a simpler case of a row of three elements, you could have Row, Column, Diagonal classes which consists that representation (array of 3 elements), but that might overwhelming a bit, so you could be fully fine with a class Entity or some other similar name, and along with some created methods, you could always deal with that simpler case:
or Indices there would need to be of location of latest placed symbol. Now in that  set, there is only 1 diagonal, so you wouldn't always need to check for that, just in some cases when the symbol is placed in one of possible diagonal locations.

Then later you could have a method in a board class to check if there is a winner:

Or to have that method in Entity class, where you could write row.hasWinner(), or row.hasWinner(Symbol.X)...

There seem to be quite few ways to solve this exercise, which one is more elegant need to try more approaches, so all would become clearer. I have never programmed this exercise myself.
 
Creator of Enthuware JWS+ V6
Posts: 3180
276
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations john quick     your question has made it to our CodeRanch Journal - March 2017.

Have a Cow!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not entirely sure what is being asked here but from what I understand a 2d array is being made for the board and turn are taken to play tick tack toe in which case I think the problem will likely be located in how the board is read to determine if the game is over or not in which case I think there should be the first check if there are any vertical rows that are filled in for a win , then horizontal then vertical, and finally it should check if all the spaces are filled in. the first check should be a for loop inside a for loop checking if all the values in the collum are the same and if they are then it checks one to determine who wins, after that the same process for horizontal except its rows instead of columns and finally, for diagonal it will check the three locations for a diagonal line to see if they match and respond in the same manner as the previous two. the last check should be for if the entire board is filled in which case if all of the previous checks reveal there is no winner and the board has all spaces filled in then it should say that there is a tie if not then there could be a counter after that to add a value to a turn counter .All of these checks could be a method outside of the main method with a character value, three options for the first three methods being vertical horizontal and diagonal, two of the possible returns would be if one of the players win and the last possible char return for if there is no winner yet, and finally the last method that checks if the board is full should send out two possible char values one saying the board is full and the other that it is not full then a switch can be used to determine if there is a winner depending on the replys and at the end of it if the reply of the last method is full then it will say tie because there were no winners.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!