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....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.
Liutauras Vilda wrote:
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....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.
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 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.
In which case you may find all books difficult to learn from.john quick wrote:. . . I am a visual learner . . .
Campbell Ritchie wrote:
In which case you may find all books difficult to learn from.john quick wrote:. . . I am a visual learner . . .
All things are lawful, but not all things are profitable.
The true visual learner can only learn from Bloch and Gafter's Java Puzzlers.Knute Snortum wrote:. . . Except maybe Head First Java.
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.
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.
At the beginning of a class, that means you have spelt class wrongly or something like that.john quick wrote:. . . class, interface, or enum expected. . . .
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.
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.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. . . .
All things are lawful, but not all things are profitable.
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.
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.
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.
...that would have been a good assignment. That would have tested your programming abilities and brainpower. But the current assignment does neither.Write a program to play noughts and crosses (tic‑tac‑toe) between two people who will use the computer.
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.
There are three kinds of actuaries: those who can count, and those who can't.
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.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |