Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Win or Tie method  RSS feed

 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a problem with that code. Or more exactly, I don't see why the compiler has a problem with it. (It's much more complicated than it needs to be, if you just want to return -2 in all cases.)

At least I assume you're talking about a compiler error, but you didn't say that either. You just seem to be dissatisfied with it for some reason and it would help if we knew what that reason was.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct in that I am not getting any errors now when I run the project.  Yet when I click to run the project it now continuously runs.  That and in the output section it only shows this...

run:
| | | |
------

| | | |
------

| | | |
------


-0's turn-


It is supposed to look like this...

||||
-------
||||
-------
||||
-------
--O’s turn--
|O| | |
-------
||||
-------
||||
-------
--X’s turn--
|O|X| |
------- |||| ------- |||| -------

--O’s turn--
|O|X| |
-------
| |O| |
-------
||||
-------
--X’s turn--
|O|X| |
-------
| |O| |
-------
|X| | |
______
—O’s turn—
|O|X| |
-------
| |O| |
-------
|X| |O|
O wins!


 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let's try to step you through the logic, since you really don't seem to be getting it. That or you just ignored the hints you've already been given.

Answer these questions: how can you tell that X won? What value does the code in your main() method expect to get back from winOrTie when X has won?

The code that implements the answers to these questions is part of what needs to go in that method. Can you at least try to answer these questions first?
 
Mike Mo
Ranch Hand
Posts: 40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john quick wrote:
Junilu Lacar wrote:Ok, here's a very small example.

This example is almost analogous to what you need to do with your winOrTie() method.

Note that there are a number of things that are not very good about this kind of code. These are pretty much the same kind of problems in the code that you gave. I have only structured this example so that it's easier for you to see the similarities with the code you're trying to complete.


Okay, so in your 'if' statement if(rand.nextBoolean)) }.  You have 'rand' (which is short for random since you created a random example,)

private Random rand = new Random();

I am looking at mine and am trying to figure out what to substitute.  Would tictactoe be used?  Or "turn", "outcome", "Playerval" be the way to go?  Am i hot or cold? I'm thinking something like this...


static int winOrTie() {
        if (tictactoe.nextBoolean){
         return -2;
    } else{
            return -2;
            }
    }

I'm using -2 because later in my code outcome = -2.  I feel dumb....trying to learn this.


I'm looking at your code as a complete noob and going on my 4th week coding now and thinking to myself... hope I'm at this point soon haha xD  I have had the EXACT same problems with my professor. she wants us to do an assignment and to do so we have to follow steps and not stray from that path. this is a kind of shitty way to go about things in my opinion. its like teaching an abstract art class but saying you can only draw a house and only use the color red.... why limit something that has almost no limit to way to do something. let the student do it their way, evaluate their output, then comment on what would/should be changed to make it better or more sound. I'm currently trying to find a friend that can remote view my desktop and skype/discord while they help me. this is an amazing way to see examples and how they are built. maybe try doing that? I have had little lucky bc by buddy just moved to Israel lol but I'm going to try to keep in contact with him as it helped a lot...well the 1 time we did it anyway.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The art analogy is pretty good. There's a lot of creativity and abstract thinking involved in programming and I don't think that being too prescriptive with the assignments helps students learn.  It's like teaching people how to paint using those color by numbers coloring books.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can tell X won with the code in the main() method....



Within the body of the method, you use the return statement to return the value.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I whole heartedly agree Mike!  We're in the same boat and it's to the point I'm down right burned out on this particular assignment.  My instructor informed me I cannot proceed further until I get this stupid tictactoe game compiled right.  Reason is the next four assignments are extensions of this assignment.  My instructors can't really aid me (a hint here or there) is all they give.

It's like telling someone in another language how to do something yet not giving that person the translation.
 
Junilu Lacar
Sheriff
Posts: 11138
160
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:I can tell X won with the code in the main() method...

Sigh.

No, John, that's not the answer we're looking for. That's like me asking you "How can you tell someone won?" and you saying, "When somebody jumps and up and yells, 'I won!'"

You have to think logically about these things. Let me rephrase the question...

