Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Tic Toe java code exists when we enter 0 row and 0 column

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Tic Toe java code exists when we enter 0 row and 0 column

    good afternoon everyone. i have been working on a tictactoe game using arrays and if statements and loops. my problem is the game exits when i type in row 0 and column 0. also i was wondering if you could help me change the input method so that the user could type in like 1 and it would place an x or an o in the box. also i just noticed it doesn't stop when you have three in a row

     import java.util.Scanner;
     
    /**
       This program runs a TicTacToe game. It prompts the
       user to set positions on the board and prints out the
       result.
    */
    public class TicTacToeRunner
    {
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          char player = 'x';
          TicTacToe game = new TicTacToe();
          boolean done = false;
        int Count=0;
          do{
             System.out.print(game.toString());
             System.out.print(
                   "Row for " + player );
             int row = in.nextInt();
     
     
     
                System.out.print("Column for " + player + ": ");
                int column = in.nextInt();
                game.set(row, column, player);
                if (player==('X'))
                   player = 'O';
                else
                   player = 'X';
             }while(!game.winner()&&Count<=9);
     
       }
    }
    /**
       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] =' ';
       }
     
       /**
          Sets 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]== ' ')
             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 = "";
         r+="   |   |   \n";
         r+=" "+board[0][0]+" |"+board[0][1]+"  |  "+board[0][2]+"\n";
         r+="   |   |   \n";
         r+="----------\n";
         r+="   |   |   \n";
         r+=" "+board[1][0]+" |"+board[1][1]+"  |  "+board[1][2]+"\n";
         r+="   |   |   \n";
         r+="----------\n";
         r+="   |   |   \n";
         r+=" "+board[2][0]+" |"+board[2][1]+"  |  "+board[2][2]+"\n";
         r+="   |   |   \n";
     
     
     
          return r;
       }
     
    public boolean winner()
    {
    boolean winner;
     
    if(board[0][0] == board[0][1] && board[0][1] == board[0][2]&&board[0][0] !=' ')
        return true;
    if(board[1][0] == board[1][1] && board[1][1] == board[1][2]&&board[1][0] !=' ')
        return true;
    if(board[2][0] == board[2][1] && board[2][1] == board[2][2]&&board[2][0] !=' ')
        return true;
    if(board[0][0] == board[1][0] && board[1][0] == board[2][0]&&board[0][0] !=' ')
        return true;
    if(board[0][1] == board[1][1] && board[1][1] == board[2][1]&&board[0][0] !=' ')
        return true;
    if(board[0][2] == board[1][2] && board[1][2] == board[2][2]&&board[0][0] !=' ')
        return true;
    if(board[0][0] == board[1][2] && board[1][2] == board[2][2]&&board[0][0] !=' ')
        return true;
    if(board[2][0] == board[1][2] && board[1][2] == board[2][1]&&board[0][0] !=' ')
        return true;
     
        return false;
    }
     
     
       private char[][] board;
       private static final int ROWS = 3;
       private static final int COLUMNS = 3;
     
    }
    thanks for all your help
    Last edited by JavaPF; May 9th, 2009 at 04:39 AM. Reason: Please use code tags


  2. #2
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help with tic tac toe

    hello again i just fixed the stopping when i put in 0,0 but it keeps going even if i have three x or o's it stops once i get all nine moves. i think i need an if statement in the tic tac toe runner? here is my updated class

    /**
       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] =' ';
       }
     
       /**
          Sets 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]== ' ')
             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 = "";
         r+="   |   |   \n";
         r+=" "+board[0][0]+" |"+board[0][1]+"  |  "+board[0][2]+"\n";
         r+="   |   |   \n";
         r+="----------\n";
         r+="   |   |   \n";
         r+=" "+board[1][0]+" |"+board[1][1]+"  |  "+board[1][2]+"\n";
         r+="   |   |   \n";
         r+="----------\n";
         r+="   |   |   \n";
         r+=" "+board[2][0]+" |"+board[2][1]+"  |  "+board[2][2]+"\n";
         r+="   |   |   \n";
     
     
     
          return r;
       }
     
    public boolean winner()
    {
    boolean winner;
     
    if(board[0][0] == board[0][1] && board[0][1] == board[0][2]&&board[0][0] !=' ')
        return true;
    if(board[1][0] == board[1][1] && board[1][1] == board[1][2]&&board[1][0] !=' ')
        return true;
    if(board[2][0] == board[2][1] && board[2][1] == board[2][2]&&board[2][0] !=' ')
        return true;
    if(board[0][0] == board[1][0] && board[1][0] == board[2][0]&&board[0][1] !=' ')
        return true;
    if(board[0][1] == board[1][1] && board[1][1] == board[2][1]&&board[0][2] !=' ')
        return true;
    if(board[0][2] == board[1][2] && board[1][2] == board[2][2]&&board[1][1] !=' ')
        return true;
    if(board[0][0] == board[1][2] && board[1][2] == board[2][2]&&board[1][2] !=' ')
        return true;
    if(board[2][0] == board[1][2] && board[1][2] == board[2][1]&&board[2][1] !=' ')
        return true;
     
        return false;
    }
     
     
       private char[][] board;
       private static final int ROWS = 3;
       private static final int COLUMNS = 3;
     
    }

    also i need help checking for a valid move.
    Last edited by big_c; May 11th, 2009 at 08:13 AM.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help with tic tac toe

    Hello big_c, glad you solved this issue.

    What was the problem in the end?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.