#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Tic Tac Toe - Char Arrays- Checking if Cell is empty


    Hi, I've been working on this java application for about 5 hours now trying to figure out how to do this.

    My Professor Gave us the task of creating a java application, and the following task is proving impossible for me to figure out.

    o checkCell() – this method accepts one parameter (the cell number), this method checks the cell to be sure that it is both empty (available) and that the cell exists (input was valid). This method returns true if the cell both exists and is empty, returns false if the cell is already marked or if the cell does not exist (some number or character other than 1 to 9)

    I've been searching forums on the internet for hours now on how to figure this out, and I'm just about to give up. Here is my code:



    Code:
    package A7; //package has my full name so I left it out.
    
    import java.util.*;
    import java.util.Arrays;
    
    public class Main {
    
    //Both BoardInit and DrawBoard I was able to do with my //teacher's notes. It wasn't to difficult.
    
        public static void BoardInit() {
            int counter = 0;
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    ticTacToeBoard[i][j] = (char) ('1' + counter++);
                }
            }
        }
    
        public static void DrawBoard() {
            System.out.println("-------------");
    
            for (int i = 0; i < 3; ++i) {
                System.out.println("|   |   |   |");
                System.out.print("| ");
                for (int j = 0; j < 3; ++j) {
                    System.out.print(ticTacToeBoard[i][j] + " | ");
                }
                System.out.println("\n|   |   |   |");
                System.out.println("-------------");
            }
        }
    
    /*checkCell is where my problem lies. I need to have the *program check to see if when the user enters a cell number, if *it is empty. If it is empty, than how do I insert an X or O into */that vacant spot. How do I test to see if it's empty?
    
        public static boolean checkCell(int check) {
    
           
             for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    ticTacToeBoard[i][j] == check;
                return true; 
               
                 }
                
             }         
            
            return false;
        }
        static char[][] ticTacToeBoard = new char[3][3];
    
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
    
            
            System.out.println("Welcome to Tic Tac Toe!");
            System.out.println("Below is a drawing of the game board."
                    + " Select the number you wish to place your X or O");
    
    
            String play = "Y";
    
            while (play.equals("Y")) {
            
            BoardInit();
            DrawBoard();
    
            
                System.out.println("Player X, select a cell.");
                int random1 = console.nextInt();
                boolean test1 = checkCell(random1);
                
                if(test1){
                    
                }
                
                BoardInit();
                DrawBoard();
                
                System.out.println("Player 0, select a cell.");
                random1 = console.nextInt();
                test1 = checkCell(random1);
                
                if(test1){
                    
                }
            }
    
    
        }
    }

    This is mostly incomplete, but if any help could be supplied for this method and concept, it would be much appreciated.

    If you need the full details of the assignment, I can supply it also.
  2. #2
  3. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    If it's not 1-9, it doesn't exist.
    If cell[row][col] is 'O' or 'X', it's not empty. Otherwise it's empty.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0
    Originally Posted by gimp
    If it's not 1-9, it doesn't exist.
    If cell[row][col] is 'O' or 'X', it's not empty. Otherwise it's empty.
    If you read the code though, the way she want's us to represent a row and column are by single characters. This is where I'm really struggling. If it were a matter of typing in row, and then col, it would be much easier. But I don't understand at all how to write:

    if(ticTacToeBoard = 1)

    If this is the case, how should I go about it then? This is why I am confused, but I understand what you're saying. In fact your way is easier and better in my opinion, but how would I write it in this case?

    I also don't understand how the board will update with the X in the right spot. How do I print that in the right place?
  6. #4
  7. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    first, we want ==, not =.

    Consider the board.

    Code:
    123
    456
    789
    How do you find the cell and row of each column?

    Well, row 1 has 1, 2, 3. Row 2 is 4, 5, 6. Row 3 is 7, 8, 9. Pattern?
    Col 1 has 1, 4, 7. Col 2 has 2, 5, 8. Col 3 has 3, 6, 9. Pattern?

    Hint: For row, you want to do integer division. For col, you want the modulus operator.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0
    Originally Posted by gimp
    first, we want ==, not =.

    Consider the board.

    Code:
    123
    456
    789
    How do you find the cell and row of each column?

    Well, row 1 has 1, 2, 3. Row 2 is 4, 5, 6. Row 3 is 7, 8, 9. Pattern?
    Col 1 has 1, 4, 7. Col 2 has 2, 5, 8. Col 3 has 3, 6, 9. Pattern?

    Hint: For row, you want to do integer division. For col, you want the modulus operator.

    To find a cell, you would have to write:

    Array[0][0].

    I assumed based off what we learned in class this would give me access to the '1' cell.

    row, column

    1 = 0, 0
    2 = 0, 1
    3 = 0, 2
    4 = 1, 0
    5 = 1, 1
    6 = 1, 2
    7 = 2, 0
    8 = 2, 1
    9 = 2, 2

    1 2 3
    4 5 6
    7 8 9

    So where do the module and division operators come into place?

    This is all confusing because I don't think I understand how the character is assigned to that given cell.

IMN logo majestic logo threadwatch logo seochat tools logo