In your winOrTie() method, how do you calculate whether X won the game and what value should you return when you determine that has happened?

I know this stuff may seem confusing but if you just stop and think logically about it for a minute, it's really not that hard.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, been away for a few days.  You calculate whether X won by using an if statement.  An if statement is used to specify a block of code to be executed.  If a specified condition is true.  An else statement is used if the same statement is false.  The value you return should be true since writing the code that X won.  So just starting the method like this...


          

 
Campbell Ritchie
Marshal
Posts: 55683
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john quick wrote:. . .  So just starting the method like this...

Please explain that; it doesn't look right to me.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we need to prime the pump a little at this point.

This is basically what you need to do in that method. All our questions have been trying to lead you to figure this part out. Now that I've primed you with this part, you need to figure out the logic that would go into a method that checks to see if X won, another method that checks to see if O won, another method to check if there are empty spaces on the board, and finally figure out what to return if none of those other outcomes happened.

If you still don't get it, try reading that code out loud and really listen to the logic that code is trying to express.
 
Norman Radder
Ranch Hand
Posts: 146
4
 
Junilu Lacar
Sheriff
Posts: 11138
160
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:You calculate whether X won by using an if statement...

No, that's not what we were trying to get you to think about. We're not asking you all these questions as a test, we're asking them to lead you to think about what you need to do logically, not mechanically. "Use an if-else" is thinking mechanically. "Check the board to see if there are three X's in a row, column, or diagonal" is thinking logically.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it's not correct, but how close am I getting?  Thanks for your patience everyone.  My call sign should be John 'slow' not 'quick'.



 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're off by a mile. It almost seems like you're just making wild guesses. There's no way that code would even compile.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, one more try...

If I told you to "go and check the game board to see if there are three Xs in any row, column or diagonal," what would you do? Don't think about the Java program for now, imagine we were playing on a real board game. You'd look at the actual board to check, right? You wouldn't pick up the box the board came in, look at the picture printed on it and say, "Let's see, does it say X won on here, or does it say O won...?" That would just be silly but that is essentially what your code does in that last guess that you posted.

How do you look through the game board in your program and check to see if X has three in a row in one of the board's rows, columns, or diagonals?

Please THINK carefully when you answer this question because I'm going to have to give up if you come back with another wild guess.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another thing that might help you think about my question.

You already have code that goes through your game board, row by row and prints out the boxes and marks made by the players, right?

How do you write code that goes through your game board, row by row and counts how many Xs or Os have been marked? How would you know that X won? That O won? Do you see why my code had if (xWon()) and if (oWon())?
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rows and columns must be in the winorTie method code....i.e. [rows] [cols].  I'm visualizing something to the extent of.......if (gameboard [rows] [cols]; == EMPTY) {
 
Campbell Ritchie
Marshal
Posts: 55683
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to test == EMPTY? If you are testing for cross, all you need to look for is other instances of CROSS.
I still think it is unfortunate that you have been told to do it this way because there is a much more elegant way to check for a win depending on the most recent move.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was a typo, I have CROSS there in my actual code.
 
Junilu Lacar
Sheriff
Posts: 11138
160
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:It was a typo, I have CROSS there in my actual code.

Same difference.

The expression gameboard [rows] [cols] == CROSS only looks at a single box on the board.  So, basically that says something like "If there is an X in the bottom left box, then CROSS wins."  Does that sound right to you?

I already hinted at you that you have code that already goes through all the boxes in the gameboard and displays them either as empty or with a mark of X or O in it.

Likewise, to check if X won, you'd have to write code that goes through all the boxes in the gameboard and counts how many of those boxes has an X mark in it.

I don't know how to make that any clearer without actually writing the code for you, which of course we won't do.

Good luck.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just occurred to me that maybe the reason you're having so much trouble with this is that you just don't understand how arrays in Java work. I think we've been going on the assumption that you understood at least the basics of array creation and manipulation. Were we wrong in thinking that? Maybe you should go through the Java Tutorial about arrays first so you can have a clearer picture of what arrays are and how you can work with them in code.

