• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Tic Tac Toe GetWinner method  RSS feed

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
So my teacher wants me to write a one method: getWinner() with a return type of string. I have the logic down I compiled it and it worked but I don't know how to incorporate the strings into the method and where I should put it. Any tweaks or help would be appreciated, I'm really stuck. The the tester code is included.



 
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your logic work correctly? It's hard to folow, but it seems like it has a couple of mistakes. Like your if statement to check if a field is empty, it dosn't check if it's empty, but if the field contains a blankspace. If you haven't set all your fields to contain that blankspace, it won't work (edit: "Strike that remark, in the meanwhile if seen that you did set all the fields to contain a blankspace"). Also your return statement is a bit mested up. Return only a string, putting your sys.out in a string isn't going to work calling the winner variable in such a manner isn't gonna work eather.
I think what your trying to do is something like:

But I don't think that was your question right? Why do you have 2 times 2 the same for-loop?
Inside that for loop you have a if to check if win = true, but nowhere win is set to true, so that statement is never being executed, you also use a singular =, that's an assignment, you need ==.
That all being said, to answer your original question, once you have your logic working, that statement (if (win == true)) is where you would call the getWinner-method, most likely you want to show an dialog with that message instead of printing it to the console like I did in the above example.

It also seems like this question is more suited for the swing/awt forum, but that's debatable, since it might be a swing program, but it's a String question (in which case it most likely belongs in the beginner forum)

Hope that helps...
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a one method assignment but I’m not sure how to include the strings to say the winner or a tie and where should I put getWinner() in the driver? Please keep in mind this is beginning java, I’m not advanced:) If you can show me how I should tweak with your own code, please do so. I would really appreciate it!
 
Marshal
Posts: 64107
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Does your logic work correctly? . . .

I think that is highly unlikely. AY has tried to use that most unpredictable and unreliable idiom, “if (win==true) ...”, which is not only poor style, but is prone to the error of only hitting the = key once. That will throw all the logic off.
AY: I am afraid I am going to suggest you forget all that code until you have worked out the logic of working out a winner. For a start, which square are you going to start from when looking for a winning move? Write out the logic in simple English, and that will make the correct code so much easier to write.
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So I did it again but now idk where to put it in the driver.
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me? Where should I put getWinner() in the driver class?
 
Marshal
Posts: 13442
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:Can someone help me? Where should I put getWinner() in the driver class?


That's a logic decision. If you don't know where to insert that decision in your program logic, then you don't fully understand your program. As a programmer, it is your responsibility to fully understand your program.

I suggest you read through your driver code again and decide at which point it makes sense to check for a winner.

The design of this TicTacToe game is not very good though. There are too many decisions being made outside of the TicTacToe object that could be made inside of it instead.

Here's an example of a game loop that uses a more encapsulated TicTacToe object:

In this version, the game loop is from lines 3 to 6. This implementation assumes that X always moves first. The logic for tracking who's moving is encapsulated in the game object itself. The logic for switching which player is moving is also in the game object itself. There are only two ways a game can end: 1) The player who moved last completed TicTacToe or 2) All spaces have been marked without a winner. If you check for a winner after every move, there is only one possible winner: the player who just moved. All of these things can help simplify your logic. As it is now, your logic is way too complicated.

You have way too much code in every method and that makes it more difficult to understand and therefore more difficult to debug. Here's the logic I have in my solution:

Do you see how this might help me figure out very quickly which part of my logic might have a bug?

The most important skill in programming is not the mastery of the programming language. It's the mastery of how to attack a problem and break it down into smaller, more manageable pieces. If you don't learn how to organize your thoughts to come up with a coherent solution, then mastery of the language will not get you very far.
 
Campbell Ritchie
Marshal
Posts: 64107
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:Can someone help me?

We already have. As Junilu says, you need to break the task down into smaller pieces. I see you have followed my hint about == true from yesterday, but before that please explain why you are checking every column. That is not how the game is played in real life. You only need to check one column and one row. You might need to check a diagonal, or both, or neither. I shall leave you to work out why.

Where should I put getWinner() in the driver class?

What makes you think the driver needs a getWinner() method at all? What is the logic and concept behind getWinner()? Why do you need to know who has won? If you watch children playing the game, they never ask who has won; they know it already. Think about it. Noughts and Crosses is not like football. In football, you have to wait until the game time finishes and calculate the score to know who has won. There is no need to look for a score in noughts and crosses, nor to try to work out who won. The same applies to games like chess and draughts (US=checkers).
And, “think about it,” is very important. If you spend two hours working out the answers to my questions, you will learn much more than from simply writing code.
 
