Results 1 to 10 of 10
  1. #1
    CATCH3624 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Question 2-dimensional tic-tac-toe

    Hello....This program should detect when a player has won, or when the game ends in a draw. I'm having trouble with my method main part. Here is my program...Can anyone help me solve this please...

    Java Code:
    package tictactoe;
    
    import java.util.Scanner;
    
    public class Main {
    
        static byte[][] board;
        public boolean xTurn;
        public Scanner keyboard;
    
        public static void main(String[] args) {
            TicTacToe game = new TicTacToe();//1st problem
            game.play();
        }
    
        public static void TicTacToe() {
    
            board = new byte[3][3];
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    board[r][c] = ' ';
                }
            }
            boolean xTurn = true;
            keyboard = new Scanner(System.in);//2nd problem
        }
    
        public void displayRow(int row) {
            System.out.println(" " + board[row][0] + " | " + board[row][1] + " | " + board[row][2]);
        }
    
        public void displayBoard() {
            displayRow(0);
            System.out.println("-----------");
            displayRow(1);
            System.out.println("-----------");
            displayRow(2);
        }
    
        public void displayMenu() {
            if (xTurn) {
                System.out.println("X's Turn!");
            } else {
                System.out.println("O's Turn!");
            }
            System.out.println("What would you like to do?");
            System.out.println("1: Make a move");
            System.out.println("2: Start Over");
            System.out.println("3: Quit");
            System.out.print("Choice: ");
        }
    
        public boolean getMove() {
            boolean invalid = true;
            int row = 0, column = 0;
            while (invalid) {
                System.out.println("Which row, column would you like to move to?"
                        + " Enter two numbers between 0-2 separated by a space to indicate position.");
                row = keyboard.nextInt();
                column = keyboard.nextInt();
                if (row >= 0 && row <= 2 && column >= 0 && column <= 2) {
                    if (board[row][column] != ' ') {
                        System.out.println("That position is already taken");
                    } else {
                        invalid = false;
                    }
                } else {
                    System.out.println("Invalid position");
                }
            }
            if (xTurn) {
                board[row][column] = 'X';
            } else {
                board[row][column] = 'O';
            }
            return winner(row, column);
        }
    
        public void restart() {
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    board[r][c] = ' ';
                }
            }
            xTurn = true;
        }
    
        public boolean winner(int lastR, int lastC) {
            boolean winner = false;
            byte symbol = board[lastR][lastC];
            int numFound = 0;
            for (int c = 0; c < 3; c++) {
                if (board[lastR][c] == symbol) {
                    numFound++;
                }
            }
            if (numFound == 3) {
                winner = true;
            }
            numFound = 0;
            for (int r = 0; r < 3; r++) {
                if (board[r][lastC] == symbol) {
                    numFound++;
                }
            }
            if (numFound == 3) {
                winner = true;
            }
            numFound = 0;
            for (int i = 0; i < 3; i++) {
                if (board[i][i] == symbol) {
                    numFound++;
                }
            }
            if (numFound == 3) {
                winner = true;
            }
            numFound = 0;
            for (int i = 0; i < 3; i++) {
                if (board[i][2 - i] == symbol) {
                    numFound++;
                }
            }
            if (numFound == 3) {
                winner = true;
            }
            return winner;
        }
    
        public boolean boardFull() {
            int numSpotsFilled = 0;
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    if (board[r][c] == 'X' || board[r][c] == 'O') {
                        numSpotsFilled++;
                    }
                }
            }
            return numSpotsFilled == 9;
        }
    
        public void play() {
            while (true) {
                displayBoard();
                displayMenu();
                int choice = keyboard.nextInt();
                if (choice == 1) {
                    if (getMove()) {
                        displayBoard();
                        if (xTurn) {
                            System.out.println("X Wins!");
                        } else {
                            System.out.println("O Wins!");
                        }
                        System.exit(0);
                    } else if (boardFull()) {
                        displayBoard();
                        System.out.println("Draw!");
                        System.exit(0);
                    } else {
                        xTurn = !xTurn;
                    }
                } else if (choice == 2) {
                    restart();
                } else if (choice == 3) {
                    System.exit(0);
                } else {
                    System.out.println("Invalid Option");
                }
            }
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Do not just dump your code on us and expect us to fix it for you. Provide details and ask specific question.

    You have placed comments sucha as "1st problem" in your code. We don't read minds. Tell us exactly what the problem is. Do you get a compiler error? Then copy and paste it so we can see it. Does it run but have incorrect behaviour? Then describe what it is doing and what it should do instead.

  3. #3
    CATCH3624 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Sorry.....

    I'm new to this and I did not mean to drop and run...I thought that it was clear that my problem laid with these two arguments...when I ran this program without the two I mentioned I got no errors and this message " BUILD SUCCESSFUL (total time: 0 seconds)"... but nothing happened...I've tried lots of different methods and args but none help...I don't know if I should remove it or leave it in and if so what else am I missing...once again sorry I am new at this...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Huh?

    I still haven't a clue as to what your problem is.

  5. #5
    CATCH3624 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Ttt

    The problem is that this program wont execute with or without:

    public static void main(String[] args) {
    TicTacToe game = new TicTacToe();//when removed
    game.play();//when removed
    }
    public static void TicTacToe() {

    board = new byte[3][3];
    for (int r = 0; r < 3; r++) {
    for (int c = 0; c < 3; c++) {
    board[r][c] = ' ';
    }
    }
    boolean xTurn = true;
    keyboard = new Scanner(System.in);//when removed
    }

    //when these 3 args are removed I get the program to run but all it does and says is "BUILD SUCCESSFUL (total time: 0 seconds)"...and that's all... I'm trying to get it to execute the program ...What are my options???

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    What are the error messages when you leave that code in there. Post them here.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Of course it compiles and runs but does nothing if you remove code from the main method.
    Java Code:
    class Foo {
        public static void main(String[] args) {
    
        }
    }
    There is nothing inside the main method so what is it supposed to do? Nothing.

    Now if you place one or more of those lines back into your code and it does not compile then TELL US WHAT THE ERROR IS AND WHICH LINE CAUSES IT. You may be new to programming but this has nothing to do with programming and everything to do with communication.

  8. #8
    CATCH3624 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    //
    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: class TicTacToe
    location: class tictactoe.Main
    at tictactoe.Main.main(Main.java:12)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 9 seconds)
    //
    also: non static variable keyboard cannot be referenced from a static context

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    In your opinion does this code
    Java Code:
     TicTacToe game = new TicTacToe();//1st problem
    call or invoke this method
    Java Code:
        public static void TicTacToe() {
    
            board = new byte[3][3];
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    board[r][c] = ' ';
                }
            }
            boolean xTurn = true;
            keyboard = new Scanner(System.in);//2nd problem
        }
    Is there a class in existence called TicTacToe?

    Problem 2:
    Scanner instance keyboard is non static and is being used in a static method

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Java Code:
    public static void TicTacToe() {
    This is a static method and not a constructor. Personally the only time I like to see the word static is in the main method signature.

Similar Threads

  1. 2 dimensional array help!
    By ber1023 in forum New To Java
    Replies: 9
    Last Post: 01-02-2011, 12:29 AM
  2. Two dimensional array
    By niu_niu in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 12:34 AM
  3. about two dimensional array
    By matin1234 in forum New To Java
    Replies: 2
    Last Post: 06-01-2010, 11:09 AM
  4. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 07:21 PM
  5. How to reverse two dimensional
    By masaka in forum New To Java
    Replies: 4
    Last Post: 05-19-2008, 10:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •