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

Thread: Tic-Tac-Toe Invalid Moves

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Tic-Tac-Toe Invalid Moves

    Every move I make is invalid, help.

    i.e.

    Enter a row (0, 1, or 2) for player X: 0
    Enter a Column (0, 1, or 2) for player X: 0
    Invalid move
    Enter a row (0, 1, or 2) for player X: 0
    Enter a Column (0, 1, or 2) for player X: 1
    Invalid move



    import java.util.Scanner;
     
    public class Week_Three {
    	private static final char EMPTY = ' ';
     
     
    	public static void main(String[] args){
     
    		char[][] board = new char[3][3];
    		printBoard(board);
     
    		 do {
    		     // User one X player
    			 play('X', board);
    		     printBoard(board);
    		      //Display result if X player win or Draw Game
    		      if (Winner(board)) {
    		        System.out.println("X Player Won");
    		        System.exit(1);
    		      }
    		      else if (drawGame(board)) {
    		        System.out.println("Draw");
    		        System.exit(2);
    		      }
     
    		      // User Two O player
    		      play('O', board);
    		      printBoard(board);
    		      //Display result if O player win or Draw Game
    		      if (Winner(board)) {
    		        System.out.println("O Player Won");
    		        System.exit(3);
    		      }
    		      else if (drawGame(board)) {
    		        System.out.println("Draw");
    		        System.exit(4);
    		      }
    		    } while (true);
    		  }
     
    	  //get user input for moves
    	  public static void play(char player, char[][] board) { 
    		    Scanner input = new Scanner(System.in);
     
    		    boolean validInput = false;
     
    		    do {
    		      System.out.print("Enter a row (0, 1, or 2) for player " + (player) +": ");
    		      int row = input.nextInt();
    		      System.out.print("Enter a Column (0, 1, or 2) for player " + (player) +": ");
    		      int column = input.nextInt();
     
    		      if (gamePiece(board, row, column, player)) { 
    		          validInput = true;
    		        }
    		      else
    		        System.out.println("Invalid move");
    		    }
    		    while (!validInput);
    		  }
     
    		  static boolean gamePiece(char[][] board, int row, int column, char player) {
    			    for (int i = 0; i < board.length; i++) {
    				      if (board[i][row] == EMPTY) {
    				        board[i][row] = player; 
    				        return true; 
    				      }
    				    }
    		    for (int j = 0; j < board.length; j++) {
    		        if (board[column][j] == EMPTY) {
    		            board[column][j] = player; 
    			        return true; 
     
    		    }
    		    }
     
    		    return false; 
    		  }
     
    	  //display game board
    	  static void printBoard(char[][] board) {
    		  System.out.println("-------------");
    		    for (int i = board.length - 1; i >= 0; i--) {
    		      System.out.print("| ");
    		      for (int j = 0; j < board[i].length; j++)
    		        System.out.print(board[i][j] != EMPTY ?  board[i][j] + " | ": "|");
    		      System.out.println();
    		      System.out.println("-------------");
    		    }
     
    		  }
     
    	  	  //Display win 
    		  public static boolean Winner(char[][] board) {
    			    return winningGame(board);
    			  }
     
    		  //Test Winner-Win if hit 3 consecutive pieces
    		  public static boolean winningGame(char[][] board) {
    			  boolean winning;
     
    				if(board[0][0]==board[0][1] && board[0][1]==board[0][2])
    					winning = true;
    				else if(board[1][0]==board[1][1] && board[1][1]==board[1][2])
    					winning = true;
    				else if(board[2][0]==board[2][1] && board[2][1]==board[2][2])
    					winning = true;
    				else if(board[0][0]==board[1][0] && board[1][0]==board[2][0])
    					winning = true;
    				else if(board[0][1]==board[1][1] && board[1][1]==board[2][1])
    					winning = true;
    				else if(board[0][2]==board[1][2] && board[1][2]==board[2][2])
    					winning = true;
    				else if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
    					winning = true;
    				else if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
    					winning = true;
    				else
    					winning = false;
     
    				return winning;
    			}
     
     
    		  //draw game conditions
    		  public static boolean drawGame(char[][] board) {
    			    for (int i = 0; i < board.length; i++) {
    			      for (int j = 0; j < board[i].length; j++) {
    			        if (board[i][j] == EMPTY) {
    			    return false;
    			        }
    			      }
    			    }
    			    return true;
    			  }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,022
    Thanks
    47
    Thanked 2,141 Times in 2,114 Posts

    Default Re: Tic-Tac-Toe Invalid Moves

    How have you tried debugging the code to see why it executes the way it does?
    One thing you should do is print out the contents of the board so you can see what's in it:
     System.out.println("board="+java.util.Arrays.deepToString(board));
    That will show you what the computer sees when it executes the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Invalid Moves

    Ok, I found the problem to invalid move-EMPTY after changing to '\u0000' only in the boolean gamepiece the code worked
    		  static boolean gamePiece(char[][] board, int row, int column, char player) {
    			    for (int i = 0; i < board.length; i++) {
    				      if (board[i][row] == '\u0000') {
    				        board[i][row] = player; 
    				        return true; 
    				      }
    				    }
    		    for (int j = 0; j < board.length; j++) {
    		        if (board[j][column] == '\u0000') {
    		            board[j][column] = player; 
    			        return true; 
     
    		    }
    		    }
     
    		    return false; 
    		  }

    after changing more codes

    	  static void printBoard(char[][] board) {
    		  System.out.println("-------------");
    		    for (int i = 0; i < board.length; i++) {
    		      System.out.print("| ");
    		      for (int j = 0; j < board[i].length; j++)
    		        System.out.print(board[i][j] != EMPTY ?  board[i][j] + " | ": "|");
    		      System.out.println();
    		      System.out.println("-------------");

    now the result is

    enter: 0,0
    ---------------
    | x | | |
    --------------
    | | | |
    --------------
    | | | |
    --------------
    X Player Won

    enter: 1,1
    ---------------
    | | x | |
    --------------
    | | | |
    --------------
    | | | |
    --------------
    X Player Won

    enter: 2,2
    ---------------
    | | | x |
    --------------
    | | | |
    --------------
    | | | |
    --------------
    X Player Won


    enter: 0,9
    ---------------
    | x | | |
    --------------
    | | | |
    --------------
    | | | |
    --------------
    X Player Won


    enter: 2,8
    ---------------
    | | | x |
    --------------
    | | | |
    --------------
    | | | |
    --------------
    X Player Won

    ect.

    more change in codes
    --after taking System.exit(1); out the game will play now but the grid is till wrong and X player win everytime
    		while (true) {
    		     // User one X player
    			 play('X', board);
    		     printBoard(board);
    		      //Display result if X player win or Draw Game
    		      if (Winner(board)) {
    		        System.out.println("X Player Won");
     
    		      }
    		      else if (drawGame(board)) {
    		        System.out.println("Draw");
     
    		      }
     
    		      // User Two O player
    		      play('O', board);
    		      printBoard(board);
    		      //Display result if O player win or Draw Game
    		      if (Winner(board)) {
    		        System.out.println("O Player Won");
     
    		      }
    		      else if (drawGame(board)) {
    		        System.out.println("Draw");
     
    		      }
    		    } 
    		  }

    i.e.
    enter: 2,8
    ---------------
    | | | x |
    --------------
    | | | |
    --------------
    | | | |
    --------------
    X Player Won
    Enter a row (0, 1, or 2) for player O: 0
    Enter a Column (0, 1, or 2) for player O: 0
    ---------------
    | O | | x |
    --------------
    | | | |
    --------------
    | | | |
    --------------
    O Player Won
    Enter a row (0, 1, or 2) for player X: 2
    Enter a Column (0, 1, or 2) for player X: 5
    ---------------
    | O | | x |
    --------------
    | | | X |
    --------------
    | | | |
    --------------
    X Player Won

    ect.


    current codes
    import java.util.Scanner;
     
    public class Week_Three {
    	public final static int EMPTY = ' ';
     
     
    	public static void main(String[] args){
     
    		char[][] board = new char[3][3];
     
    		printBoard(board);
     
    		while (true) {
    		     // User one X player
    			 play('X', board);
    		     printBoard(board);
    		      //Display result if X player win or Draw Game
    		      if (Winner(board)) {
    		        System.out.println("X Player Won");
     
    		      }
    		      else if (drawGame(board)) {
    		        System.out.println("Draw");
     
    		      }
     
    		      // User Two O player
    		      play('O', board);
    		      printBoard(board);
    		      //Display result if O player win or Draw Game
    		      if (Winner(board)) {
    		        System.out.println("O Player Won");
     
    		      }
    		      else if (drawGame(board)) {
    		        System.out.println("Draw");
     
    		      }
    		    } 
    		  }
     
    	  //get user input for moves
    	  public static void play(char player, char[][] board) { 
    		    Scanner input = new Scanner(System.in);
     
    		    boolean validInput = false;
     
    		    do {
    		      System.out.print("Enter a row (0, 1, or 2) for player " + (player) +": ");
    		      int row = input.nextInt();
    		      System.out.print("Enter a Column (0, 1, or 2) for player " + (player) +": ");
    		      int column = input.nextInt();
     
    		      if (gamePiece(board, row, column, player)) { 
    		          validInput = true;
    		        }
    		      else {
    		        System.out.println("Invalid move"); 
    		        }
    		    } while (!validInput);
    		  }
     
    		  static boolean gamePiece(char[][] board, int row, int column, char player) {
    			    for (int i = 0; i < board.length; i++) {
    				      if (board[i][row] == '\u0000') {
    				        board[i][row] = player; 
    				        return true; 
    				      }
    				    }
    		    for (int j = 0; j < board.length; j++) {
    		        if (board[j][column] == '\u0000') {
    		            board[j][column] = player; 
    			        return true; 
     
    		    }
    		    }
     
    		    return false; 
    		  }
     
    	  //display game board
    	  static void printBoard(char[][] board) {
    		  System.out.println("-------------");
    		    for (int i = 0; i < board.length; i++) {
    		      System.out.print("| ");
    		      for (int j = 0; j < board[i].length; j++)
    		        System.out.print(board[i][j] != EMPTY ?  board[i][j] + " | ": "|");
    		      System.out.println();
    		      System.out.println("-------------");
    		    }
     
    		  }
     
    	  	  //Display win 
    		  public static boolean Winner(char[][] board) {
    			    return winningGame(board);
    			  }
     
    		  //Test Winner-Win if hit 3 consecutive pieces
    		  public static boolean winningGame(char[][] board) {
    			  boolean winning;
     
    				if(board[0][0]==board[0][1] && board[0][1]==board[0][2]&&board[0][0] !=' ')
    					winning = true;
    				else if(board[1][0]==board[1][1] && board[1][1]==board[1][2]&&board[0][0] !=' ')
    					winning = true;
    				else if(board[2][0]==board[2][1] && board[2][1]==board[2][2]&&board[1][0] !=' ')
    					winning = true;
    				else if(board[0][0]==board[1][0] && board[1][0]==board[2][0]&&board[2][0] !=' ')
    					winning = true;
    				else if(board[0][1]==board[1][1] && board[1][1]==board[2][1]&&board[0][1] !=' ')
    					winning = true;
    				else if(board[0][2]==board[1][2] && board[1][2]==board[2][2]&&board[0][2] !=' ')
    					winning = true;
    				else if(board[0][0]==board[1][1] && board[1][1]==board[2][2]&&board[1][1] !=' ')
    					winning = true;
    				else if(board[0][2]==board[1][1] && board[1][1]==board[2][0]&&board[2][1] !=' ')
    					winning = true;
    				else
    					winning = false;
     
    				return winning;
    			}
     
    		  //draw game conditions
    		  public static boolean drawGame(char[][] board) {
    			    for (int i = 0; i < board.length; i++) {
    			      for (int j = 0; j < board[i].length; j++) {
    			        if (board[i][j] == EMPTY) {
    			    return false;
    			        }
    			      }
    			    }
    			    return true;
    			  }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,022
    Thanks
    47
    Thanked 2,141 Times in 2,114 Posts

    Default Re: Tic-Tac-Toe Invalid Moves

    X player win everytime
    Which statement is setting winning to true?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Tic-Tac-Toe Invalid Moves

    You never initialize values into your character array so when you check board[0][0] != ' ' of course it is going to be true. You need to initialize your array with blank values if that is the way you want to go about it. Or you could also check for empty chars.

    --- Update ---

    Next, you need to take some time to redo your gamePiece function. Not sure why you're using 2 for loops, but that function is all sorts of wrong. Take a minute to think about what your board array represents and find a better way to check and set your row/column combo. This program is put together pretty well for a beginner project and you're so close to being done. Good luck!
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    XP360 (April 11th, 2013)

  7. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Invalid Moves

    gamePiece function I redid it and it works fine now but I am not really catching what you are saying about board. I kinda understand kinda don't,
    Thanks for your words.

  8. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Tic-Tac-Toe Invalid Moves

    When you create your board which is a char[][], each element is empty which causes problems. To initialize the array, write a function that goes through each element and sets it to your EMPTY value.

    Side note, i love the fact that you used a static value of EMPTY for your empty char. You should use this everywhere you reference ' ' just to keep them all equal and understandable. Then you only have one point where the empty value is defined making it easy to find/change in the future. Great practice!
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  9. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    XP360 (April 11th, 2013)

Similar Threads

  1. Tic Tac Toe problem help!
    By vess28 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 29th, 2012, 06:59 PM
  2. Need help with a tic tac toe program
    By RodePope4546 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 22nd, 2012, 05:47 PM
  3. Tic-Tac-Toe Help
    By coke32 in forum Object Oriented Programming
    Replies: 13
    Last Post: March 12th, 2012, 07:59 AM
  4. [SOLVED] Tic-Tac-Toe program
    By Actinistia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2011, 11:18 PM
  5. [SOLVED] Tic Toe java code exists when we enter 0 row and 0 column
    By big_c in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:16 AM