Campbell Ritchie
Marshal
Posts: 64107
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Here's an example of a game loop that uses a more encapsulated TicTacToe object: . . . .

Would you want that loop in the driver class in the first place?Move the loop into the game object.
 
Junilu Lacar
Marshal
Posts: 13442
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Move the loop into the game object.


Maybe. The main consideration there would be simplicity. I'd watch out for hardening of dependencies between the domain and presentation though. However, my gut reaction to seeing something like

was "Hmm... smells like too much abstraction and possibly tighter coupling with presentation concerns... unless the game object could be configured with some kind of presentation delegate. But that would be more complex and I don't need that right now."
 
Bartender
Posts: 2212
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me throw in my suggestion about a NoughtsAndCrosses class that can be used for a CUI or a GUI board game:

 
Junilu Lacar
Marshal
Posts: 13442
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Looks like a leaky abstraction that smells like a possible case of breaking encapsulation. I would suggest a method, say iterator(), that returns an Iterator instead and that documentation be explicit about the order in which the iterator goes through the board "entries"  (why use such technical-sounding words?).
 
Junilu Lacar
Marshal
Posts: 13442
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Smells like misplaced responsibility. I just don't think a domain object like that should have any kind of association with receiving/giving input/output. Also, the API is unintuitive to me. What are those two 0s in the parameter list? Are those board coordinates? And again, why do the names have to be so clinical? Why can't we just write code that's very conversational and friendly instead of sounding so formal and robotic?
 
Marshal
Posts: 6685
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:


Personally myself I'd probably end up having something like:

Do you think it is worth taking such next step and abstract game a bit more, meaning not to show how the outcome/result is derived, but simply announce it, as well as not to show how game's play flow goes, but simply just play it. The rationale behind, not to show at this level, that really you can't customize game's flow, so why to show it in Game's Launcher class. Do you see it as an improve or over engineering?
 
salvin francis
Bartender
Posts: 2212
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:...I would suggest a method, say iterator(), that returns an Iterator instead and that documentation be explicit about the order in which the iterator goes ...


Interesting, I usually do not use an Iterator for a 3x3 board. Are we taking about linear iteration (top left to bottom right)?  
Also curious, why isn't a multidimensional array good for a board of rows and columns ?

I agree that "entry" is a technical term, maybe I can think of a better name. Also noughtsAndCrosses was not a domain object, it is the game controller. My idea about this game is to define a controller (or an engine) that maintains its own state and allows the state to be changed by calling methods on it. It would ensure that the game rules are followed and it would never go into an undesirable state.

A UI based implementation would query for the states of all entries in the controller and refresh itself to match the returned value.

 
Junilu Lacar
Marshal
Posts: 13442
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Also curious, why isn't a multidimensional array good for a board of rows and columns ?


I wasn't suggesting it was. What's bad is that the return value of getAllBoardEntries() suggests that the underlying implementation is a nested array. That's bad because it's a leaky abstraction. Why would anyone outside of the game object care about what the internal structure of the data store is? Why should that structure be exposed to the client? To a client, it would make more sense if I just got an iterator that I could go through all the spaces in a row-wise manner.

In the above example, game.allMarks() would return an Iterable<Mark>
 
salvin francis
Bartender
Posts: 2212
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:...To a client, it would make more sense if I just got an iterator that I could go through all the spaces in a row-wise manner.


Okay, let's agree to disagree I still think that a nested array would still be good instead of creating a 1 dimensional iterator, additionally, I would return a copy of the array (Effective java:Make defensive copies when needed)
Maybe we could meet mid way and agree on an iterator of MarksRow ? (a bit overkill for a 3x3 cell game)
 
Saloon Keeper
Posts: 10105
212
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a fan of either approach. The iterator is annoying to use when you're only interested in a specified cell, and the array is leaky or involves a lot of copying. The board itself already acts as a kind of matrix, so why not just give it an accessor that allows you to get a mark at a specified location? And instead of an iterator, provide a stream when you're interested in more locations.

Also, it's better to get rid of the NONE constant and use Optional instead.
 
Stephan van Hulst
Saloon Keeper
Posts: 10105
212
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And on special request, the Location class:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!