Issue
I am currently working on a Tic Tac Toe game and I am running into a problem that may or may not have an easy fix. I am making a class and method-based Tic Tac Toe game, I am working in multiple classes which have numerous methods.
Here is my problem:
I have a class that is responsible for setting owners to certain tiles on the board.
package game;
public class GameTile{
/**Stores a String representing the owner "X" or "O" of a GameTile
*/
protected String owner;
/**Constructs a GameTile, sets owner to null by default
*/
public GameTile (){
owner = null;
}
(I have more code, although I only showed the important stuff, the rest of the code basically declares "X" and "O" etc...)
Now I am taking the above class and printing it into the game board class.
package game;
/**Class for Tic Tac Toe gameboard
*@author Put Name Here
*/
public class GameBoard{
protected static char[][] cells;
/**An array of 9 GameTiles representing the TicTacToe gameboard
*/
GameTile[] board;
/**Constructs an empty gameboard of 9 GameTiles and fills it with unowned tiles
*/
public GameBoard(){
board = new GameTile[9];
for (int i = 0; i < 9; i++) {
board[i] = new GameTile();
}
}
/**This will draw the current gameboard on the screen
*/
public void drawBoard(){
{
System.out.println();
System.out.println("+---+---+---+");
System.out.println("| " + board[0] + " | " + board[1] + " | " + board[2] + " |");
System.out.println("+---+---+---+");
System.out.println("| " + board[3] + " | " + board[4] + " | " + board[5] + " |");
System.out.println("+---+---+---+");
System.out.println("| " + board[6] + " | " + board[7] + " | " + board[8] + " |");
System.out.println("+---+---+---+");
}
}
}//end class
Everything in my code seems to work fine, although when I print anything from "board" (board[1], board[2], etc...) the output is always something like this: "game.GameTile@7de26db8"
Is there any way I can get my code to print the String values from GameTile()?
Solution
For the way you're using it, override the toString()
method in your GameTile class. What you're seeing now is the result of the Object class' version of that method.
Answered By - nitind
Answer Checked By - Gilberto Lyons (JavaFixing Admin)