• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Win or Tie method

 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a complete noob when it comes to Java.  I am using Netbeans to create a tic tac toe game.  My issue is with the win or tie method starting on line 52-58.  I realize what I have currently is wrong, I am hoping for guidance in correcting the issues.  I am lost in what exactly to put in in order to find a win or tie for the tic tac toe game.

thank you

 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't search this forum for noughts and crosses or tic tac toe, otherwise you might find the answer faster
I am afraid your solution doesn't look object‑oriented to me. I would have a Game class, a Board class, a Player class and a Symbol class. You will probably have more classes than that. But I suggest, just for your current problem, you start by writing down the rules, particularly the one about how to win a game.
Then you go through the entire board looking for a row of three. No, you don't. Lots of people think that is what you do, but that is quite incorrect. Consider where you have to start searching for a complete series of the same symbol from. Consider how you are going to work out which squares are in the same row, the same column, the same diagonal (down right) or the same diagonal (down left). Consider how you calculate that from the indices, if you use arrays (and everybody else seems to use arrays). Also work out what the relationship is between the last move and the winner. Once you have all that lot worked out, you will find the win/lose method becomes much easier to write.
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I have noticed as well is that your winOrTie method is of type "int" it's supposed to return an integer of some sort, but it fails to do so.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naziru Gelajo wrote:One thing I have noticed as well is that your winOrTie method is of type "int" it's supposed to return an integer of some sort, but it fails to do so.

That is the smallest problem OP is facing  currently. Having said that, you're right, it shouldn't compile code as it is now, that suggests another thing, that OP is writing code without testing it. i.e. system.out.... should read as System.out....

However, I think OP needs to follow Campbell's advice and re-write the solution in an easier and more readable way by creating some classes to represent the game.

@OP
Is it some college assignment or your own training session? If former, have you been asked to write the code in the way you writing it now? (put all logic in the main() method, use static identifiers prior each method declaration...)

Regardless of the way you do, you need to indent and format your code properly, that would tremendously would help in having program more clean and nicer to read.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Naziru Gelajo wrote:One thing I have noticed as well is that your winOrTie method is of type "int" it's supposed to return an integer of some sort, but it fails to do so.

That is the smallest problem OP is facing  currently. Having said that, you're right, it shouldn't compile code as it is now, that suggests another thing, that OP is writing code without testing it. i.e. system.out.... should read as System.out....

However, I think OP needs to follow Campbell's advice and re-write the solution in an easier and more readable way by creating some classes to represent the game.

@OP
Is it some college assignment or your own training session? If former, have you been asked to write the code in the way you writing it now? (put all logic in the main() method, use static identifiers prior each method declaration...)

Regardless of the way you do, you need to indent and format your code properly, that would tremendously would help in having program more clean and nicer to read.




Yes it is an assignment which is why I'd prefer guidance instead of the answer. Yes the code above is how code has been asked to be written.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry; we don't let people give complete answers. Some of us think this is a programming style which Java® is not ideal for.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john quick wrote:Yes it is an assignment which is why I'd prefer guidance instead of the answer. Yes the code above is how code has been asked to be written.


Would you mind posting the instructions exactly as they were given to you? It's hard to believe that you would be told to write code that way because it's not at all how a Java program for this problem should be written. I'd like to think that something was simply lost in translation.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for the problem you've asked about, look at lines 52-60 and lines 91-102.  Why are those lines very similar? The two sections of code look to me like they represent the same thing. Why have the same idea in two different places in your code then?
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a copy/paste of the instructions...




Data Types and Program Control


