Jump to content

  • Log In with Google      Sign In   
  • Create Account


Creating a Tic Tac Toe Board (Java)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 XxRenegadeXx   Members   -  Reputation: 136

Like
0Likes
Like

Posted 21 March 2005 - 01:35 PM

I am having trouble creating a Tic Tac Toe Board in Java. This is the code that I have so far (i've left out the board positions). All i need to know is how to create a 3 X 3 board using the style that ive chosen. If anyone can help, tell me what to do. public class TicTacToe { public static char board[][]={ {' ',' ',' '}, {' ',' ',' '}, {' ',' ',' '} }; public static int count=0; public static void main(String[] args) { printBoard(board); } public static void printBoard(char a[][]) { //Top Line of Board System.out.print((char)201); for(int i=0;i<=5;i++) { System.out.print((char)205); if((i+1)%2==0) System.out.print((char)203); System.out.print((char)205); } System.out.println((char)187); //Middle of Board for(int c=0;c<=5;c++) { if((c+1)%2==0) { System.out.print((char)186); for(int x=0;x<=5;x++) { System.out.print(" "); if((x+1)%2==0) System.out.print((char)186); System.out.print(" "); } System.out.print((char)186); System.out.println(); } else { System.out.print((char)204); for(int x=0;x<=5;x++) { System.out.print((char)205); if((x+1)%2==0) System.out.print((char)206); System.out.print((char)205); } System.out.print((char)185); System.out.println(); } } //Bottom of Board System.out.print((char)200); for(int i=0;i<5;i++) { System.out.print((char)205); if((i+1)%2==0) System.out.print((char)202); System.out.print((char)205); } System.out.println((char)188); } }

Sponsor:

#2 Beren77   Members   -  Reputation: 133

Like
0Likes
Like

Posted 22 March 2005 - 01:39 AM

Hi,

I am not sure if I understand your question correctly, but if I run your code, I'll get the following display on the screen:

Quote:

ÉÍÍÍËÍÍÍÍËÍÍÍÍËÍ»
ÌÍÍÍÎÍÍÍÍÎÍÍÍÍÎ͹
º º º º º
ÌÍÍÍÎÍÍÍÍÎÍÍÍÍÎ͹
º º º º º
ÌÍÍÍÎÍÍÍÍÎÍÍÍÍÎ͹
º º º º º
ÈÍÍÍÊÍÍÍÍÊÍÍͼ


So first of all, instead of using ASCII codes, why don't you use the characters in quotes (') and let java do the conversion?
And then what you have is a board, right?
Or do you want to know, how you can actually "make a move" and then display it? To do that, you'd have to change the content of your board-Array and then display the position of the board when looping through your display routine.

I.e.:


public void showBoard() {
for (int y = 0; y < 3; y++) {
System.out.println("---------");
for (int x = 0; x < 3; x++) {
System.out.print("|" + board[x][y]);
}
System.out.println("|");
}
System.out.println("---------");
}


On a more personal note: If you don't have a reason to make every method static, you would probably be better off to don't do that. (If you have a reason, ignore my last comment).





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS