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 5 of 5

Thread: TicTactoe program on BlueJ

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry TicTactoe program on BlueJ

    I'm currently writing this TicTacToe program on BlueJ.....and for some reason the while loop in the main method runs infinitely

    import java.util.Scanner;
     
    public class TicTacToe
    {
     
        public static void main()
        {
            Board game = new Board();
            Scanner getPosition = new Scanner(System.in);
            Scanner getPlayer = new Scanner(System.in);
     
     
            while(game.getSentinel() < 1){
                System.out.println("Enter Position:");
                int pos = getPosition.nextInt();
     
                System.out.println("Enter Player:");
                String player = getPlayer.nextLine();
     
                game.assignMove(player, pos);
                game.checkForWin();
     
                System.out.println(game.getValue(pos));
                System.out.println(game.printWinner()+ " wins");
     
     
     
            }
     
     
        }
     
    }

    public class Board //implements BoardUI
    {
     
        private String[][] board = new String[3][3];
        private String winner = "No player";
        private int sentinel = 0;
     
     
     
        public Board()
        {
     
        }
     
        public String getValue(int position)
        {
             if(position ==1){
               return board[0][0];
            }
            if(position ==2){
               return board[0][1];
            }
            if(position ==3){
              return board[0][2];
            }
            if(position ==4){
               return board[1][0];
            }
            if(position ==5){
               return board[1][1];
            }
            if(position ==6){
              return board[1][2];
            }
            if(position ==7){
               return board[2][0];
            }
            if(position ==8){
               return board[2][1];
            }
            if(position ==9){
               return board[2][2];
            }
            else{
                 return "";
            }
        }
     
        public int getSentinel()
        {
           return sentinel;
        }
        public void assignMove(String value, int position)
        {
            if(position ==1){
               board[0][0] = value;
            }
            if(position ==2){
               board[0][1] = value;
            }
            if(position ==3){
               board[0][2] = value;
            }
            if(position ==4){
               board[1][0] = value;
            }
            if(position ==5){
               board[1][1] = value;
            }
            if(position ==6){
               board[1][2] = value;
            }
            if(position ==7){
               board[2][0] = value;
            }
            if(position ==8){
               board[2][1] = value;
            }
            if(position ==9){
               board[2][2] = value;
            }
     
        }
     
        public String printWinner()
        {
           return winner;
        }
     
        public void checkForWin()
        {
            if
            ( 
            ((board[0][0]=="x") &&(board[0][1]=="x") && (board[0][2]=="x")) ||
            ((board[1][0]=="x") &&(board[1][1]=="x") && (board[1][2]=="x")) ||     
            ((board[2][0]=="x") &&(board[2][1]=="x") && (board[2][2]=="x")) ||
     
            ((board[0][0]=="x") &&(board[1][0]=="x") && (board[2][0]=="x")) ||
            ((board[1][0]=="x") &&(board[1][1]=="x") && (board[2][1]=="x")) ||
            ((board[0][2]=="x") &&(board[1][2]=="x") && (board[2][2]=="x")) ||
     
            ((board[0][0]=="x") &&(board[1][1]=="x") && (board[2][2]=="x")) ||
            ((board[0][2]=="x") &&(board[1][1]=="x") && (board[2][0]=="x")) 
     
            )
            {
             winner = "Player 1";   
             sentinel= 1;
            }
     
           if
            ( 
            ((board[0][0]=="o") &&(board[0][1]=="o") && (board[0][2]=="o")) ||
            ((board[1][0]=="o") &&(board[1][1]=="o") && (board[1][2]=="o")) ||     
            ((board[2][0]=="o") &&(board[2][1]=="o") && (board[2][2]=="o")) ||
     
            ((board[0][0]=="o") &&(board[1][0]=="o") && (board[2][0]=="o")) ||
            ((board[1][0]=="o") &&(board[1][1]=="o") && (board[2][1]=="o")) ||
            ((board[0][2]=="o") &&(board[1][2]=="o") && (board[2][2]=="o")) ||
     
            ((board[0][0]=="o") &&(board[1][1]=="o") && (board[2][2]=="o")) ||
            ((board[0][2]=="o") &&(board[1][1]=="o") && (board[2][0]=="o")) 
     
            )
            {
             winner = "Player 2";
             sentinel= 1;
            }
     
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: TicTactoe program on BlueJ

    board[0][0]=="x"
    A quick glance at your code: use the equals() method of the String class to check for String equality. Suggested reading: http://www.javaprogrammingforums.com...html#post18725

  3. The Following User Says Thank You to copeg For This Useful Post:

    d34dli (May 19th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: TicTactoe program on BlueJ

    the code works fine when I ran tests with this segment of code in the main method
           game.assignMove("o",1);
            game.assignMove("o",4);
            game.assignMove("o",7);
     
            System.out.println(game.getValue(0,0));
            System.out.println(game.getValue(1,0));
            System.out.println(game.getValue(2,0));
            game.checkForWin();
            System.out.println(game.printWinner());
            System.out.println(game.getSentinel());

    NOTE:
    the positions on the board
    1|2|3|
    ------
    4|5|6|
    ------
    7|8|9|

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: TicTactoe program on BlueJ

    You can ignore the advice given or take it. If you don't understand what someone is trying to tell you, then please ask for clarification.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    d34dli (May 19th, 2014)

  7. #5
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: TicTactoe program on BlueJ

    Quote Originally Posted by GregBrannon View Post
    You can ignore the advice given or take it. If you don't understand what someone is trying to tell you, then please ask for clarification.
    okay.

    --- Update ---

    Quote Originally Posted by copeg View Post
    A quick glance at your code: use the equals() method of the String class to check for String equality. Suggested reading: http://www.javaprogrammingforums.com...html#post18725
    I used the equals() method, then I had some nullpointer error which i fixed in the constructor. Thanks

    public class Board implements BoardUI
    {
     
        private String[][] board = new String[3][3];
        private String winner = "No player";
        private int sentinel = 0;
     
     
     
        public Board()
        {
            board[0][0] = "";
            board[0][1] ="";
            board[0][2] = "";
            board[1][0] = "";
            board[1][1] = "";
            board[1][2] = "";
            board[2][0] = "";
            board[2][1] = "";
            board[2][2] = "";
        }
     
        public String getValue(int position)
        {
             if(position ==1){
               return board[0][0];
            }
            if(position ==2){
               return board[0][1];
            }
            if(position ==3){
              return board[0][2];
            }
            if(position ==4){
               return board[1][0];
            }
            if(position ==5){
               return board[1][1];
            }
            if(position ==6){
              return board[1][2];
            }
            if(position ==7){
               return board[2][0];
            }
            if(position ==8){
               return board[2][1];
            }
            if(position ==9){
               return board[2][2];
            }
            else{
                 return "";
            }
        }
     
        public int getSentinel()
        {
           return sentinel;
        }
        public void assignMove(String value, int position)
        {
            if(position ==1){
               board[0][0] = value;
            }
            if(position ==2){
               board[0][1] = value;
            }
            if(position ==3){
               board[0][2] = value;
            }
            if(position ==4){
               board[1][0] = value;
            }
            if(position ==5){
               board[1][1] = value;
            }
            if(position ==6){
               board[1][2] = value;
            }
            if(position ==7){
               board[2][0] = value;
            }
            if(position ==8){
               board[2][1] = value;
            }
            if(position ==9){
               board[2][2] = value;
            }
     
        }
     
        public String printWinner()
        {
           return winner;
        }
     
        public void checkForWin()
        {
            if
            ( 
            ((board[0][0].equals("x")) &&(board[0][1].equals("x")) && (board[0][2].equals("x"))) ||
            ((board[1][0].equals("x")) &&(board[1][1].equals("x")) && (board[1][2].equals("x"))) ||     
            ((board[2][0].equals("x")) &&(board[2][1].equals("x")) && (board[2][2].equals("x"))) ||
     
            ((board[0][0].equals("x")) &&(board[1][0].equals("x")) && (board[2][0].equals("x"))) ||
            ((board[1][0].equals("x")) &&(board[1][1].equals("x")) && (board[2][1].equals("x"))) ||
            ((board[0][2]=="x") &&(board[1][2]=="x") && (board[2][2]=="x")) ||
     
            ((board[0][0].equals("x")) &&(board[1][1].equals("x")) && (board[2][2].equals("x"))) ||
            ((board[0][2].equals("x")) &&(board[1][1].equals("x")) && (board[2][0].equals("x"))) 
     
            )
            {
             winner = "Player 1";   
             sentinel= 1;
            }
     
           if
            ( 
            ((board[0][0].equals("o")) &&(board[0][1].equals("o")) && (board[0][2].equals("o"))) ||
            ((board[1][0].equals("o")) &&(board[1][1].equals("o")) && (board[1][2].equals("o"))) ||     
            ((board[2][0].equals("o")) &&(board[2][1].equals("o")) && (board[2][2].equals("o"))) ||
     
            ((board[0][0].equals("o")) &&(board[1][0].equals("o")) && (board[2][0].equals("o"))) ||
            ((board[1][0].equals("o")) &&(board[1][1].equals("o")) && (board[2][1].equals("o"))) ||
            ((board[0][2].equals("o")) &&(board[1][2].equals("o")) && (board[2][2].equals("o"))) ||
     
            ((board[0][0].equals("o")) &&(board[1][1].equals("o")) && (board[2][2].equals("o"))) ||
            ((board[0][2].equals("o")) &&(board[1][1].equals("o")) && (board[2][0].equals("o"))) 
     
            )
            {
             winner = "Player 2";
             sentinel= 1;
            }
     
     
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: February 6th, 2014, 01:11 PM
  2. BlueJ program !
    By fevdjyg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 16th, 2013, 04:35 PM
  3. Replies: 14
    Last Post: November 5th, 2013, 07:28 PM
  4. BLUEJ Program Help
    By threlot in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 5th, 2013, 09:42 PM
  5. [SOLVED] TicTacToe program - Space taken?
    By Actinistia in forum Java Theory & Questions
    Replies: 6
    Last Post: May 2nd, 2011, 11:06 PM

Tags for this Thread