• Post Reply Bookmark Topic Watch Topic
  • New Topic

TicTacToe AI not taking the win  RSS feed

 
James Kilgarriff
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to Java, learning for approximately 2 months, and I am creating a TicTacToe game for a university assignment. For part of my game, I need to create a smart AI and a naive AI. I'm having a lot of trouble with my SmartAI class, it doesn't take the win when the opportunity presents itself. All that happens is that the buttons within the GUI disable and I get the following errors...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GameBoard.getPos(GameBoard.java)
at GameBoard.move(GameBoard.java)
at GameLogic.move(GameLogic.java)
at SmartPlayer.takeTurn(SmartPlayer.java)
at GameLogic.promptMove(GameLogic.java)
at GameLogic.move(GameLogic.java)
at TicTacToeGUI.actionPerformed(TicTacToeGUI.java)


All other aspects of the game work, my naive AI takes random moves as needed, and wondered if someone could point me in the right direction as I have been staring at this for 2 weeks now and I can't see where I am going wrong. I apologize in advance for the amount of code I've posted, any help would be greatly appreciated.

Abstract ComputerPlayer class


SmartPlayer class

Apologies for the number of if...elseif...else statements, I want to get this working first before experiment with a shorter sequence/tidy things up.



GameLogic class



GameBoard class

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, James Kilgarriff!

Good first post!  Well formatted; you posted the error message, but...

You didn't post the full stack trace.  Specifically, we're missing the line number of the program where the NullPointerException (NPE) occurred.  We're also missing the code of for TicTacToeGUI.  And I believe we're missing the import and package statements of the programs.

So first, I would post the entire stack trace (the error message you get).  We can go from there.

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other points:

* If you have too many classes to post, you can create a free https://github.com account, create a repository and post your code there.  Then you can just post the URL to the repository.

* In general, an NPE means that some object reference was null when it shouldn't be.  If the line of code where you get the error looks like this: foo.bar(); that would mean the foo is null.  Then you would start looking for why.
 
James Kilgarriff
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies, still getting to grips with Java and the forums in general!

Below are the errors with the line number, as well as the TicTacToeGUI class. I can't see where to edit my original post so I will also state the imports and their classes below:

GameLogic class - import java.util.ArrayList;

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GameBoard.getPos(GameBoard.java:13)
at GameBoard.move(GameBoard.java:37)
at GameLogic.move(GameLogic.java:123)
at SmartPlayer.takeTurn(SmartPlayer.java:116)
at GameLogic.promptMove(GameLogic.java:155)
at GameLogic.move(GameLogic.java:142)
at TicTacToeGUI.actionPerformed(TicTacToeGUI.java:112)


 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the exception is thrown by line 13 of the GameBoard class, which as you can see is this one:



That means that either board is null, or that board[pos[0]] is null. It's pretty clear that board isn't null, because right there in the constructor you assign it a value which is clearly not null. And you don't ever assign it any other value. So it follows that board[pos[0]] is null.

You're missing something about Java arrays; when you create an array of an object type (i.e. not a primitive type like int or double), the array is filled with null values. So your ComputerPlayer[3][3] is an array with three entries which are all null. Hence the exception.

You'll need to initialize each of those array entries to hold a ComputerPlayer[3] array; and of course each of those will be an array with three entries which are all null, so you might need in turn to assign something non-null to those entries. I'm not sure what you expected their initial values to be -- maybe null would be okay.
 
James Kilgarriff
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a NaivePlayer class that works, no issues with the GameBoard at all, this is why I'm stumped as to why it doesn't seem to work with my SmartPlayer class.   
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Kilgarriff wrote:I have a NaivePlayer class that works, no issues with the GameBoard at all, this is why I'm stumped as to why it doesn't seem to work with my SmartPlayer class.   

This very last post of yours is confusing whether you understand the issue or you don't. How is actually?
 
James Kilgarriff
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand. I'm attempting to set the array values of GameBoard in SmartPlayer. It's ok that they are initially set to null because the GameBoard is empty at game start.

This is my NaivePlayer class...



It extends ComputerPlayer, which holds the randomPos method that sets the array, and displays it within the GameBoard correctly. My problem (I think), is within the SmartPlayer class, it's not setting value for the array, and I'm struggling to see why.

I'm a complete n00b with this, so I apologize if I'm completely wrong, but I'd be extremely grateful for any help.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not really sure why any kind of Player should be initializing the GameBoard. Why doesn't the GameBoard initialize itself by setting up an empty board configuration to start with?
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, nope, probably makes little sense the design you came up with.

Apart from the fact that you pass in GameBoard and GameLogic to your NaivePlayer couple of methods, in neither of them you use them. However, doesn't make sense to pass Board to Player nor GameLogic. Well, GameLogic might could be researched a bit more, I could think of cases where it might would make sense, but let's see what your thinking is.

So why you chose that design?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!