• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

tic-tac-toe board

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like this isn't J2ME question, since all your other posts aren't either.

Moving to Java In General (Beginner) forum.

Mark
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
kofi ofei
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcourse it works. Add a main method and try:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic