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

console tic tac toe in java

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an assignment for college and i need to create a console version of tic tac toe with some code they have provided to us. I have been looking at this code for hours and can't figure it out. I get a null pointer exception when i run it.  I just need to get the ball rolling so to speak so i can continue.



this is the contructor for the grid object, i am getting a null pointer exception when it trys to create the board variable. one thing i notice here is the loop doesn't appear to be iterating col and row are always 0 and FYI ROWS and COLUMNS are both set to 3. it is a 3 by 3 games of tic tac toe.



heres how i've setup the Box constructor



i am sorry if this doesn't make a lot of sense, i didn't want to paste the entire codebase as it would be too long.
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have written does make sense. You need to be careful about posting too much code in case you are accused of taking too much outside help.

Please start by showing us what your design for the game is. You want to have the design completed before you write a single line of code. I worry about assignments with over‑specification, because that constrains you to what might not be the best possible design. But you are stuck with it.
 
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a single way to put you back on the track, but I will ask some of the questions along the way, so you possibly could improve your program.

First, I don't see the reason why do you have:
private Player winner defined in your GameMain class? What purpose it serves? Does the assigned some non-null value means there is a winner, and so the null value means what, tie?

Could you check the board for the 3 consecutive same symbols to see if there is a winner? So that would eliminate the need to have extra Player field.