What I've been trying to get through to you in my last few responses is that you can't just examine one spot on the board. That's essentially what you're doing by writing if (gameboard[row][col] == CROSS) -- you have to iterate over the gameboard so that you're examining as many of that array's elements that you need to make some kind of determination.  Iterating over an array is one of the things that you should have learned by now. If you haven't or are not clear on how to do that, then please go through that tutorial or review the notes and lessons you've been given. Without a basic understanding of how to iterate over an array, we'll be stuck here just going around in circles.

The Java Tutorial that I linked to above doesn't actually give any good examples for iterating over an array. You can find better information and examples by searching for Java for loops and arrays
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good information, yes you're correct I am not all that familiar with arrays.  This tictactoe project is only the second one in this semester I am in.  It is also the first in which we deal with this stuff.  So I will do some google searches and review the link you gave.  I will also look through my text book for arrays.

Thanks
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, here is an update on my winOrTie method...

 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You brute forced what you could have done in a loop. Lines 2 - 37
2. Some that logic looks wrong. Lines 39 onwards
3. It's incomplete: you only return two possible values. You are missing two more values: one to indicate a tie, another to indicate that nobody has won yet and the game should continue.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do admire that you still haven't given up on finishing this. Aren't you well past the submission deadline at this point?
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will tell you this: even though you brute forced those checks on lines 2-37, that code is a small step in the right direction. There's still a lot of room for improvement though.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, yes I realize I have to insert two more values.  You are referring to a for loop correct?  I have until February 2018 to finish this semester, (it's an online college).  I can also ask for an extension if need be.  This college I am attending is a go at your own pace type.  Of course I am way behind.  If I can get past this particular assignment I can catch up.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm referring to a for loop which would have helped you avoid the mistake you made on line 4. Examine that line very carefully for differences with all other similar lines.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woops!  Had a plus sign in their by mistake.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's one danger of duplicate code. You may not think that all those lines are duplicated because they are all using different values for the array index but the idea is the same: you're trying to sum up a row or column of elements. It doesn't matter that that the indices are different, it's still code duplication and that's always a bad thing because it invites bugs like this one you didn't notice had I not brought it to your attention.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know there is no for loop but here is an updated version...


 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would work. Now try to clean it up. Try not to leave messy code behind even if it already works. Messy code will always find a way to come back and bite you. Don't wait for that to happen--clean up as you go or suffer the consequences later.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only problem I am having now is when I compile I still get the following in the output...

| | | |
------

| | | |
------

| | | |
------


-0's turn-
Enter row and Column:




I am supposed to have this in the output...


||||
-------
||||
-------
||||
-------
--O’s turn--
|O| | |
-------
||||
-------
||||
-------
--X’s turn--
|O|X| |
------- |||| ------- |||| -------

--O’s turn--
|O|X| |
-------
| |O| |
-------
||||
-------
--X’s turn--
|O|X| |
-------
| |O| |
-------
|X| | |
______
—O’s turn—
|O|X| |
-------
| |O| |
-------
|X| |O|
O wins!

I had to declare the variable 'turn' because it isn't declared in my code.  Same with TIE.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Please use code tags around the text that you want to show as example output. Nobody can make sense of what you're posting because it looks all garbled.

2. I'm pretty sure that what you say is supposed to be your output is just an example of what it might look to actually play the game with your program. There are literally tens of thousands of ways a tic tac toe game can go (depending on how you count, anywhere from 26,000+ to 255,000+ ways). Look up Game Complexity. Just want to make sure you understand that.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apology..

This is what I am getting in the output...







This is what is supposed to be in the output...







 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so we're on the same page, please confirm that you understand that what you say is "supposed to be in the output" is only an EXAMPLE output.  You can't expect your program to output that same sequence of moves every time you run it. That wouldn't make sense.  As I said, there are literally tens of thousands of possible outputs you will see from the program depending on what moves are made. What you showed there, which by the way still looks messed up, is only one of thousands of possible outputs you will see.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the issue, the printed assignment shows the out put looking like that.

Anyways I was chatting with my instructor.  He said the following...

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

i

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.



 
Liutauras Vilda
Marshal
Posts: 4635
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand the problem your instructor pointed out?
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!