Your Project
In this project, you’ll create a text-based Tic-Tac-Toe game in which each player places either an X or O mark on a nine- grid square. An X mark is known as a cross, while an O is called a nought. The winner is the first player to place their mark on three contiguous squares vertically, horizontally or diagonally across. You can read about it in more detail on Wikipedia (http://en.wikipedia.org/wiki/Tic-tac-toe).
The output of this project will be referenced in the subsequent graded projects for this course.

Instructions
1. In NetBeans, create a new Java Application project named TicTacToeGame.

2. Add the following variable and constant declarations to the TicTacToeGame class:

Note: The variable gameboard is a two-dimensional int array. Think of it as a table with rows and columns, and cells can be empty (0) or contain a nought (–1) or cross (1). The constants EMPTY, NOUGHT, and CROSS will simplify your code.

3. Add these utility methods to the TicTacToeGame class:


4. Add the following method signatures to the TicTacToeGame class:
a. Define the createBoard method.
b. Define the winOrTie method. Check first for a win with rows and columns and then diagonally. Finally, check to see if there are any empty cells without a cross or naught.
5. Define the createBoard method.
6. Define the winOrTie method. Check first for a win with rows and columns and then diagonally. Finally, check to see if there are any empty cells without a cross or naught.
static void createBoard(int rows, int cols) { } //TODO Initialize the gameboard
static int winOrTie() {
} //TODO Determine whether X or O won or there is a tie

Study pages 144–145 and 156–158 in your textbook to learn how to initialize and iterate through a multidimensional array. A player wins if all cells in a row, column, or diagonally are the same mark. The players tie if all cells have a mark, but no player has three horizontal, vertical, or diagonal marks. Return NOUGHT if nought wins, CROSS if cross wins, 0 if there’s a tie, and another value (like –2, for example) if there are empty cells on the board.

7. In the main() method, perform the following actions:
a. Create the board and initialize a turn counter, player value, and game outcome. For nought, the value is – 1, while 1 is the value for cross.
b. While there’s no winner or tie, display the board and prompt for a row and column for the current player.
Graded Project

c. Use a try/catch block to handle the set method exception. You can try using the System.err.println method rather than the System.out.println method to output the exception. The message will now dis- play in red.
d. Display the final board and a message on which player won or if there’s a tie.
8. When completed, the contents of the main() method should resemble the following:

9. Compile and run the project to ensure it works as expected. Try a few games to verify all wins and ties are correctly detected.
The application should behave as follows in the Output window:
||||
-------
||||
-------
||||
-------
--O’s turn--
Enter row and column:0 0 |O| | |
-------
||||
-------
||||
-------
--X’s turn--
Enter row and column:0 1 |O|X| |
------- |||| ------- |||| -------
Graded Project
69
--O’s turn--
Enter row and column:1 1 |O|X| |
-------
| |O| |
-------
||||
-------
--X’s turn--
Enter row and column:2 0 |O|X| |
-------
| |O| |
-------
|X| | |
______
—O’s turn—
Enter row and column: 2 2. |O|X| |
-------
| |O| |
-------
|X| |O|
O wins!
70
Graded Project
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. That is quite horrific.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at line 88, where you call the winOrTie() method. The result of that call will be assigned to the variable outcome.  That means that your winOrTie() method needs to return an integer. The value that winOrTie() returns must match one of the values that the code in your main() method expects outcome to be.  If you can figure out what those values are, then you can figure out what winOrTie() needs to return.  Look at lines 17, 18, 19, 92, 95, and 98.

EDIT: I know this doesn't help you much but whoever gave you those requirements should have their license to teach revoked.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not only is the style not what Java® is ideal for, but also I don't believe in being so prescriptive in an assignment. That robs the students of the opportunity of working things out for themselves. It also means you are using a less‑efficient method to test for completeness of the game.
Junilu is correct, I am afraid, that your teaching isn't good.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew it wasn't just me!  Even the book I received is of absolutely no help.  They sort of show examples but their explanations is clearly for someone who has experience in Java already.  I am a visual learner, show me an example and I can usually figure it out.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john quick wrote:. . .  I am a visual learner . . .

In which case you may find all books difficult to learn from.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

john quick wrote:. . .  I am a visual learner . . .

In which case you may find all books difficult to learn from.


Except maybe Head First Java.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as OP said, by "visual" he really means "show me an example."  The problem with that is there are just as many, if not more, bad examples than good examples out there.

Now, as I was saying, the original code has a number of problems related to the winOrTie() method and similarly, the example code I gave had the same kind of problems with the oddOrEven() method. One of the first things that should raise a red flag is the name of the method which contains the conjunction "or".  This is a sign that the method may be trying to do too much. The expression "win or tie" is either true or false so basically, it's a boolean. However, win and tie are two distinct and mutually exclusive outcomes of the game. In addition to that, there is also the outcome where neither win or tie is true. Here is where you have a dilemma. "Win or Tie" is a boolean expression, one that has two values: true or false. Yet, the method winOrTie must be able to indicate one of three possible outcomes: win, tie, or neither.

The solution in the original code was to encode and translate. That is, instead of having winOrTie return the boolean value that the name naturally implied, it was designed to instead encode the state to an int value. Another part of the program, lines 92-102, would then translate or decode that value into a String that would be display to indicate whether someone had won the game or the game ended in a tie or that neither a win or tie had occurred.

This is a bad design.

A better design would be to treat these outcomes separately. You would also rework the logic a little bit.  A win or tie in Tic Tac Toe means the game has ended. When neither a win or tie has occurred, then the game has not ended. So really, the state you should check first is whether or not the game ended.  When you know that the game ended, then there are only two possible outcomes to report: either someone won or the game ended in a draw. This is a better way to structure the logic:


Notice that the above code has introduced an object of type TicTacToe and we've named that object game. If you read that code, you won't see a whole lot of details but you'll know exactly what is going on. This is called abstraction. The original code does not have much of that. It drowns the reader (and the programmer) in details of the implementation.  This is a big part of the reason that OP feels very lost -- you have lost sight of the forest for all the trees that are blocking your line of sight.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:. . . Except maybe Head First Java.

The true visual learner can only learn from Bloch and Gafter's Java Puzzlers.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thank you, I will tinker with it tonight.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the fact that both options return minus 2...
We have already decided that using numbers to represent outcomes is not good programming, but you appear stuck with it. I fail to see why you are trying to get the result randomised, but this is a more elegant way to do it:-
return myRandom.nextBoolean() ? 2 : -2;
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am now getting more errors.  From the 'public static void main all the way down to the bottom, lines 65-99.  It says class, interface, or enum expected.  Also when I click the run project button it now says no main classes found.  Incredibly frustrating for a rookie.  I'm sure it's frustrating for those trying to walk a noob through it it too.


 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john quick wrote:. . . class, interface, or enum expected. . . .

At the beginning of a class, that means you have spelt class wrongly or something like that.
At the end of the class, it usually means you have got one } too many and the compiler perceives you have reached the end of the class before you stopped writing.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding my example that uses a random generator, the use of Random is not supposed to be part of the analogy to what you should do. You can replace rand.nextBoolean() with a method call that does some sort of calculation to help determine what you should return from winOrTie(). But that's the problem with this design that I wrote about previously regarding the dissonance between the meaning we would normally attach to the phrase "win or tie" and returning a number.

