6 Replies - 6950 Views - Last Post: 16 May 2010 - 12:56 PM Rate Topic: -----

#1 izinzin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-May 10

Tic Tac Toe game

Posted 16 May 2010 - 07:50 AM

hi everybody, i just only a beginner in Java and i has some problem as handling Tic-tac-toe game. Here is the question: " Write a program that will allow two users to play tic-tac-toe. The program should ask for moves alternately from player X and player O. The program displays the game positions as follows:

1 2 3
4 5 6
7 8 9
The players enter their moves by entering the position number they wish to mark. After each move, the program displays the changed board. A sample board configuration is as follows:

X X O
4 5 6
O 8 9"
I do not know how to replace a number which player choose with a character "X" or "O", so can you give me some suggestion? tks alot>

This post has been edited by izinzin: 16 May 2010 - 07:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe game

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1985
  • View blog
  • Posts: 4,119
  • Joined: 11-December 07

Re: Tic Tac Toe game

Posted 16 May 2010 - 08:06 AM

How are you storing the board? It would help to see your code.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,065
  • Joined: 27-December 08

Re: Tic Tac Toe game

Posted 16 May 2010 - 08:09 AM

Think about using a 3x3 char[][] to hold the digits 1-9, 3 per row. Then, just replace the number with X or O depending on the spot the user selects.
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Tic Tac Toe game

Posted 16 May 2010 - 08:09 AM

String has a replace method which you can use for this purpose.

So for instance:
String board = "1 2 3\n4 5 6\n 7 8 9"
board = board.replace("1", "X");
board = board.replace "5", "O");
//etc.



Although, I don't think that this would be the best way of designing your game

Dogstopper wrote a tutorial on designing a Tic-Tac-Toe game. He created a graphical game, but the logic is essentially the same :)
http://www.dreaminco...-1-tic-tac-toe/
Was This Post Helpful? 0
  • +
  • -

#5 izinzin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-May 10

Re: Tic Tac Toe game

Posted 16 May 2010 - 08:32 AM

View Postcfoley, on 16 May 2010 - 07:06 AM, said:

How are you storing the board? It would help to see your code.

Here is my code ti display the board
public class TicTacToe {
    
    public static void main( String args[]) {
     { 
                    int[][]board = new int[3][3];
                    
                    int row =0;
                    int column =0;
                    int i=1;
                    System.out.println ("Tic-tac-toe game!"); 
                    for (row=0;row<board.length; row++)
                    { 
                           for(column=0;column<board[row].length;column++)
                           
                           {board[row][column]=i;
                           i++;
                           System.out.print (" "+(board[row][column])+" ");
                           }
                           System.out.println();
                    }
               
            
              System.out.println();
              System.out.println ("Let's start Tic-tac-toe!");
     }
             
              }
     }
    

Was This Post Helpful? 0
  • +
  • -

#6 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Tic Tac Toe game

Posted 16 May 2010 - 08:46 AM

I would consider using a different data type for your array, but you definitely have the right idea. I would go for a char[] array, as a few people above have mentioned because not only do you have to display numbers, but Xs and Os. You can initially store the numbers in the char[] array by doing this

//if (i >= 0 && i <= 9) 
char c = Character.forDigit(i, 10); 


so your loop part of the program thus far would look like this:


//note this change
char[][]board = new int[3][3];
                    
                    int row =0;
                    int column =0;
                    int i=1;
                    //we need a char character
                    char c;
                    System.out.println ("Tic-tac-toe game!"); 
                    for (row=0;row<board.length; row++)
                    { 
                           for(column=0;column<board[row].length;column++)
                           
                           {
                           //char representation of int
                           char c = Character.forDigit(i, 10);
                           //store it in the board
                           board[row][column]=c;
                           //increment i
                           i++;
                           System.out.print (" "+(board[row][column])+" ");
                           }
                           System.out.println();
                    }


This post has been edited by Luckless: 16 May 2010 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Tic Tac Toe game

Posted 16 May 2010 - 12:56 PM

View Posterik.price, on 16 May 2010 - 10:09 AM, said:

String has a replace method which you can use for this purpose.

So for instance:
String board = "1 2 3\n4 5 6\n 7 8 9"
board = board.replace("1", "X");
board = board.replace "5", "O");
//etc.



Although, I don't think that this would be the best way of designing your game

Dogstopper wrote a tutorial on designing a Tic-Tac-Toe game. He created a graphical game, but the logic is essentially the same :)
http://www.dreaminco...-1-tic-tac-toe/


Yep. Actually, though mine is Graphical, most of the program centers around 2D Array manipulation. The logic is the exact same.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1