I personally don't like the class name GameMain. It doesn't tell me what game that is, nor what that Main is. Is there also GameNonMain? (Sure not, but see how this Main doesn't make sense). Why not just call it TicTacToe?

[will continue]
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need variable private boolean gameOver;? You left the comment next to it: "Whether game is playing or over".

Isn't the game over when there is winner or tie?
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think I like your intentions of what you are trying to achieve here, but the class name also doesn't communicate well. Why content of it is Player? Isn't the symbol (O or X) what Player places?

Do you think you might would have cleaner design if row and column would abstract to a location? So you could say:
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . I personally don't like the class name GameMain. . . .

Maybe it was specified in the code supplied to start off the assignment.
I think I would retain player fields, but you would need two of them, or maybe an array. Having a player field makes it easier to swap player in and out as current players.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Liutauras Vilda wrote:. . . I personally don't like the class name GameMain. . . .

Maybe it was specified in the code supplied to start off the assignment.


That would be more worse, wouldn't it
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is what I meant by, “over‑specification”.
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Doug,

Is that the complete code for your Grid and Box constructor?

If so, where are your board and content variables initialized? (in Grid and Box, respectively)
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes like Grid and Box smells like over-engineering to me. If that code is what the instructor gave then is there any supporting text that explains what these classes are supposed to be responsible for and how they are supposed to work together? I hope the students aren't expected to just "figure out" what the design intents were in coming up with those classes
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . . I hope the students aren't expected to just "figure out" what the design intents were in coming up with those classes

No, that sounds dreadful.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Doug Xander wrote:
this is the contructor for the grid object, i am getting a null pointer exception when it trys to create the board variable. one thing i notice here is the loop doesn't appear to be iterating col and row are always 0 and FYI ROWS and COLUMNS are both set to 3. it is a 3 by 3 games of tic tac toe.



Line 7 is not "trying to create the board variable" as you say. Rather, that line is trying to access an element of the array referenced by the board variable.

How is the board variable declared and initialized? You will get a NullPointerException if you try to access array elements before you've even created an array and assigned it to board.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . . I hope the students aren't expected to just "figure out" what the design intents were in coming up with those classes

No, that sounds dreadful.


Yeah, but I suspect that could very well be what's happening, unfortunately.
 
Doug Xander
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone,

thank you for your input so far.
to answer your questions, yes that is exactly what we are expected to do, look at some code that has already been written, and fill in the missing parts to make the application work. i think the idea is this is kind of the scenario that you might face as a real world programmer.
i would have prefered just to create it from scratch as i would have probably done it differently, but it is what it is i guess.

the easiest way would be for you to have a look at the full code on the git hub page. im not even sure where i should start with this code, the first step i wanted was just to get it to print and empty 3x3 grid to the screen and then move through the code from there.

https://github.com/BIT504-OP-CourseWare/Assignment2/tree/master/Assignment2/src
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Doug Xander wrote:i think the idea is this is kind of the scenario that you might face as a real world programmer.


No, that definitely isn't how real-world programming is done.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . I suspect that could very well be what's happening . . .

I have complained about assignments before, haven't I.
 
Doug Xander
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What you have written does make sense. You need to be careful about posting too much code in case you are accused of taking too much outside help.

Please start by showing us what your design for the game is. You want to have the design completed before you write a single line of code. I worry about assignments with over‑specification, because that constrains you to what might not be the best possible design. But you are stuck with it.



yeah its ok for us to ask for help, unfortunately my tutor isn't much help. im not expecting the code to be written for me..just being pointed in the right direction would be a good start..as i am really stuck.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you start with a description of the game. That is the basic specification you have to work to. You may already have been given such a specification.
 
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just had a look at the code in GitHub, and the bashing it is given here is unjustified. It is a simple setup with three classes and an enum.

@OP
are you asking for a lecture here in Game Design? Or do you want a solution for your NullPointerException (see your opening post). In the latter case: answer Alfars questions.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:I just had a look at the code in GitHub, and the bashing it is given here is unjustified. It is a simple setup with three classes and an enum


I know it doesn’t help with solution, but the given template is terrible. Comments are off too. Clearly somebody didn’t invest a reasonable amount of time in setting this up.
 
Doug Xander
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alfar Travers wrote:Hi Doug,

Is that the complete code for your Grid and Box constructor?

If so, where are your board and content variables initialized? (in Grid and Box, respectively)



box:



grid:


i don't want a lecture in game design, an answer to why i am getting a null pointer exception here might help me progress with this. i don't really get what is meant by

// TODO: Initialise the board array using ROWS and COLUMNS

what does he mean initialise the board?
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Doug Xander wrote:i don't want a lecture in game design, an answer to why i am getting a null pointer exception here might help me progress with this.


You are getting an exception because Box[][] board; an array of arrays never gets initialized, while is being tried to set its elements.

Do you know how to initialize an array? An array of arrays?
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

// Define the amount of rows and columns


I know you can't alter that, but just for your reference. The numeric value of the rows and columns are better called as "Number of rows" and "Number of columns", not the amount.

If that were you, spell such variable in full - numberOfRows or if that were constant, then NUMBER_OF_ROWS and make such variable final and private as you don't want it to be altered by some other parts of the code.


So what you have to do?

There are comments which contain "TODO", and this is esentially what you have to do - it is explained in there. For instance:

I think you got that part correct in this set-up. A bit weirdly reads content = Player.EMPTY, but that's what it is.
 
Bartender
Posts: 20980
128
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Classes like Grid and Box smells like over-engineering to me.



Yeah, but the last time I did a TicTacToe game was in FORTRAN IV, which was literally decades before Java was invented. FORTRAN wasn't an object-oriented language, so I used numeric values.

These days, I'd probably define an enum:


Note: the above is probably neither syntactically valid Java or C++, but presumably you can figure it out.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


An else if part might be confusing for you, so the "appropriate" value is so called no value - a null, when the reference variable does not refer to any object.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other than that, it is a good chance to get a good mark, so roll up your sleeves Just the chosen form to introduce this exercise is a bit questionable, that really doesn't help to reveal student's creativity.
 
Doug Xander
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right i did not initialize board

i have another issue now but at least this helps me move on from this one.

thank you.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't go so far as to say the reservations expressed so far were wholly unjustified but the starter code is certainly not as bad as I imagined it would be. There are things that could be done better, certainly, but if you have been reasonably taught and followed along with some degree of attention and practice I imagine it shouldn't too hard to fill in the missing parts.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An enum named "cellContents" would probably be better named something like "Mark" with values X, O, and NONE. The former is too tied to implementation. You dont play tictactoe and say "I'm going to put X as the cell content." Rather, you would probably say "I'm going to mark this square/spot with X" or "O" as the case may be.
 
Doug Xander
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
having said all of the above, does anyone know why my board is printing all messed up? example output
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Doug Xander wrote:having said all of the above, does anyone know why my board is printing all messed up? example output


When asking questions like this, it's best to show the code that produced that output. Otherwise, we'd be just guessing at the problem.
 
Doug Xander
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Doug Xander wrote:having said all of the above, does anyone know why my board is printing all messed up? example output


When asking questions like this, it's best to show the code that produced that output. Otherwise, we'd be just guessing at the problem.



i fixed that problem, just a problem with using system.out.println instead of just print

i have a new problem with my tic tac toe game though. i have a method that checks for the winner that looks like this



and here it where currentRow and currentCol are populated




this issue is


works to check the row if it has won

but for some reason



does not work to check the column if it has won

seems logical that it would work..but it doesn't.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Does this index within the first pair of brackets really refer to column?
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an array of arrays, or perhaps how you have been told 2D array, the content looks like:


Let's say you have:

If you write:
A[rowIndex][0] you'd get 3
A[columnIndex][0] you'd get 3 <-- that is not what you want

If you were to write:
A[0][columnIndex] you'd get 1
A[0][rowIndex] you'd get also 1 <-- that is not what you want

In other words, first pair of brackets refer to row, while second pair of brackets refer to column if you were imagine an array of arrays to be written as I did.

 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must say I'd apologise probably an instructor, there is a reason why so much code were written for students.

So basically the design was provided (regardless of the quality of some of the parts), while the students are expected probably just to solve some logical problems as it seems they aren't there yet to design such game themselves.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Needless to say, that code for checking whether you have a winner is incorrect. You are checking thee squares per row/column. You don't check thee squares. You check two. At least you haven't fallen into the trap of testing all three rows
And you are always checking the diagonals. You only check them sometimes.
You need some design thoughts before you implement all that lot.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lack of decomposition to methods is what makes all this more complicated than it could be.

@OP
Are you allowed to decompose the provided methods to some smaller methods. That's what you supposed to do.

For instance if you are given an index of the row, you could create a method which returns an array of those 3 elements of the row. Same with columns. Same with diagonals. By looking to those indices all over the place and comments what supposed to check what is pretty dreadful.
 
Doug Xander
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:In an array of arrays, or perhaps how you have been told 2D array, the content looks like:


Let's say you have:

If you write:
A[rowIndex][0] you'd get 3
A[columnIndex][0] you'd get 3 <-- that is not what you want

If you were to write:
A[0][columnIndex] you'd get 1
A[0][rowIndex] you'd get also 1 <-- that is not what you want

In other words, first pair of brackets refer to row, while second pair of brackets refer to column if you were imagine an array of arrays to be written as I did.



this is what i needed thanks. i am learning this is just my first semester of java programming.
 
Junilu Lacar
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Doug Xander wrote:this is what i needed thanks. i am learning this is just my first semester of java programming.


To that point, I think Piet's sentiment about some of the comments made in this thread is something we probably should note and perhaps use as a gut check.

While many of us may not think this is a very good exercise or a good way to teach programming in general, it is what OP has been given by his instructor. Perhaps we should just take the same tack as Piet right now and not muddy the waters for OP with comments about concerns other than just completing this assignment. Comments about design and object-orientation might be well-intentioned but I doubt they will be of much use or even understood by OP at this point of his learning journey.
 
You didn't tell me he was so big. Unlike this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!