Imagine asking someone "Did a win or tie happen?" and getting some number as an answer. It doesn't make any sense. You'd expect an answer of yes or no but that's not what your program does. The design is bad because it doesn't represent a sensible conversation.

That's kind of what I was trying to illustrate with my example, that returning a number instead of a Boolean value when the method name implies a yes or no answer doesn't make a whole lot of sense.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john quick wrote:
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...

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


You're just confused because the design of the code you were given is inherently confusing. This exercise is like giving a rookie mechanic something that's made up of random car parts from a junkyard and expecting him to learn how to fix engines. Regardless of that, with a little bit of effort you can still learn something and maybe get this pile of junk code to work. You're just going to have to keep in mind that the most important lesson you're going to learn here is what bad code looks like and how difficult it is to work with because of the mental gymnastics it forces you to perform.

Your main() method expects some numbers back from the winOrTie() method. Before you can write a correct implementation for winOrTie(), you have to be clear on what all the values are that main() is expecting from it.  You already identified one value, -2. What does -2 mean? What are the other values that winOrTie() should return to main()? What do each of those values mean? Only when you have the answer to these questions should you try to go into winOrTie() and try to write the code that makes it do what it needs to do to return any of those values that you've identified.

Look at what your current implementation does. It has two return statements (on lines 53 and 55) but they return the same value. That's a lot like saying, "If whatever, say 'Minus two', otherwise say 'Minus two'". That makes no sense at all, right? So, forget about writing code for a minute and try to explain what exactly you want this part of your program to do.  If you know what all the different values your main() method expects back from this method, then try to explain in plain English sentences the process for figuring out which one of those numbers to return. When you can explain that process in a way that make sense, then you'll be closer to being able to write the code that does it.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . This exercise is like giving a rookie mechanic something that's made up of random car parts from a junkyard and expecting him to learn how to fix engines. . . .

