permaculture playing cards*
The moose likes Beginning Java and the fly likes tic-tac-toe board Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "tic-tac-toe board" Watch "tic-tac-toe board" New topic
Author

tic-tac-toe board

kofi ofei
Ranch Hand

Joined: May 22, 2005
Posts: 39
the programme below contains no errors but when i run it nothing is displayed can u please help me rectify this problems. thanks you again!!!


/**
A 3 x 3 tic-tac-toe board.
*/
public class TicTacToe
{
/**
Constructs an empty board.
*/
public TicTacToe()
{
board = new char[ROWS][COLUMNS];

// fill with spaces
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = ' ';
}
/**
Set a field in the board. The field must be unoccupied.
@param i the row index
@param j the column index
@param player the player('x' or 'o')
*/
public void set(int i, int j, char player)
{
if (board[i][j] != ' ')
throw new IllegalArgumentException("Position occupied");
board[i][j] = player;
}
/**
Creates a string representation of the board such as
|x o|
| x |
| o|.
@return the string representation
*/
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for(int j = 0; j < COLUMNS; j++)
r = r + board[i][j];
r = r + "|\n";
}
return r;
}
private char[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Is this for a Mobile device?

First you are creating a board object, but you have no paint or Graphics going on here. That is why nothing is displayed.

If youa re using J2ME, I would create a Form that extends Canvas and in the paint method write the code to draw the lines of the tic-tac-toe game.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Looks like this isn't J2ME question, since all your other posts aren't either.

Moving to Java In General (Beginner) forum.

Mark
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
at a glance it looks like it should do what you want. Do you have another program that creates an instance, sets a few cells to X or O, then prints the toString()? What do you get from that?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
kofi ofei
Ranch Hand

Joined: May 22, 2005
Posts: 39
no
Fahd Shariff
Ranch Hand

Joined: Nov 22, 2002
Posts: 38
Ofcourse it works. Add a main method and try:


Fahd Shariff<br />"Let the code do the talking"
 
wood burning stoves
 
subject: tic-tac-toe board
 
Similar Threads
Game Tutorials -->> TicTacToe
oxo game
TicTacToe game
TicTacToe Game
TicTacToe Game..