I would say the exercise is worse that. It is as bad as taking a diesel‑engined car for repairs to the engine and finding they have given the apprentice mechanic instructions about a carburettor.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This probably isn't going to do what you think it will.  The main() method signature that clues Java where to start looks like this:
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

john quick wrote:
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...

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


You're just confused because the design of the code you were given is inherently confusing. This exercise is like giving a rookie mechanic something that's made up of random car parts from a junkyard and expecting him to learn how to fix engines. Regardless of that, with a little bit of effort you can still learn something and maybe get this pile of junk code to work. You're just going to have to keep in mind that the most important lesson you're going to learn here is what bad code looks like and how difficult it is to work with because of the mental gymnastics it forces you to perform.

Your main() method expects some numbers back from the winOrTie() method. Before you can write a correct implementation for winOrTie(), you have to be clear on what all the values are that main() is expecting from it.  You already identified one value, -2. What does -2 mean? What are the other values that winOrTie() should return to main()? What do each of those values mean? Only when you have the answer to these questions should you try to go into winOrTie() and try to write the code that makes it do what it needs to do to return any of those values that you've identified.

Look at what your current implementation does. It has two return statements (on lines 53 and 55) but they return the same value. That's a lot like saying, "If whatever, say 'Minus two', otherwise say 'Minus two'". That makes no sense at all, right? So, forget about writing code for a minute and try to explain what exactly you want this part of your program to do.  If you know what all the different values your main() method expects back from this method, then try to explain in plain English sentences the process for figuring out which one of those numbers to return. When you can explain that process in a way that make sense, then you'll be closer to being able to write the code that does it.



Okay here is an update. I have corrected a couple of issues I was having earlier.  When I compile I no longer get 'no main class found.'  Also, I have corrected an issue in the 'public static void main'.  I no longer get 'class, interface, or enum expected.

I am still working on the winortie() method.  I am now thinking I should have X wins, O wins, and Tie in the winortie method.  I'm thinking this way because they would be the outcome I am looking for.  Also, at the bottom of the main method is the 'switch' (outcome) System.out.println("0 wins!");  System.out.prinln("X wins!");  System.out.println("Tie.");  Those are the three outcomes the tictactoe game would have.  Tell me if I'm hot or cold on this...lol.  If hot then I will need to figure out exactly how to write the winortie() code.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't quote entire responses, it makes the thread unnecessarily long and more difficult to follow.

You're getting warmer but you're still missing one outcome. You have only accounted for X wins, O wins, or X and O are tied. There's a fourth outcome. Again, what does -2 mean in that do-while condition?

This again demonstrates how poorly formulated this exercise is. It's giving you a lot of breadcrumbs to follow but I think it's leading you down a path that's confusing rather than illuminating.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-2 would represent empty cells on the board.  
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, so now you know all the questions that the winOrTie() method must answer.

1. Did X win? If so, return (what value?)
2. Did O win? If so, return (what value?)
3. Did a tie happen? If so, return (what value?)
4. Are there empty space still on the board? If so, return (what value?)

This makes it clearer how illogical it is to name that method winOrTie.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so I am now not getting any errors at all.  It's nice not to see a lot of red errors.  When I try to compile the tic tac toe game is continuously runs.  I stop it manually and it shows the following...

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

| | | |
------

| | | |
------


-0's turn-

My winOrtie method is as follows so far...



I know I'm not done though.  In the instructions it states:

Define the winOrTie method.  Check first for a win with rows and columns and then diagonally.  Finally, check to see if there are any empty cells without a cross or nought.  



Study pages 144–145 and 156–158 in your textbook to learn how to initialize and iterate through a multidimensional array. A player wins if all cells in a row, column, or diagonally are the same mark. The players tie if all cells have a mark, but no player has three horizontal, vertical, or diagonal marks. Return NOUGHT if nought wins, CROSS if cross wins, 0 if there’s a tie, and another value (like –2, for example) if there are empty cells on the board.





Earlier in the instructions it states:

Note: The variable gameboard is a two-dimensional int array. Think of it as a table with rows and columns, and cells can be empty (0) or contain a nought (–1) or cross (1). The constants EMPTY, NOUGHT, and CROSS will simplify your code.



How am I doing?  Am I on the right path?  The fact I have no red errors is a good sign.

Thanks again!

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I honestly can't tell how you're doing. I thought you understood what you needed to do after listing out the outcomes you'd have to account for in the winOrTie method but in this last post you seem to still not know what to do in there. Getting rid of compile-time errors is good but you still haven't shown any progress in making winOrTie() do something that it's supposed to do.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't help that the instructions are not actually clear. If the instructions had read like this:-

Write a program to play noughts and crosses (tic‑tac‑toe) between two people who will use the computer.

...that would have been a good assignment. That would have tested your programming abilities and brainpower. But the current assignment does neither.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I said, not clear, I meant the instructions for winOrTie. Even that little bit of instructions isn't clear.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed on the instructions.  It is confusing to say the least.  It is true that I'm still having problems in the winortie method.  It all boils down to not knowing exactly what code to insert.  As I stated earlier the book and the assignment instructions are of no help at all.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing you need to do is forget about Java and just state in plain old English sentences what you need to do. When you can say it clearly and precisely in English then translating to Java is much easier. Don't get so caught up in the technical details that you lose sight of what you're really trying to do: solve a problem.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john quick wrote:Agreed on the instructions.  It is confusing to say the least.  It is true that I'm still having problems in the winortie method.  It all boils down to not knowing exactly what code to insert.  As I stated earlier the book and the assignment instructions are of no help at all.


Well, I have to disagree with you and some of the other repliers. I think the instructions given are pretty straightforward, although the way you presented these are awkward. Were they rally presentd this way?

You state that the instructions and the book are of no help with the winortie method. I'm not sure about that. First of all, it is clearly explained in what cases there is a winner or not. And you are being pointed to the course book that handles how to travel through a multidimensional array.

Given that, can you explain what particular problems you have with the winortie method?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Study pages 144–145 and 156–158 in your textbook to learn how to initialize and iterate through a multidimensional array. A player wins if all cells in a row, column, or diagonally are the same mark. The players tie if all cells have a mark, but no player has three horizontal, vertical, or diagonal marks. Return NOUGHT if nought wins, CROSS if cross wins, 0 if there’s a tie, and another value (like –2, for example) if there are empty cells on the board.


To Piet's point, we probably should make a distinction between the instructions being clear and being logical.  As far as the former, I'd tend to agree, the instructions are pretty clear as to what the student is expected to do. In fact, the instructions are also actually complete in specifying what the winOrTie() method must do; it points out the four distinct outcomes we've already identified in the preceding discussion.

As far as being logical, however, the behavior that the instructions specify is not very different from saying that you had to make a light switch that has only ON and OFF positions but can completely control a variable wattage lightbulb with three different brightness levels and an unlighted state. In this sense, there's a mismatch between what the name winOrTie() implies (true or false) and the set of outcomes (four distinct values) that the method is supposed to report.
 
john quick
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the instructions that I have posted are exactly as is called for.  I honestly don't really know what exactly is supposed to be inserted.  I have highlighted in bold the trouble area.  According to the instructions I am to have a game that works and am to verify all wins and ties are correctly detected.  So I am to insert code that shows X wins, O wins, or a Tie.  What I have apparently isn't correct. "game board[0][1] == game board[0][0].  I have even been using Google for examples of methods and there are numerous method examples.  So what exactly goes in between the parenthesis and after?  I have been working on this tictactoe assignment as a whole, since November.  So I am beyond frustrated.

static int winOrTie() {
       if (gameboard[0][1] == gameboard[0][0]){
        return -2;
   } else{
           return -2;

           }
   }
reply
    Bookmark Topic Watch Topic
  • New Topic